mempool.c 20 KB

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