memarea.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. * equal to mem+mem_size, this chunk is full. */
  78. #ifdef USE_ALIGNED_ATTRIBUTE
  79. /** Actual content of the memory chunk. */
  80. char mem[FLEXIBLE_ARRAY_MEMBER] __attribute__((aligned(MEMAREA_ALIGN)));
  81. #else
  82. union {
  83. char mem[1]; /**< Memory space in this chunk. */
  84. void *void_for_alignment_; /**< Dummy; used to make sure mem is aligned. */
  85. } u; /**< Union used to enforce alignment when we don't have support for
  86. * doing it right. */
  87. #endif
  88. } memarea_chunk_t;
  89. /** How many bytes are needed for overhead before we get to the memory part
  90. * of a chunk? */
  91. #define CHUNK_HEADER_SIZE STRUCT_OFFSET(memarea_chunk_t, U_MEM)
  92. /** What's the smallest that we'll allocate a chunk? */
  93. #define CHUNK_SIZE 4096
  94. /** A memarea_t is an allocation region for a set of small memory requests
  95. * that will all be freed at once. */
  96. struct memarea_t {
  97. memarea_chunk_t *first; /**< Top of the chunk stack: never NULL. */
  98. };
  99. /** Helper: allocate a new memarea chunk of around <b>chunk_size</b> bytes. */
  100. static memarea_chunk_t *
  101. alloc_chunk(size_t sz)
  102. {
  103. tor_assert(sz < SIZE_T_CEILING);
  104. size_t chunk_size = sz < CHUNK_SIZE ? CHUNK_SIZE : sz;
  105. memarea_chunk_t *res;
  106. chunk_size += SENTINEL_LEN;
  107. res = tor_malloc(chunk_size);
  108. res->next_chunk = NULL;
  109. res->mem_size = chunk_size - CHUNK_HEADER_SIZE - SENTINEL_LEN;
  110. res->next_mem = res->U_MEM;
  111. tor_assert(res->next_mem+res->mem_size+SENTINEL_LEN ==
  112. ((char*)res)+chunk_size);
  113. tor_assert(realign_pointer(res->next_mem) == res->next_mem);
  114. SET_SENTINEL(res);
  115. return res;
  116. }
  117. /** Release <b>chunk</b> from a memarea. */
  118. static void
  119. memarea_chunk_free_unchecked(memarea_chunk_t *chunk)
  120. {
  121. CHECK_SENTINEL(chunk);
  122. tor_free(chunk);
  123. }
  124. /** Allocate and return new memarea. */
  125. memarea_t *
  126. memarea_new(void)
  127. {
  128. memarea_t *head = tor_malloc(sizeof(memarea_t));
  129. head->first = alloc_chunk(CHUNK_SIZE);
  130. return head;
  131. }
  132. /** Free <b>area</b>, invalidating all pointers returned from memarea_alloc()
  133. * and friends for this area */
  134. void
  135. memarea_drop_all(memarea_t *area)
  136. {
  137. memarea_chunk_t *chunk, *next;
  138. for (chunk = area->first; chunk; chunk = next) {
  139. next = chunk->next_chunk;
  140. memarea_chunk_free_unchecked(chunk);
  141. }
  142. area->first = NULL; /*fail fast on */
  143. tor_free(area);
  144. }
  145. /** Forget about having allocated anything in <b>area</b>, and free some of
  146. * the backing storage associated with it, as appropriate. Invalidates all
  147. * pointers returned from memarea_alloc() for this area. */
  148. void
  149. memarea_clear(memarea_t *area)
  150. {
  151. memarea_chunk_t *chunk, *next;
  152. if (area->first->next_chunk) {
  153. for (chunk = area->first->next_chunk; chunk; chunk = next) {
  154. next = chunk->next_chunk;
  155. memarea_chunk_free_unchecked(chunk);
  156. }
  157. area->first->next_chunk = NULL;
  158. }
  159. area->first->next_mem = area->first->U_MEM;
  160. }
  161. /** Return true iff <b>p</b> is in a range that has been returned by an
  162. * allocation from <b>area</b>. */
  163. int
  164. memarea_owns_ptr(const memarea_t *area, const void *p)
  165. {
  166. memarea_chunk_t *chunk;
  167. const char *ptr = p;
  168. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  169. if (ptr >= chunk->U_MEM && ptr < chunk->next_mem)
  170. return 1;
  171. }
  172. return 0;
  173. }
  174. /** Return a pointer to a chunk of memory in <b>area</b> of at least <b>sz</b>
  175. * bytes. <b>sz</b> should be significantly smaller than the area's chunk
  176. * size, though we can deal if it isn't. */
  177. void *
  178. memarea_alloc(memarea_t *area, size_t sz)
  179. {
  180. memarea_chunk_t *chunk = area->first;
  181. char *result;
  182. tor_assert(chunk);
  183. CHECK_SENTINEL(chunk);
  184. tor_assert(sz < SIZE_T_CEILING);
  185. if (sz == 0)
  186. sz = 1;
  187. tor_assert(chunk->next_mem <= chunk->U_MEM + chunk->mem_size);
  188. const size_t space_remaining =
  189. (chunk->U_MEM + chunk->mem_size) - chunk->next_mem;
  190. if (sz > space_remaining) {
  191. if (sz+CHUNK_HEADER_SIZE >= CHUNK_SIZE) {
  192. /* This allocation is too big. Stick it in a special chunk, and put
  193. * that chunk second in the list. */
  194. memarea_chunk_t *new_chunk = alloc_chunk(sz+CHUNK_HEADER_SIZE);
  195. new_chunk->next_chunk = chunk->next_chunk;
  196. chunk->next_chunk = new_chunk;
  197. chunk = new_chunk;
  198. } else {
  199. memarea_chunk_t *new_chunk = alloc_chunk(CHUNK_SIZE);
  200. new_chunk->next_chunk = chunk;
  201. area->first = chunk = new_chunk;
  202. }
  203. tor_assert(chunk->mem_size >= sz);
  204. }
  205. result = chunk->next_mem;
  206. chunk->next_mem = chunk->next_mem + sz;
  207. /* Reinstate these if bug 930 ever comes back
  208. tor_assert(chunk->next_mem >= chunk->U_MEM);
  209. tor_assert(chunk->next_mem <= chunk->U_MEM+chunk->mem_size);
  210. */
  211. chunk->next_mem = realign_pointer(chunk->next_mem);
  212. return result;
  213. }
  214. /** As memarea_alloc(), but clears the memory it returns. */
  215. void *
  216. memarea_alloc_zero(memarea_t *area, size_t sz)
  217. {
  218. void *result = memarea_alloc(area, sz);
  219. memset(result, 0, sz);
  220. return result;
  221. }
  222. /** As memdup, but returns the memory from <b>area</b>. */
  223. void *
  224. memarea_memdup(memarea_t *area, const void *s, size_t n)
  225. {
  226. char *result = memarea_alloc(area, n);
  227. memcpy(result, s, n);
  228. return result;
  229. }
  230. /** As strdup, but returns the memory from <b>area</b>. */
  231. char *
  232. memarea_strdup(memarea_t *area, const char *s)
  233. {
  234. return memarea_memdup(area, s, strlen(s)+1);
  235. }
  236. /** As strndup, but returns the memory from <b>area</b>. */
  237. char *
  238. memarea_strndup(memarea_t *area, const char *s, size_t n)
  239. {
  240. size_t ln = 0;
  241. char *result;
  242. tor_assert(n < SIZE_T_CEILING);
  243. for (ln = 0; ln < n && s[ln]; ++ln)
  244. ;
  245. result = memarea_alloc(area, ln+1);
  246. memcpy(result, s, ln);
  247. result[ln]='\0';
  248. return result;
  249. }
  250. /** Set <b>allocated_out</b> to the number of bytes allocated in <b>area</b>,
  251. * and <b>used_out</b> to the number of bytes currently used. */
  252. void
  253. memarea_get_stats(memarea_t *area, size_t *allocated_out, size_t *used_out)
  254. {
  255. size_t a = 0, u = 0;
  256. memarea_chunk_t *chunk;
  257. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  258. CHECK_SENTINEL(chunk);
  259. a += CHUNK_HEADER_SIZE + chunk->mem_size;
  260. tor_assert(chunk->next_mem >= chunk->U_MEM);
  261. u += CHUNK_HEADER_SIZE + (chunk->next_mem - chunk->U_MEM);
  262. }
  263. *allocated_out = a;
  264. *used_out = u;
  265. }
  266. /** Assert that <b>area</b> is okay. */
  267. void
  268. memarea_assert_ok(memarea_t *area)
  269. {
  270. memarea_chunk_t *chunk;
  271. tor_assert(area->first);
  272. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  273. CHECK_SENTINEL(chunk);
  274. tor_assert(chunk->next_mem >= chunk->U_MEM);
  275. tor_assert(chunk->next_mem <=
  276. (char*) realign_pointer(chunk->U_MEM+chunk->mem_size));
  277. }
  278. }