| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- /*
- * Copyright (c) 2002-2006 MontaVista Software, Inc.
- * Copyright (c) 2006-2009 Red Hat, Inc.
- *
- * All rights reserved.
- *
- * Author: Steven Dake (sdake@redhat.com)
- *
- * This software licensed under BSD license, the text of which follows:
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * - Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * - Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * - Neither the name of the MontaVista Software, Inc. nor the names of its
- * contributors may be used to endorse or promote products derived from this
- * software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
- #ifndef HDB_H_DEFINED
- #define HDB_H_DEFINED
- #include <errno.h>
- #include <assert.h>
- #include <stdlib.h>
- #include <string.h>
- #include <pthread.h>
- #include <stdint.h>
- #include <inttypes.h>
- typedef uint64_t hdb_handle_t;
- /*
- * Formatting for string printing on 32/64 bit systems
- */
- #define HDB_D_FORMAT "%"PRIu64
- #define HDB_X_FORMAT "%"PRIx64
- enum HDB_HANDLE_STATE {
- HDB_HANDLE_STATE_EMPTY,
- HDB_HANDLE_STATE_PENDINGREMOVAL,
- HDB_HANDLE_STATE_ACTIVE
- };
- struct hdb_handle {
- int state;
- void *instance;
- int check;
- int ref_count;
- };
- struct hdb_handle_database {
- unsigned int handle_count;
- struct hdb_handle *handles;
- unsigned int iterator;
- void (*destructor) (void *);
- #if defined(HAVE_PTHREAD_SPIN_LOCK)
- pthread_spinlock_t lock;
- #else
- pthread_mutex_t lock;
- #endif
- unsigned int first_run;
- };
- #if defined(HAVE_PTHREAD_SPIN_LOCK)
- static inline void hdb_database_lock (pthread_spinlock_t *spinlock)
- {
- pthread_spin_lock (spinlock);
- }
- static inline void hdb_database_unlock (pthread_spinlock_t *spinlock)
- {
- pthread_spin_unlock (spinlock);
- }
- static inline void hdb_database_lock_init (pthread_spinlock_t *spinlock)
- {
- pthread_spin_init (spinlock, 0);
- }
- static inline void hdb_database_lock_destroy (pthread_spinlock_t *spinlock)
- {
- pthread_spin_destroy (spinlock);
- }
- #else
- static inline void hdb_database_lock (pthread_mutex_t *mutex)
- {
- pthread_mutex_lock (mutex);
- }
- static inline void hdb_database_unlock (pthread_mutex_t *mutex)
- {
- pthread_mutex_unlock (mutex);
- }
- static inline void hdb_database_lock_init (pthread_mutex_t *mutex)
- {
- pthread_mutex_init (mutex, NULL);
- }
- static inline void hdb_database_lock_destroy (pthread_mutex_t *mutex)
- {
- pthread_mutex_destroy (mutex);
- }
- #endif
- #define DECLARE_HDB_DATABASE(database_name,destructor_function) \
- static struct hdb_handle_database (database_name) = { \
- .handle_count = 0, \
- .handles = NULL, \
- .iterator = 0, \
- .destructor = destructor_function, \
- .first_run = 0 \
- }; \
- static void database_name##_init(void)__attribute__((constructor)); \
- static void database_name##_init(void) \
- { \
- hdb_database_lock_init (&(database_name).lock); \
- }
- #define DECLARE_HDB_DATABASE_FIRSTRUN(database_name) \
- static struct hdb_handle_database (database_name) = { \
- .first_run = 1, \
- }; \
- static void database_name##_init(void)__attribute__((constructor)); \
- static void database_name##_init(void) \
- { \
- memset (&(database_name), 0, sizeof (struct hdb_handle_database));\
- hdb_database_lock_init (&(database_name).lock); \
- }
- static inline void hdb_create (
- struct hdb_handle_database *handle_database)
- {
- memset (handle_database, 0, sizeof (struct hdb_handle_database));
- hdb_database_lock_init (&handle_database->lock);
- }
- static inline void hdb_destroy (
- struct hdb_handle_database *handle_database)
- {
- free (handle_database->handles);
- hdb_database_lock_destroy (&handle_database->lock);
- memset (handle_database, 0, sizeof (struct hdb_handle_database));
- }
- static inline int hdb_handle_create (
- struct hdb_handle_database *handle_database,
- int instance_size,
- hdb_handle_t *handle_id_out)
- {
- int handle;
- unsigned int check;
- void *new_handles;
- int found = 0;
- void *instance;
- int i;
- if (handle_database->first_run == 1) {
- memset (handle_database, 0, sizeof (struct hdb_handle_database));
- hdb_database_lock_init (&handle_database->lock);
- }
- hdb_database_lock (&handle_database->lock);
- for (handle = 0; handle < handle_database->handle_count; handle++) {
- if (handle_database->handles[handle].state == HDB_HANDLE_STATE_EMPTY) {
- found = 1;
- break;
- }
- }
- if (found == 0) {
- handle_database->handle_count += 1;
- new_handles = (struct hdb_handle *)realloc (handle_database->handles,
- sizeof (struct hdb_handle) * handle_database->handle_count);
- if (new_handles == NULL) {
- hdb_database_unlock (&handle_database->lock);
- errno = ENOMEM;
- return (-1);
- }
- handle_database->handles = new_handles;
- }
- instance = (void *)malloc (instance_size);
- if (instance == 0) {
- errno = ENOMEM;
- return (-1);
- }
- /*
- * This code makes sure the random number isn't zero
- * We use 0 to specify an invalid handle out of the 1^64 address space
- * If we get 0 200 times in a row, the RNG may be broken
- */
- for (i = 0; i < 200; i++) {
- check = random();
- if (check != 0 && check != 0xffffffff) {
- break;
- }
- }
- memset (instance, 0, instance_size);
- handle_database->handles[handle].state = HDB_HANDLE_STATE_ACTIVE;
- handle_database->handles[handle].instance = instance;
- handle_database->handles[handle].ref_count = 1;
- handle_database->handles[handle].check = check;
- *handle_id_out = (((unsigned long long)(check)) << 32) | handle;
- hdb_database_unlock (&handle_database->lock);
- return (0);
- }
- static inline int hdb_handle_get (
- struct hdb_handle_database *handle_database,
- hdb_handle_t handle_in,
- void **instance)
- {
- unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
- unsigned int handle = handle_in & 0xffffffff;
- hdb_database_lock (&handle_database->lock);
- *instance = NULL;
- if (handle >= handle_database->handle_count) {
- hdb_database_unlock (&handle_database->lock);
- errno = EBADF;
- return (-1);
- }
- if (handle_database->handles[handle].state != HDB_HANDLE_STATE_ACTIVE) {
- hdb_database_unlock (&handle_database->lock);
- errno = EBADF;
- return (-1);
- }
- if (check != 0xffffffff &&
- check != handle_database->handles[handle].check) {
- hdb_database_unlock (&handle_database->lock);
- errno = EBADF;
- return (-1);
- }
- *instance = handle_database->handles[handle].instance;
- handle_database->handles[handle].ref_count += 1;
- hdb_database_unlock (&handle_database->lock);
- return (0);
- }
- static inline int hdb_handle_put (
- struct hdb_handle_database *handle_database,
- hdb_handle_t handle_in)
- {
- unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
- unsigned int handle = handle_in & 0xffffffff;
- hdb_database_lock (&handle_database->lock);
- if (handle >= handle_database->handle_count) {
- hdb_database_unlock (&handle_database->lock);
- errno = EBADF;
- return (-1);
- }
- if (check != 0xffffffff &&
- check != handle_database->handles[handle].check) {
- hdb_database_unlock (&handle_database->lock);
- errno = EBADF;
- return (-1);
- }
- handle_database->handles[handle].ref_count -= 1;
- assert (handle_database->handles[handle].ref_count >= 0);
- if (handle_database->handles[handle].ref_count == 0) {
- if (handle_database->destructor) {
- handle_database->destructor (handle_database->handles[handle].instance);
- }
- free (handle_database->handles[handle].instance);
- memset (&handle_database->handles[handle], 0, sizeof (struct hdb_handle));
- }
- hdb_database_unlock (&handle_database->lock);
- return (0);
- }
- static inline int hdb_handle_destroy (
- struct hdb_handle_database *handle_database,
- hdb_handle_t handle_in)
- {
- unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
- unsigned int handle = handle_in & 0xffffffff;
- int res;
- hdb_database_lock (&handle_database->lock);
- if (handle >= handle_database->handle_count) {
- hdb_database_unlock (&handle_database->lock);
- errno = EBADF;
- return (-1);
- }
- if (check != 0xffffffff &&
- check != handle_database->handles[handle].check) {
- hdb_database_unlock (&handle_database->lock);
- errno = EBADF;
- return (-1);
- }
- handle_database->handles[handle].state = HDB_HANDLE_STATE_PENDINGREMOVAL;
- hdb_database_unlock (&handle_database->lock);
- res = hdb_handle_put (handle_database, handle_in);
- return (res);
- }
- static inline void hdb_iterator_reset (
- struct hdb_handle_database *handle_database)
- {
- handle_database->iterator = 0;
- }
- static inline int hdb_iterator_next (
- struct hdb_handle_database *handle_database,
- void **instance,
- hdb_handle_t *handle)
- {
- int res = -1;
- while (handle_database->iterator < handle_database->handle_count) {
- *handle = ((unsigned long long)(handle_database->handles[handle_database->iterator].check) << 32) | handle_database->iterator;
- res = hdb_handle_get (
- handle_database,
- *handle,
- instance);
- handle_database->iterator += 1;
- if (res == 0) {
- break;
- }
- }
- return (res);
- }
- static inline unsigned int hdb_base_convert (hdb_handle_t handle)
- {
- return (handle & 0xffffffff);
- }
- static inline unsigned long long hdb_nocheck_convert (unsigned int handle)
- {
- unsigned long long retvalue = 0xffffffffULL << 32 | handle;
- return (retvalue);
- }
- #endif /* HDB_H_DEFINED */
|