1 /* Copyright (C) 2001-2004 Bart Massey and Jamey Sharp.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a
4 * copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation
6 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 * and/or sell copies of the Software, and to permit persons to whom the
8 * Software is furnished to do so, subject to the following conditions:
9 *
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
12 *
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 *
20 * Except as contained in this notice, the names of the authors or their
21 * institutions shall not be used in advertising or otherwise to promote the
22 * sale, use or other dealings in this Software without prior written
23 * authorization from the authors.
24 */
25
26 /* A cache for QueryExtension results. */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "xcb.h"
36 #include "xcbext.h"
37 #include "xcbint.h"
38
39 typedef struct lazyreply {
40 enum lazy_reply_tag tag;
41 union {
42 xcb_query_extension_cookie_t cookie;
43 xcb_query_extension_reply_t *reply;
44 } value;
45 } lazyreply;
46
get_index(xcb_connection_t * c,int idx)47 static lazyreply *get_index(xcb_connection_t *c, int idx)
48 {
49 if(idx > c->ext.extensions_size)
50 {
51 int new_size = idx << 1;
52 lazyreply *new_extensions = realloc(c->ext.extensions, sizeof(lazyreply) * new_size);
53 if(!new_extensions)
54 return 0;
55 memset(new_extensions + c->ext.extensions_size, 0, sizeof(lazyreply) * (new_size - c->ext.extensions_size));
56 c->ext.extensions = new_extensions;
57 c->ext.extensions_size = new_size;
58 }
59 return c->ext.extensions + idx - 1;
60 }
61
get_lazyreply(xcb_connection_t * c,xcb_extension_t * ext)62 static lazyreply *get_lazyreply(xcb_connection_t *c, xcb_extension_t *ext)
63 {
64 static pthread_mutex_t global_lock = PTHREAD_MUTEX_INITIALIZER;
65 static int next_global_id;
66
67 lazyreply *data;
68
69 pthread_mutex_lock(&global_lock);
70 if(!ext->global_id)
71 ext->global_id = ++next_global_id;
72 pthread_mutex_unlock(&global_lock);
73
74 data = get_index(c, ext->global_id);
75 if(data && data->tag == LAZY_NONE)
76 {
77 /* cache miss: query the server */
78 data->tag = LAZY_COOKIE;
79 data->value.cookie = xcb_query_extension(c, strlen(ext->name), ext->name);
80 }
81 return data;
82 }
83
84 /* Public interface */
85
86 /* Do not free the returned xcb_query_extension_reply_t - on return, it's aliased
87 * from the cache. */
xcb_get_extension_data(xcb_connection_t * c,xcb_extension_t * ext)88 const xcb_query_extension_reply_t *xcb_get_extension_data(xcb_connection_t *c, xcb_extension_t *ext)
89 {
90 lazyreply *data;
91 if(c->has_error)
92 return 0;
93
94 pthread_mutex_lock(&c->ext.lock);
95 data = get_lazyreply(c, ext);
96 if(data && data->tag == LAZY_COOKIE)
97 {
98 data->tag = LAZY_FORCED;
99 data->value.reply = xcb_query_extension_reply(c, data->value.cookie, 0);
100 }
101 pthread_mutex_unlock(&c->ext.lock);
102
103 return data ? data->value.reply : 0;
104 }
105
xcb_prefetch_extension_data(xcb_connection_t * c,xcb_extension_t * ext)106 void xcb_prefetch_extension_data(xcb_connection_t *c, xcb_extension_t *ext)
107 {
108 if(c->has_error)
109 return;
110 pthread_mutex_lock(&c->ext.lock);
111 get_lazyreply(c, ext);
112 pthread_mutex_unlock(&c->ext.lock);
113 }
114
115 /* Private interface */
116
_xcb_ext_init(xcb_connection_t * c)117 int _xcb_ext_init(xcb_connection_t *c)
118 {
119 if(pthread_mutex_init(&c->ext.lock, 0))
120 return 0;
121 return 1;
122 }
123
_xcb_ext_destroy(xcb_connection_t * c)124 void _xcb_ext_destroy(xcb_connection_t *c)
125 {
126 pthread_mutex_destroy(&c->ext.lock);
127 while(c->ext.extensions_size-- > 0)
128 if(c->ext.extensions[c->ext.extensions_size].tag == LAZY_FORCED)
129 free(c->ext.extensions[c->ext.extensions_size].value.reply);
130 free(c->ext.extensions);
131 }
132