dl.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. #ifdef HAVE_CONFIG_H
  39. # include <config.h>
  40. #endif
  41. /* DataList access functions */
  42. #define INIT_ALLOC 8
  43. #define ALLOC_INCREMENT 4
  44. #include "slap.h"
  45. DataList* dl_new ()
  46. {
  47. return (DataList*)slapi_ch_malloc (sizeof (DataList));
  48. }
  49. void dl_free (DataList **dl)
  50. {
  51. slapi_ch_free ((void**) dl);
  52. }
  53. void dl_init (DataList *dl, int init_alloc)
  54. {
  55. PR_ASSERT (dl);
  56. memset (dl, 0, sizeof (*dl));
  57. if (init_alloc <= 0)
  58. dl->alloc_count = INIT_ALLOC;
  59. else
  60. dl->alloc_count = init_alloc;
  61. dl->elements = (void**)slapi_ch_calloc (dl->alloc_count, sizeof (void*));
  62. }
  63. void dl_cleanup (DataList *dl, FREEFN freefn)
  64. {
  65. PR_ASSERT (dl);
  66. if (freefn && dl->elements)
  67. {
  68. int i;
  69. for (i = 0; i < dl->element_count; i++)
  70. {
  71. freefn (&(dl->elements[i]));
  72. }
  73. }
  74. if (dl->elements)
  75. {
  76. slapi_ch_free ((void**)&dl->elements);
  77. }
  78. memset (dl, 0, sizeof (*dl));
  79. }
  80. /* index == 1 : insert first */
  81. void dl_add_index (DataList *dl, void *element, int index)
  82. {
  83. int i = 0;
  84. PR_ASSERT (dl);
  85. PR_ASSERT (element);
  86. if (dl->element_count == dl->alloc_count)
  87. {
  88. dl->alloc_count += ALLOC_INCREMENT;
  89. dl->elements = (void**)slapi_ch_realloc ((char*)dl->elements, dl->alloc_count * sizeof (void*));
  90. }
  91. dl->element_count ++;
  92. for (i = dl->element_count-1; i >= index; i--)
  93. {
  94. dl->elements[i] = dl->elements[i-1];
  95. }
  96. if ( dl->element_count < index )
  97. {
  98. /* Means that we are adding the first element */
  99. dl->elements[0] = element;
  100. }
  101. else
  102. {
  103. dl->elements[i] = element;
  104. }
  105. }
  106. void dl_add (DataList *dl, void *element)
  107. {
  108. PR_ASSERT (dl);
  109. PR_ASSERT (element);
  110. if (dl->element_count == dl->alloc_count)
  111. {
  112. dl->alloc_count += ALLOC_INCREMENT;
  113. dl->elements = (void**)slapi_ch_realloc ((char*)dl->elements, dl->alloc_count * sizeof (void*));
  114. }
  115. dl->elements[dl->element_count] = element;
  116. dl->element_count ++;
  117. }
  118. void *dl_get_first (const DataList *dl, int *cookie)
  119. {
  120. PR_ASSERT (dl);
  121. PR_ASSERT (cookie);
  122. if (dl->element_count == 0)
  123. return NULL;
  124. *cookie = 1;
  125. return dl->elements[0];
  126. }
  127. void *dl_get_next (const DataList *dl, int *cookie)
  128. {
  129. PR_ASSERT (dl);
  130. PR_ASSERT (cookie && *cookie > 0);
  131. if (*cookie >= dl->element_count)
  132. return NULL;
  133. return dl->elements[(*cookie)++];
  134. }
  135. void *dl_replace(const DataList *dl, const void *elementOld, void *elementNew, CMPFN cmpfn, FREEFN freefn)
  136. {
  137. int i;
  138. void *save;
  139. PR_ASSERT (dl);
  140. PR_ASSERT (elementOld);
  141. PR_ASSERT (elementNew);
  142. PR_ASSERT (cmpfn);
  143. for (i = 0; i < dl->element_count; i++)
  144. {
  145. if (cmpfn (dl->elements[i], elementOld) == 0)
  146. {
  147. /* if we have destructor - free the data; otherwise, return it to the client */
  148. if (freefn)
  149. {
  150. freefn (&dl->elements[i]);
  151. save = NULL;
  152. }
  153. else
  154. save = dl->elements[i];
  155. dl->elements[i] = elementNew;
  156. return save;
  157. }
  158. }
  159. return NULL;
  160. }
  161. void *dl_get (const DataList *dl, const void *element, CMPFN cmpfn)
  162. {
  163. int i;
  164. PR_ASSERT (dl);
  165. PR_ASSERT (element);
  166. PR_ASSERT (cmpfn);
  167. for (i = 0; i < dl->element_count; i++)
  168. {
  169. if (cmpfn (dl->elements[i], element) == 0)
  170. {
  171. return dl->elements[i];
  172. }
  173. }
  174. return NULL;
  175. }
  176. void *dl_delete (DataList *dl, const void *element, CMPFN cmpfn, FREEFN freefn)
  177. {
  178. int i;
  179. void *save;
  180. PR_ASSERT (dl);
  181. PR_ASSERT (element);
  182. PR_ASSERT (cmpfn);
  183. for (i = 0; i < dl->element_count; i++)
  184. {
  185. if (cmpfn (dl->elements[i], element) == 0)
  186. {
  187. /* if we have destructor - free the data; otherwise, return it to the client */
  188. if (freefn)
  189. {
  190. freefn (&dl->elements[i]);
  191. save = NULL;
  192. }
  193. else
  194. save = dl->elements[i];
  195. if (i != dl->element_count - 1)
  196. {
  197. memcpy (&dl->elements[i], &dl->elements[i+1], (dl->element_count - i - 1) * sizeof (void*));
  198. }
  199. dl->element_count --;
  200. return save;
  201. }
  202. }
  203. return NULL;
  204. }
  205. int dl_get_count (const DataList *dl)
  206. {
  207. PR_ASSERT (dl);
  208. return dl->element_count;
  209. }