Stop installing the protocol descriptions for extensions to an extensions/
[free-sw/xcb/libxcb] / src / xcb_xid.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 /* XID allocators. */
27
28 #include <stdlib.h>
29 #include "xcb.h"
30 #include "xcbext.h"
31 #include "xcbint.h"
32 #include "xc_misc.h"
33
34 /* Public interface */
35
36 uint32_t xcb_generate_id(xcb_connection_t *c)
37 {
38     uint32_t ret;
39     if(c->has_error)
40         return -1;
41     pthread_mutex_lock(&c->xid.lock);
42     if(c->xid.last == c->xid.max)
43     {
44         xcb_xc_misc_get_xid_range_reply_t *range;
45         range = xcb_xc_misc_get_xid_range_reply(c, xcb_xc_misc_get_xid_range(c), 0);
46         if(!range)
47         {
48             pthread_mutex_unlock(&c->xid.lock);
49             return -1;
50         }
51         c->xid.last = range->start_id;
52         c->xid.max = range->start_id + (range->count - 1) * c->xid.inc;
53         free(range);
54     }
55     ret = c->xid.last | c->xid.base;
56     c->xid.last += c->xid.inc;
57     pthread_mutex_unlock(&c->xid.lock);
58     return ret;
59 }
60
61 /* Private interface */
62
63 int _xcb_xid_init(xcb_connection_t *c)
64 {
65     if(pthread_mutex_init(&c->xid.lock, 0))
66         return 0;
67     c->xid.last = 0;
68     c->xid.base = c->setup->resource_id_base;
69     c->xid.max = c->setup->resource_id_mask;
70     c->xid.inc = c->setup->resource_id_mask & -(c->setup->resource_id_mask);
71     return 1;
72 }
73
74 void _xcb_xid_destroy(xcb_connection_t *c)
75 {
76     pthread_mutex_destroy(&c->xid.lock);
77 }