xcb_xid.c revision 602e473d
1/* Copyright (C) 2001-2008 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 <assert.h> 29#include <stdlib.h> 30#include "xcb.h" 31#include "xcbext.h" 32#include "xcbint.h" 33#include "xc_misc.h" 34 35/* Public interface */ 36 37uint32_t xcb_generate_id(xcb_connection_t *c) 38{ 39 uint32_t ret; 40 if(c->has_error) 41 return -1; 42 pthread_mutex_lock(&c->xid.lock); 43 if(c->xid.last >= c->xid.max - c->xid.inc + 1) 44 { 45 xcb_xc_misc_get_xid_range_reply_t *range; 46 assert(c->xid.last == c->xid.max); 47 if (c->xid.last == 0) { 48 /* finish setting up initial range */ 49 c->xid.max = c->setup->resource_id_mask; 50 } else { 51 /* check for extension */ 52 const xcb_query_extension_reply_t *xc_misc_reply = 53 xcb_get_extension_data(c, &xcb_xc_misc_id); 54 if (!xc_misc_reply) { 55 pthread_mutex_unlock(&c->xid.lock); 56 return -1; 57 } 58 /* get new range */ 59 range = xcb_xc_misc_get_xid_range_reply(c, 60 xcb_xc_misc_get_xid_range(c), 0); 61 /* XXX The latter disjunct is what the server returns 62 when it is out of XIDs. Sweet. */ 63 if(!range || (range->start_id == 0 && range->count == 1)) 64 { 65 pthread_mutex_unlock(&c->xid.lock); 66 return -1; 67 } 68 assert(range->count > 0 && range->start_id > 0); 69 c->xid.last = range->start_id; 70 c->xid.max = range->start_id + (range->count - 1) * c->xid.inc; 71 free(range); 72 } 73 } else { 74 c->xid.last += c->xid.inc; 75 } 76 ret = c->xid.last | c->xid.base; 77 pthread_mutex_unlock(&c->xid.lock); 78 return ret; 79} 80 81/* Private interface */ 82 83int _xcb_xid_init(xcb_connection_t *c) 84{ 85 if(pthread_mutex_init(&c->xid.lock, 0)) 86 return 0; 87 c->xid.last = 0; 88 c->xid.max = 0; 89 c->xid.base = c->setup->resource_id_base; 90 c->xid.inc = c->setup->resource_id_mask & -(c->setup->resource_id_mask); 91 return 1; 92} 93 94void _xcb_xid_destroy(xcb_connection_t *c) 95{ 96 pthread_mutex_destroy(&c->xid.lock); 97} 98