free.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: free.c /main/1 1996/04/21 19:23:16 drk $ */
  24. /*
  25. * (c) Copyright 1993, 1994 Hewlett-Packard Company
  26. * (c) Copyright 1993, 1994 International Business Machines Corp.
  27. * (c) Copyright 1993, 1994 Novell, Inc.
  28. * (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  29. */
  30. #include <EUSCompat.h>
  31. #include <stdlib.h>
  32. #include "csa.h"
  33. #include "free.h"
  34. #define _DtCM_OUR_MEMORY 0x55555555
  35. typedef void (*_DtCmFreeProc)(uint num_elem, void *ptr);
  36. typedef struct {
  37. int mark;
  38. uint num_elem;
  39. _DtCmFreeProc fproc;
  40. } _DtCmMemHeader;
  41. /******************************************************************************
  42. * forward declaration of static functions used within the file
  43. ******************************************************************************/
  44. static void _DtCm_free_calendar_users(uint num_elem, void *vptr);
  45. /*****************************************************************************
  46. * extern functions used in the library
  47. *****************************************************************************/
  48. extern CSA_calendar_user *
  49. _DtCm_alloc_calendar_users(uint num_elem)
  50. {
  51. char *ptr;
  52. _DtCmMemHeader *hptr;
  53. if ((ptr = calloc(1, sizeof(_DtCmMemHeader) +
  54. sizeof(CSA_calendar_user) * num_elem)) == NULL)
  55. return (NULL);
  56. else {
  57. hptr = (_DtCmMemHeader *)ptr;
  58. hptr->mark = _DtCM_OUR_MEMORY;
  59. hptr->num_elem = num_elem;
  60. hptr->fproc = _DtCm_free_calendar_users;
  61. return ((CSA_calendar_user *)(ptr + sizeof(_DtCmMemHeader)));
  62. }
  63. }
  64. extern char **
  65. _DtCm_alloc_character_pointers(uint num_elem)
  66. {
  67. char *ptr;
  68. _DtCmMemHeader *hptr;
  69. if ((ptr = calloc(1, sizeof(_DtCmMemHeader) +
  70. sizeof(char *) * num_elem)) == NULL)
  71. return (NULL);
  72. else {
  73. hptr = (_DtCmMemHeader *)ptr;
  74. hptr->mark = _DtCM_OUR_MEMORY;
  75. hptr->num_elem = num_elem;
  76. hptr->fproc = _DtCm_free_character_pointers;
  77. return ((char **)(ptr + sizeof(_DtCmMemHeader)));
  78. }
  79. }
  80. extern CSA_attribute *
  81. _DtCm_alloc_attributes(uint num_elem)
  82. {
  83. char *ptr;
  84. _DtCmMemHeader *hptr;
  85. extern void _DtCm_free_attributes(uint size, void *attrs);
  86. if ((ptr = calloc(1, sizeof(_DtCmMemHeader) +
  87. sizeof(CSA_attribute) * num_elem)) == NULL)
  88. return (NULL);
  89. else {
  90. hptr = (_DtCmMemHeader *)ptr;
  91. hptr->mark = _DtCM_OUR_MEMORY;
  92. hptr->num_elem = num_elem;
  93. hptr->fproc = _DtCm_free_attributes;
  94. return ((CSA_attribute *)(ptr + sizeof(_DtCmMemHeader)));
  95. }
  96. }
  97. extern CSA_entry_handle *
  98. _DtCm_alloc_entry_handles(uint num_elem)
  99. {
  100. char *ptr;
  101. _DtCmMemHeader *hptr;
  102. extern void _DtCm_free_entry_handles(uint num_entries, void *entries);
  103. if ((ptr = calloc(1, sizeof(_DtCmMemHeader) +
  104. sizeof(CSA_entry_handle) * num_elem)) == NULL)
  105. return (NULL);
  106. else {
  107. hptr = (_DtCmMemHeader *)ptr;
  108. hptr->mark = _DtCM_OUR_MEMORY;
  109. hptr->num_elem = num_elem;
  110. hptr->fproc = _DtCm_free_entry_handles;
  111. return ((CSA_entry_handle *)(ptr + sizeof(_DtCmMemHeader)));
  112. }
  113. }
  114. extern void *
  115. _DtCm_alloc_entry(uint size)
  116. {
  117. char *ptr;
  118. _DtCmMemHeader *hptr;
  119. extern void _DtCm_free_entry_content(uint dummy, void *entry);
  120. if ((ptr = calloc(1, sizeof(_DtCmMemHeader) + size)) == NULL)
  121. return (NULL);
  122. else {
  123. hptr = (_DtCmMemHeader *)ptr;
  124. hptr->mark = _DtCM_OUR_MEMORY;
  125. hptr->num_elem = 1;
  126. hptr->fproc = _DtCm_free_entry_content;
  127. return ((void *)(ptr + sizeof(_DtCmMemHeader)));
  128. }
  129. }
  130. extern CSA_reminder_reference *
  131. _DtCm_alloc_reminder_references(uint num_elem)
  132. {
  133. char *ptr;
  134. _DtCmMemHeader *hptr;
  135. extern void _DtCm_free_reminder_references(uint num_rems, void *rems);
  136. if ((ptr = calloc(1, sizeof(_DtCmMemHeader) +
  137. sizeof(CSA_reminder_reference) * num_elem)) == NULL)
  138. return (NULL);
  139. else {
  140. hptr = (_DtCmMemHeader *)ptr;
  141. hptr->mark = _DtCM_OUR_MEMORY;
  142. hptr->num_elem = num_elem;
  143. hptr->fproc = _DtCm_free_reminder_references;
  144. return ((CSA_reminder_reference *)(ptr +
  145. sizeof(_DtCmMemHeader)));
  146. }
  147. }
  148. extern CSA_return_code
  149. _DtCm_free(void *ptr)
  150. {
  151. _DtCmMemHeader *hptr;
  152. if (ptr == NULL)
  153. return (CSA_E_INVALID_MEMORY);
  154. hptr = (_DtCmMemHeader *)(((char *)ptr) - sizeof(_DtCmMemHeader));
  155. if (hptr->mark != _DtCM_OUR_MEMORY)
  156. return (CSA_E_INVALID_MEMORY);
  157. else {
  158. hptr->mark = 0;
  159. hptr->fproc(hptr->num_elem, ptr);
  160. free(hptr);
  161. return (CSA_SUCCESS);
  162. }
  163. }
  164. extern void
  165. _DtCm_free_character_pointers(uint num_elem, void *vptr)
  166. {
  167. char **ptr = (char **)vptr;
  168. int i;
  169. for (i = 0; i < num_elem; i++) {
  170. if (ptr[i])
  171. free(ptr[i]);
  172. else
  173. break;
  174. }
  175. }
  176. /******************************************************************************
  177. * static functions used within in the file
  178. ******************************************************************************/
  179. static void
  180. _DtCm_free_calendar_users(uint num_elem, void *vptr)
  181. {
  182. CSA_calendar_user *ptr = (CSA_calendar_user *)vptr;
  183. int i;
  184. for (i = 0; i < num_elem; i++) {
  185. if (ptr[i].user_name)
  186. free(ptr[i].user_name);
  187. if (ptr[i].calendar_address)
  188. free(ptr[i].calendar_address);
  189. }
  190. }