mempool.c 20 KB

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