hdb.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 = 1 \
  115. }; \
  116. static inline void hdb_create (
  117. struct hdb_handle_database *handle_database)
  118. {
  119. memset (handle_database, 0, sizeof (struct hdb_handle_database));
  120. hdb_database_lock_init (&handle_database->lock);
  121. }
  122. static inline void hdb_destroy (
  123. struct hdb_handle_database *handle_database)
  124. {
  125. free (handle_database->handles);
  126. hdb_database_lock_destroy (&handle_database->lock);
  127. memset (handle_database, 0, sizeof (struct hdb_handle_database));
  128. }
  129. static inline int hdb_handle_create (
  130. struct hdb_handle_database *handle_database,
  131. int instance_size,
  132. hdb_handle_t *handle_id_out)
  133. {
  134. int handle;
  135. unsigned int check;
  136. void *new_handles;
  137. int found = 0;
  138. void *instance;
  139. int i;
  140. if (handle_database->first_run == 1) {
  141. handle_database->first_run = 0;
  142. hdb_database_lock_init (&handle_database->lock);
  143. }
  144. hdb_database_lock (&handle_database->lock);
  145. for (handle = 0; handle < handle_database->handle_count; handle++) {
  146. if (handle_database->handles[handle].state == HDB_HANDLE_STATE_EMPTY) {
  147. found = 1;
  148. break;
  149. }
  150. }
  151. if (found == 0) {
  152. handle_database->handle_count += 1;
  153. new_handles = (struct hdb_handle *)realloc (handle_database->handles,
  154. sizeof (struct hdb_handle) * handle_database->handle_count);
  155. if (new_handles == NULL) {
  156. hdb_database_unlock (&handle_database->lock);
  157. errno = ENOMEM;
  158. return (-1);
  159. }
  160. handle_database->handles = new_handles;
  161. }
  162. instance = (void *)malloc (instance_size);
  163. if (instance == 0) {
  164. errno = ENOMEM;
  165. return (-1);
  166. }
  167. /*
  168. * This code makes sure the random number isn't zero
  169. * We use 0 to specify an invalid handle out of the 1^64 address space
  170. * If we get 0 200 times in a row, the RNG may be broken
  171. */
  172. for (i = 0; i < 200; i++) {
  173. check = random();
  174. if (check != 0 && check != 0xffffffff) {
  175. break;
  176. }
  177. }
  178. memset (instance, 0, instance_size);
  179. handle_database->handles[handle].state = HDB_HANDLE_STATE_ACTIVE;
  180. handle_database->handles[handle].instance = instance;
  181. handle_database->handles[handle].ref_count = 1;
  182. handle_database->handles[handle].check = check;
  183. *handle_id_out = (((unsigned long long)(check)) << 32) | handle;
  184. hdb_database_unlock (&handle_database->lock);
  185. return (0);
  186. }
  187. static inline int hdb_handle_get (
  188. struct hdb_handle_database *handle_database,
  189. hdb_handle_t handle_in,
  190. void **instance)
  191. {
  192. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  193. unsigned int handle = handle_in & 0xffffffff;
  194. if (handle_database->first_run == 1) {
  195. handle_database->first_run = 0;
  196. hdb_database_lock_init (&handle_database->lock);
  197. }
  198. hdb_database_lock (&handle_database->lock);
  199. *instance = NULL;
  200. if (handle >= handle_database->handle_count) {
  201. hdb_database_unlock (&handle_database->lock);
  202. errno = EBADF;
  203. return (-1);
  204. }
  205. if (handle_database->handles[handle].state != HDB_HANDLE_STATE_ACTIVE) {
  206. hdb_database_unlock (&handle_database->lock);
  207. errno = EBADF;
  208. return (-1);
  209. }
  210. if (check != 0xffffffff &&
  211. check != handle_database->handles[handle].check) {
  212. hdb_database_unlock (&handle_database->lock);
  213. errno = EBADF;
  214. return (-1);
  215. }
  216. *instance = handle_database->handles[handle].instance;
  217. handle_database->handles[handle].ref_count += 1;
  218. hdb_database_unlock (&handle_database->lock);
  219. return (0);
  220. }
  221. static inline int hdb_handle_put (
  222. struct hdb_handle_database *handle_database,
  223. hdb_handle_t handle_in)
  224. {
  225. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  226. unsigned int handle = handle_in & 0xffffffff;
  227. if (handle_database->first_run == 1) {
  228. handle_database->first_run = 0;
  229. hdb_database_lock_init (&handle_database->lock);
  230. }
  231. hdb_database_lock (&handle_database->lock);
  232. if (handle >= handle_database->handle_count) {
  233. hdb_database_unlock (&handle_database->lock);
  234. errno = EBADF;
  235. return (-1);
  236. }
  237. if (check != 0xffffffff &&
  238. check != handle_database->handles[handle].check) {
  239. hdb_database_unlock (&handle_database->lock);
  240. errno = EBADF;
  241. return (-1);
  242. }
  243. handle_database->handles[handle].ref_count -= 1;
  244. assert (handle_database->handles[handle].ref_count >= 0);
  245. if (handle_database->handles[handle].ref_count == 0) {
  246. if (handle_database->destructor) {
  247. handle_database->destructor (handle_database->handles[handle].instance);
  248. }
  249. free (handle_database->handles[handle].instance);
  250. memset (&handle_database->handles[handle], 0, sizeof (struct hdb_handle));
  251. }
  252. hdb_database_unlock (&handle_database->lock);
  253. return (0);
  254. }
  255. static inline int hdb_handle_destroy (
  256. struct hdb_handle_database *handle_database,
  257. hdb_handle_t handle_in)
  258. {
  259. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  260. unsigned int handle = handle_in & 0xffffffff;
  261. int res;
  262. if (handle_database->first_run == 1) {
  263. handle_database->first_run = 0;
  264. hdb_database_lock_init (&handle_database->lock);
  265. }
  266. hdb_database_lock (&handle_database->lock);
  267. if (handle >= handle_database->handle_count) {
  268. hdb_database_unlock (&handle_database->lock);
  269. errno = EBADF;
  270. return (-1);
  271. }
  272. if (check != 0xffffffff &&
  273. check != handle_database->handles[handle].check) {
  274. hdb_database_unlock (&handle_database->lock);
  275. errno = EBADF;
  276. return (-1);
  277. }
  278. handle_database->handles[handle].state = HDB_HANDLE_STATE_PENDINGREMOVAL;
  279. hdb_database_unlock (&handle_database->lock);
  280. res = hdb_handle_put (handle_database, handle_in);
  281. return (res);
  282. }
  283. static inline int hdb_handle_refcount_get (
  284. struct hdb_handle_database *handle_database,
  285. hdb_handle_t handle_in)
  286. {
  287. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  288. unsigned int handle = handle_in & 0xffffffff;
  289. int refcount = 0;
  290. if (handle_database->first_run == 1) {
  291. handle_database->first_run = 0;
  292. hdb_database_lock_init (&handle_database->lock);
  293. }
  294. hdb_database_lock (&handle_database->lock);
  295. if (handle >= handle_database->handle_count) {
  296. hdb_database_unlock (&handle_database->lock);
  297. errno = EBADF;
  298. return (-1);
  299. }
  300. if (check != 0xffffffff &&
  301. check != handle_database->handles[handle].check) {
  302. hdb_database_unlock (&handle_database->lock);
  303. errno = EBADF;
  304. return (-1);
  305. }
  306. refcount = handle_database->handles[handle].ref_count;
  307. hdb_database_unlock (&handle_database->lock);
  308. return (refcount);
  309. }
  310. static inline void hdb_iterator_reset (
  311. struct hdb_handle_database *handle_database)
  312. {
  313. handle_database->iterator = 0;
  314. }
  315. static inline int hdb_iterator_next (
  316. struct hdb_handle_database *handle_database,
  317. void **instance,
  318. hdb_handle_t *handle)
  319. {
  320. int res = -1;
  321. while (handle_database->iterator < handle_database->handle_count) {
  322. *handle = ((unsigned long long)(handle_database->handles[handle_database->iterator].check) << 32) | handle_database->iterator;
  323. res = hdb_handle_get (
  324. handle_database,
  325. *handle,
  326. instance);
  327. handle_database->iterator += 1;
  328. if (res == 0) {
  329. break;
  330. }
  331. }
  332. return (res);
  333. }
  334. static inline unsigned int hdb_base_convert (hdb_handle_t handle)
  335. {
  336. return (handle & 0xffffffff);
  337. }
  338. static inline unsigned long long hdb_nocheck_convert (unsigned int handle)
  339. {
  340. unsigned long long retvalue = 0xffffffffULL << 32 | handle;
  341. return (retvalue);
  342. }
  343. #endif /* HDB_H_DEFINED */