/tags/rel-1-3-30rc1-b4beautify/SWIG/Source/DOH/memory.c

# · C · 219 lines · 149 code · 28 blank · 42 comment · 28 complexity · 456327c5b0c724bc90d11689f69bc99c MD5 · raw file

  1. /* -----------------------------------------------------------------------------
  2. * memory.c
  3. *
  4. * This file implements all of DOH's memory management including allocation
  5. * of objects and checking of objects.
  6. *
  7. * Author(s) : David Beazley (beazley@cs.uchicago.edu)
  8. *
  9. * Copyright (C) 1999-2000. The University of Chicago
  10. * See the file LICENSE for information on usage and redistribution.
  11. * ----------------------------------------------------------------------------- */
  12. char cvsroot_memory_c[] = "$Header$";
  13. #include "dohint.h"
  14. #ifndef DOH_POOL_SIZE
  15. #define DOH_POOL_SIZE 16384
  16. #endif
  17. static int PoolSize = DOH_POOL_SIZE;
  18. DOH *DohNone = 0; /* The DOH None object */
  19. typedef struct pool {
  20. DohBase *ptr; /* Start of pool */
  21. int len; /* Length of pool */
  22. int blen; /* Byte length of pool */
  23. int current; /* Current position for next allocation */
  24. char* pbeg; /* Beg of pool */
  25. char* pend; /* End of pool */
  26. struct pool *next; /* Next pool */
  27. } Pool;
  28. static DohBase *FreeList = 0; /* List of free objects */
  29. static Pool *Pools = 0;
  30. static int pools_initialized = 0;
  31. /* ----------------------------------------------------------------------
  32. * CreatePool() - Create a new memory pool
  33. * ---------------------------------------------------------------------- */
  34. static void
  35. CreatePool() {
  36. Pool *p = 0;
  37. p = (Pool *) DohMalloc(sizeof(Pool));
  38. assert(p);
  39. p->ptr = (DohBase *) DohMalloc(sizeof(DohBase)*PoolSize);
  40. assert(p->ptr);
  41. memset(p->ptr,0,sizeof(DohBase)*PoolSize);
  42. p->len = PoolSize;
  43. p->blen = PoolSize*sizeof(DohBase);
  44. p->current = 0;
  45. p->pbeg = ((char *)p->ptr);
  46. p->pend = p->pbeg + p->blen;
  47. p->next = Pools;
  48. Pools = p;
  49. }
  50. /* ----------------------------------------------------------------------
  51. * InitPools() - Initialize the memory allocator
  52. * ---------------------------------------------------------------------- */
  53. static void
  54. InitPools() {
  55. if (pools_initialized) return;
  56. CreatePool(); /* Create initial pool */
  57. pools_initialized = 1;
  58. DohNone = NewVoid(0,0); /* Create the None object */
  59. DohIntern(DohNone);
  60. }
  61. /* ----------------------------------------------------------------------
  62. * DohCheck()
  63. *
  64. * Returns 1 if an arbitrary pointer is a DOH object.
  65. * ---------------------------------------------------------------------- */
  66. int
  67. DohCheck(const DOH *ptr) {
  68. register Pool *p = Pools;
  69. register char *cptr = (char *) ptr;
  70. while (p) {
  71. if ((cptr >= p->pbeg) && (cptr < p->pend)) return 1;
  72. /*
  73. pptr = (char *) p->ptr;
  74. if ((cptr >= pptr) && (cptr < (pptr+(p->current*sizeof(DohBase))))) return 1; */
  75. p = p->next;
  76. }
  77. return 0;
  78. }
  79. /* -----------------------------------------------------------------------------
  80. * DohIntern()
  81. * ----------------------------------------------------------------------------- */
  82. void
  83. DohIntern(DOH *obj) {
  84. DohBase *b = (DohBase *) obj;
  85. b->flag_intern = 1;
  86. }
  87. /* ----------------------------------------------------------------------
  88. * DohObjMalloc()
  89. *
  90. * Allocate memory for a new object.
  91. * ---------------------------------------------------------------------- */
  92. DOH *
  93. DohObjMalloc(DohObjInfo *type, void *data) {
  94. DohBase *obj;
  95. if (!pools_initialized) InitPools();
  96. if (FreeList) {
  97. obj = FreeList;
  98. FreeList = (DohBase *) obj->data;
  99. } else {
  100. while (Pools->current == Pools->len) {
  101. CreatePool();
  102. }
  103. obj = Pools->ptr + Pools->current;
  104. ++Pools->current;
  105. }
  106. obj->type = type;
  107. obj->data = data;
  108. obj->meta = 0;
  109. obj->refcount = 1;
  110. obj->flag_intern = 0;
  111. obj->flag_marked = 0;
  112. obj->flag_user = 0;
  113. obj->flag_usermark = 0;
  114. return (DOH *) obj;
  115. }
  116. /* ----------------------------------------------------------------------
  117. * DohObjFree() - Free a DOH object
  118. * ---------------------------------------------------------------------- */
  119. void
  120. DohObjFree(DOH *ptr) {
  121. DohBase *b, *meta;
  122. b = (DohBase *) ptr;
  123. if (b->flag_intern) return;
  124. meta = (DohBase *)b->meta;
  125. b->data = (void *) FreeList;
  126. b->meta = 0;
  127. b->type = 0;
  128. FreeList = b;
  129. if (meta) {
  130. Delete(meta);
  131. }
  132. }
  133. /* ----------------------------------------------------------------------
  134. * DohMemoryDebug()
  135. *
  136. * Display memory usage statistics
  137. * ---------------------------------------------------------------------- */
  138. void
  139. DohMemoryDebug(void) {
  140. extern DohObjInfo DohStringType;
  141. extern DohObjInfo DohListType;
  142. extern DohObjInfo DohHashType;
  143. Pool *p;
  144. int totsize = 0;
  145. int totused = 0;
  146. int totfree = 0;
  147. int numstring = 0;
  148. int numlist = 0;
  149. int numhash = 0;
  150. printf("Memory statistics:\n\n");
  151. printf("Pools:\n");
  152. p = Pools;
  153. while(p) {
  154. /* Calculate number of used, free items */
  155. int i;
  156. int nused = 0, nfree = 0;
  157. for (i = 0; i < p->len; i++) {
  158. if (p->ptr[i].refcount <= 0) nfree++;
  159. else {
  160. nused++;
  161. if (p->ptr[i].type == &DohStringType) numstring++;
  162. else if (p->ptr[i].type == &DohListType) numlist++;
  163. else if (p->ptr[i].type == &DohHashType) numhash++;
  164. }
  165. }
  166. printf(" Pool %8p: size = %10d. used = %10d. free = %10d\n", (void *)p, p->len, nused, nfree);
  167. totsize += p->len;
  168. totused+= nused;
  169. totfree+= nfree;
  170. p = p->next;
  171. }
  172. printf("\n Total: size = %10d, used = %10d, free = %10d\n", totsize, totused, totfree);
  173. printf("\nObject types\n");
  174. printf(" Strings : %d\n", numstring);
  175. printf(" Lists : %d\n", numlist);
  176. printf(" Hashes : %d\n", numhash);
  177. #if 0
  178. p = Pools;
  179. while(p) {
  180. int i;
  181. for (i = 0; i < p->len; i++) {
  182. if (p->ptr[i].refcount > 0) {
  183. if (p->ptr[i].type == &DohStringType) {
  184. Printf(stdout,"%s\n", p->ptr+i);
  185. }
  186. }
  187. }
  188. p = p->next;
  189. }
  190. #endif
  191. }