hdb.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. #ifndef _GNU_SOURCE
  38. #define _GNU_SOURCE
  39. #endif
  40. #include <errno.h>
  41. #include <assert.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <pthread.h>
  45. #include <stdint.h>
  46. #include <inttypes.h>
  47. typedef uint64_t hdb_handle_t;
  48. /*
  49. * Formatting for string printing on 32/64 bit systems
  50. */
  51. #define HDB_D_FORMAT "%"PRIu64
  52. #define HDB_X_FORMAT "%"PRIx64
  53. enum HDB_HANDLE_STATE {
  54. HDB_HANDLE_STATE_EMPTY,
  55. HDB_HANDLE_STATE_PENDINGREMOVAL,
  56. HDB_HANDLE_STATE_ACTIVE
  57. };
  58. struct hdb_handle {
  59. int state;
  60. void *instance;
  61. int check;
  62. int ref_count;
  63. };
  64. struct hdb_handle_database {
  65. unsigned int handle_count;
  66. struct hdb_handle *handles;
  67. unsigned int iterator;
  68. void (*destructor) (void *);
  69. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  70. pthread_spinlock_t lock;
  71. #else
  72. pthread_mutex_t lock;
  73. #endif
  74. unsigned int first_run;
  75. };
  76. #if defined(HAVE_PTHREAD_SPIN_LOCK)
  77. static inline void hdb_database_lock (pthread_spinlock_t *spinlock)
  78. {
  79. pthread_spin_lock (spinlock);
  80. }
  81. static inline void hdb_database_unlock (pthread_spinlock_t *spinlock)
  82. {
  83. pthread_spin_unlock (spinlock);
  84. }
  85. static inline void hdb_database_lock_init (pthread_spinlock_t *spinlock)
  86. {
  87. pthread_spin_init (spinlock, 0);
  88. }
  89. static inline void hdb_database_lock_destroy (pthread_spinlock_t *spinlock)
  90. {
  91. pthread_spin_destroy (spinlock);
  92. }
  93. #else
  94. static inline void hdb_database_lock (pthread_mutex_t *mutex)
  95. {
  96. pthread_mutex_lock (mutex);
  97. }
  98. static inline void hdb_database_unlock (pthread_mutex_t *mutex)
  99. {
  100. pthread_mutex_unlock (mutex);
  101. }
  102. static inline void hdb_database_lock_init (pthread_mutex_t *mutex)
  103. {
  104. pthread_mutex_init (mutex, NULL);
  105. }
  106. static inline void hdb_database_lock_destroy (pthread_mutex_t *mutex)
  107. {
  108. pthread_mutex_destroy (mutex);
  109. }
  110. #endif
  111. #define DECLARE_HDB_DATABASE(database_name,destructor_function) \
  112. static struct hdb_handle_database (database_name) = { \
  113. .handle_count = 0, \
  114. .handles = NULL, \
  115. .iterator = 0, \
  116. .destructor = destructor_function, \
  117. .first_run = 1 \
  118. }; \
  119. static inline void hdb_create (
  120. struct hdb_handle_database *handle_database)
  121. {
  122. memset (handle_database, 0, sizeof (struct hdb_handle_database));
  123. hdb_database_lock_init (&handle_database->lock);
  124. }
  125. static inline void hdb_destroy (
  126. struct hdb_handle_database *handle_database)
  127. {
  128. free (handle_database->handles);
  129. hdb_database_lock_destroy (&handle_database->lock);
  130. memset (handle_database, 0, sizeof (struct hdb_handle_database));
  131. }
  132. static inline int hdb_handle_create (
  133. struct hdb_handle_database *handle_database,
  134. int instance_size,
  135. hdb_handle_t *handle_id_out)
  136. {
  137. int handle;
  138. unsigned int check;
  139. void *new_handles;
  140. int found = 0;
  141. void *instance;
  142. int i;
  143. if (handle_database->first_run == 1) {
  144. handle_database->first_run = 0;
  145. hdb_database_lock_init (&handle_database->lock);
  146. }
  147. hdb_database_lock (&handle_database->lock);
  148. for (handle = 0; handle < handle_database->handle_count; handle++) {
  149. if (handle_database->handles[handle].state == HDB_HANDLE_STATE_EMPTY) {
  150. found = 1;
  151. break;
  152. }
  153. }
  154. if (found == 0) {
  155. handle_database->handle_count += 1;
  156. new_handles = (struct hdb_handle *)realloc (handle_database->handles,
  157. sizeof (struct hdb_handle) * handle_database->handle_count);
  158. if (new_handles == NULL) {
  159. hdb_database_unlock (&handle_database->lock);
  160. errno = ENOMEM;
  161. return (-1);
  162. }
  163. handle_database->handles = new_handles;
  164. }
  165. instance = (void *)malloc (instance_size);
  166. if (instance == 0) {
  167. errno = ENOMEM;
  168. return (-1);
  169. }
  170. /*
  171. * This code makes sure the random number isn't zero
  172. * We use 0 to specify an invalid handle out of the 1^64 address space
  173. * If we get 0 200 times in a row, the RNG may be broken
  174. */
  175. for (i = 0; i < 200; i++) {
  176. check = random();
  177. if (check != 0 && check != 0xffffffff) {
  178. break;
  179. }
  180. }
  181. memset (instance, 0, instance_size);
  182. handle_database->handles[handle].state = HDB_HANDLE_STATE_ACTIVE;
  183. handle_database->handles[handle].instance = instance;
  184. handle_database->handles[handle].ref_count = 1;
  185. handle_database->handles[handle].check = check;
  186. *handle_id_out = (((unsigned long long)(check)) << 32) | handle;
  187. hdb_database_unlock (&handle_database->lock);
  188. return (0);
  189. }
  190. static inline int hdb_handle_get (
  191. struct hdb_handle_database *handle_database,
  192. hdb_handle_t handle_in,
  193. void **instance)
  194. {
  195. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  196. unsigned int handle = handle_in & 0xffffffff;
  197. if (handle_database->first_run == 1) {
  198. handle_database->first_run = 0;
  199. hdb_database_lock_init (&handle_database->lock);
  200. }
  201. hdb_database_lock (&handle_database->lock);
  202. *instance = NULL;
  203. if (handle >= handle_database->handle_count) {
  204. hdb_database_unlock (&handle_database->lock);
  205. errno = EBADF;
  206. return (-1);
  207. }
  208. if (handle_database->handles[handle].state != HDB_HANDLE_STATE_ACTIVE) {
  209. hdb_database_unlock (&handle_database->lock);
  210. errno = EBADF;
  211. return (-1);
  212. }
  213. if (check != 0xffffffff &&
  214. check != handle_database->handles[handle].check) {
  215. hdb_database_unlock (&handle_database->lock);
  216. errno = EBADF;
  217. return (-1);
  218. }
  219. *instance = handle_database->handles[handle].instance;
  220. handle_database->handles[handle].ref_count += 1;
  221. hdb_database_unlock (&handle_database->lock);
  222. return (0);
  223. }
  224. static inline int hdb_handle_get_always (
  225. struct hdb_handle_database *handle_database,
  226. hdb_handle_t handle_in,
  227. void **instance)
  228. {
  229. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  230. unsigned int handle = handle_in & 0xffffffff;
  231. if (handle_database->first_run == 1) {
  232. handle_database->first_run = 0;
  233. hdb_database_lock_init (&handle_database->lock);
  234. }
  235. hdb_database_lock (&handle_database->lock);
  236. *instance = NULL;
  237. if (handle >= handle_database->handle_count) {
  238. hdb_database_unlock (&handle_database->lock);
  239. errno = EBADF;
  240. return (-1);
  241. }
  242. if (handle_database->handles[handle].state == HDB_HANDLE_STATE_EMPTY) {
  243. hdb_database_unlock (&handle_database->lock);
  244. errno = EBADF;
  245. return (-1);
  246. }
  247. if (check != 0xffffffff &&
  248. check != handle_database->handles[handle].check) {
  249. hdb_database_unlock (&handle_database->lock);
  250. errno = EBADF;
  251. return (-1);
  252. }
  253. *instance = handle_database->handles[handle].instance;
  254. handle_database->handles[handle].ref_count += 1;
  255. hdb_database_unlock (&handle_database->lock);
  256. return (0);
  257. }
  258. static inline int hdb_handle_put (
  259. struct hdb_handle_database *handle_database,
  260. hdb_handle_t handle_in)
  261. {
  262. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  263. unsigned int handle = handle_in & 0xffffffff;
  264. if (handle_database->first_run == 1) {
  265. handle_database->first_run = 0;
  266. hdb_database_lock_init (&handle_database->lock);
  267. }
  268. hdb_database_lock (&handle_database->lock);
  269. if (handle >= handle_database->handle_count) {
  270. hdb_database_unlock (&handle_database->lock);
  271. errno = EBADF;
  272. return (-1);
  273. }
  274. if (check != 0xffffffff &&
  275. check != handle_database->handles[handle].check) {
  276. hdb_database_unlock (&handle_database->lock);
  277. errno = EBADF;
  278. return (-1);
  279. }
  280. handle_database->handles[handle].ref_count -= 1;
  281. assert (handle_database->handles[handle].ref_count >= 0);
  282. if (handle_database->handles[handle].ref_count == 0) {
  283. if (handle_database->destructor) {
  284. handle_database->destructor (handle_database->handles[handle].instance);
  285. }
  286. free (handle_database->handles[handle].instance);
  287. memset (&handle_database->handles[handle], 0, sizeof (struct hdb_handle));
  288. }
  289. hdb_database_unlock (&handle_database->lock);
  290. return (0);
  291. }
  292. static inline int hdb_handle_destroy (
  293. struct hdb_handle_database *handle_database,
  294. hdb_handle_t handle_in)
  295. {
  296. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  297. unsigned int handle = handle_in & 0xffffffff;
  298. int res;
  299. if (handle_database->first_run == 1) {
  300. handle_database->first_run = 0;
  301. hdb_database_lock_init (&handle_database->lock);
  302. }
  303. hdb_database_lock (&handle_database->lock);
  304. if (handle >= handle_database->handle_count) {
  305. hdb_database_unlock (&handle_database->lock);
  306. errno = EBADF;
  307. return (-1);
  308. }
  309. if (check != 0xffffffff &&
  310. check != handle_database->handles[handle].check) {
  311. hdb_database_unlock (&handle_database->lock);
  312. errno = EBADF;
  313. return (-1);
  314. }
  315. handle_database->handles[handle].state = HDB_HANDLE_STATE_PENDINGREMOVAL;
  316. hdb_database_unlock (&handle_database->lock);
  317. res = hdb_handle_put (handle_database, handle_in);
  318. return (res);
  319. }
  320. static inline int hdb_handle_refcount_get (
  321. struct hdb_handle_database *handle_database,
  322. hdb_handle_t handle_in)
  323. {
  324. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  325. unsigned int handle = handle_in & 0xffffffff;
  326. int refcount = 0;
  327. if (handle_database->first_run == 1) {
  328. handle_database->first_run = 0;
  329. hdb_database_lock_init (&handle_database->lock);
  330. }
  331. hdb_database_lock (&handle_database->lock);
  332. if (handle >= handle_database->handle_count) {
  333. hdb_database_unlock (&handle_database->lock);
  334. errno = EBADF;
  335. return (-1);
  336. }
  337. if (check != 0xffffffff &&
  338. check != handle_database->handles[handle].check) {
  339. hdb_database_unlock (&handle_database->lock);
  340. errno = EBADF;
  341. return (-1);
  342. }
  343. refcount = handle_database->handles[handle].ref_count;
  344. hdb_database_unlock (&handle_database->lock);
  345. return (refcount);
  346. }
  347. static inline void hdb_iterator_reset (
  348. struct hdb_handle_database *handle_database)
  349. {
  350. handle_database->iterator = 0;
  351. }
  352. static inline int hdb_iterator_next (
  353. struct hdb_handle_database *handle_database,
  354. void **instance,
  355. hdb_handle_t *handle)
  356. {
  357. int res = -1;
  358. while (handle_database->iterator < handle_database->handle_count) {
  359. *handle = ((unsigned long long)(handle_database->handles[handle_database->iterator].check) << 32) | handle_database->iterator;
  360. res = hdb_handle_get (
  361. handle_database,
  362. *handle,
  363. instance);
  364. handle_database->iterator += 1;
  365. if (res == 0) {
  366. break;
  367. }
  368. }
  369. return (res);
  370. }
  371. static inline unsigned int hdb_base_convert (hdb_handle_t handle)
  372. {
  373. return (handle & 0xffffffff);
  374. }
  375. static inline unsigned long long hdb_nocheck_convert (unsigned int handle)
  376. {
  377. unsigned long long retvalue = 0xffffffffULL << 32 | handle;
  378. return (retvalue);
  379. }
  380. #endif /* HDB_H_DEFINED */