memarea.c 9.1 KB

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