memarea.c 11 KB

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