memarea.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* Copyright (c) 2008, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /** \file memarea.c
  5. * \brief Implementation for memarea_t, an allocator for allocating lots of
  6. * small objects that will be freed all at once.
  7. */
  8. #include "orconfig.h"
  9. #include <stdlib.h>
  10. #include "memarea.h"
  11. #include "util.h"
  12. #include "compat.h"
  13. #include "log.h"
  14. /** All returned pointers should be aligned to the nearest multiple of this
  15. * value. */
  16. #define MEMAREA_ALIGN SIZEOF_VOID_P
  17. #if MEMAREA_ALIGN == 4
  18. #define MEMAREA_ALIGN_MASK 3lu
  19. #elif MEMAREA_ALIGN == 8
  20. #define MEMAREA_ALIGN_MASK 7lu
  21. #else
  22. #error "void* is neither 4 nor 8 bytes long. I don't know how to align stuff."
  23. #endif
  24. /* Increment <b>ptr</b> until it is aligned to MEMAREA_ALIGN. */
  25. static INLINE void *
  26. realign_pointer(void *ptr)
  27. {
  28. uintptr_t x = (uintptr_t)ptr;
  29. x = (x+MEMAREA_ALIGN_MASK) & ~MEMAREA_ALIGN_MASK;
  30. return (void*)x;
  31. }
  32. /** Implements part of a memarea. New memory is carved off from chunk->mem in
  33. * increasing order until a request is too big, at which point a new chunk is
  34. * allocated. */
  35. typedef struct memarea_chunk_t {
  36. /** Next chunk in this area. Only kept around so we can free it. */
  37. struct memarea_chunk_t *next_chunk;
  38. size_t mem_size; /**< How much RAM is available in u.mem, total? */
  39. char *next_mem; /**< Next position in u.mem to allocate data at. If it's
  40. * greater than or equal to mem+mem_size, this chunk is
  41. * full. */
  42. union {
  43. char mem[1]; /**< Memory space in this chunk. */
  44. void *_void_for_alignment; /**< Dummy; used to make sure mem is aligned. */
  45. } u;
  46. } memarea_chunk_t;
  47. #define CHUNK_HEADER_SIZE STRUCT_OFFSET(memarea_chunk_t, u)
  48. #define CHUNK_SIZE 4096
  49. /** A memarea_t is an allocation region for a set of small memory requests
  50. * that will all be freed at once. */
  51. struct memarea_t {
  52. memarea_chunk_t *first; /**< Top of the chunk stack: never NULL. */
  53. };
  54. #define MAX_FREELIST_LEN 4
  55. int freelist_len=0;
  56. static memarea_chunk_t *freelist = NULL;
  57. /** Helper: allocate a new memarea chunk of around <b>chunk_size</b> bytes. */
  58. static memarea_chunk_t *
  59. alloc_chunk(size_t sz, int freelist_ok)
  60. {
  61. if (freelist && freelist_ok) {
  62. memarea_chunk_t *res = freelist;
  63. freelist = res->next_chunk;
  64. res->next_chunk = NULL;
  65. --freelist_len;
  66. return res;
  67. } else {
  68. size_t chunk_size = freelist_ok ? CHUNK_SIZE : sz;
  69. memarea_chunk_t *res = tor_malloc_roundup(&chunk_size);
  70. res->next_chunk = NULL;
  71. res->mem_size = chunk_size - CHUNK_HEADER_SIZE;
  72. res->next_mem = res->u.mem;
  73. return res;
  74. }
  75. }
  76. static void
  77. chunk_free(memarea_chunk_t *chunk)
  78. {
  79. if (freelist_len < MAX_FREELIST_LEN) {
  80. ++freelist_len;
  81. chunk->next_chunk = freelist;
  82. freelist = chunk;
  83. chunk->next_mem = chunk->u.mem;
  84. } else {
  85. tor_free(chunk);
  86. }
  87. }
  88. /** Allocate and return new memarea. */
  89. memarea_t *
  90. memarea_new(void)
  91. {
  92. memarea_t *head = tor_malloc(sizeof(memarea_t));
  93. head->first = alloc_chunk(CHUNK_SIZE, 1);
  94. return head;
  95. }
  96. /** Free <b>area</b>, invalidating all pointers returned from memarea_alloc()
  97. * and friends for this area */
  98. void
  99. memarea_drop_all(memarea_t *area)
  100. {
  101. memarea_chunk_t *chunk, *next;
  102. for (chunk = area->first; chunk; chunk = next) {
  103. next = chunk->next_chunk;
  104. chunk_free(chunk);
  105. }
  106. area->first = NULL; /*fail fast on */
  107. tor_free(area);
  108. }
  109. /** Forget about having allocated anything in <b>area</b>, and free some of
  110. * the backing storage associated with it, as appropriate. Invalidates all
  111. * pointers returned from memarea_alloc() for this area. */
  112. void
  113. memarea_clear(memarea_t *area)
  114. {
  115. memarea_chunk_t *chunk, *next;
  116. if (area->first->next_chunk) {
  117. for (chunk = area->first->next_chunk; chunk; chunk = next) {
  118. next = chunk->next_chunk;
  119. chunk_free(chunk);
  120. }
  121. area->first->next_chunk = NULL;
  122. }
  123. area->first->next_mem = area->first->u.mem;
  124. }
  125. /** DOCDOC */
  126. void
  127. memarea_clear_freelist(void)
  128. {
  129. memarea_chunk_t *chunk, *next;
  130. freelist_len = 0;
  131. for (chunk = freelist; chunk; chunk = next) {
  132. next = chunk->next_chunk;
  133. tor_free(chunk);
  134. }
  135. freelist = NULL;
  136. }
  137. /** Return true iff <b>p</b> is in a range that has been returned by an
  138. * allocation from <b>area</b>. */
  139. int
  140. memarea_owns_ptr(const memarea_t *area, const void *p)
  141. {
  142. memarea_chunk_t *chunk;
  143. const char *ptr = p;
  144. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  145. if (ptr >= chunk->u.mem && ptr < chunk->next_mem)
  146. return 1;
  147. }
  148. return 0;
  149. }
  150. /** Return a pointer to a chunk of memory in <b>area</b> of at least <b>sz</b>
  151. * bytes. <b>sz</b> should be significantly smaller than the area's chunk
  152. * size, though we can deal if it isn't. */
  153. void *
  154. memarea_alloc(memarea_t *area, size_t sz)
  155. {
  156. memarea_chunk_t *chunk = area->first;
  157. char *result;
  158. tor_assert(chunk);
  159. if (chunk->next_mem+sz > chunk->u.mem+chunk->mem_size) {
  160. if (sz+CHUNK_HEADER_SIZE >= CHUNK_SIZE) {
  161. /* This allocation is too big. Stick it in a special chunk, and put
  162. * that chunk second in the list. */
  163. memarea_chunk_t *new_chunk = alloc_chunk(sz+CHUNK_HEADER_SIZE, 0);
  164. new_chunk->next_chunk = chunk->next_chunk;
  165. chunk->next_chunk = new_chunk;
  166. chunk = new_chunk;
  167. } else {
  168. memarea_chunk_t *new_chunk = alloc_chunk(CHUNK_SIZE, 1);
  169. new_chunk->next_chunk = chunk;
  170. area->first = chunk = new_chunk;
  171. }
  172. tor_assert(chunk->mem_size >= sz);
  173. }
  174. result = chunk->next_mem;
  175. chunk->next_mem = realign_pointer(chunk->next_mem + sz);
  176. return result;
  177. }
  178. /** As memarea_alloc(), but clears the memory it returns. */
  179. void *
  180. memarea_alloc_zero(memarea_t *area, size_t sz)
  181. {
  182. void *result = memarea_alloc(area, sz);
  183. memset(result, 0, sz);
  184. return result;
  185. }
  186. /** As memdup, but returns the memory from <b>area</b>. */
  187. void *
  188. memarea_memdup(memarea_t *area, const void *s, size_t n)
  189. {
  190. char *result = memarea_alloc(area, n);
  191. memcpy(result, s, n);
  192. return result;
  193. }
  194. /** As strdup, but returns the memory from <b>area</b>. */
  195. char *
  196. memarea_strdup(memarea_t *area, const char *s)
  197. {
  198. return memarea_memdup(area, s, strlen(s)+1);
  199. }
  200. /** As strndup, but returns the memory from <b>area</b>. */
  201. char *
  202. memarea_strndup(memarea_t *area, const char *s, size_t n)
  203. {
  204. size_t ln;
  205. char *result;
  206. const char *cp, *end = s+n;
  207. for (cp = s; *cp && cp < end; ++cp)
  208. ;
  209. /* cp now points to s+n, or to the 0 in the string. */
  210. ln = cp-s;
  211. result = memarea_memdup(area, s, ln+1);
  212. result[ln]='\0';
  213. return result;
  214. }
  215. /** Set <b>allocated_out</b> to the number of bytes allocated in <b>area</b>,
  216. * and <b>used_out</b> to the number of bytes currently used. */
  217. void
  218. memarea_get_stats(memarea_t *area, size_t *allocated_out, size_t *used_out)
  219. {
  220. size_t a = 0, u = 0;
  221. memarea_chunk_t *chunk;
  222. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  223. a += CHUNK_HEADER_SIZE + chunk->mem_size;
  224. tor_assert(chunk->next_mem >= chunk->u.mem);
  225. u += CHUNK_HEADER_SIZE + (chunk->next_mem - chunk->u.mem);
  226. }
  227. *allocated_out = a;
  228. *used_out = u;
  229. }
  230. /** Assert that <b>area</b> is okay. */
  231. void
  232. memarea_assert_ok(memarea_t *area)
  233. {
  234. memarea_chunk_t *chunk;
  235. tor_assert(area->first);
  236. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  237. tor_assert(chunk->next_mem >= chunk->u.mem);
  238. tor_assert(chunk->next_mem <= chunk->u.mem+chunk->mem_size+MEMAREA_ALIGN);
  239. }
  240. }