memarea.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /* Copyright (c) 2008-2011, 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. tor_assert(((void*)x) >= ptr); // XXXX021 remove this once bug 930 is solved
  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. /** How many chunks will we put into the freelist before freeing them? */
  55. #define MAX_FREELIST_LEN 4
  56. /** The number of memarea chunks currently in our freelist. */
  57. static int freelist_len=0;
  58. /** A linked list of unused memory area chunks. Used to prevent us from
  59. * spinning in malloc/free loops. */
  60. static memarea_chunk_t *freelist = NULL;
  61. /** Helper: allocate a new memarea chunk of around <b>chunk_size</b> bytes. */
  62. static memarea_chunk_t *
  63. alloc_chunk(size_t sz, int freelist_ok)
  64. {
  65. tor_assert(sz < SIZE_T_CEILING);
  66. if (freelist && freelist_ok) {
  67. memarea_chunk_t *res = freelist;
  68. freelist = res->next_chunk;
  69. res->next_chunk = NULL;
  70. --freelist_len;
  71. return res;
  72. } else {
  73. size_t chunk_size = freelist_ok ? CHUNK_SIZE : sz;
  74. memarea_chunk_t *res = tor_malloc_roundup(&chunk_size);
  75. res->next_chunk = NULL;
  76. res->mem_size = chunk_size - CHUNK_HEADER_SIZE;
  77. res->next_mem = res->u.mem;
  78. tor_assert(res->next_mem+res->mem_size == ((char*)res)+chunk_size);
  79. tor_assert(realign_pointer(res->next_mem) == res->next_mem);
  80. return res;
  81. }
  82. }
  83. /** Release <b>chunk</b> from a memarea, either by adding it to the freelist
  84. * or by freeing it if the freelist is already too big. */
  85. static void
  86. chunk_free(memarea_chunk_t *chunk)
  87. {
  88. if (freelist_len < MAX_FREELIST_LEN) {
  89. ++freelist_len;
  90. chunk->next_chunk = freelist;
  91. freelist = chunk;
  92. chunk->next_mem = chunk->u.mem;
  93. } else {
  94. tor_free(chunk);
  95. }
  96. }
  97. /** Allocate and return new memarea. */
  98. memarea_t *
  99. memarea_new(void)
  100. {
  101. memarea_t *head = tor_malloc(sizeof(memarea_t));
  102. head->first = alloc_chunk(CHUNK_SIZE, 1);
  103. return head;
  104. }
  105. /** Free <b>area</b>, invalidating all pointers returned from memarea_alloc()
  106. * and friends for this area */
  107. void
  108. memarea_drop_all(memarea_t *area)
  109. {
  110. memarea_chunk_t *chunk, *next;
  111. for (chunk = area->first; chunk; chunk = next) {
  112. next = chunk->next_chunk;
  113. chunk_free(chunk);
  114. }
  115. area->first = NULL; /*fail fast on */
  116. tor_free(area);
  117. }
  118. /** Forget about having allocated anything in <b>area</b>, and free some of
  119. * the backing storage associated with it, as appropriate. Invalidates all
  120. * pointers returned from memarea_alloc() for this area. */
  121. void
  122. memarea_clear(memarea_t *area)
  123. {
  124. memarea_chunk_t *chunk, *next;
  125. if (area->first->next_chunk) {
  126. for (chunk = area->first->next_chunk; chunk; chunk = next) {
  127. next = chunk->next_chunk;
  128. chunk_free(chunk);
  129. }
  130. area->first->next_chunk = NULL;
  131. }
  132. area->first->next_mem = area->first->u.mem;
  133. }
  134. /** Remove all unused memarea chunks from the internal freelist. */
  135. void
  136. memarea_clear_freelist(void)
  137. {
  138. memarea_chunk_t *chunk, *next;
  139. freelist_len = 0;
  140. for (chunk = freelist; chunk; chunk = next) {
  141. next = chunk->next_chunk;
  142. tor_free(chunk);
  143. }
  144. freelist = NULL;
  145. }
  146. /** Return true iff <b>p</b> is in a range that has been returned by an
  147. * allocation from <b>area</b>. */
  148. int
  149. memarea_owns_ptr(const memarea_t *area, const void *p)
  150. {
  151. memarea_chunk_t *chunk;
  152. const char *ptr = p;
  153. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  154. if (ptr >= chunk->u.mem && ptr < chunk->next_mem)
  155. return 1;
  156. }
  157. return 0;
  158. }
  159. /** Return a pointer to a chunk of memory in <b>area</b> of at least <b>sz</b>
  160. * bytes. <b>sz</b> should be significantly smaller than the area's chunk
  161. * size, though we can deal if it isn't. */
  162. void *
  163. memarea_alloc(memarea_t *area, size_t sz)
  164. {
  165. memarea_chunk_t *chunk = area->first;
  166. char *result;
  167. tor_assert(chunk);
  168. tor_assert(sz < SIZE_T_CEILING);
  169. if (sz == 0)
  170. sz = 1;
  171. if (chunk->next_mem+sz > chunk->u.mem+chunk->mem_size) {
  172. if (sz+CHUNK_HEADER_SIZE >= CHUNK_SIZE) {
  173. /* This allocation is too big. Stick it in a special chunk, and put
  174. * that chunk second in the list. */
  175. memarea_chunk_t *new_chunk = alloc_chunk(sz+CHUNK_HEADER_SIZE, 0);
  176. new_chunk->next_chunk = chunk->next_chunk;
  177. chunk->next_chunk = new_chunk;
  178. chunk = new_chunk;
  179. } else {
  180. memarea_chunk_t *new_chunk = alloc_chunk(CHUNK_SIZE, 1);
  181. new_chunk->next_chunk = chunk;
  182. area->first = chunk = new_chunk;
  183. }
  184. tor_assert(chunk->mem_size >= sz);
  185. }
  186. result = chunk->next_mem;
  187. chunk->next_mem = chunk->next_mem + sz;
  188. // XXXX021 remove these once bug 930 is solved.
  189. tor_assert(chunk->next_mem >= chunk->u.mem);
  190. tor_assert(chunk->next_mem <= chunk->u.mem+chunk->mem_size);
  191. chunk->next_mem = realign_pointer(chunk->next_mem);
  192. return result;
  193. }
  194. /** As memarea_alloc(), but clears the memory it returns. */
  195. void *
  196. memarea_alloc_zero(memarea_t *area, size_t sz)
  197. {
  198. void *result = memarea_alloc(area, sz);
  199. memset(result, 0, sz);
  200. return result;
  201. }
  202. /** As memdup, but returns the memory from <b>area</b>. */
  203. void *
  204. memarea_memdup(memarea_t *area, const void *s, size_t n)
  205. {
  206. char *result = memarea_alloc(area, n);
  207. memcpy(result, s, n);
  208. return result;
  209. }
  210. /** As strdup, but returns the memory from <b>area</b>. */
  211. char *
  212. memarea_strdup(memarea_t *area, const char *s)
  213. {
  214. return memarea_memdup(area, s, strlen(s)+1);
  215. }
  216. /** As strndup, but returns the memory from <b>area</b>. */
  217. char *
  218. memarea_strndup(memarea_t *area, const char *s, size_t n)
  219. {
  220. size_t ln;
  221. char *result;
  222. const char *cp, *end = s+n;
  223. tor_assert(n < SIZE_T_CEILING);
  224. for (cp = s; cp < end && *cp; ++cp)
  225. ;
  226. /* cp now points to s+n, or to the 0 in the string. */
  227. ln = cp-s;
  228. result = memarea_alloc(area, ln+1);
  229. memcpy(result, s, ln);
  230. result[ln]='\0';
  231. return result;
  232. }
  233. /** Set <b>allocated_out</b> to the number of bytes allocated in <b>area</b>,
  234. * and <b>used_out</b> to the number of bytes currently used. */
  235. void
  236. memarea_get_stats(memarea_t *area, size_t *allocated_out, size_t *used_out)
  237. {
  238. size_t a = 0, u = 0;
  239. memarea_chunk_t *chunk;
  240. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  241. a += CHUNK_HEADER_SIZE + chunk->mem_size;
  242. tor_assert(chunk->next_mem >= chunk->u.mem);
  243. u += CHUNK_HEADER_SIZE + (chunk->next_mem - chunk->u.mem);
  244. }
  245. *allocated_out = a;
  246. *used_out = u;
  247. }
  248. /** Assert that <b>area</b> is okay. */
  249. void
  250. memarea_assert_ok(memarea_t *area)
  251. {
  252. memarea_chunk_t *chunk;
  253. tor_assert(area->first);
  254. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  255. tor_assert(chunk->next_mem >= chunk->u.mem);
  256. tor_assert(chunk->next_mem <=
  257. (char*) realign_pointer(chunk->u.mem+chunk->mem_size));
  258. }
  259. }