memarea.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /* Copyright (c) 2008-2018, 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 <stddef.h>
  9. #include <stdlib.h>
  10. #include "memarea.h"
  11. #include "util.h"
  12. #include "compat.h"
  13. #include "torlog.h"
  14. #include "container.h"
  15. #ifndef DISABLE_MEMORY_SENTINELS
  16. /** If true, we try to detect any attempts to write beyond the length of a
  17. * memarea. */
  18. #define USE_SENTINELS
  19. /** All returned pointers should be aligned to the nearest multiple of this
  20. * value. */
  21. #define MEMAREA_ALIGN SIZEOF_VOID_P
  22. /** A value which, when masked out of a pointer, produces a maximally aligned
  23. * pointer. */
  24. #if MEMAREA_ALIGN == 4
  25. #define MEMAREA_ALIGN_MASK ((uintptr_t)3)
  26. #elif MEMAREA_ALIGN == 8
  27. #define MEMAREA_ALIGN_MASK ((uintptr_t)7)
  28. #else
  29. #error "void* is neither 4 nor 8 bytes long. I don't know how to align stuff."
  30. #endif /* MEMAREA_ALIGN == 4 || ... */
  31. #if defined(__GNUC__) && defined(FLEXIBLE_ARRAY_MEMBER)
  32. #define USE_ALIGNED_ATTRIBUTE
  33. /** Name for the 'memory' member of a memory chunk. */
  34. #define U_MEM mem
  35. #else
  36. #define U_MEM u.mem
  37. #endif /* defined(__GNUC__) && defined(FLEXIBLE_ARRAY_MEMBER) */
  38. #ifdef USE_SENTINELS
  39. /** Magic value that we stick at the end of a memarea so we can make sure
  40. * there are no run-off-the-end bugs. */
  41. #define SENTINEL_VAL 0x90806622u
  42. /** How many bytes per area do we devote to the sentinel? */
  43. #define SENTINEL_LEN sizeof(uint32_t)
  44. /** Given a mem_area_chunk_t with SENTINEL_LEN extra bytes allocated at the
  45. * end, set those bytes. */
  46. #define SET_SENTINEL(chunk) \
  47. STMT_BEGIN \
  48. set_uint32( &(chunk)->U_MEM[chunk->mem_size], SENTINEL_VAL ); \
  49. STMT_END
  50. /** Assert that the sentinel on a memarea is set correctly. */
  51. #define CHECK_SENTINEL(chunk) \
  52. STMT_BEGIN \
  53. uint32_t sent_val = get_uint32(&(chunk)->U_MEM[chunk->mem_size]); \
  54. tor_assert(sent_val == SENTINEL_VAL); \
  55. STMT_END
  56. #else /* !(defined(USE_SENTINELS)) */
  57. #define SENTINEL_LEN 0
  58. #define SET_SENTINEL(chunk) STMT_NIL
  59. #define CHECK_SENTINEL(chunk) STMT_NIL
  60. #endif /* defined(USE_SENTINELS) */
  61. /** Increment <b>ptr</b> until it is aligned to MEMAREA_ALIGN. */
  62. static inline void *
  63. realign_pointer(void *ptr)
  64. {
  65. uintptr_t x = (uintptr_t)ptr;
  66. x = (x+MEMAREA_ALIGN_MASK) & ~MEMAREA_ALIGN_MASK;
  67. /* Reinstate this if bug 930 ever reappears
  68. tor_assert(((void*)x) >= ptr);
  69. */
  70. return (void*)x;
  71. }
  72. /** Implements part of a memarea. New memory is carved off from chunk->mem in
  73. * increasing order until a request is too big, at which point a new chunk is
  74. * allocated. */
  75. typedef struct memarea_chunk_t {
  76. /** Next chunk in this area. Only kept around so we can free it. */
  77. struct memarea_chunk_t *next_chunk;
  78. size_t mem_size; /**< How much RAM is available in mem, total? */
  79. char *next_mem; /**< Next position in mem to allocate data at. If it's
  80. * equal to mem+mem_size, this chunk is full. */
  81. #ifdef USE_ALIGNED_ATTRIBUTE
  82. /** Actual content of the memory chunk. */
  83. char mem[FLEXIBLE_ARRAY_MEMBER] __attribute__((aligned(MEMAREA_ALIGN)));
  84. #else
  85. union {
  86. char mem[1]; /**< Memory space in this chunk. */
  87. void *void_for_alignment_; /**< Dummy; used to make sure mem is aligned. */
  88. } u; /**< Union used to enforce alignment when we don't have support for
  89. * doing it right. */
  90. #endif /* defined(USE_ALIGNED_ATTRIBUTE) */
  91. } memarea_chunk_t;
  92. /** How many bytes are needed for overhead before we get to the memory part
  93. * of a chunk? */
  94. #define CHUNK_HEADER_SIZE offsetof(memarea_chunk_t, U_MEM)
  95. /** What's the smallest that we'll allocate a chunk? */
  96. #define CHUNK_SIZE 4096
  97. /** A memarea_t is an allocation region for a set of small memory requests
  98. * that will all be freed at once. */
  99. struct memarea_t {
  100. memarea_chunk_t *first; /**< Top of the chunk stack: never NULL. */
  101. };
  102. /** Helper: allocate a new memarea chunk of around <b>chunk_size</b> bytes. */
  103. static memarea_chunk_t *
  104. alloc_chunk(size_t sz)
  105. {
  106. tor_assert(sz < SIZE_T_CEILING);
  107. size_t chunk_size = sz < CHUNK_SIZE ? CHUNK_SIZE : sz;
  108. memarea_chunk_t *res;
  109. chunk_size += SENTINEL_LEN;
  110. res = tor_malloc(chunk_size);
  111. res->next_chunk = NULL;
  112. res->mem_size = chunk_size - CHUNK_HEADER_SIZE - SENTINEL_LEN;
  113. res->next_mem = res->U_MEM;
  114. tor_assert(res->next_mem+res->mem_size+SENTINEL_LEN ==
  115. ((char*)res)+chunk_size);
  116. tor_assert(realign_pointer(res->next_mem) == res->next_mem);
  117. SET_SENTINEL(res);
  118. return res;
  119. }
  120. /** Release <b>chunk</b> from a memarea. */
  121. static void
  122. memarea_chunk_free_unchecked(memarea_chunk_t *chunk)
  123. {
  124. CHECK_SENTINEL(chunk);
  125. tor_free(chunk);
  126. }
  127. /** Allocate and return new memarea. */
  128. memarea_t *
  129. memarea_new(void)
  130. {
  131. memarea_t *head = tor_malloc(sizeof(memarea_t));
  132. head->first = alloc_chunk(CHUNK_SIZE);
  133. return head;
  134. }
  135. /** Free <b>area</b>, invalidating all pointers returned from memarea_alloc()
  136. * and friends for this area */
  137. void
  138. memarea_drop_all_(memarea_t *area)
  139. {
  140. memarea_chunk_t *chunk, *next;
  141. for (chunk = area->first; chunk; chunk = next) {
  142. next = chunk->next_chunk;
  143. memarea_chunk_free_unchecked(chunk);
  144. }
  145. area->first = NULL; /*fail fast on */
  146. tor_free(area);
  147. }
  148. /** Forget about having allocated anything in <b>area</b>, and free some of
  149. * the backing storage associated with it, as appropriate. Invalidates all
  150. * pointers returned from memarea_alloc() for this area. */
  151. void
  152. memarea_clear(memarea_t *area)
  153. {
  154. memarea_chunk_t *chunk, *next;
  155. if (area->first->next_chunk) {
  156. for (chunk = area->first->next_chunk; chunk; chunk = next) {
  157. next = chunk->next_chunk;
  158. memarea_chunk_free_unchecked(chunk);
  159. }
  160. area->first->next_chunk = NULL;
  161. }
  162. area->first->next_mem = area->first->U_MEM;
  163. }
  164. /** Return true iff <b>p</b> is in a range that has been returned by an
  165. * allocation from <b>area</b>. */
  166. int
  167. memarea_owns_ptr(const memarea_t *area, const void *p)
  168. {
  169. memarea_chunk_t *chunk;
  170. const char *ptr = p;
  171. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  172. if (ptr >= chunk->U_MEM && ptr < chunk->next_mem)
  173. return 1;
  174. }
  175. return 0;
  176. }
  177. /** Return a pointer to a chunk of memory in <b>area</b> of at least <b>sz</b>
  178. * bytes. <b>sz</b> should be significantly smaller than the area's chunk
  179. * size, though we can deal if it isn't. */
  180. void *
  181. memarea_alloc(memarea_t *area, size_t sz)
  182. {
  183. memarea_chunk_t *chunk = area->first;
  184. char *result;
  185. tor_assert(chunk);
  186. CHECK_SENTINEL(chunk);
  187. tor_assert(sz < SIZE_T_CEILING);
  188. if (sz == 0)
  189. sz = 1;
  190. tor_assert(chunk->next_mem <= chunk->U_MEM + chunk->mem_size);
  191. const size_t space_remaining =
  192. (chunk->U_MEM + chunk->mem_size) - chunk->next_mem;
  193. if (sz > space_remaining) {
  194. if (sz+CHUNK_HEADER_SIZE >= CHUNK_SIZE) {
  195. /* This allocation is too big. Stick it in a special chunk, and put
  196. * that chunk second in the list. */
  197. memarea_chunk_t *new_chunk = alloc_chunk(sz+CHUNK_HEADER_SIZE);
  198. new_chunk->next_chunk = chunk->next_chunk;
  199. chunk->next_chunk = new_chunk;
  200. chunk = new_chunk;
  201. } else {
  202. memarea_chunk_t *new_chunk = alloc_chunk(CHUNK_SIZE);
  203. new_chunk->next_chunk = chunk;
  204. area->first = chunk = new_chunk;
  205. }
  206. tor_assert(chunk->mem_size >= sz);
  207. }
  208. result = chunk->next_mem;
  209. chunk->next_mem = chunk->next_mem + sz;
  210. /* Reinstate these if bug 930 ever comes back
  211. tor_assert(chunk->next_mem >= chunk->U_MEM);
  212. tor_assert(chunk->next_mem <= chunk->U_MEM+chunk->mem_size);
  213. */
  214. chunk->next_mem = realign_pointer(chunk->next_mem);
  215. return result;
  216. }
  217. /** As memarea_alloc(), but clears the memory it returns. */
  218. void *
  219. memarea_alloc_zero(memarea_t *area, size_t sz)
  220. {
  221. void *result = memarea_alloc(area, sz);
  222. memset(result, 0, sz);
  223. return result;
  224. }
  225. /** As memdup, but returns the memory from <b>area</b>. */
  226. void *
  227. memarea_memdup(memarea_t *area, const void *s, size_t n)
  228. {
  229. char *result = memarea_alloc(area, n);
  230. memcpy(result, s, n);
  231. return result;
  232. }
  233. /** As strdup, but returns the memory from <b>area</b>. */
  234. char *
  235. memarea_strdup(memarea_t *area, const char *s)
  236. {
  237. return memarea_memdup(area, s, strlen(s)+1);
  238. }
  239. /** As strndup, but returns the memory from <b>area</b>. */
  240. char *
  241. memarea_strndup(memarea_t *area, const char *s, size_t n)
  242. {
  243. size_t ln = 0;
  244. char *result;
  245. tor_assert(n < SIZE_T_CEILING);
  246. for (ln = 0; ln < n && s[ln]; ++ln)
  247. ;
  248. result = memarea_alloc(area, ln+1);
  249. memcpy(result, s, ln);
  250. result[ln]='\0';
  251. return result;
  252. }
  253. /** Set <b>allocated_out</b> to the number of bytes allocated in <b>area</b>,
  254. * and <b>used_out</b> to the number of bytes currently used. */
  255. void
  256. memarea_get_stats(memarea_t *area, size_t *allocated_out, size_t *used_out)
  257. {
  258. size_t a = 0, u = 0;
  259. memarea_chunk_t *chunk;
  260. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  261. CHECK_SENTINEL(chunk);
  262. a += CHUNK_HEADER_SIZE + chunk->mem_size;
  263. tor_assert(chunk->next_mem >= chunk->U_MEM);
  264. u += CHUNK_HEADER_SIZE + (chunk->next_mem - chunk->U_MEM);
  265. }
  266. *allocated_out = a;
  267. *used_out = u;
  268. }
  269. /** Assert that <b>area</b> is okay. */
  270. void
  271. memarea_assert_ok(memarea_t *area)
  272. {
  273. memarea_chunk_t *chunk;
  274. tor_assert(area->first);
  275. for (chunk = area->first; chunk; chunk = chunk->next_chunk) {
  276. CHECK_SENTINEL(chunk);
  277. tor_assert(chunk->next_mem >= chunk->U_MEM);
  278. tor_assert(chunk->next_mem <=
  279. (char*) realign_pointer(chunk->U_MEM+chunk->mem_size));
  280. }
  281. }
  282. #else /* !(!defined(DISABLE_MEMORY_SENTINELS)) */
  283. struct memarea_t {
  284. smartlist_t *pieces;
  285. };
  286. memarea_t *
  287. memarea_new(void)
  288. {
  289. memarea_t *ma = tor_malloc_zero(sizeof(memarea_t));
  290. ma->pieces = smartlist_new();
  291. return ma;
  292. }
  293. void
  294. memarea_drop_all_(memarea_t *area)
  295. {
  296. memarea_clear(area);
  297. smartlist_free(area->pieces);
  298. tor_free(area);
  299. }
  300. void
  301. memarea_clear(memarea_t *area)
  302. {
  303. SMARTLIST_FOREACH(area->pieces, void *, p, tor_free_(p));
  304. smartlist_clear(area->pieces);
  305. }
  306. int
  307. memarea_owns_ptr(const memarea_t *area, const void *ptr)
  308. {
  309. SMARTLIST_FOREACH(area->pieces, const void *, p, if (ptr == p) return 1;);
  310. return 0;
  311. }
  312. void *
  313. memarea_alloc(memarea_t *area, size_t sz)
  314. {
  315. void *result = tor_malloc(sz);
  316. smartlist_add(area->pieces, result);
  317. return result;
  318. }
  319. void *
  320. memarea_alloc_zero(memarea_t *area, size_t sz)
  321. {
  322. void *result = tor_malloc_zero(sz);
  323. smartlist_add(area->pieces, result);
  324. return result;
  325. }
  326. void *
  327. memarea_memdup(memarea_t *area, const void *s, size_t n)
  328. {
  329. void *r = memarea_alloc(area, n);
  330. memcpy(r, s, n);
  331. return r;
  332. }
  333. char *
  334. memarea_strdup(memarea_t *area, const char *s)
  335. {
  336. size_t n = strlen(s);
  337. char *r = memarea_alloc(area, n+1);
  338. memcpy(r, s, n);
  339. r[n] = 0;
  340. return r;
  341. }
  342. char *
  343. memarea_strndup(memarea_t *area, const char *s, size_t n)
  344. {
  345. size_t ln = strnlen(s, n);
  346. char *r = memarea_alloc(area, ln+1);
  347. memcpy(r, s, ln);
  348. r[ln] = 0;
  349. return r;
  350. }
  351. void
  352. memarea_get_stats(memarea_t *area,
  353. size_t *allocated_out, size_t *used_out)
  354. {
  355. (void)area;
  356. *allocated_out = *used_out = 128;
  357. }
  358. void
  359. memarea_assert_ok(memarea_t *area)
  360. {
  361. (void)area;
  362. }
  363. #endif /* !defined(DISABLE_MEMORY_SENTINELS) */