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