xpool.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * $Id$
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Jabber
  19. * Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
  20. *
  21. * 2/27/00:3am, random plans by jer
  22. *
  23. * ok based on gprof, we really need some innovation here... my thoughs are this:
  24. *
  25. * most things are strings, so have a string-based true-blue garbage collector
  26. * one big global hash containing all the strings created by any pstrdup, returning const char *
  27. * a refcount on each string block
  28. * when a pool is freed, it moves down the refcount
  29. * garbage collector collects pools on the free stack, and runs through the hash for unused strings
  30. * j_strcmp can check for == (if they are both from a pstrdup)
  31. *
  32. * let's see... this would change:
  33. * pstrdup: do a hash lookup, success=return, fail=pmalloc & hash put
  34. * pool_free:
  35. *
  36. */
  37. /*! \file
  38. * \ingroup xmpp
  39. */
  40. #include "xode.h"
  41. //#include "config.h"
  42. #define _xode_pool__malloc malloc
  43. #define _xode_pool__free free
  44. /* xode_pfree - a linked list node which stores an
  45. allocation chunk, plus a callback */
  46. struct xode_pool_free
  47. {
  48. xode_pool_cleaner f;
  49. void *arg;
  50. struct xode_pool_heap *heap;
  51. struct xode_pool_free *next;
  52. };
  53. /* make an empty pool */
  54. xode_pool _xode_pool_new(void)
  55. {
  56. xode_pool p;
  57. while((p = _xode_pool__malloc(sizeof(_xode_pool))) == NULL) sleep(1);
  58. p->cleanup = NULL;
  59. p->heap = NULL;
  60. p->size = 0;
  61. return p;
  62. }
  63. /* free a heap */
  64. void _xode_pool_heapfree(void *arg)
  65. {
  66. struct xode_pool_heap *h = (struct xode_pool_heap *)arg;
  67. _xode_pool__free(h->block);
  68. _xode_pool__free(h);
  69. }
  70. /* mem should always be freed last */
  71. void _xode_pool_cleanup_append(xode_pool p, struct xode_pool_free *pf)
  72. {
  73. struct xode_pool_free *cur;
  74. if(p->cleanup == NULL)
  75. {
  76. p->cleanup = pf;
  77. return;
  78. }
  79. /* fast forward to end of list */
  80. for(cur = p->cleanup; cur->next != NULL; cur = cur->next);
  81. cur->next = pf;
  82. }
  83. /* create a cleanup tracker */
  84. struct xode_pool_free *_xode_pool_free(xode_pool p, xode_pool_cleaner f, void *arg)
  85. {
  86. struct xode_pool_free *ret;
  87. /* make the storage for the tracker */
  88. while((ret = _xode_pool__malloc(sizeof(struct xode_pool_free))) == NULL) sleep(1);
  89. ret->f = f;
  90. ret->arg = arg;
  91. ret->next = NULL;
  92. return ret;
  93. }
  94. /* create a heap and make sure it get's cleaned up */
  95. struct xode_pool_heap *_xode_pool_heap(xode_pool p, int size)
  96. {
  97. struct xode_pool_heap *ret;
  98. struct xode_pool_free *clean;
  99. /* make the return heap */
  100. while((ret = _xode_pool__malloc(sizeof(struct xode_pool_heap))) == NULL) sleep(1);
  101. while((ret->block = _xode_pool__malloc(size)) == NULL) sleep(1);
  102. ret->size = size;
  103. p->size += size;
  104. ret->used = 0;
  105. /* append to the cleanup list */
  106. clean = _xode_pool_free(p, _xode_pool_heapfree, (void *)ret);
  107. clean->heap = ret; /* for future use in finding used mem for pstrdup */
  108. _xode_pool_cleanup_append(p, clean);
  109. return ret;
  110. }
  111. xode_pool _xode_pool_newheap(int bytes)
  112. {
  113. xode_pool p;
  114. p = _xode_pool_new();
  115. p->heap = _xode_pool_heap(p,bytes);
  116. return p;
  117. }
  118. void *xode_pool_malloc(xode_pool p, int size)
  119. {
  120. void *block;
  121. if(p == NULL)
  122. {
  123. fprintf(stderr,"Memory Leak! xode_pmalloc received NULL pool, unable to track allocation, exiting]\n");
  124. abort();
  125. }
  126. /* if there is no heap for this pool or it's a big request, just raw, I like how we clean this :) */
  127. if(p->heap == NULL || size > (p->heap->size / 2))
  128. {
  129. while((block = _xode_pool__malloc(size)) == NULL) sleep(1);
  130. p->size += size;
  131. _xode_pool_cleanup_append(p, _xode_pool_free(p, _xode_pool__free, block));
  132. return block;
  133. }
  134. /* we have to preserve boundaries, long story :) */
  135. if(size >= 4)
  136. while(p->heap->used&7) p->heap->used++;
  137. /* if we don't fit in the old heap, replace it */
  138. if(size > (p->heap->size - p->heap->used))
  139. p->heap = _xode_pool_heap(p, p->heap->size);
  140. /* the current heap has room */
  141. block = (char *)p->heap->block + p->heap->used;
  142. p->heap->used += size;
  143. return block;
  144. }
  145. void *xode_pool_mallocx(xode_pool p, int size, char c)
  146. {
  147. void* result = xode_pool_malloc(p, size);
  148. if (result != NULL)
  149. memset(result, c, size);
  150. return result;
  151. }
  152. /* easy safety utility (for creating blank mem for structs, etc) */
  153. void *xode_pool_malloco(xode_pool p, int size)
  154. {
  155. void *block = xode_pool_malloc(p, size);
  156. memset(block, 0, size);
  157. return block;
  158. }
  159. /* XXX efficient: move this to const char * and then loop through the existing heaps to see if src is within a block in this pool */
  160. char *xode_pool_strdup(xode_pool p, const char *src)
  161. {
  162. char *ret;
  163. if(src == NULL)
  164. return NULL;
  165. ret = xode_pool_malloc(p,strlen(src) + 1);
  166. strcpy(ret,src);
  167. return ret;
  168. }
  169. /* when move above, this one would actually return a new block */
  170. char *xode_pool_strdupx(xode_pool p, const char *src)
  171. {
  172. return xode_pool_strdup(p, src);
  173. }
  174. int xode_pool_size(xode_pool p)
  175. {
  176. if(p == NULL) return 0;
  177. return p->size;
  178. }
  179. void xode_pool_free(xode_pool p)
  180. {
  181. struct xode_pool_free *cur, *stub;
  182. if(p == NULL) return;
  183. cur = p->cleanup;
  184. while(cur != NULL)
  185. {
  186. (*cur->f)(cur->arg);
  187. stub = cur->next;
  188. _xode_pool__free(cur);
  189. cur = stub;
  190. }
  191. _xode_pool__free(p);
  192. }
  193. /* public cleanup utils, insert in a way that they are run FIFO, before mem frees */
  194. void xode_pool_cleanup(xode_pool p, xode_pool_cleaner f, void *arg)
  195. {
  196. struct xode_pool_free *clean;
  197. clean = _xode_pool_free(p, f, arg);
  198. clean->next = p->cleanup;
  199. p->cleanup = clean;
  200. }
  201. xode_pool xode_pool_new(void)
  202. {
  203. return _xode_pool_new();
  204. }
  205. xode_pool xode_pool_heap(const int bytes)
  206. {
  207. return _xode_pool_newheap(bytes);
  208. }