memarea.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* Copyright (c) 2008-2012, 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 "torlog.h"
  13. /** If true, we try to detect any attempts to write beyond the length of a
  14. * memarea. */
  15. #define USE_SENTINELS
  16. /** All returned pointers should be aligned to the nearest multiple of this
  17. * value. */
  18. #define MEMAREA_ALIGN SIZEOF_VOID_P
  19. #if MEMAREA_ALIGN == 4
  20. #define MEMAREA_ALIGN_MASK 3lu
  21. #elif MEMAREA_ALIGN == 8
  22. #define MEMAREA_ALIGN_MASK 7lu
  23. #else
  24. #error "void* is neither 4 nor 8 bytes long. I don't know how to align stuff."
  25. #endif
  26. #ifdef USE_SENTINELS
  27. /** Magic value that we stick at the end of a memarea so we can make sure
  28. * there are no run-off-the-end bugs. */
  29. #define SENTINEL_VAL 0x90806622u
  30. /** How many bytes per area do we devote to the sentinel? */
  31. #define SENTINEL_LEN sizeof(uint32_t)
  32. /** Given a mem_area_chunk_t with SENTINEL_LEN extra bytes allocated at the
  33. * end, set those bytes. */
  34. #define SET_SENTINEL(chunk) \
  35. STMT_BEGIN \
  36. set_uint32( &(chunk)->u.mem[chunk->mem_size], SENTINEL_VAL ); \
  37. STMT_END
  38. /** Assert that the sentinel on a memarea is set correctly. */
  39. #define CHECK_SENTINEL(chunk) \
  40. STMT_BEGIN \
  41. uint32_t sent_val = get_uint32(&(chunk)->u.mem[chunk->mem_size]); \
  42. tor_assert(sent_val == SENTINEL_VAL); \
  43. STMT_END
  44. #else
  45. #define SENTINEL_LEN 0
  46. #define SET_SENTINEL(chunk) STMT_NIL
  47. #define CHECK_SENTINEL(chunk) STMT_NIL
  48. #endif
  49. /** Increment <b>ptr</b> until it is aligned to MEMAREA_ALIGN. */
  50. static INLINE void *
  51. realign_pointer(void *ptr)
  52. {
  53. uintptr_t x = (uintptr_t)ptr;
  54. x = (x+MEMAREA_ALIGN_MASK) & ~MEMAREA_ALIGN_MASK;
  55. /* Reinstate this if bug 930 ever reappears
  56. tor_assert(((void*)x) >= ptr);
  57. */
  58. return (void*)x;
  59. }
  60. /** Implements part of a memarea. New memory is carved off from chunk->mem in
  61. * increasing order until a request is too big, at which point a new chunk is
  62. * allocated. */
  63. typedef struct memarea_chunk_t {
  64. /** Next chunk in this area. Only kept around so we can free it. */
  65. struct memarea_chunk_t *next_chunk;
  66. size_t mem_size; /**< How much RAM is available in u.mem, total? */
  67. char *next_mem; /**< Next position in u.mem to allocate data at. If it's
  68. * greater than or equal to mem+mem_size, this chunk is
  69. * full. */
  70. union {
  71. char mem[1]; /**< Memory space in this chunk. */
  72. void *_void_for_alignment; /**< Dummy; used to make sure mem is aligned. */
  73. } u;
  74. } memarea_chunk_t;
  75. /** How many bytes are needed for overhead before we get to the memory part
  76. * of a chunk? */
  77. #define CHUNK_HEADER_SIZE STRUCT_OFFSET(memarea_chunk_t, u)
  78. /** What's the smallest that we'll allocate a chunk? */
  79. #define CHUNK_SIZE 4096
  80. /** A memarea_t is an allocation region for a set of small memory requests
  81. * that will all be freed at once. */
  82. struct memarea_t {
  83. memarea_chunk_t *first; /**< Top of the chunk stack: never NULL. */
  84. };
  85. /** How many chunks will we put into the freelist before freeing them? */
  86. #define MAX_FREELIST_LEN 4
  87. /** The number of memarea chunks currently in our freelist. */
  88. static int freelist_len=0;
  89. /** A linked list of unused memory area chunks. Used to prevent us from
  90. * spinning in malloc/free loops. */
  91. static memarea_chunk_t *freelist = NULL;
  92. /** Helper: allocate a new memarea chunk of around <b>chunk_size</b> bytes. */
  93. static memarea_chunk_t *
  94. alloc_chunk(size_t sz, int freelist_ok)
  95. {
  96. tor_assert(sz < SIZE_T_CEILING);
  97. if (freelist && freelist_ok) {
  98. memarea_chunk_t *res = freelist;
  99. freelist = res->next_chunk;
  100. res->next_chunk = NULL;
  101. --freelist_len;
  102. CHECK_SENTINEL(res);
  103. return res;
  104. } else {
  105. size_t chunk_size = freelist_ok ? CHUNK_SIZE : sz;
  106. memarea_chunk_t *res;
  107. chunk_size += SENTINEL_LEN;
  108. res = tor_malloc_roundup(&chunk_size);
  109. res->next_chunk = NULL;
  110. res->mem_size = chunk_size - CHUNK_HEADER_SIZE - SENTINEL_LEN;
  111. res->next_mem = res->u.mem;
  112. tor_assert(res->next_mem+res->mem_size+SENTINEL_LEN ==
  113. ((char*)res)+chunk_size);
  114. tor_assert(realign_pointer(res->next_mem) == res->next_mem);
  115. SET_SENTINEL(res);
  116. return res;
  117. }
  118. }
  119. /** Release <b>chunk</b> from a memarea, either by adding it to the freelist
  120. * or by freeing it if the freelist is already too big. */
  121. static void
  122. chunk_free_unchecked(memarea_chunk_t *chunk)
  123. {
  124. CHECK_SENTINEL(chunk);
  125. if (freelist_len < MAX_FREELIST_LEN) {
  126. ++freelist_len;
  127. chunk->next_chunk = freelist;
  128. freelist = chunk;
  129. chunk->next_mem = chunk->u.mem;
  130. } else {
  131. tor_free(chunk);
  132. }
  133. }
  134. /** Allocate and return new memarea. */
  135. memarea_t *
  136. memarea_new(void)
  137. {
  138. memarea_t *head = tor_malloc(sizeof(memarea_t));
  139. head->first = alloc_chunk(CHUNK_SIZE, 1);
  140. return head;
  141. }
  142. /** Free <b>area</b>, invalidating all pointers returned from memarea_alloc()
  143. * and friends for this area */
  144. void
  145. memarea_drop_all(memarea_t *area)
  146. {
  147. memarea_chunk_t *chunk, *next;
  148. for (chunk = area->first; chunk; chunk = next) {
  149. next = chunk->next_chunk;
  150. chunk_free_unchecked(chunk);
  151. }
  152. area->first = NULL; /*fail fast on */
  153. tor_free(area);
  154. }
  155. /** Forget about having allocated anything in <b>area</b>, and free some of
  156. * the backing storage associated with it, as appropriate. Invalidates all
  157. * pointers returned from memarea_alloc() for this area. */
  158. void
  159. memarea_clear(memarea_t *area)
  160. {
  161. memarea_chunk_t *chunk, *next;
  162. if (area->first->next_chunk) {
  163. for (chunk = area->first->next_chunk; chunk; chunk = next) {
  164. next = chunk->next_chunk;
  165. chunk_free_unchecked(chunk);
  166. }
  167. area->first->next_chunk = NULL;
  168. }
  169. area->first->next_mem = area->first->u.mem;
  170. }
  171. /** Remove all unused memarea chunks from the internal freelist. */
  172. void
  173. memarea_clear_freelist(void)
  174. {
  175. memarea_chunk_t *chunk, *next;
  176. freelist_len = 0;
  177. for (chunk = freelist; chunk; chunk = next) {
  178. next = chunk->next_chunk;
  179. tor_free(chunk);
  180. }
  181. freelist = NULL;
  182. }
  183. /** Return true iff <b>p</b> is in a range that has been returned by an
  184. * allocation from <b>area</b>. */
  185. int
  186. memarea_owns_ptr(const memarea_t *area, const void *p)
  187. {
  188. memarea_chunk_t *chunk;
  189. const char *ptr = p;
  190. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  191. if (ptr >= chunk->u.mem && ptr < chunk->next_mem)
  192. return 1;
  193. }
  194. return 0;
  195. }
  196. /** Return a pointer to a chunk of memory in <b>area</b> of at least <b>sz</b>
  197. * bytes. <b>sz</b> should be significantly smaller than the area's chunk
  198. * size, though we can deal if it isn't. */
  199. void *
  200. memarea_alloc(memarea_t *area, size_t sz)
  201. {
  202. memarea_chunk_t *chunk = area->first;
  203. char *result;
  204. tor_assert(chunk);
  205. CHECK_SENTINEL(chunk);
  206. tor_assert(sz < SIZE_T_CEILING);
  207. if (sz == 0)
  208. sz = 1;
  209. if (chunk->next_mem+sz > chunk->u.mem+chunk->mem_size) {
  210. if (sz+CHUNK_HEADER_SIZE >= CHUNK_SIZE) {
  211. /* This allocation is too big. Stick it in a special chunk, and put
  212. * that chunk second in the list. */
  213. memarea_chunk_t *new_chunk = alloc_chunk(sz+CHUNK_HEADER_SIZE, 0);
  214. new_chunk->next_chunk = chunk->next_chunk;
  215. chunk->next_chunk = new_chunk;
  216. chunk = new_chunk;
  217. } else {
  218. memarea_chunk_t *new_chunk = alloc_chunk(CHUNK_SIZE, 1);
  219. new_chunk->next_chunk = chunk;
  220. area->first = chunk = new_chunk;
  221. }
  222. tor_assert(chunk->mem_size >= sz);
  223. }
  224. result = chunk->next_mem;
  225. chunk->next_mem = chunk->next_mem + sz;
  226. /* Reinstate these if bug 930 ever comes back
  227. tor_assert(chunk->next_mem >= chunk->u.mem);
  228. tor_assert(chunk->next_mem <= chunk->u.mem+chunk->mem_size);
  229. */
  230. chunk->next_mem = realign_pointer(chunk->next_mem);
  231. return result;
  232. }
  233. /** As memarea_alloc(), but clears the memory it returns. */
  234. void *
  235. memarea_alloc_zero(memarea_t *area, size_t sz)
  236. {
  237. void *result = memarea_alloc(area, sz);
  238. memset(result, 0, sz);
  239. return result;
  240. }
  241. /** As memdup, but returns the memory from <b>area</b>. */
  242. void *
  243. memarea_memdup(memarea_t *area, const void *s, size_t n)
  244. {
  245. char *result = memarea_alloc(area, n);
  246. memcpy(result, s, n);
  247. return result;
  248. }
  249. /** As strdup, but returns the memory from <b>area</b>. */
  250. char *
  251. memarea_strdup(memarea_t *area, const char *s)
  252. {
  253. return memarea_memdup(area, s, strlen(s)+1);
  254. }
  255. /** As strndup, but returns the memory from <b>area</b>. */
  256. char *
  257. memarea_strndup(memarea_t *area, const char *s, size_t n)
  258. {
  259. size_t ln;
  260. char *result;
  261. const char *cp, *end = s+n;
  262. tor_assert(n < SIZE_T_CEILING);
  263. for (cp = s; cp < end && *cp; ++cp)
  264. ;
  265. /* cp now points to s+n, or to the 0 in the string. */
  266. ln = cp-s;
  267. result = memarea_alloc(area, ln+1);
  268. memcpy(result, s, ln);
  269. result[ln]='\0';
  270. return result;
  271. }
  272. /** Set <b>allocated_out</b> to the number of bytes allocated in <b>area</b>,
  273. * and <b>used_out</b> to the number of bytes currently used. */
  274. void
  275. memarea_get_stats(memarea_t *area, size_t *allocated_out, size_t *used_out)
  276. {
  277. size_t a = 0, u = 0;
  278. memarea_chunk_t *chunk;
  279. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  280. CHECK_SENTINEL(chunk);
  281. a += CHUNK_HEADER_SIZE + chunk->mem_size;
  282. tor_assert(chunk->next_mem >= chunk->u.mem);
  283. u += CHUNK_HEADER_SIZE + (chunk->next_mem - chunk->u.mem);
  284. }
  285. *allocated_out = a;
  286. *used_out = u;
  287. }
  288. /** Assert that <b>area</b> is okay. */
  289. void
  290. memarea_assert_ok(memarea_t *area)
  291. {
  292. memarea_chunk_t *chunk;
  293. tor_assert(area->first);
  294. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  295. CHECK_SENTINEL(chunk);
  296. tor_assert(chunk->next_mem >= chunk->u.mem);
  297. tor_assert(chunk->next_mem <=
  298. (char*) realign_pointer(chunk->u.mem+chunk->mem_size));
  299. }
  300. }