12cb164632f2829b0a380c036b495aeb1bbc2492
[free-sw/xcb/libxcb] / src / xcb_ext.c
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 #include <stdlib.h>
29 #include <string.h>
30
31 #include "xcb.h"
32 #include "xcbext.h"
33 #include "xcbint.h"
34
35 typedef struct lazyreply {
36     enum lazy_reply_tag tag;
37     union {
38         xcb_query_extension_cookie_t cookie;
39         xcb_query_extension_reply_t *reply;
40     } value;
41 } lazyreply;
42
43 static lazyreply *get_index(xcb_connection_t *c, int index)
44 {
45     if(index > c->ext.extensions_size)
46     {
47         int new_size = index << 1;
48         lazyreply *new_extensions = realloc(c->ext.extensions, sizeof(lazyreply) * new_size);
49         if(!new_extensions)
50             return 0;
51         memset(new_extensions + c->ext.extensions_size, 0, sizeof(lazyreply) * (new_size - c->ext.extensions_size));
52         c->ext.extensions = new_extensions;
53         c->ext.extensions_size = new_size;
54     }
55     return c->ext.extensions + index - 1;
56 }
57
58 static lazyreply *get_lazyreply(xcb_connection_t *c, xcb_extension_t *ext)
59 {
60     static pthread_mutex_t global_lock = PTHREAD_MUTEX_INITIALIZER;
61     static int next_global_id;
62
63     lazyreply *data;
64
65     pthread_mutex_lock(&global_lock);
66     if(!ext->global_id)
67         ext->global_id = ++next_global_id;
68     pthread_mutex_unlock(&global_lock);
69
70     data = get_index(c, ext->global_id);
71     if(data && data->tag == LAZY_NONE)
72     {
73         /* cache miss: query the server */
74         data->tag = LAZY_COOKIE;
75         data->value.cookie = xcb_query_extension(c, strlen(ext->name), ext->name);
76     }
77     return data;
78 }
79
80 /* Public interface */
81
82 /* Do not free the returned xcb_query_extension_reply_t - on return, it's aliased
83  * from the cache. */
84 const xcb_query_extension_reply_t *xcb_get_extension_data(xcb_connection_t *c, xcb_extension_t *ext)
85 {
86     lazyreply *data;
87     if(c->has_error)
88         return 0;
89
90     pthread_mutex_lock(&c->ext.lock);
91     data = get_lazyreply(c, ext);
92     if(data && data->tag == LAZY_COOKIE)
93     {
94         data->tag = LAZY_FORCED;
95         data->value.reply = xcb_query_extension_reply(c, data->value.cookie, 0);
96     }
97     pthread_mutex_unlock(&c->ext.lock);
98
99     return data ? data->value.reply : 0;
100 }
101
102 void xcb_prefetch_extension_data(xcb_connection_t *c, xcb_extension_t *ext)
103 {
104     if(c->has_error)
105         return;
106     pthread_mutex_lock(&c->ext.lock);
107     get_lazyreply(c, ext);
108     pthread_mutex_unlock(&c->ext.lock);
109 }
110
111 /* Private interface */
112
113 int _xcb_ext_init(xcb_connection_t *c)
114 {
115     if(pthread_mutex_init(&c->ext.lock, 0))
116         return 0;
117     return 1;
118 }
119
120 void _xcb_ext_destroy(xcb_connection_t *c)
121 {
122     pthread_mutex_destroy(&c->ext.lock);
123     while(c->ext.extensions_size-- > 0)
124         if(c->ext.extensions[c->ext.extensions_size].tag == LAZY_FORCED)
125             free(c->ext.extensions[c->ext.extensions_size].value.reply);
126     free(c->ext.extensions);
127 }