mempool.c 20 KB

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