memarea.c 8.8 KB

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