hdb.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 <stdlib.h>
  38. #include <string.h>
  39. #include <assert.h>
  40. #include <pthread.h>
  41. typedef unsigned long long hdb_handle_t;
  42. enum HDB_HANDLE_STATE {
  43. HDB_HANDLE_STATE_EMPTY,
  44. HDB_HANDLE_STATE_PENDINGREMOVAL,
  45. HDB_HANDLE_STATE_ACTIVE
  46. };
  47. struct hdb_handle {
  48. int state;
  49. void *instance;
  50. int check;
  51. int ref_count;
  52. };
  53. struct hdb_handle_database {
  54. unsigned int handle_count;
  55. struct hdb_handle *handles;
  56. unsigned int iterator;
  57. pthread_mutex_t mutex;
  58. };
  59. static inline void hdb_create (
  60. struct hdb_handle_database *handle_database)
  61. {
  62. memset (handle_database, 0, sizeof (struct hdb_handle_database));
  63. pthread_mutex_init (&handle_database->mutex, NULL);
  64. }
  65. static inline void hdb_destroy (
  66. struct hdb_handle_database *handle_database)
  67. {
  68. if (handle_database->handles) {
  69. free (handle_database->handles);
  70. }
  71. pthread_mutex_destroy (&handle_database->mutex);
  72. memset (handle_database, 0, sizeof (struct hdb_handle_database));
  73. }
  74. static inline int hdb_handle_create (
  75. struct hdb_handle_database *handle_database,
  76. int instance_size,
  77. hdb_handle_t *handle_id_out)
  78. {
  79. int handle;
  80. unsigned int check;
  81. void *new_handles;
  82. int found = 0;
  83. void *instance;
  84. int i;
  85. pthread_mutex_lock (&handle_database->mutex);
  86. for (handle = 0; handle < handle_database->handle_count; handle++) {
  87. if (handle_database->handles[handle].state == HDB_HANDLE_STATE_EMPTY) {
  88. found = 1;
  89. break;
  90. }
  91. }
  92. if (found == 0) {
  93. handle_database->handle_count += 1;
  94. new_handles = (struct hdb_handle *)realloc (handle_database->handles,
  95. sizeof (struct hdb_handle) * handle_database->handle_count);
  96. if (new_handles == NULL) {
  97. pthread_mutex_unlock (&handle_database->mutex);
  98. return (-1);
  99. }
  100. handle_database->handles = new_handles;
  101. }
  102. instance = (void *)malloc (instance_size);
  103. if (instance == 0) {
  104. return (-1);
  105. }
  106. /*
  107. * This code makes sure the random number isn't zero
  108. * We use 0 to specify an invalid handle out of the 1^64 address space
  109. * If we get 0 200 times in a row, the RNG may be broken
  110. */
  111. for (i = 0; i < 200; i++) {
  112. check = random();
  113. if (check != 0 && check != 0xffffffff) {
  114. break;
  115. }
  116. }
  117. memset (instance, 0, instance_size);
  118. handle_database->handles[handle].state = HDB_HANDLE_STATE_ACTIVE;
  119. handle_database->handles[handle].instance = instance;
  120. handle_database->handles[handle].ref_count = 1;
  121. handle_database->handles[handle].check = check;
  122. *handle_id_out = (((unsigned long long)(check)) << 32) | handle;
  123. pthread_mutex_unlock (&handle_database->mutex);
  124. return (0);
  125. }
  126. static inline int hdb_handle_get (
  127. struct hdb_handle_database *handle_database,
  128. hdb_handle_t handle_in,
  129. void **instance)
  130. {
  131. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  132. unsigned int handle = handle_in & 0xffffffff;
  133. pthread_mutex_lock (&handle_database->mutex);
  134. if (check != 0xffffffff &&
  135. check != handle_database->handles[handle].check) {
  136. pthread_mutex_unlock (&handle_database->mutex);
  137. return (-1);
  138. }
  139. *instance = NULL;
  140. if (handle >= handle_database->handle_count) {
  141. pthread_mutex_unlock (&handle_database->mutex);
  142. return (-1);
  143. }
  144. if (handle_database->handles[handle].state != HDB_HANDLE_STATE_ACTIVE) {
  145. pthread_mutex_unlock (&handle_database->mutex);
  146. return (-1);
  147. }
  148. *instance = handle_database->handles[handle].instance;
  149. handle_database->handles[handle].ref_count += 1;
  150. pthread_mutex_unlock (&handle_database->mutex);
  151. return (0);
  152. }
  153. static inline int hdb_handle_put (
  154. struct hdb_handle_database *handle_database,
  155. hdb_handle_t handle_in)
  156. {
  157. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  158. unsigned int handle = handle_in & 0xffffffff;
  159. pthread_mutex_lock (&handle_database->mutex);
  160. if (check != 0xffffffff &&
  161. check != handle_database->handles[handle].check) {
  162. pthread_mutex_unlock (&handle_database->mutex);
  163. return (-1);
  164. }
  165. handle_database->handles[handle].ref_count -= 1;
  166. assert (handle_database->handles[handle].ref_count >= 0);
  167. if (handle_database->handles[handle].ref_count == 0) {
  168. free (handle_database->handles[handle].instance);
  169. memset (&handle_database->handles[handle], 0, sizeof (struct hdb_handle));
  170. }
  171. pthread_mutex_unlock (&handle_database->mutex);
  172. return (0);
  173. }
  174. static inline int hdb_handle_destroy (
  175. struct hdb_handle_database *handle_database,
  176. hdb_handle_t handle_in)
  177. {
  178. unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
  179. unsigned int handle = handle_in & 0xffffffff;
  180. int res;
  181. pthread_mutex_lock (&handle_database->mutex);
  182. if (check != 0xffffffff &&
  183. check != handle_database->handles[handle].check) {
  184. pthread_mutex_unlock (&handle_database->mutex);
  185. return (-1);
  186. }
  187. handle_database->handles[handle].state = HDB_HANDLE_STATE_PENDINGREMOVAL;
  188. pthread_mutex_unlock (&handle_database->mutex);
  189. res = hdb_handle_put (handle_database, handle);
  190. return (res);
  191. }
  192. static inline void hdb_iterator_reset (
  193. struct hdb_handle_database *handle_database)
  194. {
  195. handle_database->iterator = 0;
  196. }
  197. static inline int hdb_iterator_next (
  198. struct hdb_handle_database *handle_database,
  199. void **instance,
  200. hdb_handle_t *handle)
  201. {
  202. int res = -1;
  203. while (handle_database->iterator < handle_database->handle_count) {
  204. *handle = ((unsigned long long)(handle_database->handles[handle_database->iterator].check) << 32) | handle_database->iterator;
  205. res = hdb_handle_get (
  206. handle_database,
  207. *handle,
  208. instance);
  209. handle_database->iterator += 1;
  210. if (res == 0) {
  211. break;
  212. }
  213. }
  214. return (res);
  215. }
  216. static inline unsigned int hdb_base_convert (hdb_handle_t handle)
  217. {
  218. return (handle & 0xffffffff);
  219. }
  220. static inline unsigned long long hdb_nocheck_convert (unsigned int handle)
  221. {
  222. unsigned long long retvalue = 0xffffffffULL << 32 | handle;
  223. return (retvalue);
  224. }
  225. #endif /* HDB_H_DEFINED */