memarea.c 9.8 KB

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