apibroker.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /* ABAPI Broker */
  13. /* Pete Rowley */
  14. #include "stdio.h"
  15. #include "slap.h"
  16. #include "prlock.h"
  17. #include "prerror.h"
  18. #include "prcvar.h"
  19. #include "prio.h"
  20. static Slapi_Mutex *buffer_lock = 0;
  21. /* circular api buffer */
  22. typedef struct _THEABAPI
  23. {
  24. char *guid;
  25. void **api;
  26. struct _THEABAPI *next;
  27. struct _THEABAPI *prev;
  28. } ABAPI;
  29. typedef struct _API_FEATURES
  30. {
  31. int refcount;
  32. slapi_apib_callback_on_zero callback_on_zero;
  33. Slapi_Mutex *lock;
  34. } APIB_FEATURES;
  35. static ABAPI *head = NULL;
  36. static ABAPI **ABAPIBroker_FindInterface(char *guid);
  37. static void ***ABAPIBroker_FindInterface_All(char *guid);
  38. int slapi_apib_register(char *guid, void **api )
  39. {
  40. int ret = -1;
  41. ABAPI *item;
  42. if(buffer_lock == 0)
  43. {
  44. if(0 == (buffer_lock = slapi_new_mutex())) /* we never free this mutex */
  45. /* badness */
  46. return -1;
  47. }
  48. /* simple - we don't check for duplicates */
  49. item = (ABAPI*)slapi_ch_malloc(sizeof(ABAPI));
  50. if(item)
  51. {
  52. item->guid = guid;
  53. item->api = api;
  54. slapi_lock_mutex(buffer_lock);
  55. if(head == NULL)
  56. {
  57. head = item;
  58. head->next = head;
  59. head->prev = head;
  60. }
  61. else
  62. {
  63. item->next = head;
  64. item->prev = head->prev;
  65. head->prev = item;
  66. item->prev->next = item;
  67. }
  68. slapi_unlock_mutex(buffer_lock);
  69. ret = 0;
  70. }
  71. return ret;
  72. }
  73. int slapi_apib_unregister(char *guid)
  74. {
  75. int ret = -1;
  76. ABAPI **api;
  77. if(buffer_lock == 0)
  78. return ret;
  79. if(buffer_lock == 0)
  80. {
  81. if(0 == (buffer_lock = slapi_new_mutex())) /* we never free this mutex */
  82. /* badness */
  83. return -1;
  84. }
  85. slapi_lock_mutex(buffer_lock);
  86. if((api = ABAPIBroker_FindInterface(guid)) != NULL)
  87. {
  88. (*api)->prev->next = (*api)->next;
  89. (*api)->next->prev = (*api)->prev;
  90. if(*api == head)
  91. {
  92. head = (*api)->next;
  93. }
  94. if(*api == head) /* must be the last item, turn off the lights */
  95. head = 0;
  96. (*api)->guid = NULL;
  97. (*api)->api = NULL;
  98. slapi_ch_free((void**)api);
  99. *api = 0;
  100. ret = 0;
  101. }
  102. slapi_unlock_mutex(buffer_lock);
  103. return ret;
  104. }
  105. int slapi_apib_get_interface(char *guid, void ***api)
  106. {
  107. int ret = -1;
  108. ABAPI **theapi;
  109. if(buffer_lock == 0)
  110. return ret;
  111. if(buffer_lock == 0)
  112. {
  113. if(0 == (buffer_lock = slapi_new_mutex())) /* we never free this mutex */
  114. /* badness */
  115. return -1;
  116. }
  117. slapi_lock_mutex(buffer_lock);
  118. if((theapi = ABAPIBroker_FindInterface(guid)) != NULL)
  119. {
  120. *api = (*theapi)->api;
  121. if((*api)[0])
  122. {
  123. slapi_apib_addref(*api);
  124. }
  125. ret = 0;
  126. }
  127. slapi_unlock_mutex(buffer_lock);
  128. return ret;
  129. }
  130. int slapi_apib_get_interface_all(char *guid, void ****api)
  131. {
  132. void ***retapi = NULL;
  133. int idx = 0;
  134. if(buffer_lock == 0)
  135. return -1;
  136. if(buffer_lock == 0)
  137. {
  138. if(0 == (buffer_lock = slapi_new_mutex())) /* we never free this mutex */
  139. /* badness */
  140. return -1;
  141. }
  142. slapi_lock_mutex(buffer_lock);
  143. retapi = ABAPIBroker_FindInterface_All(guid);
  144. for (idx = 0; retapi && retapi[idx]; ++idx) {
  145. void **theapi = retapi[idx];
  146. if(theapi[0])
  147. {
  148. slapi_apib_addref(theapi);
  149. }
  150. }
  151. *api = retapi;
  152. slapi_unlock_mutex(buffer_lock);
  153. return 0;
  154. }
  155. int slapi_apib_make_reference_counted(void **api, slapi_apib_callback_on_zero callback_on_zero)
  156. {
  157. int ret = -1;
  158. if(api[0] == 0)
  159. {
  160. api[0] = slapi_ch_malloc(sizeof(APIB_FEATURES));
  161. if(api[0])
  162. {
  163. ((APIB_FEATURES*)(api[0]))->lock = slapi_new_mutex();
  164. if(((APIB_FEATURES*)(api[0]))->lock)
  165. {
  166. ((APIB_FEATURES*)(api[0]))->refcount = 0; /* the ref count */
  167. ((APIB_FEATURES*)(api[0]))->callback_on_zero = callback_on_zero;
  168. ret = 0;
  169. }
  170. else
  171. slapi_ch_free(&(api[0]));
  172. }
  173. }
  174. return ret;
  175. }
  176. int slapi_apib_addref(void **api)
  177. {
  178. int ret;
  179. slapi_lock_mutex(((APIB_FEATURES*)(api[0]))->lock);
  180. ret = ++(((APIB_FEATURES*)(api[0]))->refcount);
  181. slapi_unlock_mutex(((APIB_FEATURES*)(api[0]))->lock);
  182. return ret;
  183. }
  184. int slapi_apib_release(void **api)
  185. {
  186. APIB_FEATURES *features;
  187. int ret;
  188. slapi_lock_mutex(((APIB_FEATURES*)(api[0]))->lock);
  189. ret = --(((APIB_FEATURES*)(api[0]))->refcount);
  190. if(((APIB_FEATURES*)(api[0]))->refcount == 0 && ((APIB_FEATURES*)(api[0]))->callback_on_zero)
  191. {
  192. /* save our stuff for when it gets zapped */
  193. features = (APIB_FEATURES*)api[0];
  194. if(0==((APIB_FEATURES*)(api[0]))->callback_on_zero(api)) /* this should deregister the interface */
  195. {
  196. slapi_unlock_mutex(features->lock);
  197. slapi_destroy_mutex(features->lock);
  198. slapi_ch_free((void **)&features);
  199. }
  200. else
  201. slapi_unlock_mutex(features->lock);
  202. }
  203. else
  204. slapi_unlock_mutex(((APIB_FEATURES*)(api[0]))->lock);
  205. return ret;
  206. }
  207. static ABAPI **ABAPIBroker_FindInterface(char *guid)
  208. {
  209. static ABAPI *api = 0; /* simple gut feeling optimization for constant calls on same api */
  210. ABAPI *start_api = api;
  211. if(!api) {
  212. start_api = api = head;
  213. }
  214. if(api)
  215. {
  216. do
  217. {
  218. if(0 == strcmp(guid, api->guid))
  219. {
  220. return &api;
  221. }
  222. api = api->next;
  223. }
  224. while(api != start_api);
  225. }
  226. return 0;
  227. }
  228. static void ***ABAPIBroker_FindInterface_All(char *guid)
  229. {
  230. ABAPI *api = NULL;
  231. ABAPI *start_api = head;
  232. void ***apilist = NULL;
  233. int idx = 0;
  234. api = start_api;
  235. if (!api) {
  236. return NULL;
  237. }
  238. do {
  239. if(0 == strcmp(guid, api->guid)) {
  240. apilist = (void ***)slapi_ch_realloc((char *)apilist, (idx+2)*sizeof(void **));
  241. apilist[idx++] = api->api;
  242. apilist[idx] = NULL;
  243. }
  244. api = api->next;
  245. } while(api != start_api);
  246. return apilist;
  247. }