memarea.c 9.3 KB

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