mempool.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /* Copyright (c) 2007-2012, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #if 1
  4. /* Tor dependencies */
  5. #include "orconfig.h"
  6. #endif
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "torint.h"
  10. #define MEMPOOL_PRIVATE
  11. #include "mempool.h"
  12. /* OVERVIEW:
  13. *
  14. * This is an implementation of memory pools for Tor cells. It may be
  15. * useful for you too.
  16. *
  17. * Generally, a memory pool is an allocation strategy optimized for large
  18. * numbers of identically-sized objects. Rather than the elaborate arena
  19. * and coalescing strategies you need to get good performance for a
  20. * general-purpose malloc(), pools use a series of large memory "chunks",
  21. * each of which is carved into a bunch of smaller "items" or
  22. * "allocations".
  23. *
  24. * To get decent performance, you need to:
  25. * - Minimize the number of times you hit the underlying allocator.
  26. * - Try to keep accesses as local in memory as possible.
  27. * - Try to keep the common case fast.
  28. *
  29. * Our implementation uses three lists of chunks per pool. Each chunk can
  30. * be either "full" (no more room for items); "empty" (no items); or
  31. * "used" (not full, not empty). There are independent doubly-linked
  32. * lists for each state.
  33. *
  34. * CREDIT:
  35. *
  36. * I wrote this after looking at 3 or 4 other pooling allocators, but
  37. * without copying. The strategy this most resembles (which is funny,
  38. * since that's the one I looked at longest ago) is the pool allocator
  39. * underlying Python's obmalloc code. Major differences from obmalloc's
  40. * pools are:
  41. * - We don't even try to be threadsafe.
  42. * - We only handle objects of one size.
  43. * - Our list of empty chunks is doubly-linked, not singly-linked.
  44. * (This could change pretty easily; it's only doubly-linked for
  45. * consistency.)
  46. * - We keep a list of full chunks (so we can have a "nuke everything"
  47. * function). Obmalloc's pools leave full chunks to float unanchored.
  48. *
  49. * LIMITATIONS:
  50. * - Not even slightly threadsafe.
  51. * - Likes to have lots of items per chunks.
  52. * - One pointer overhead per allocated thing. (The alternative is
  53. * something like glib's use of an RB-tree to keep track of what
  54. * chunk any given piece of memory is in.)
  55. * - Only aligns allocated things to void* level: redefine ALIGNMENT_TYPE
  56. * if you need doubles.
  57. * - Could probably be optimized a bit; the representation contains
  58. * a bit more info than it really needs to have.
  59. */
  60. #if 1
  61. /* Tor dependencies */
  62. #include "util.h"
  63. #include "compat.h"
  64. #include "torlog.h"
  65. #define ALLOC(x) tor_malloc(x)
  66. #define FREE(x) tor_free(x)
  67. #define ASSERT(x) tor_assert(x)
  68. #undef ALLOC_CAN_RETURN_NULL
  69. #define TOR
  70. //#define ALLOC_ROUNDUP(p) tor_malloc_roundup(p)
  71. /* End Tor dependencies */
  72. #else
  73. /* If you're not building this as part of Tor, you'll want to define the
  74. * following macros. For now, these should do as defaults.
  75. */
  76. #include <assert.h>
  77. #define PREDICT_UNLIKELY(x) (x)
  78. #define PREDICT_LIKELY(x) (x)
  79. #define ALLOC(x) malloc(x)
  80. #define FREE(x) free(x)
  81. #define STRUCT_OFFSET(tp, member) \
  82. ((off_t) (((char*)&((tp*)0)->member)-(char*)0))
  83. #define ASSERT(x) assert(x)
  84. #define ALLOC_CAN_RETURN_NULL
  85. #endif
  86. /* Tuning parameters */
  87. /** Largest type that we need to ensure returned memory items are aligned to.
  88. * Change this to "double" if we need to be safe for structs with doubles. */
  89. #define ALIGNMENT_TYPE void *
  90. /** Increment that we need to align allocated. */
  91. #define ALIGNMENT sizeof(ALIGNMENT_TYPE)
  92. /** Largest memory chunk that we should allocate. */
  93. #define MAX_CHUNK (8*(1L<<20))
  94. /** Smallest memory chunk size that we should allocate. */
  95. #define MIN_CHUNK 4096
  96. typedef struct mp_allocated_t mp_allocated_t;
  97. typedef struct mp_chunk_t mp_chunk_t;
  98. /** Holds a single allocated item, allocated as part of a chunk. */
  99. struct mp_allocated_t {
  100. /** The chunk that this item is allocated in. This adds overhead to each
  101. * allocated item, thus making this implementation inappropriate for
  102. * very small items. */
  103. mp_chunk_t *in_chunk;
  104. union {
  105. /** If this item is free, the next item on the free list. */
  106. mp_allocated_t *next_free;
  107. /** If this item is not free, the actual memory contents of this item.
  108. * (Not actual size.) */
  109. char mem[1];
  110. /** An extra element to the union to insure correct alignment. */
  111. ALIGNMENT_TYPE _dummy;
  112. } u;
  113. };
  114. /** 'Magic' value used to detect memory corruption. */
  115. #define MP_CHUNK_MAGIC 0x09870123
  116. /** A chunk of memory. Chunks come from malloc; we use them */
  117. struct mp_chunk_t {
  118. unsigned long magic; /**< Must be MP_CHUNK_MAGIC if this chunk is valid. */
  119. mp_chunk_t *next; /**< The next free, used, or full chunk in sequence. */
  120. mp_chunk_t *prev; /**< The previous free, used, or full chunk in sequence. */
  121. mp_pool_t *pool; /**< The pool that this chunk is part of. */
  122. /** First free item in the freelist for this chunk. Note that this may be
  123. * NULL even if this chunk is not at capacity: if so, the free memory at
  124. * next_mem has not yet been carved into items.
  125. */
  126. mp_allocated_t *first_free;
  127. int n_allocated; /**< Number of currently allocated items in this chunk. */
  128. int capacity; /**< Number of items that can be fit into this chunk. */
  129. size_t mem_size; /**< Number of usable bytes in mem. */
  130. char *next_mem; /**< Pointer into part of <b>mem</b> not yet carved up. */
  131. char mem[FLEXIBLE_ARRAY_MEMBER]; /**< Storage for this chunk. */
  132. };
  133. /** Number of extra bytes needed beyond mem_size to allocate a chunk. */
  134. #define CHUNK_OVERHEAD STRUCT_OFFSET(mp_chunk_t, mem[0])
  135. /** Given a pointer to a mp_allocated_t, return a pointer to the memory
  136. * item it holds. */
  137. #define A2M(a) (&(a)->u.mem)
  138. /** Given a pointer to a memory_item_t, return a pointer to its enclosing
  139. * mp_allocated_t. */
  140. #define M2A(p) ( ((char*)p) - STRUCT_OFFSET(mp_allocated_t, u.mem) )
  141. #ifdef ALLOC_CAN_RETURN_NULL
  142. /** If our ALLOC() macro can return NULL, check whether <b>x</b> is NULL,
  143. * and if so, return NULL. */
  144. #define CHECK_ALLOC(x) \
  145. if (PREDICT_UNLIKELY(!x)) { return NULL; }
  146. #else
  147. /** If our ALLOC() macro can't return NULL, do nothing. */
  148. #define CHECK_ALLOC(x)
  149. #endif
  150. /** Helper: Allocate and return a new memory chunk for <b>pool</b>. Does not
  151. * link the chunk into any list. */
  152. static mp_chunk_t *
  153. mp_chunk_new(mp_pool_t *pool)
  154. {
  155. size_t sz = pool->new_chunk_capacity * pool->item_alloc_size;
  156. #ifdef ALLOC_ROUNDUP
  157. size_t alloc_size = CHUNK_OVERHEAD + sz;
  158. mp_chunk_t *chunk = ALLOC_ROUNDUP(&alloc_size);
  159. #else
  160. mp_chunk_t *chunk = ALLOC(CHUNK_OVERHEAD + sz);
  161. #endif
  162. #ifdef MEMPOOL_STATS
  163. ++pool->total_chunks_allocated;
  164. #endif
  165. CHECK_ALLOC(chunk);
  166. memset(chunk, 0, sizeof(mp_chunk_t)); /* Doesn't clear the whole thing. */
  167. chunk->magic = MP_CHUNK_MAGIC;
  168. #ifdef ALLOC_ROUNDUP
  169. chunk->mem_size = alloc_size - CHUNK_OVERHEAD;
  170. chunk->capacity = chunk->mem_size / pool->item_alloc_size;
  171. #else
  172. chunk->capacity = pool->new_chunk_capacity;
  173. chunk->mem_size = sz;
  174. #endif
  175. chunk->next_mem = chunk->mem;
  176. chunk->pool = pool;
  177. return chunk;
  178. }
  179. /** Take a <b>chunk</b> that has just been allocated or removed from
  180. * <b>pool</b>'s empty chunk list, and add it to the head of the used chunk
  181. * list. */
  182. static INLINE void
  183. add_newly_used_chunk_to_used_list(mp_pool_t *pool, mp_chunk_t *chunk)
  184. {
  185. chunk->next = pool->used_chunks;
  186. if (chunk->next)
  187. chunk->next->prev = chunk;
  188. pool->used_chunks = chunk;
  189. ASSERT(!chunk->prev);
  190. }
  191. /** Return a newly allocated item from <b>pool</b>. */
  192. void *
  193. mp_pool_get(mp_pool_t *pool)
  194. {
  195. mp_chunk_t *chunk;
  196. mp_allocated_t *allocated;
  197. if (PREDICT_LIKELY(pool->used_chunks != NULL)) {
  198. /* Common case: there is some chunk that is neither full nor empty. Use
  199. * that one. (We can't use the full ones, obviously, and we should fill
  200. * up the used ones before we start on any empty ones. */
  201. chunk = pool->used_chunks;
  202. } else if (pool->empty_chunks) {
  203. /* We have no used chunks, but we have an empty chunk that we haven't
  204. * freed yet: use that. (We pull from the front of the list, which should
  205. * get us the most recently emptied chunk.) */
  206. chunk = pool->empty_chunks;
  207. /* Remove the chunk from the empty list. */
  208. pool->empty_chunks = chunk->next;
  209. if (chunk->next)
  210. chunk->next->prev = NULL;
  211. /* Put the chunk on the 'used' list*/
  212. add_newly_used_chunk_to_used_list(pool, chunk);
  213. ASSERT(!chunk->prev);
  214. --pool->n_empty_chunks;
  215. if (pool->n_empty_chunks < pool->min_empty_chunks)
  216. pool->min_empty_chunks = pool->n_empty_chunks;
  217. } else {
  218. /* We have no used or empty chunks: allocate a new chunk. */
  219. chunk = mp_chunk_new(pool);
  220. CHECK_ALLOC(chunk);
  221. /* Add the new chunk to the used list. */
  222. add_newly_used_chunk_to_used_list(pool, chunk);
  223. }
  224. ASSERT(chunk->n_allocated < chunk->capacity);
  225. if (chunk->first_free) {
  226. /* If there's anything on the chunk's freelist, unlink it and use it. */
  227. allocated = chunk->first_free;
  228. chunk->first_free = allocated->u.next_free;
  229. allocated->u.next_free = NULL; /* For debugging; not really needed. */
  230. ASSERT(allocated->in_chunk == chunk);
  231. } else {
  232. /* Otherwise, the chunk had better have some free space left on it. */
  233. ASSERT(chunk->next_mem + pool->item_alloc_size <=
  234. chunk->mem + chunk->mem_size);
  235. /* Good, it did. Let's carve off a bit of that free space, and use
  236. * that. */
  237. allocated = (void*)chunk->next_mem;
  238. chunk->next_mem += pool->item_alloc_size;
  239. allocated->in_chunk = chunk;
  240. allocated->u.next_free = NULL; /* For debugging; not really needed. */
  241. }
  242. ++chunk->n_allocated;
  243. #ifdef MEMPOOL_STATS
  244. ++pool->total_items_allocated;
  245. #endif
  246. if (PREDICT_UNLIKELY(chunk->n_allocated == chunk->capacity)) {
  247. /* This chunk just became full. */
  248. ASSERT(chunk == pool->used_chunks);
  249. ASSERT(chunk->prev == NULL);
  250. /* Take it off the used list. */
  251. pool->used_chunks = chunk->next;
  252. if (chunk->next)
  253. chunk->next->prev = NULL;
  254. /* Put it on the full list. */
  255. chunk->next = pool->full_chunks;
  256. if (chunk->next)
  257. chunk->next->prev = chunk;
  258. pool->full_chunks = chunk;
  259. }
  260. /* And return the memory portion of the mp_allocated_t. */
  261. return A2M(allocated);
  262. }
  263. /** Return an allocated memory item to its memory pool. */
  264. void
  265. mp_pool_release(void *item)
  266. {
  267. mp_allocated_t *allocated = (void*) M2A(item);
  268. mp_chunk_t *chunk = allocated->in_chunk;
  269. ASSERT(chunk);
  270. ASSERT(chunk->magic == MP_CHUNK_MAGIC);
  271. ASSERT(chunk->n_allocated > 0);
  272. allocated->u.next_free = chunk->first_free;
  273. chunk->first_free = allocated;
  274. if (PREDICT_UNLIKELY(chunk->n_allocated == chunk->capacity)) {
  275. /* This chunk was full and is about to be used. */
  276. mp_pool_t *pool = chunk->pool;
  277. /* unlink from the full list */
  278. if (chunk->prev)
  279. chunk->prev->next = chunk->next;
  280. if (chunk->next)
  281. chunk->next->prev = chunk->prev;
  282. if (chunk == pool->full_chunks)
  283. pool->full_chunks = chunk->next;
  284. /* link to the used list. */
  285. chunk->next = pool->used_chunks;
  286. chunk->prev = NULL;
  287. if (chunk->next)
  288. chunk->next->prev = chunk;
  289. pool->used_chunks = chunk;
  290. } else if (PREDICT_UNLIKELY(chunk->n_allocated == 1)) {
  291. /* This was used and is about to be empty. */
  292. mp_pool_t *pool = chunk->pool;
  293. /* Unlink from the used list */
  294. if (chunk->prev)
  295. chunk->prev->next = chunk->next;
  296. if (chunk->next)
  297. chunk->next->prev = chunk->prev;
  298. if (chunk == pool->used_chunks)
  299. pool->used_chunks = chunk->next;
  300. /* Link to the empty list */
  301. chunk->next = pool->empty_chunks;
  302. chunk->prev = NULL;
  303. if (chunk->next)
  304. chunk->next->prev = chunk;
  305. pool->empty_chunks = chunk;
  306. /* Reset the guts of this chunk to defragment it, in case it gets
  307. * used again. */
  308. chunk->first_free = NULL;
  309. chunk->next_mem = chunk->mem;
  310. ++pool->n_empty_chunks;
  311. }
  312. --chunk->n_allocated;
  313. }
  314. /** Allocate a new memory pool to hold items of size <b>item_size</b>. We'll
  315. * try to fit about <b>chunk_capacity</b> bytes in each chunk. */
  316. mp_pool_t *
  317. mp_pool_new(size_t item_size, size_t chunk_capacity)
  318. {
  319. mp_pool_t *pool;
  320. size_t alloc_size, new_chunk_cap;
  321. tor_assert(item_size < SIZE_T_CEILING);
  322. tor_assert(chunk_capacity < SIZE_T_CEILING);
  323. tor_assert(SIZE_T_CEILING / item_size > chunk_capacity);
  324. pool = ALLOC(sizeof(mp_pool_t));
  325. CHECK_ALLOC(pool);
  326. memset(pool, 0, sizeof(mp_pool_t));
  327. /* First, we figure out how much space to allow per item. We'll want to
  328. * use make sure we have enough for the overhead plus the item size. */
  329. alloc_size = (size_t)(STRUCT_OFFSET(mp_allocated_t, u.mem) + item_size);
  330. /* If the item_size is less than sizeof(next_free), we need to make
  331. * the allocation bigger. */
  332. if (alloc_size < sizeof(mp_allocated_t))
  333. alloc_size = sizeof(mp_allocated_t);
  334. /* If we're not an even multiple of ALIGNMENT, round up. */
  335. if (alloc_size % ALIGNMENT) {
  336. alloc_size = alloc_size + ALIGNMENT - (alloc_size % ALIGNMENT);
  337. }
  338. if (alloc_size < ALIGNMENT)
  339. alloc_size = ALIGNMENT;
  340. ASSERT((alloc_size % ALIGNMENT) == 0);
  341. /* Now we figure out how many items fit in each chunk. We need to fit at
  342. * least 2 items per chunk. No chunk can be more than MAX_CHUNK bytes long,
  343. * or less than MIN_CHUNK. */
  344. if (chunk_capacity > MAX_CHUNK)
  345. chunk_capacity = MAX_CHUNK;
  346. /* Try to be around a power of 2 in size, since that's what allocators like
  347. * handing out. 512K-1 byte is a lot better than 512K+1 byte. */
  348. chunk_capacity = (size_t) round_to_power_of_2(chunk_capacity);
  349. while (chunk_capacity < alloc_size * 2 + CHUNK_OVERHEAD)
  350. chunk_capacity *= 2;
  351. if (chunk_capacity < MIN_CHUNK)
  352. chunk_capacity = MIN_CHUNK;
  353. new_chunk_cap = (chunk_capacity-CHUNK_OVERHEAD) / alloc_size;
  354. tor_assert(new_chunk_cap < INT_MAX);
  355. pool->new_chunk_capacity = (int)new_chunk_cap;
  356. pool->item_alloc_size = alloc_size;
  357. log_debug(LD_MM, "Capacity is %lu, item size is %lu, alloc size is %lu",
  358. (unsigned long)pool->new_chunk_capacity,
  359. (unsigned long)pool->item_alloc_size,
  360. (unsigned long)(pool->new_chunk_capacity*pool->item_alloc_size));
  361. return pool;
  362. }
  363. /** Helper function for qsort: used to sort pointers to mp_chunk_t into
  364. * descending order of fullness. */
  365. static int
  366. mp_pool_sort_used_chunks_helper(const void *_a, const void *_b)
  367. {
  368. mp_chunk_t *a = *(mp_chunk_t**)_a;
  369. mp_chunk_t *b = *(mp_chunk_t**)_b;
  370. return b->n_allocated - a->n_allocated;
  371. }
  372. /** Sort the used chunks in <b>pool</b> into descending order of fullness,
  373. * so that we preferentially fill up mostly full chunks before we make
  374. * nearly empty chunks less nearly empty. */
  375. static void
  376. mp_pool_sort_used_chunks(mp_pool_t *pool)
  377. {
  378. int i, n=0, inverted=0;
  379. mp_chunk_t **chunks, *chunk;
  380. for (chunk = pool->used_chunks; chunk; chunk = chunk->next) {
  381. ++n;
  382. if (chunk->next && chunk->next->n_allocated > chunk->n_allocated)
  383. ++inverted;
  384. }
  385. if (!inverted)
  386. return;
  387. //printf("Sort %d/%d\n",inverted,n);
  388. chunks = ALLOC(sizeof(mp_chunk_t *)*n);
  389. #ifdef ALLOC_CAN_RETURN_NULL
  390. if (PREDICT_UNLIKELY(!chunks)) return;
  391. #endif
  392. for (i=0,chunk = pool->used_chunks; chunk; chunk = chunk->next)
  393. chunks[i++] = chunk;
  394. qsort(chunks, n, sizeof(mp_chunk_t *), mp_pool_sort_used_chunks_helper);
  395. pool->used_chunks = chunks[0];
  396. chunks[0]->prev = NULL;
  397. for (i=1;i<n;++i) {
  398. chunks[i-1]->next = chunks[i];
  399. chunks[i]->prev = chunks[i-1];
  400. }
  401. chunks[n-1]->next = NULL;
  402. FREE(chunks);
  403. mp_pool_assert_ok(pool);
  404. }
  405. /** If there are more than <b>n</b> empty chunks in <b>pool</b>, free the
  406. * excess ones that have been empty for the longest. If
  407. * <b>keep_recently_used</b> is true, do not free chunks unless they have been
  408. * empty since the last call to this function.
  409. **/
  410. void
  411. mp_pool_clean(mp_pool_t *pool, int n_to_keep, int keep_recently_used)
  412. {
  413. mp_chunk_t *chunk, **first_to_free;
  414. mp_pool_sort_used_chunks(pool);
  415. ASSERT(n_to_keep >= 0);
  416. if (keep_recently_used) {
  417. int n_recently_used = pool->n_empty_chunks - pool->min_empty_chunks;
  418. if (n_to_keep < n_recently_used)
  419. n_to_keep = n_recently_used;
  420. }
  421. ASSERT(n_to_keep >= 0);
  422. first_to_free = &pool->empty_chunks;
  423. while (*first_to_free && n_to_keep > 0) {
  424. first_to_free = &(*first_to_free)->next;
  425. --n_to_keep;
  426. }
  427. if (!*first_to_free) {
  428. pool->min_empty_chunks = pool->n_empty_chunks;
  429. return;
  430. }
  431. chunk = *first_to_free;
  432. while (chunk) {
  433. mp_chunk_t *next = chunk->next;
  434. chunk->magic = 0xdeadbeef;
  435. FREE(chunk);
  436. #ifdef MEMPOOL_STATS
  437. ++pool->total_chunks_freed;
  438. #endif
  439. --pool->n_empty_chunks;
  440. chunk = next;
  441. }
  442. pool->min_empty_chunks = pool->n_empty_chunks;
  443. *first_to_free = NULL;
  444. }
  445. /** Helper: Given a list of chunks, free all the chunks in the list. */
  446. static void
  447. destroy_chunks(mp_chunk_t *chunk)
  448. {
  449. mp_chunk_t *next;
  450. while (chunk) {
  451. chunk->magic = 0xd3adb33f;
  452. next = chunk->next;
  453. FREE(chunk);
  454. chunk = next;
  455. }
  456. }
  457. /** Free all space held in <b>pool</b> This makes all pointers returned from
  458. * mp_pool_get(<b>pool</b>) invalid. */
  459. void
  460. mp_pool_destroy(mp_pool_t *pool)
  461. {
  462. destroy_chunks(pool->empty_chunks);
  463. destroy_chunks(pool->used_chunks);
  464. destroy_chunks(pool->full_chunks);
  465. memset(pool, 0xe0, sizeof(mp_pool_t));
  466. FREE(pool);
  467. }
  468. /** Helper: make sure that a given chunk list is not corrupt. */
  469. static int
  470. assert_chunks_ok(mp_pool_t *pool, mp_chunk_t *chunk, int empty, int full)
  471. {
  472. mp_allocated_t *allocated;
  473. int n = 0;
  474. if (chunk)
  475. ASSERT(chunk->prev == NULL);
  476. while (chunk) {
  477. n++;
  478. ASSERT(chunk->magic == MP_CHUNK_MAGIC);
  479. ASSERT(chunk->pool == pool);
  480. for (allocated = chunk->first_free; allocated;
  481. allocated = allocated->u.next_free) {
  482. ASSERT(allocated->in_chunk == chunk);
  483. }
  484. if (empty)
  485. ASSERT(chunk->n_allocated == 0);
  486. else if (full)
  487. ASSERT(chunk->n_allocated == chunk->capacity);
  488. else
  489. ASSERT(chunk->n_allocated > 0 && chunk->n_allocated < chunk->capacity);
  490. ASSERT(chunk->capacity == pool->new_chunk_capacity);
  491. ASSERT(chunk->mem_size ==
  492. pool->new_chunk_capacity * pool->item_alloc_size);
  493. ASSERT(chunk->next_mem >= chunk->mem &&
  494. chunk->next_mem <= chunk->mem + chunk->mem_size);
  495. if (chunk->next)
  496. ASSERT(chunk->next->prev == chunk);
  497. chunk = chunk->next;
  498. }
  499. return n;
  500. }
  501. /** Fail with an assertion if <b>pool</b> is not internally consistent. */
  502. void
  503. mp_pool_assert_ok(mp_pool_t *pool)
  504. {
  505. int n_empty;
  506. n_empty = assert_chunks_ok(pool, pool->empty_chunks, 1, 0);
  507. assert_chunks_ok(pool, pool->full_chunks, 0, 1);
  508. assert_chunks_ok(pool, pool->used_chunks, 0, 0);
  509. ASSERT(pool->n_empty_chunks == n_empty);
  510. }
  511. #ifdef TOR
  512. /** Dump information about <b>pool</b>'s memory usage to the Tor log at level
  513. * <b>severity</b>. */
  514. /*FFFF uses Tor logging functions. */
  515. void
  516. mp_pool_log_status(mp_pool_t *pool, int severity)
  517. {
  518. uint64_t bytes_used = 0;
  519. uint64_t bytes_allocated = 0;
  520. uint64_t bu = 0, ba = 0;
  521. mp_chunk_t *chunk;
  522. int n_full = 0, n_used = 0;
  523. ASSERT(pool);
  524. for (chunk = pool->empty_chunks; chunk; chunk = chunk->next) {
  525. bytes_allocated += chunk->mem_size;
  526. }
  527. log_fn(severity, LD_MM, U64_FORMAT" bytes in %d empty chunks",
  528. U64_PRINTF_ARG(bytes_allocated), pool->n_empty_chunks);
  529. for (chunk = pool->used_chunks; chunk; chunk = chunk->next) {
  530. ++n_used;
  531. bu += chunk->n_allocated * pool->item_alloc_size;
  532. ba += chunk->mem_size;
  533. log_fn(severity, LD_MM, " used chunk: %d items allocated",
  534. chunk->n_allocated);
  535. }
  536. log_fn(severity, LD_MM, U64_FORMAT"/"U64_FORMAT
  537. " bytes in %d partially full chunks",
  538. U64_PRINTF_ARG(bu), U64_PRINTF_ARG(ba), n_used);
  539. bytes_used += bu;
  540. bytes_allocated += ba;
  541. bu = ba = 0;
  542. for (chunk = pool->full_chunks; chunk; chunk = chunk->next) {
  543. ++n_full;
  544. bu += chunk->n_allocated * pool->item_alloc_size;
  545. ba += chunk->mem_size;
  546. }
  547. log_fn(severity, LD_MM, U64_FORMAT"/"U64_FORMAT
  548. " bytes in %d full chunks",
  549. U64_PRINTF_ARG(bu), U64_PRINTF_ARG(ba), n_full);
  550. bytes_used += bu;
  551. bytes_allocated += ba;
  552. log_fn(severity, LD_MM, "Total: "U64_FORMAT"/"U64_FORMAT" bytes allocated "
  553. "for cell pools are full.",
  554. U64_PRINTF_ARG(bytes_used), U64_PRINTF_ARG(bytes_allocated));
  555. #ifdef MEMPOOL_STATS
  556. log_fn(severity, LD_MM, U64_FORMAT" cell allocations ever; "
  557. U64_FORMAT" chunk allocations ever; "
  558. U64_FORMAT" chunk frees ever.",
  559. U64_PRINTF_ARG(pool->total_items_allocated),
  560. U64_PRINTF_ARG(pool->total_chunks_allocated),
  561. U64_PRINTF_ARG(pool->total_chunks_freed));
  562. #endif
  563. }
  564. #endif