Blame src/xcb_xid.c

Packit Service 612474
/* Copyright (C) 2001-2008 Bart Massey and Jamey Sharp.
Packit Service 612474
 *
Packit Service 612474
 * Permission is hereby granted, free of charge, to any person obtaining a
Packit Service 612474
 * copy of this software and associated documentation files (the "Software"),
Packit Service 612474
 * to deal in the Software without restriction, including without limitation
Packit Service 612474
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
Packit Service 612474
 * and/or sell copies of the Software, and to permit persons to whom the
Packit Service 612474
 * Software is furnished to do so, subject to the following conditions:
Packit Service 612474
 * 
Packit Service 612474
 * The above copyright notice and this permission notice shall be included in
Packit Service 612474
 * all copies or substantial portions of the Software.
Packit Service 612474
 * 
Packit Service 612474
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Packit Service 612474
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Packit Service 612474
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Packit Service 612474
 * AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
Packit Service 612474
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
Packit Service 612474
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Packit Service 612474
 * 
Packit Service 612474
 * Except as contained in this notice, the names of the authors or their
Packit Service 612474
 * institutions shall not be used in advertising or otherwise to promote the
Packit Service 612474
 * sale, use or other dealings in this Software without prior written
Packit Service 612474
 * authorization from the authors.
Packit Service 612474
 */
Packit Service 612474
Packit Service 612474
/* XID allocators. */
Packit Service 612474
Packit Service 612474
#ifdef HAVE_CONFIG_H
Packit Service 612474
#include "config.h"
Packit Service 612474
#endif
Packit Service 612474
Packit Service 612474
#include <assert.h>
Packit Service 612474
#include <stdlib.h>
Packit Service 612474
#include "xcb.h"
Packit Service 612474
#include "xcbext.h"
Packit Service 612474
#include "xcbint.h"
Packit Service 612474
#include "xc_misc.h"
Packit Service 612474
Packit Service 612474
/* Public interface */
Packit Service 612474
Packit Service 612474
uint32_t xcb_generate_id(xcb_connection_t *c)
Packit Service 612474
{
Packit Service 612474
    uint32_t ret;
Packit Service 612474
    if(c->has_error)
Packit Service 612474
        return -1;
Packit Service 612474
    pthread_mutex_lock(&c->xid.lock);
Packit Service 612474
    if(c->xid.last >= c->xid.max - c->xid.inc + 1)
Packit Service 612474
    {
Packit Service 612474
        xcb_xc_misc_get_xid_range_reply_t *range;
Packit Service 612474
        assert(c->xid.last == c->xid.max);
Packit Service 612474
        if (c->xid.last == 0) {
Packit Service 612474
            /* finish setting up initial range */
Packit Service 612474
            c->xid.max = c->setup->resource_id_mask;
Packit Service 612474
        } else {
Packit Service 612474
            /* check for extension */
Packit Service 612474
            const xcb_query_extension_reply_t *xc_misc_reply =
Packit Service 612474
              xcb_get_extension_data(c, &xcb_xc_misc_id);
Packit Service 612474
            if (!xc_misc_reply) {
Packit Service 612474
                pthread_mutex_unlock(&c->xid.lock);
Packit Service 612474
                return -1;
Packit Service 612474
            }
Packit Service 612474
            /* get new range */
Packit Service 612474
            range = xcb_xc_misc_get_xid_range_reply(c,
Packit Service 612474
                      xcb_xc_misc_get_xid_range(c), 0);
Packit Service 612474
            /* XXX The latter disjunct is what the server returns
Packit Service 612474
               when it is out of XIDs.  Sweet. */
Packit Service 612474
            if(!range || (range->start_id == 0 && range->count == 1))
Packit Service 612474
            {
Packit Service 612474
                pthread_mutex_unlock(&c->xid.lock);
Packit Service 612474
                return -1;
Packit Service 612474
            }
Packit Service 612474
            assert(range->count > 0 && range->start_id > 0);
Packit Service 612474
            c->xid.last = range->start_id;
Packit Service 612474
            c->xid.max = range->start_id + (range->count - 1) * c->xid.inc;
Packit Service 612474
            free(range);
Packit Service 612474
        }
Packit Service 612474
    } else {
Packit Service 612474
        c->xid.last += c->xid.inc;
Packit Service 612474
    }
Packit Service 612474
    ret = c->xid.last | c->xid.base;
Packit Service 612474
    pthread_mutex_unlock(&c->xid.lock);
Packit Service 612474
    return ret;
Packit Service 612474
}
Packit Service 612474
Packit Service 612474
/* Private interface */
Packit Service 612474
Packit Service 612474
int _xcb_xid_init(xcb_connection_t *c)
Packit Service 612474
{
Packit Service 612474
    if(pthread_mutex_init(&c->xid.lock, 0))
Packit Service 612474
        return 0;
Packit Service 612474
    c->xid.last = 0;
Packit Service 612474
    c->xid.max = 0;
Packit Service 612474
    c->xid.base = c->setup->resource_id_base;
Packit Service 612474
    c->xid.inc = c->setup->resource_id_mask & -(c->setup->resource_id_mask);
Packit Service 612474
    return 1;
Packit Service 612474
}
Packit Service 612474
Packit Service 612474
void _xcb_xid_destroy(xcb_connection_t *c)
Packit Service 612474
{
Packit Service 612474
    pthread_mutex_destroy(&c->xid.lock);
Packit Service 612474
}