memarea.c 11 KB

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