memarea.c 11 KB

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