dl.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. /* DataList access functions */
  39. #define INIT_ALLOC 8
  40. #define ALLOC_INCREMENT 4
  41. #include "slap.h"
  42. DataList* dl_new ()
  43. {
  44. return (DataList*)slapi_ch_malloc (sizeof (DataList));
  45. }
  46. void dl_free (DataList **dl)
  47. {
  48. slapi_ch_free ((void**) dl);
  49. }
  50. void dl_init (DataList *dl, int init_alloc)
  51. {
  52. PR_ASSERT (dl);
  53. memset (dl, 0, sizeof (*dl));
  54. if (init_alloc <= 0)
  55. dl->alloc_count = INIT_ALLOC;
  56. else
  57. dl->alloc_count = init_alloc;
  58. dl->elements = (void**)slapi_ch_calloc (dl->alloc_count, sizeof (void*));
  59. }
  60. void dl_cleanup (DataList *dl, FREEFN freefn)
  61. {
  62. PR_ASSERT (dl);
  63. if (freefn && dl->elements)
  64. {
  65. int i;
  66. for (i = 0; i < dl->element_count; i++)
  67. {
  68. freefn (&(dl->elements[i]));
  69. }
  70. }
  71. if (dl->elements)
  72. {
  73. slapi_ch_free ((void**)&dl->elements);
  74. }
  75. memset (dl, 0, sizeof (*dl));
  76. }
  77. /* index == 1 : insert first */
  78. void dl_add_index (DataList *dl, void *element, int index)
  79. {
  80. int i = 0;
  81. PR_ASSERT (dl);
  82. PR_ASSERT (element);
  83. if (dl->element_count == dl->alloc_count)
  84. {
  85. dl->alloc_count += ALLOC_INCREMENT;
  86. dl->elements = (void**)slapi_ch_realloc ((char*)dl->elements, dl->alloc_count * sizeof (void*));
  87. }
  88. dl->element_count ++;
  89. for (i = dl->element_count-1; i >= index; i--)
  90. {
  91. dl->elements[i] = dl->elements[i-1];
  92. }
  93. if ( dl->element_count < index )
  94. {
  95. /* Means that we are adding the first element */
  96. dl->elements[0] = element;
  97. }
  98. else
  99. {
  100. dl->elements[i] = element;
  101. }
  102. }
  103. void dl_add (DataList *dl, void *element)
  104. {
  105. PR_ASSERT (dl);
  106. PR_ASSERT (element);
  107. if (dl->element_count == dl->alloc_count)
  108. {
  109. dl->alloc_count += ALLOC_INCREMENT;
  110. dl->elements = (void**)slapi_ch_realloc ((char*)dl->elements, dl->alloc_count * sizeof (void*));
  111. }
  112. dl->elements[dl->element_count] = element;
  113. dl->element_count ++;
  114. }
  115. void *dl_get_first (const DataList *dl, int *cookie)
  116. {
  117. PR_ASSERT (dl);
  118. PR_ASSERT (cookie);
  119. if (dl->element_count == 0)
  120. return NULL;
  121. *cookie = 1;
  122. return dl->elements[0];
  123. }
  124. void *dl_get_next (const DataList *dl, int *cookie)
  125. {
  126. PR_ASSERT (dl);
  127. PR_ASSERT (cookie && *cookie > 0);
  128. if (*cookie >= dl->element_count)
  129. return NULL;
  130. return dl->elements[(*cookie)++];
  131. }
  132. void *dl_replace(const DataList *dl, const void *elementOld, void *elementNew, CMPFN cmpfn, FREEFN freefn)
  133. {
  134. int i;
  135. void *save;
  136. PR_ASSERT (dl);
  137. PR_ASSERT (elementOld);
  138. PR_ASSERT (elementNew);
  139. PR_ASSERT (cmpfn);
  140. for (i = 0; i < dl->element_count; i++)
  141. {
  142. if (cmpfn (dl->elements[i], elementOld) == 0)
  143. {
  144. /* if we have destructor - free the data; otherwise, return it to the client */
  145. if (freefn)
  146. {
  147. freefn (&dl->elements[i]);
  148. save = NULL;
  149. }
  150. else
  151. save = dl->elements[i];
  152. dl->elements[i] = elementNew;
  153. return save;
  154. }
  155. }
  156. return NULL;
  157. }
  158. void *dl_get (const DataList *dl, const void *element, CMPFN cmpfn)
  159. {
  160. int i;
  161. PR_ASSERT (dl);
  162. PR_ASSERT (element);
  163. PR_ASSERT (cmpfn);
  164. for (i = 0; i < dl->element_count; i++)
  165. {
  166. if (cmpfn (dl->elements[i], element) == 0)
  167. {
  168. return dl->elements[i];
  169. }
  170. }
  171. return NULL;
  172. }
  173. void *dl_delete (DataList *dl, const void *element, CMPFN cmpfn, FREEFN freefn)
  174. {
  175. int i;
  176. void *save;
  177. PR_ASSERT (dl);
  178. PR_ASSERT (element);
  179. PR_ASSERT (cmpfn);
  180. for (i = 0; i < dl->element_count; i++)
  181. {
  182. if (cmpfn (dl->elements[i], element) == 0)
  183. {
  184. /* if we have destructor - free the data; otherwise, return it to the client */
  185. if (freefn)
  186. {
  187. freefn (&dl->elements[i]);
  188. save = NULL;
  189. }
  190. else
  191. save = dl->elements[i];
  192. if (i != dl->element_count - 1)
  193. {
  194. memcpy (&dl->elements[i], &dl->elements[i+1], (dl->element_count - i - 1) * sizeof (void*));
  195. }
  196. dl->element_count --;
  197. return save;
  198. }
  199. }
  200. return NULL;
  201. }
  202. int dl_get_count (const DataList *dl)
  203. {
  204. PR_ASSERT (dl);
  205. return dl->element_count;
  206. }