alloc.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include <unistd.h>
  2. #include <sys/mman.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. struct chunkControl {
  6. size_t nodeCount;
  7. size_t chunkSize;
  8. };
  9. struct nodeControl {
  10. struct chunkControl *chunk;
  11. size_t nodeSize;
  12. };
  13. #define ROUND_UP_LENGTH(len) ((len+7) & ~0x07)
  14. extern struct nodeControl *mallocNextNode;
  15. #ifdef L_malloc
  16. /* This variable is a pointer to the next place to allocate from.
  17. * Note: This variable makes the code NOT thread save. */
  18. struct nodeControl *mallocNextNode = 0;
  19. static size_t PageSize = 0;
  20. #endif
  21. #ifdef L_calloc_dbg
  22. void *
  23. calloc_dbg(size_t num, size_t size, char * function, char * file, int line)
  24. {
  25. void * ptr;
  26. fprintf(stderr, "calloc of %d bytes at %s @%s:%d = ", num*size, function, file, line);
  27. ptr = calloc(num,size);
  28. fprtinf(stderr, "%p\n", ptr);
  29. return ptr;
  30. }
  31. #endif
  32. #ifdef L_malloc_dbg
  33. void *
  34. malloc_dbg(size_t len, char * function, char * file, int line)
  35. {
  36. void * result;
  37. fprintf(stderr, "malloc of %d bytes at %s @%s:%d = ", len, function, file, line);
  38. result = malloc(len);
  39. fprintf(stderr, "%p\n", result);
  40. return result;
  41. }
  42. #endif
  43. #ifdef L_free_dbg
  44. void
  45. free_dbg(void * ptr, char * function, char * file, int line)
  46. {
  47. fprintf(stderr, "free of %p at %s @%s:%d\n", ptr, function, file, line);
  48. free(ptr);
  49. }
  50. #endif
  51. #ifdef L_calloc
  52. void *
  53. calloc(size_t num, size_t size)
  54. {
  55. void * ptr = malloc(num*size);
  56. if (ptr)
  57. memset(ptr, 0, num*size);
  58. return ptr;
  59. }
  60. #endif
  61. #ifdef L_malloc
  62. void *
  63. malloc(size_t len)
  64. {
  65. void *result;
  66. struct chunkControl *chunk;
  67. struct nodeControl *next;
  68. size_t size;
  69. /* round len up to keep things on even boundaries */
  70. len = ROUND_UP_LENGTH(len);
  71. if (len == 0)
  72. return 0;
  73. TryAgain:
  74. if (mallocNextNode != 0) {
  75. /* first see if this request will fit on this chunk */
  76. next = mallocNextNode;
  77. chunk = next->chunk;
  78. if (((char *)next + sizeof(struct nodeControl)*2 + len) <
  79. ((char *)chunk + chunk->chunkSize))
  80. {
  81. /* this request will fit, so simply move the next
  82. * pointer ahead and update chunk node count */
  83. next->nodeSize = len;
  84. result = (char *)next + sizeof(struct nodeControl);
  85. chunk->nodeCount++;
  86. next = (struct nodeControl *)
  87. ((char *)next + (sizeof(struct nodeControl) + len));
  88. next->chunk = chunk;
  89. next->nodeSize = 0;
  90. mallocNextNode = next;
  91. return result; /* normal return path */
  92. }
  93. }
  94. /* the request will not fit on this chunk, so get another chunk */
  95. if (PageSize == 0) {
  96. PageSize = getpagesize();
  97. }
  98. size = len + (sizeof(struct chunkControl) + (sizeof(struct nodeControl) * 2));
  99. if (size < PageSize * 2) {
  100. size = PageSize * 2;
  101. }
  102. size = (size + (PageSize-1)) & ~(PageSize-1);
  103. chunk = mmap((void *)0, size, PROT_READ | PROT_WRITE,
  104. MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  105. if (chunk == (void*)-1)
  106. return 0;
  107. chunk->chunkSize = size;
  108. chunk->nodeCount = 0;
  109. next = (struct nodeControl *)
  110. ((char *)chunk + sizeof(struct chunkControl));
  111. next->chunk = chunk;
  112. mallocNextNode = next;
  113. goto TryAgain;
  114. }
  115. #endif
  116. #ifdef L_free
  117. void
  118. free(void * ptr)
  119. {
  120. struct chunkControl *chunk;
  121. struct nodeControl *node;
  122. if (ptr == 0) {
  123. return;
  124. }
  125. /* get a pointer to the control information for this memory node
  126. * and the chunk it belongs to */
  127. node = (struct nodeControl *)ptr - 1;
  128. chunk = node->chunk;
  129. /* decrement the node count and if it is zero free the chunk */
  130. chunk->nodeCount--;
  131. if (chunk->nodeCount == 0) {
  132. if ((void *)mallocNextNode >= (void *)chunk &&
  133. ((void *)mallocNextNode < (void *)((char *)chunk + chunk->chunkSize)))
  134. {
  135. mallocNextNode = 0;
  136. }
  137. munmap(chunk, chunk->chunkSize);
  138. }
  139. }
  140. #endif
  141. #ifdef L_realloc
  142. void *
  143. realloc(void *ptr, size_t len)
  144. {
  145. struct nodeControl *node;
  146. size_t oldSize;
  147. void *new;
  148. if (ptr == 0) {
  149. return malloc(len);
  150. }
  151. if (len == 0) {
  152. free(ptr);
  153. return 0;
  154. }
  155. node = (struct nodeControl *)ptr - 1;
  156. oldSize = node->nodeSize;
  157. if (oldSize >= len) {
  158. return ptr;
  159. }
  160. new = malloc(len);
  161. memcpy(new, ptr, len);
  162. free(ptr);
  163. return new;
  164. }
  165. #endif