1
0

repl_helper.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "repl_helper.h"
  13. ReplGenericList *
  14. ReplGenericListNew(void)
  15. {
  16. ReplGenericList *list=NULL;
  17. if(NULL == (list = (ReplGenericList *)
  18. slapi_ch_calloc(1,sizeof(ReplGenericList)))) {
  19. return(NULL);
  20. }
  21. list->object = NULL;
  22. list->next = NULL;
  23. list->prev = NULL;
  24. return(list);
  25. }
  26. void
  27. ReplGenericListAddObject(ReplGenericList *list,
  28. void *newObject)
  29. {
  30. if(list) {
  31. ReplGenericList *new_struct = (ReplGenericList *)
  32. slapi_ch_calloc(1, sizeof(ReplGenericList));
  33. if (!new_struct)
  34. return;
  35. /* set back pointer of old first element */
  36. if(list->next) {
  37. list->next->prev = new_struct;
  38. }
  39. /* we might have a next but since we are the first we WONT have
  40. a previous */
  41. new_struct->object = newObject;
  42. new_struct->next = list->next;
  43. new_struct->prev = NULL;
  44. /* the new element is the first one */
  45. list->next = new_struct;
  46. /* if this is the only element it is the end too */
  47. if(NULL == list->prev)
  48. list->prev = new_struct;
  49. }
  50. return;
  51. }
  52. ReplGenericList *
  53. ReplGenericListFindObject(ReplGenericList *list,
  54. void *object)
  55. {
  56. if(!list)
  57. return(NULL);
  58. list = list->next; /* the first list item never has data */
  59. while (list) {
  60. if(list->object == object)
  61. return(list);
  62. list = list->next;
  63. }
  64. return(NULL);
  65. }
  66. void
  67. ReplGenericListDestroy(ReplGenericList *list,
  68. ReplGenericListObjectDestroyFn destroyFn)
  69. {
  70. ReplGenericList *list_ptr;
  71. while (list) {
  72. list_ptr = list;
  73. list = list->next;
  74. if(destroyFn && list_ptr->object) {
  75. (destroyFn)(list_ptr->object);
  76. }
  77. slapi_ch_free((void **)(&list_ptr));
  78. }
  79. return;
  80. }