objset.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. #include "slapi-plugin.h"
  13. #include "slapi-private.h"
  14. /*
  15. * The "wrapper" placed around objects in our object set.
  16. */
  17. typedef struct objset_object
  18. {
  19. Object *obj; /* pointer to actual object */
  20. struct objset_object *next; /* pointer to next object in list */
  21. } objset_object;
  22. /*
  23. * The object set itself.
  24. */
  25. typedef struct objset
  26. {
  27. objset_object *head; /* pointer to linked list of objects */
  28. objset_object *tail; /* pointer to tail of linked list */
  29. PRLock *lock; /* Lock - protects addition/deletion from list */
  30. FNFree destructor; /* Destructor callback for objset itself */
  31. } objset;
  32. /* Forward declarations */
  33. static void unlinkObjsetObjectNoLock(Objset *o, objset_object *obj_to_unlink);
  34. /*
  35. * Create a new, empty object set.
  36. * Returns a pointer to the new object set, or NULL if an error occurred.
  37. */
  38. Objset *
  39. objset_new(FNFree objset_destructor)
  40. {
  41. objset *set;
  42. set = (objset *)slapi_ch_malloc(sizeof(objset));
  43. set->lock = PR_NewLock();
  44. if (NULL == set->lock)
  45. {
  46. slapi_ch_free((void **)&set);
  47. }
  48. else
  49. {
  50. set->head = set->tail = NULL;
  51. set->destructor = objset_destructor;
  52. }
  53. return set;
  54. }
  55. /*
  56. * Delete an object set. All references to contained objects
  57. * are released, and the objset is deleted.
  58. */
  59. void
  60. objset_delete(Objset **setp)
  61. {
  62. objset_object *o, *o_next;
  63. Objset *set;
  64. PR_ASSERT(NULL != setp);
  65. set = *setp;
  66. PR_ASSERT(NULL != set);
  67. PR_Lock(set->lock);
  68. o = set->head;
  69. while (NULL != o)
  70. {
  71. o_next = o->next;
  72. object_release(o->obj); /* release our reference */
  73. slapi_ch_free((void **)&o); /* Free wrapper */
  74. o = o_next;
  75. }
  76. PR_Unlock(set->lock);
  77. PR_DestroyLock(set->lock);
  78. if (NULL != set->destructor)
  79. {
  80. set->destructor((void **)setp);
  81. }
  82. slapi_ch_free((void **)setp);
  83. }
  84. /*
  85. * Add a new object to an object set.
  86. * Return values:
  87. * OBJSET_SUCCESS: the insertion was succesful.
  88. * OBJSET_ALREADY_EXISTS: the object already exists in the set.
  89. */
  90. int
  91. objset_add_obj(Objset *set, Object *object)
  92. {
  93. objset_object *p;
  94. int exists = 0;
  95. int rc = OBJSET_SUCCESS;
  96. PR_ASSERT(NULL != set);
  97. PR_ASSERT(NULL != object);
  98. PR_Lock(set->lock);
  99. /* Make sure this object isn't already in the set */
  100. p = set->head;
  101. while (NULL != p)
  102. {
  103. if (p->obj == object)
  104. {
  105. exists = 1;
  106. break;
  107. }
  108. p = p->next;
  109. }
  110. if (exists)
  111. {
  112. rc = OBJSET_ALREADY_EXISTS;
  113. }
  114. else
  115. {
  116. objset_object *new_node = (objset_object *)slapi_ch_malloc(sizeof(objset_object));
  117. object_acquire(object); /* Record our reference */
  118. new_node->obj = object;
  119. new_node->next = NULL;
  120. if (NULL == set->head)
  121. {
  122. set->head = set->tail = new_node;
  123. }
  124. else
  125. {
  126. set->tail->next = new_node;
  127. set->tail = new_node;
  128. }
  129. }
  130. PR_Unlock(set->lock);
  131. return rc;
  132. }
  133. /*
  134. * Locate an object in an object set.
  135. *
  136. * Arguments:
  137. * set: the object set to search
  138. * compare_fn: a caller-provided function used to compare the
  139. * name against an object. This function should return
  140. * a negtive value, zero, or a positive value if the
  141. * object's name is, respectively, less that, equal
  142. * to, or greater than the provided name.
  143. * name: the name (value) to find.
  144. *
  145. * The returned object, if any, is referenced. The caller must
  146. * call object_release() when finished with the object.
  147. *
  148. * Returns the object, if found. Otherwise, returns NULL.
  149. * Implementation note: since we store objects in an unordered
  150. * linked list, all that's really important is that the compare_fn
  151. * return 0 when a match is found, and non-zero otherwise.
  152. * Other types of implementations might try to optimize the
  153. * storage, e.g. binary search.
  154. */
  155. Object *
  156. objset_find(Objset *set, CMPFn compare_fn, const void *name)
  157. {
  158. objset_object *found = NULL;
  159. PR_ASSERT(NULL != set);
  160. PR_ASSERT(NULL != name);
  161. PR_ASSERT(NULL != compare_fn);
  162. PR_Lock(set->lock);
  163. found = set->head;
  164. while (NULL != found)
  165. {
  166. if (compare_fn(found->obj, name) == 0)
  167. {
  168. break;
  169. }
  170. found = found->next;
  171. }
  172. if (NULL != found)
  173. {
  174. /* acquire object */
  175. object_acquire(found->obj);
  176. }
  177. PR_Unlock(set->lock);
  178. return found == NULL ? NULL : found->obj;
  179. }
  180. /*
  181. * Remove an object from an objset.
  182. * Returns OBJSET_SUCCESS if the object was found and removed, or
  183. * OBJSET_NO_SUCH_OBJECT if the object was not found in the list.
  184. */
  185. int
  186. objset_remove_obj(Objset *set, Object *object)
  187. {
  188. int rc = OBJSET_SUCCESS;
  189. objset_object *found;
  190. PR_ASSERT(NULL != set);
  191. PR_ASSERT(NULL != object);
  192. PR_Lock(set->lock);
  193. found = set->head;
  194. while (NULL != found)
  195. {
  196. if (found->obj == object)
  197. {
  198. break;
  199. }
  200. found = found->next;
  201. }
  202. if (NULL == found)
  203. {
  204. rc = OBJSET_NO_SUCH_OBJECT;
  205. }
  206. else
  207. {
  208. Object *saved = found->obj;
  209. /* Unlink from list */
  210. unlinkObjsetObjectNoLock(set, found);
  211. /* Release reference on object */
  212. object_release(saved);
  213. }
  214. PR_Unlock(set->lock);
  215. return rc;
  216. }
  217. /*
  218. * Prepare for iteration across an object set. Returns the first
  219. * object in the set. The returned object is referenced, therefore
  220. * the caller must either release the object, or
  221. * pass it to an objset_next_obj call, which will
  222. * implicitly release the object.
  223. * Returns the first object, or NULL if the objset contains no
  224. * objects.
  225. */
  226. Object *
  227. objset_first_obj(Objset *set)
  228. {
  229. Object *return_object;
  230. /* Be tolerant (for the replication plugin) */
  231. if (set == NULL) return NULL;
  232. PR_Lock(set->lock);
  233. if (NULL == set->head)
  234. {
  235. return_object = NULL;
  236. }
  237. else
  238. {
  239. object_acquire(set->head->obj);
  240. return_object = set->head->obj;
  241. }
  242. PR_Unlock(set->lock);
  243. return return_object;
  244. }
  245. /*
  246. * Return the next object in an object set, or NULL if there are
  247. * no more objects.
  248. * The returned object is referenced, therefore
  249. * the caller must either release the object, or
  250. * pass it to an objset_next_ob call, which will
  251. * implicitly release the object.
  252. */
  253. Object *
  254. objset_next_obj(Objset *set, Object *previous)
  255. {
  256. Object *return_object = NULL;
  257. objset_object *p;
  258. PR_ASSERT(NULL != set);
  259. PR_Lock(set->lock);
  260. /* First, find the current object */
  261. p = set->head;
  262. while (NULL != p && p->obj != previous)
  263. {
  264. p = p->next;
  265. }
  266. /* Find the next object */
  267. if (NULL != p && NULL != p->next)
  268. {
  269. return_object = p->next->obj;
  270. object_acquire(return_object);
  271. }
  272. PR_Unlock(set->lock);
  273. object_release(previous); /* Release the previous object */
  274. return return_object;
  275. }
  276. /*
  277. * Return a non-zero value if the object set is empty, or
  278. * zero if the object set is empty.
  279. */
  280. int
  281. objset_is_empty(Objset *set)
  282. {
  283. int return_value;
  284. PR_ASSERT(NULL != set);
  285. PR_Lock(set->lock);
  286. return_value = (set->head == NULL);
  287. PR_Unlock(set->lock);
  288. return return_value;
  289. }
  290. /*
  291. * Return the count of objects in the object set.
  292. */
  293. int objset_size(Objset *set)
  294. {
  295. int count = 0;
  296. objset_object *p;
  297. PR_ASSERT(NULL != set);
  298. PR_Lock(set->lock);
  299. for (p = set->head; p; p = p->next)
  300. count++;
  301. PR_Unlock(set->lock);
  302. return count;
  303. }
  304. /*
  305. * Utility function: remove object from list.
  306. * This needs to be called with the objset locked.
  307. */
  308. static void
  309. unlinkObjsetObjectNoLock(Objset *o, objset_object *obj_to_unlink)
  310. {
  311. objset_object *p = o->head;
  312. PR_ASSERT(NULL != o->head);
  313. /* Unlink from list */
  314. if (o->head == obj_to_unlink) {
  315. /* Object to unlink was at head of list */
  316. p = o->head->next;
  317. o->head = obj_to_unlink->next;
  318. } else {
  319. while (NULL != p->next && p->next != obj_to_unlink) {
  320. p = p->next;
  321. }
  322. if (NULL != p->next)
  323. {
  324. /* p points to object prior to one being removed */
  325. p->next = p->next->next;
  326. }
  327. }
  328. if (o->tail == obj_to_unlink)
  329. {
  330. o->tail = p;
  331. }
  332. /* Free the wrapper */
  333. slapi_ch_free((void **)&obj_to_unlink);
  334. }