hdb.h 11 KB

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