hdb.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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_get_always (
  222. struct hdb_handle_database *handle_database,
  223. hdb_handle_t handle_in,
  224. void **instance)
  225. {
  226. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  227. unsigned int handle = handle_in & 0xffffffff;
  228. if (handle_database->first_run == 1) {
  229. handle_database->first_run = 0;
  230. hdb_database_lock_init (&handle_database->lock);
  231. }
  232. hdb_database_lock (&handle_database->lock);
  233. *instance = NULL;
  234. if (handle >= handle_database->handle_count) {
  235. hdb_database_unlock (&handle_database->lock);
  236. errno = EBADF;
  237. return (-1);
  238. }
  239. if (handle_database->handles[handle].state == HDB_HANDLE_STATE_EMPTY) {
  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. *instance = handle_database->handles[handle].instance;
  251. handle_database->handles[handle].ref_count += 1;
  252. hdb_database_unlock (&handle_database->lock);
  253. return (0);
  254. }
  255. static inline int hdb_handle_put (
  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. if (handle_database->first_run == 1) {
  262. handle_database->first_run = 0;
  263. hdb_database_lock_init (&handle_database->lock);
  264. }
  265. hdb_database_lock (&handle_database->lock);
  266. if (handle >= handle_database->handle_count) {
  267. hdb_database_unlock (&handle_database->lock);
  268. errno = EBADF;
  269. return (-1);
  270. }
  271. if (check != 0xffffffff &&
  272. check != handle_database->handles[handle].check) {
  273. hdb_database_unlock (&handle_database->lock);
  274. errno = EBADF;
  275. return (-1);
  276. }
  277. handle_database->handles[handle].ref_count -= 1;
  278. assert (handle_database->handles[handle].ref_count >= 0);
  279. if (handle_database->handles[handle].ref_count == 0) {
  280. if (handle_database->destructor) {
  281. handle_database->destructor (handle_database->handles[handle].instance);
  282. }
  283. free (handle_database->handles[handle].instance);
  284. memset (&handle_database->handles[handle], 0, sizeof (struct hdb_handle));
  285. }
  286. hdb_database_unlock (&handle_database->lock);
  287. return (0);
  288. }
  289. static inline int hdb_handle_destroy (
  290. struct hdb_handle_database *handle_database,
  291. hdb_handle_t handle_in)
  292. {
  293. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  294. unsigned int handle = handle_in & 0xffffffff;
  295. int res;
  296. if (handle_database->first_run == 1) {
  297. handle_database->first_run = 0;
  298. hdb_database_lock_init (&handle_database->lock);
  299. }
  300. hdb_database_lock (&handle_database->lock);
  301. if (handle >= handle_database->handle_count) {
  302. hdb_database_unlock (&handle_database->lock);
  303. errno = EBADF;
  304. return (-1);
  305. }
  306. if (check != 0xffffffff &&
  307. check != handle_database->handles[handle].check) {
  308. hdb_database_unlock (&handle_database->lock);
  309. errno = EBADF;
  310. return (-1);
  311. }
  312. handle_database->handles[handle].state = HDB_HANDLE_STATE_PENDINGREMOVAL;
  313. hdb_database_unlock (&handle_database->lock);
  314. res = hdb_handle_put (handle_database, handle_in);
  315. return (res);
  316. }
  317. static inline int hdb_handle_refcount_get (
  318. struct hdb_handle_database *handle_database,
  319. hdb_handle_t handle_in)
  320. {
  321. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  322. unsigned int handle = handle_in & 0xffffffff;
  323. int refcount = 0;
  324. if (handle_database->first_run == 1) {
  325. handle_database->first_run = 0;
  326. hdb_database_lock_init (&handle_database->lock);
  327. }
  328. hdb_database_lock (&handle_database->lock);
  329. if (handle >= handle_database->handle_count) {
  330. hdb_database_unlock (&handle_database->lock);
  331. errno = EBADF;
  332. return (-1);
  333. }
  334. if (check != 0xffffffff &&
  335. check != handle_database->handles[handle].check) {
  336. hdb_database_unlock (&handle_database->lock);
  337. errno = EBADF;
  338. return (-1);
  339. }
  340. refcount = handle_database->handles[handle].ref_count;
  341. hdb_database_unlock (&handle_database->lock);
  342. return (refcount);
  343. }
  344. static inline void hdb_iterator_reset (
  345. struct hdb_handle_database *handle_database)
  346. {
  347. handle_database->iterator = 0;
  348. }
  349. static inline int hdb_iterator_next (
  350. struct hdb_handle_database *handle_database,
  351. void **instance,
  352. hdb_handle_t *handle)
  353. {
  354. int res = -1;
  355. while (handle_database->iterator < handle_database->handle_count) {
  356. *handle = ((unsigned long long)(handle_database->handles[handle_database->iterator].check) << 32) | handle_database->iterator;
  357. res = hdb_handle_get (
  358. handle_database,
  359. *handle,
  360. instance);
  361. handle_database->iterator += 1;
  362. if (res == 0) {
  363. break;
  364. }
  365. }
  366. return (res);
  367. }
  368. static inline unsigned int hdb_base_convert (hdb_handle_t handle)
  369. {
  370. return (handle & 0xffffffff);
  371. }
  372. static inline unsigned long long hdb_nocheck_convert (unsigned int handle)
  373. {
  374. unsigned long long retvalue = 0xffffffffULL << 32 | handle;
  375. return (retvalue);
  376. }
  377. #endif /* HDB_H_DEFINED */