repl_helper.c 1.8 KB

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