hdb.h 5.0 KB

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