alloc.c 4.0 KB

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