PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-26/SWIG/Source/DOH/memory.c

#
C | 220 lines | 150 code | 28 blank | 42 comment | 26 complexity | db7d8b3d2880cf53676686619f37e58e MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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. PoolSize *= 2;
  102. CreatePool();
  103. }
  104. obj = Pools->ptr + Pools->current;
  105. Pools->current++;
  106. }
  107. obj->type = type;
  108. obj->data = data;
  109. obj->meta = 0;
  110. obj->refcount = 1;
  111. obj->flag_intern = 0;
  112. obj->flag_marked = 0;
  113. obj->flag_user = 0;
  114. obj->flag_usermark = 0;
  115. return (DOH *) obj;
  116. }
  117. /* ----------------------------------------------------------------------
  118. * DohObjFree() - Free a DOH object
  119. * ---------------------------------------------------------------------- */
  120. void
  121. DohObjFree(DOH *ptr) {
  122. DohBase *b, *meta;
  123. b = (DohBase *) ptr;
  124. if (b->flag_intern) return;
  125. meta = (DohBase *)b->meta;
  126. b->data = (void *) FreeList;
  127. b->meta = 0;
  128. b->type = 0;
  129. FreeList = b;
  130. if (meta) {
  131. Delete(meta);
  132. }
  133. }
  134. /* ----------------------------------------------------------------------
  135. * DohMemoryDebug()
  136. *
  137. * Display memory usage statistics
  138. * ---------------------------------------------------------------------- */
  139. void
  140. DohMemoryDebug(void) {
  141. extern DohObjInfo DohStringType;
  142. extern DohObjInfo DohListType;
  143. extern DohObjInfo DohHashType;
  144. Pool *p;
  145. int totsize = 0;
  146. int totused = 0;
  147. int totfree = 0;
  148. int numstring = 0;
  149. int numlist = 0;
  150. int numhash = 0;
  151. printf("Memory statistics:\n\n");
  152. printf("Pools:\n");
  153. p = Pools;
  154. while(p) {
  155. /* Calculate number of used, free items */
  156. int i;
  157. int nused = 0, nfree = 0;
  158. for (i = 0; i < p->len; i++) {
  159. if (p->ptr[i].refcount <= 0) nfree++;
  160. else {
  161. nused++;
  162. if (p->ptr[i].type == &DohStringType) numstring++;
  163. else if (p->ptr[i].type == &DohListType) numlist++;
  164. else if (p->ptr[i].type == &DohHashType) numhash++;
  165. }
  166. }
  167. printf(" Pool %8p: size = %10d. used = %10d. free = %10d\n", (void *)p, p->len, nused, nfree);
  168. totsize += p->len;
  169. totused+= nused;
  170. totfree+= nfree;
  171. p = p->next;
  172. }
  173. printf("\n Total: size = %10d, used = %10d, free = %10d\n", totsize, totused, totfree);
  174. printf("\nObject types\n");
  175. printf(" Strings : %d\n", numstring);
  176. printf(" Lists : %d\n", numlist);
  177. printf(" Hashes : %d\n", numhash);
  178. #if 0
  179. p = Pools;
  180. while(p) {
  181. int i;
  182. for (i = 0; i < p->len; i++) {
  183. if (p->ptr[i].refcount > 0) {
  184. if (p->ptr[i].type == &DohStringType) {
  185. Printf(stdout,"%s\n", p->ptr+i);
  186. }
  187. }
  188. }
  189. p = p->next;
  190. }
  191. #endif
  192. }