4
0

hdb.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2002-2006 MontaVista Software, Inc.
  3. * Copyright (c) 2006 Sun Microsystems, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Author: Steven Dake (sdake@mvista.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. enum HDB_HANDLE_STATE {
  42. HDB_HANDLE_STATE_EMPTY,
  43. HDB_HANDLE_STATE_PENDINGREMOVAL,
  44. HDB_HANDLE_STATE_ACTIVE
  45. };
  46. struct hdb_handle {
  47. int state;
  48. void *instance;
  49. int ref_count;
  50. };
  51. struct hdb_handle_database {
  52. unsigned int handle_count;
  53. struct hdb_handle *handles;
  54. unsigned int iterator;
  55. pthread_mutex_t mutex;
  56. };
  57. static inline void hdb_create (
  58. struct hdb_handle_database *handle_database)
  59. {
  60. memset (handle_database, 0, sizeof (struct hdb_handle_database));
  61. pthread_mutex_init (&handle_database->mutex, NULL);
  62. }
  63. static inline void hdb_destroy (
  64. struct hdb_handle_database *handle_database)
  65. {
  66. if (handle_database->handles) {
  67. free (handle_database->handles);
  68. }
  69. memset (handle_database, 0, sizeof (struct hdb_handle_database));
  70. }
  71. static inline int hdb_handle_create (
  72. struct hdb_handle_database *handle_database,
  73. int instance_size,
  74. unsigned int *handle_id_out)
  75. {
  76. int handle;
  77. void *new_handles;
  78. int found = 0;
  79. void *instance;
  80. pthread_mutex_lock (&handle_database->mutex);
  81. for (handle = 0; handle < handle_database->handle_count; handle++) {
  82. if (handle_database->handles[handle].state == HDB_HANDLE_STATE_EMPTY) {
  83. found = 1;
  84. break;
  85. }
  86. }
  87. if (found == 0) {
  88. handle_database->handle_count += 1;
  89. new_handles = (struct hdb_handle *)realloc (handle_database->handles,
  90. sizeof (struct hdb_handle) * handle_database->handle_count);
  91. if (new_handles == NULL) {
  92. pthread_mutex_unlock (&handle_database->mutex);
  93. return (-1);
  94. }
  95. handle_database->handles = new_handles;
  96. }
  97. instance = (void *)malloc (instance_size);
  98. if (instance == 0) {
  99. return (-1);
  100. }
  101. memset (instance, 0, instance_size);
  102. handle_database->handles[handle].state = HDB_HANDLE_STATE_ACTIVE;
  103. handle_database->handles[handle].instance = instance;
  104. handle_database->handles[handle].ref_count = 1;
  105. *handle_id_out = handle;
  106. pthread_mutex_unlock (&handle_database->mutex);
  107. return (0);
  108. }
  109. static inline int hdb_handle_get (
  110. struct hdb_handle_database *handle_database,
  111. unsigned int handle,
  112. void **instance)
  113. {
  114. pthread_mutex_lock (&handle_database->mutex);
  115. *instance = NULL;
  116. if (handle >= handle_database->handle_count) {
  117. pthread_mutex_unlock (&handle_database->mutex);
  118. return (-1);
  119. }
  120. if (handle_database->handles[handle].state != HDB_HANDLE_STATE_ACTIVE) {
  121. pthread_mutex_unlock (&handle_database->mutex);
  122. return (-1);
  123. }
  124. *instance = handle_database->handles[handle].instance;
  125. handle_database->handles[handle].ref_count += 1;
  126. pthread_mutex_unlock (&handle_database->mutex);
  127. return (0);
  128. }
  129. static inline void hdb_handle_put (
  130. struct hdb_handle_database *handle_database,
  131. unsigned int handle)
  132. {
  133. pthread_mutex_lock (&handle_database->mutex);
  134. handle_database->handles[handle].ref_count -= 1;
  135. assert (handle_database->handles[handle].ref_count >= 0);
  136. if (handle_database->handles[handle].ref_count == 0) {
  137. free (handle_database->handles[handle].instance);
  138. memset (&handle_database->handles[handle], 0, sizeof (struct hdb_handle));
  139. }
  140. pthread_mutex_unlock (&handle_database->mutex);
  141. }
  142. static inline void hdb_handle_destroy (
  143. struct hdb_handle_database *handle_database,
  144. unsigned int handle)
  145. {
  146. pthread_mutex_lock (&handle_database->mutex);
  147. handle_database->handles[handle].state = HDB_HANDLE_STATE_PENDINGREMOVAL;
  148. pthread_mutex_unlock (&handle_database->mutex);
  149. hdb_handle_put (handle_database, handle);
  150. }
  151. static inline void hdb_iterator_reset (
  152. struct hdb_handle_database *handle_database)
  153. {
  154. handle_database->iterator = 0;
  155. }
  156. static inline int hdb_iterator_next (
  157. struct hdb_handle_database *handle_database,
  158. void **instance,
  159. unsigned int *handle)
  160. {
  161. int res = -1;
  162. while (handle_database->iterator < handle_database->handle_count) {
  163. *handle = handle_database->iterator;
  164. res = hdb_handle_get (
  165. handle_database,
  166. handle_database->iterator,
  167. instance);
  168. handle_database->iterator += 1;
  169. if (res == 0) {
  170. break;
  171. }
  172. }
  173. return (res);
  174. }
  175. #endif /* HDB_H_DEFINED */