hdb.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006-2009 Red Hat, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@redhat.com)
  8. *
  9. * This software licensed under BSD license, the text of which follows:
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above copyright notice,
  15. * this list of conditions and the following disclaimer.
  16. * - Redistributions in binary form must reproduce the above copyright notice,
  17. * this list of conditions and the following disclaimer in the documentation
  18. * and/or other materials provided with the distribution.
  19. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  20. * contributors may be used to endorse or promote products derived from this
  21. * software without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  33. * THE POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. #ifndef HDB_H_DEFINED
  36. #define HDB_H_DEFINED
  37. #include <errno.h>
  38. #include <assert.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <pthread.h>
  42. #include <stdint.h>
  43. #include <inttypes.h>
  44. typedef uint64_t hdb_handle_t;
  45. /*
  46. * Formatting for string printing on 32/64 bit systems
  47. */
  48. #define HDB_D_FORMAT "%"PRIu64
  49. #define HDB_X_FORMAT "%"PRIx64
  50. enum HDB_HANDLE_STATE {
  51. HDB_HANDLE_STATE_EMPTY,
  52. HDB_HANDLE_STATE_PENDINGREMOVAL,
  53. HDB_HANDLE_STATE_ACTIVE
  54. };
  55. struct hdb_handle {
  56. int state;
  57. void *instance;
  58. int check;
  59. int ref_count;
  60. };
  61. struct hdb_handle_database {
  62. unsigned int handle_count;
  63. struct hdb_handle *handles;
  64. unsigned int iterator;
  65. void (*destructor) (void *);
  66. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  67. pthread_spinlock_t lock;
  68. #else
  69. pthread_mutex_t lock;
  70. #endif
  71. unsigned int first_run;
  72. };
  73. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  74. static inline void hdb_database_lock (pthread_spinlock_t *spinlock)
  75. {
  76. pthread_spin_lock (spinlock);
  77. }
  78. static inline void hdb_database_unlock (pthread_spinlock_t *spinlock)
  79. {
  80. pthread_spin_unlock (spinlock);
  81. }
  82. static inline void hdb_database_lock_init (pthread_spinlock_t *spinlock)
  83. {
  84. pthread_spin_init (spinlock, 0);
  85. }
  86. static inline void hdb_database_lock_destroy (pthread_spinlock_t *spinlock)
  87. {
  88. pthread_spin_destroy (spinlock);
  89. }
  90. #else
  91. static inline void hdb_database_lock (pthread_mutex_t *mutex)
  92. {
  93. pthread_mutex_lock (mutex);
  94. }
  95. static inline void hdb_database_unlock (pthread_mutex_t *mutex)
  96. {
  97. pthread_mutex_unlock (mutex);
  98. }
  99. static inline void hdb_database_lock_init (pthread_mutex_t *mutex)
  100. {
  101. pthread_mutex_init (mutex, NULL);
  102. }
  103. static inline void hdb_database_lock_destroy (pthread_mutex_t *mutex)
  104. {
  105. pthread_mutex_destroy (mutex);
  106. }
  107. #endif
  108. #define DECLARE_HDB_DATABASE(database_name,destructor_function) \
  109. static struct hdb_handle_database (database_name) = { \
  110. .handle_count = 0, \
  111. .handles = NULL, \
  112. .iterator = 0, \
  113. .destructor = destructor_function, \
  114. .first_run = 0 \
  115. }; \
  116. static void database_name##_init(void)__attribute__((constructor)); \
  117. static void database_name##_init(void) \
  118. { \
  119. hdb_database_lock_init (&(database_name).lock); \
  120. }
  121. #define DECLARE_HDB_DATABASE_FIRSTRUN(database_name) \
  122. static struct hdb_handle_database (database_name) = { \
  123. .first_run = 1, \
  124. }; \
  125. static void database_name##_init(void)__attribute__((constructor)); \
  126. static void database_name##_init(void) \
  127. { \
  128. memset (&(database_name), 0, sizeof (struct hdb_handle_database));\
  129. hdb_database_lock_init (&(database_name).lock); \
  130. }
  131. static inline void hdb_create (
  132. struct hdb_handle_database *handle_database)
  133. {
  134. memset (handle_database, 0, sizeof (struct hdb_handle_database));
  135. hdb_database_lock_init (&handle_database->lock);
  136. }
  137. static inline void hdb_destroy (
  138. struct hdb_handle_database *handle_database)
  139. {
  140. free (handle_database->handles);
  141. hdb_database_lock_destroy (&handle_database->lock);
  142. memset (handle_database, 0, sizeof (struct hdb_handle_database));
  143. }
  144. static inline int hdb_handle_create (
  145. struct hdb_handle_database *handle_database,
  146. int instance_size,
  147. hdb_handle_t *handle_id_out)
  148. {
  149. int handle;
  150. unsigned int check;
  151. void *new_handles;
  152. int found = 0;
  153. void *instance;
  154. int i;
  155. if (handle_database->first_run == 1) {
  156. memset (handle_database, 0, sizeof (struct hdb_handle_database));
  157. hdb_database_lock_init (&handle_database->lock);
  158. }
  159. hdb_database_lock (&handle_database->lock);
  160. for (handle = 0; handle < handle_database->handle_count; handle++) {
  161. if (handle_database->handles[handle].state == HDB_HANDLE_STATE_EMPTY) {
  162. found = 1;
  163. break;
  164. }
  165. }
  166. if (found == 0) {
  167. handle_database->handle_count += 1;
  168. new_handles = (struct hdb_handle *)realloc (handle_database->handles,
  169. sizeof (struct hdb_handle) * handle_database->handle_count);
  170. if (new_handles == NULL) {
  171. hdb_database_unlock (&handle_database->lock);
  172. errno = ENOMEM;
  173. return (-1);
  174. }
  175. handle_database->handles = new_handles;
  176. }
  177. instance = (void *)malloc (instance_size);
  178. if (instance == 0) {
  179. errno = ENOMEM;
  180. return (-1);
  181. }
  182. /*
  183. * This code makes sure the random number isn't zero
  184. * We use 0 to specify an invalid handle out of the 1^64 address space
  185. * If we get 0 200 times in a row, the RNG may be broken
  186. */
  187. for (i = 0; i < 200; i++) {
  188. check = random();
  189. if (check != 0 && check != 0xffffffff) {
  190. break;
  191. }
  192. }
  193. memset (instance, 0, instance_size);
  194. handle_database->handles[handle].state = HDB_HANDLE_STATE_ACTIVE;
  195. handle_database->handles[handle].instance = instance;
  196. handle_database->handles[handle].ref_count = 1;
  197. handle_database->handles[handle].check = check;
  198. *handle_id_out = (((unsigned long long)(check)) << 32) | handle;
  199. hdb_database_unlock (&handle_database->lock);
  200. return (0);
  201. }
  202. static inline int hdb_handle_get (
  203. struct hdb_handle_database *handle_database,
  204. hdb_handle_t handle_in,
  205. void **instance)
  206. {
  207. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  208. unsigned int handle = handle_in & 0xffffffff;
  209. hdb_database_lock (&handle_database->lock);
  210. *instance = NULL;
  211. if (handle >= handle_database->handle_count) {
  212. hdb_database_unlock (&handle_database->lock);
  213. errno = EBADF;
  214. return (-1);
  215. }
  216. if (handle_database->handles[handle].state != HDB_HANDLE_STATE_ACTIVE) {
  217. hdb_database_unlock (&handle_database->lock);
  218. errno = EBADF;
  219. return (-1);
  220. }
  221. if (check != 0xffffffff &&
  222. check != handle_database->handles[handle].check) {
  223. hdb_database_unlock (&handle_database->lock);
  224. errno = EBADF;
  225. return (-1);
  226. }
  227. *instance = handle_database->handles[handle].instance;
  228. handle_database->handles[handle].ref_count += 1;
  229. hdb_database_unlock (&handle_database->lock);
  230. return (0);
  231. }
  232. static inline int hdb_handle_put (
  233. struct hdb_handle_database *handle_database,
  234. hdb_handle_t handle_in)
  235. {
  236. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  237. unsigned int handle = handle_in & 0xffffffff;
  238. hdb_database_lock (&handle_database->lock);
  239. if (handle >= handle_database->handle_count) {
  240. hdb_database_unlock (&handle_database->lock);
  241. errno = EBADF;
  242. return (-1);
  243. }
  244. if (check != 0xffffffff &&
  245. check != handle_database->handles[handle].check) {
  246. hdb_database_unlock (&handle_database->lock);
  247. errno = EBADF;
  248. return (-1);
  249. }
  250. handle_database->handles[handle].ref_count -= 1;
  251. assert (handle_database->handles[handle].ref_count >= 0);
  252. if (handle_database->handles[handle].ref_count == 0) {
  253. if (handle_database->destructor) {
  254. handle_database->destructor (handle_database->handles[handle].instance);
  255. }
  256. free (handle_database->handles[handle].instance);
  257. memset (&handle_database->handles[handle], 0, sizeof (struct hdb_handle));
  258. }
  259. hdb_database_unlock (&handle_database->lock);
  260. return (0);
  261. }
  262. static inline int hdb_handle_destroy (
  263. struct hdb_handle_database *handle_database,
  264. hdb_handle_t handle_in)
  265. {
  266. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  267. unsigned int handle = handle_in & 0xffffffff;
  268. int res;
  269. hdb_database_lock (&handle_database->lock);
  270. if (handle >= handle_database->handle_count) {
  271. hdb_database_unlock (&handle_database->lock);
  272. errno = EBADF;
  273. return (-1);
  274. }
  275. if (check != 0xffffffff &&
  276. check != handle_database->handles[handle].check) {
  277. hdb_database_unlock (&handle_database->lock);
  278. errno = EBADF;
  279. return (-1);
  280. }
  281. handle_database->handles[handle].state = HDB_HANDLE_STATE_PENDINGREMOVAL;
  282. hdb_database_unlock (&handle_database->lock);
  283. res = hdb_handle_put (handle_database, handle_in);
  284. return (res);
  285. }
  286. static inline void hdb_iterator_reset (
  287. struct hdb_handle_database *handle_database)
  288. {
  289. handle_database->iterator = 0;
  290. }
  291. static inline int hdb_iterator_next (
  292. struct hdb_handle_database *handle_database,
  293. void **instance,
  294. hdb_handle_t *handle)
  295. {
  296. int res = -1;
  297. while (handle_database->iterator < handle_database->handle_count) {
  298. *handle = ((unsigned long long)(handle_database->handles[handle_database->iterator].check) << 32) | handle_database->iterator;
  299. res = hdb_handle_get (
  300. handle_database,
  301. *handle,
  302. instance);
  303. handle_database->iterator += 1;
  304. if (res == 0) {
  305. break;
  306. }
  307. }
  308. return (res);
  309. }
  310. static inline unsigned int hdb_base_convert (hdb_handle_t handle)
  311. {
  312. return (handle & 0xffffffff);
  313. }
  314. static inline unsigned long long hdb_nocheck_convert (unsigned int handle)
  315. {
  316. unsigned long long retvalue = 0xffffffffULL << 32 | handle;
  317. return (retvalue);
  318. }
  319. #endif /* HDB_H_DEFINED */