memarea.c 7.7 KB

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