hdb.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Red Hat, Inc.
  4. * Copyright (c) 2006 Sun Microsystems, Inc.
  5. *
  6. * All rights reserved.
  7. *
  8. * Author: Steven Dake (sdake@redhat.com)
  9. *
  10. * This software licensed under BSD license, the text of which follows:
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. * - Redistributions in binary form must reproduce the above copyright notice,
  18. * this list of conditions and the following disclaimer in the documentation
  19. * and/or other materials provided with the distribution.
  20. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  21. * contributors may be used to endorse or promote products derived from this
  22. * software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  25. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  28. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  30. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  31. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  32. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  33. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  34. * THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #ifndef HDB_H_DEFINED
  37. #define HDB_H_DEFINED
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include <assert.h>
  41. #include <pthread.h>
  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 ref_count;
  51. };
  52. struct hdb_handle_database {
  53. unsigned int handle_count;
  54. struct hdb_handle *handles;
  55. unsigned int iterator;
  56. pthread_mutex_t mutex;
  57. };
  58. static inline void hdb_create (
  59. struct hdb_handle_database *handle_database)
  60. {
  61. memset (handle_database, 0, sizeof (struct hdb_handle_database));
  62. pthread_mutex_init (&handle_database->mutex, NULL);
  63. }
  64. static inline void hdb_destroy (
  65. struct hdb_handle_database *handle_database)
  66. {
  67. if (handle_database->handles) {
  68. free (handle_database->handles);
  69. }
  70. pthread_mutex_destroy (&handle_database->mutex);
  71. memset (handle_database, 0, sizeof (struct hdb_handle_database));
  72. }
  73. static inline int hdb_handle_create (
  74. struct hdb_handle_database *handle_database,
  75. int instance_size,
  76. unsigned int *handle_id_out)
  77. {
  78. int handle;
  79. void *new_handles;
  80. int found = 0;
  81. void *instance;
  82. pthread_mutex_lock (&handle_database->mutex);
  83. for (handle = 0; handle < handle_database->handle_count; handle++) {
  84. if (handle_database->handles[handle].state == HDB_HANDLE_STATE_EMPTY) {
  85. found = 1;
  86. break;
  87. }
  88. }
  89. if (found == 0) {
  90. handle_database->handle_count += 1;
  91. new_handles = (struct hdb_handle *)realloc (handle_database->handles,
  92. sizeof (struct hdb_handle) * handle_database->handle_count);
  93. if (new_handles == NULL) {
  94. pthread_mutex_unlock (&handle_database->mutex);
  95. return (-1);
  96. }
  97. handle_database->handles = new_handles;
  98. }
  99. instance = (void *)malloc (instance_size);
  100. if (instance == 0) {
  101. return (-1);
  102. }
  103. memset (instance, 0, instance_size);
  104. handle_database->handles[handle].state = HDB_HANDLE_STATE_ACTIVE;
  105. handle_database->handles[handle].instance = instance;
  106. handle_database->handles[handle].ref_count = 1;
  107. *handle_id_out = handle;
  108. pthread_mutex_unlock (&handle_database->mutex);
  109. return (0);
  110. }
  111. static inline int hdb_handle_get (
  112. struct hdb_handle_database *handle_database,
  113. unsigned int handle,
  114. void **instance)
  115. {
  116. pthread_mutex_lock (&handle_database->mutex);
  117. *instance = NULL;
  118. if (handle >= handle_database->handle_count) {
  119. pthread_mutex_unlock (&handle_database->mutex);
  120. return (-1);
  121. }
  122. if (handle_database->handles[handle].state != HDB_HANDLE_STATE_ACTIVE) {
  123. pthread_mutex_unlock (&handle_database->mutex);
  124. return (-1);
  125. }
  126. *instance = handle_database->handles[handle].instance;
  127. handle_database->handles[handle].ref_count += 1;
  128. pthread_mutex_unlock (&handle_database->mutex);
  129. return (0);
  130. }
  131. static inline void hdb_handle_put (
  132. struct hdb_handle_database *handle_database,
  133. unsigned int handle)
  134. {
  135. pthread_mutex_lock (&handle_database->mutex);
  136. handle_database->handles[handle].ref_count -= 1;
  137. assert (handle_database->handles[handle].ref_count >= 0);
  138. if (handle_database->handles[handle].ref_count == 0) {
  139. free (handle_database->handles[handle].instance);
  140. memset (&handle_database->handles[handle], 0, sizeof (struct hdb_handle));
  141. }
  142. pthread_mutex_unlock (&handle_database->mutex);
  143. }
  144. static inline void hdb_handle_destroy (
  145. struct hdb_handle_database *handle_database,
  146. unsigned int handle)
  147. {
  148. pthread_mutex_lock (&handle_database->mutex);
  149. handle_database->handles[handle].state = HDB_HANDLE_STATE_PENDINGREMOVAL;
  150. pthread_mutex_unlock (&handle_database->mutex);
  151. hdb_handle_put (handle_database, handle);
  152. }
  153. static inline void hdb_iterator_reset (
  154. struct hdb_handle_database *handle_database)
  155. {
  156. handle_database->iterator = 0;
  157. }
  158. static inline int hdb_iterator_next (
  159. struct hdb_handle_database *handle_database,
  160. void **instance,
  161. unsigned int *handle)
  162. {
  163. int res = -1;
  164. while (handle_database->iterator < handle_database->handle_count) {
  165. *handle = handle_database->iterator;
  166. res = hdb_handle_get (
  167. handle_database,
  168. handle_database->iterator,
  169. instance);
  170. handle_database->iterator += 1;
  171. if (res == 0) {
  172. break;
  173. }
  174. }
  175. return (res);
  176. }
  177. #endif /* HDB_H_DEFINED */