mempool.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /* Copyright (c) 2007-2008, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #if 1
  5. /* Tor dependencies */
  6. #include "orconfig.h"
  7. #endif
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include "torint.h"
  11. #define MEMPOOL_PRIVATE
  12. #include "mempool.h"
  13. //#define LAZY_CHUNK_SORT
  14. /* OVERVIEW:
  15. *
  16. * This is an implementation of memory pools for Tor cells. It may be
  17. * useful for you too.
  18. *
  19. * Generally, a memory pool is an allocation strategy optimized for large
  20. * numbers of identically-sized objects. Rather than the elaborate arena
  21. * and coalescing strategies you need to get good performance for a
  22. * general-purpose malloc(), pools use a series of large memory "chunks",
  23. * each of which is carved into a bunch of smaller "items" or
  24. * "allocations".
  25. *
  26. * To get decent performance, you need to:
  27. * - Minimize the number of times you hit the underlying allocator.
  28. * - Try to keep accesses as local in memory as possible.
  29. * - Try to keep the common case fast.
  30. *
  31. * Our implementation uses three lists of chunks per pool. Each chunk can
  32. * be either "full" (no more room for items); "empty" (no items); or
  33. * "used" (not full, not empty). There are independent doubly-linked
  34. * lists for each state.
  35. *
  36. * CREDIT:
  37. *
  38. * I wrote this after looking at 3 or 4 other pooling allocators, but
  39. * without copying. The strategy this most resembles (which is funny,
  40. * since that's the one I looked at longest ago) is the pool allocator
  41. * underlying Python's obmalloc code. Major differences from obmalloc's
  42. * pools are:
  43. * - We don't even try to be threadsafe.
  44. * - We only handle objects of one size.
  45. * - Our list of empty chunks is doubly-linked, not singly-linked.
  46. * (This could change pretty easily; it's only doubly-linked for
  47. * consistency.)
  48. * - We keep a list of full chunks (so we can have a "nuke everything"
  49. * function). Obmalloc's pools leave full chunks to float unanchored.
  50. *
  51. * LIMITATIONS:
  52. * - Not even slightly threadsafe.
  53. * - Likes to have lots of items per chunks.
  54. * - One pointer overhead per allocated thing. (The alternative is
  55. * something like glib's use of an RB-tree to keep track of what
  56. * chunk any given piece of memory is in.)
  57. * - Only aligns allocated things to void* level: redefign ALIGNMENT_TYPE
  58. * if you need doubles.
  59. * - Could probably be optimized a bit; the representation contains
  60. * a bit more info than it really needs to have.
  61. */
  62. #if 1
  63. /* Tor dependencies */
  64. #include "orconfig.h"
  65. #include "util.h"
  66. #include "compat.h"
  67. #include "log.h"
  68. #define ALLOC(x) tor_malloc(x)
  69. #define FREE(x) tor_free(x)
  70. #define ASSERT(x) tor_assert(x)
  71. #undef ALLOC_CAN_RETURN_NULL
  72. #define TOR
  73. //#define ALLOC_ROUNDUP(p) tor_malloc_roundup(p)
  74. /* End Tor dependencies */
  75. #else
  76. /* If you're not building this as part of Tor, you'll want to define the
  77. * following macros. For now, these should do as defaults.
  78. */
  79. #include <assert.h>
  80. #define PREDICT_UNLIKELY(x) (x)
  81. #define PREDICT_LIKELY(x) (x)
  82. #define ALLOC(x) malloc(x)
  83. #define FREE(x) free(x)
  84. #define STRUCT_OFFSET(tp, member) \
  85. ((off_t) (((char*)&((tp*)0)->member)-(char*)0))
  86. #define ASSERT(x) assert(x)
  87. #define ALLOC_CAN_RETURN_NULL
  88. #endif
  89. /* Tuning parameters */
  90. /** Largest type that we need to ensure returned memory items are aligned to.
  91. * Change this to "double" if we need to be safe for structs with doubles. */
  92. #define ALIGNMENT_TYPE void *
  93. /** Increment that we need to align allocated. */
  94. #define ALIGNMENT sizeof(ALIGNMENT_TYPE)
  95. /** Largest memory chunk that we should allocate. */
  96. #define MAX_CHUNK (8*(1L<<20))
  97. /** Smallest memory chunk size that we should allocate. */
  98. #define MIN_CHUNK 4096
  99. typedef struct mp_allocated_t mp_allocated_t;
  100. typedef struct mp_chunk_t mp_chunk_t;
  101. /** Holds a single allocated item, allocated as part of a chunk. */
  102. struct mp_allocated_t {
  103. /** The chunk that this item is allocated in. This adds overhead to each
  104. * allocated item, thus making this implementation inappropriate for
  105. * very small items. */
  106. mp_chunk_t *in_chunk;
  107. union {
  108. /** If this item is free, the next item on the free list. */
  109. mp_allocated_t *next_free;
  110. /** If this item is not free, the actual memory contents of this item.
  111. * (Not actual size.) */
  112. char mem[1];
  113. /** An extra element to the union to insure correct alignment. */
  114. ALIGNMENT_TYPE _dummy;
  115. } u;
  116. };
  117. /** 'Magic' value used to detect memory corruption. */
  118. #define MP_CHUNK_MAGIC 0x09870123
  119. /** A chunk of memory. Chunks come from malloc; we use them */
  120. struct mp_chunk_t {
  121. unsigned long magic; /**< Must be MP_CHUNK_MAGIC if this chunk is valid. */
  122. mp_chunk_t *next; /**< The next free, used, or full chunk in sequence. */
  123. mp_chunk_t *prev; /**< The previous free, used, or full chunk in sequence. */
  124. mp_pool_t *pool; /**< The pool that this chunk is part of. */
  125. /** First free item in the freelist for this chunk. Note that this may be
  126. * NULL even if this chunk is not at capacity: if so, the free memory at
  127. * next_mem has not yet been carved into items.
  128. */
  129. mp_allocated_t *first_free;
  130. int n_allocated; /**< Number of currently allocated items in this chunk. */
  131. int capacity; /**< Number of items that can be fit into this chunk. */
  132. size_t mem_size; /**< Number of usable bytes in mem. */
  133. char *next_mem; /**< Pointer into part of <b>mem</b> not yet carved up. */
  134. char mem[1]; /**< Storage for this chunk. (Not actual size.) */
  135. };
  136. /** Number of extra bytes needed beyond mem_size to allocate a chunk. */
  137. #define CHUNK_OVERHEAD (sizeof(mp_chunk_t)-1)
  138. /** Given a pointer to a mp_allocated_t, return a pointer to the memory
  139. * item it holds. */
  140. #define A2M(a) (&(a)->u.mem)
  141. /** Given a pointer to a memory_item_t, return a pointer to its enclosing
  142. * mp_allocated_t. */
  143. #define M2A(p) ( ((char*)p) - STRUCT_OFFSET(mp_allocated_t, u.mem) )
  144. #ifdef ALLOC_CAN_RETURN_NULL
  145. /** If our ALLOC() macro can return NULL, check whether <b>x</b> is NULL,
  146. * and if so, return NULL. */
  147. #define CHECK_ALLOC(x) \
  148. if (PREDICT_UNLIKELY(!x)) { return NULL; }
  149. #else
  150. /** If our ALLOC() macro can't return NULL, do nothing. */
  151. #define CHECK_ALLOC(x)
  152. #endif
  153. /** Helper: Allocate and return a new memory chunk for <b>pool</b>. Does not
  154. * link the chunk into any list. */
  155. static mp_chunk_t *
  156. mp_chunk_new(mp_pool_t *pool)
  157. {
  158. size_t sz = pool->new_chunk_capacity * pool->item_alloc_size;
  159. #ifdef ALLOC_ROUNDUP
  160. size_t alloc_size = CHUNK_OVERHEAD + sz;
  161. mp_chunk_t *chunk = ALLOC_ROUNDUP(&alloc_size);
  162. #else
  163. mp_chunk_t *chunk = ALLOC(CHUNK_OVERHEAD + sz);
  164. #endif
  165. #ifdef MEMPOOL_STATS
  166. ++pool->total_chunks_allocated;
  167. #endif
  168. CHECK_ALLOC(chunk);
  169. memset(chunk, 0, sizeof(mp_chunk_t)); /* Doesn't clear the whole thing. */
  170. chunk->magic = MP_CHUNK_MAGIC;
  171. #ifdef ALLOC_ROUNDUP
  172. chunk->mem_size = alloc_size - CHUNK_OVERHEAD;
  173. chunk->capacity = chunk->mem_size / pool->item_alloc_size;
  174. #else
  175. chunk->capacity = pool->new_chunk_capacity;
  176. chunk->mem_size = sz;
  177. #endif
  178. chunk->next_mem = chunk->mem;
  179. chunk->pool = pool;
  180. return chunk;
  181. }
  182. /** DOCDOC */
  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;
  322. pool = ALLOC(sizeof(mp_pool_t));
  323. CHECK_ALLOC(pool);
  324. memset(pool, 0, sizeof(mp_pool_t));
  325. /* First, we figure out how much space to allow per item. We'll want to
  326. * use make sure we have enough for the overhead plus the item size. */
  327. alloc_size = (size_t)(STRUCT_OFFSET(mp_allocated_t, u.mem) + item_size);
  328. /* If the item_size is less than sizeof(next_free), we need to make
  329. * the allocation bigger. */
  330. if (alloc_size < sizeof(mp_allocated_t))
  331. alloc_size = sizeof(mp_allocated_t);
  332. /* If we're not an even multiple of ALIGNMENT, round up. */
  333. if (alloc_size % ALIGNMENT) {
  334. alloc_size = alloc_size + ALIGNMENT - (alloc_size % ALIGNMENT);
  335. }
  336. if (alloc_size < ALIGNMENT)
  337. alloc_size = ALIGNMENT;
  338. ASSERT((alloc_size % ALIGNMENT) == 0);
  339. /* Now we figure out how many items fit in each chunk. We need to fit at
  340. * least 2 items per chunk. No chunk can be more than MAX_CHUNK bytes long,
  341. * or less than MIN_CHUNK. */
  342. if (chunk_capacity > MAX_CHUNK)
  343. chunk_capacity = MAX_CHUNK;
  344. /* Try to be around a power of 2 in size, since that's what allocators like
  345. * handing out. 512K-1 byte is a lot better than 512K+1 byte. */
  346. chunk_capacity = (size_t) round_to_power_of_2(chunk_capacity);
  347. while (chunk_capacity < alloc_size * 2 + CHUNK_OVERHEAD)
  348. chunk_capacity *= 2;
  349. if (chunk_capacity < MIN_CHUNK)
  350. chunk_capacity = MIN_CHUNK;
  351. pool->new_chunk_capacity = (chunk_capacity-CHUNK_OVERHEAD) / alloc_size;
  352. pool->item_alloc_size = alloc_size;
  353. log_debug(LD_MM, "Capacity is %lu, item size is %lu, alloc size is %lu",
  354. (unsigned long)pool->new_chunk_capacity,
  355. (unsigned long)pool->item_alloc_size,
  356. (unsigned long)(pool->new_chunk_capacity*pool->item_alloc_size));
  357. return pool;
  358. }
  359. #ifdef LAZY_CHUNK_SORT
  360. /** DOCDOC */
  361. static int
  362. mp_pool_sort_used_chunks_helper(const void *_a, const void *_b)
  363. {
  364. mp_chunk_t *a = *(mp_chunk_t**)_a;
  365. mp_chunk_t *b = *(mp_chunk_t**)_b;
  366. return b->n_allocated - a->n_allocated;
  367. }
  368. /** DOCDOC */
  369. static void
  370. mp_pool_sort_used_chunks(mp_pool_t *pool)
  371. {
  372. int i, n=0, inverted=0;
  373. mp_chunk_t **chunks, *chunk;
  374. for (chunk = pool->used_chunks; chunk; chunk = chunk->next) {
  375. ++n;
  376. if (chunk->next && chunk->next->n_allocated > chunk->n_allocated)
  377. ++inverted;
  378. }
  379. if (!inverted)
  380. return;
  381. ASSERT(n);
  382. //printf("Sort %d/%d\n",inverted,n);
  383. chunks = ALLOC(sizeof(mp_chunk_t *)*n);
  384. #ifdef ALLOC_CAN_RETURN_NULL
  385. if (PREDICT_UNLIKELY(!chunks)) return;
  386. #endif
  387. for (i=0,chunk = pool->used_chunks; chunk; chunk = chunk->next)
  388. chunks[i++] = chunk;
  389. qsort(chunks, n, sizeof(mp_chunk_t *), mp_pool_sort_used_chunks_helper);
  390. pool->used_chunks = chunks[0];
  391. chunks[0]->prev = NULL;
  392. for (i=1;i<n;++i) {
  393. chunks[i-1]->next = chunks[i];
  394. chunks[i]->prev = chunks[i-1];
  395. }
  396. chunks[n-1]->next = NULL;
  397. FREE(chunks);
  398. #if 0
  399. inverted = 0;
  400. for (chunk = pool->used_chunks; chunk; chunk = chunk->next) {
  401. if (chunk->next) {
  402. ASSERT(chunk->next->n_allocated <= chunk->n_allocated);
  403. }
  404. }
  405. #endif
  406. mp_pool_assert_ok(pool);
  407. }
  408. #endif
  409. /** If there are more than <b>n</b> empty chunks in <b>pool</b>, free the
  410. * excess ones that have been empty for the longest. (If <b>n</b> is less
  411. * than zero, free only empty chunks that were not used since the last
  412. * call to mp_pool_clean(), leaving only -<b>n</b>.)
  413. * DOCDOC Keep_recently_used, n_to_keep
  414. * XXXX020 maybe dump negative n_to_keep behavior, if k_r_u turns out to be
  415. * smarter.
  416. **/
  417. void
  418. mp_pool_clean(mp_pool_t *pool, int n_to_keep, int keep_recently_used)
  419. {
  420. mp_chunk_t *chunk, **first_to_free;
  421. #ifdef LAZY_CHUNK_SORT
  422. mp_pool_sort_used_chunks(pool);
  423. #endif
  424. if (n_to_keep < 0) {
  425. /* As said in the documentation, "negative n" means "leave an additional
  426. * -n chunks". So replace n with a positive number. */
  427. n_to_keep = pool->min_empty_chunks + (-n_to_keep);
  428. }
  429. if (keep_recently_used) {
  430. int n_recently_used = pool->n_empty_chunks - pool->min_empty_chunks;
  431. if (n_to_keep < n_recently_used)
  432. n_to_keep = n_recently_used;
  433. }
  434. ASSERT(n_to_keep >= 0);
  435. first_to_free = &pool->empty_chunks;
  436. while (*first_to_free && n_to_keep > 0) {
  437. first_to_free = &(*first_to_free)->next;
  438. --n_to_keep;
  439. }
  440. if (!*first_to_free) {
  441. pool->min_empty_chunks = pool->n_empty_chunks;
  442. return;
  443. }
  444. chunk = *first_to_free;
  445. while (chunk) {
  446. mp_chunk_t *next = chunk->next;
  447. chunk->magic = 0xdeadbeef;
  448. FREE(chunk);
  449. #ifdef MEMPOOL_STATS
  450. ++pool->total_chunks_freed;
  451. #endif
  452. --pool->n_empty_chunks;
  453. chunk = next;
  454. }
  455. pool->min_empty_chunks = pool->n_empty_chunks;
  456. *first_to_free = NULL;
  457. }
  458. /** Helper: Given a list of chunks, free all the chunks in the list. */
  459. static void
  460. destroy_chunks(mp_chunk_t *chunk)
  461. {
  462. mp_chunk_t *next;
  463. while (chunk) {
  464. chunk->magic = 0xd3adb33f;
  465. next = chunk->next;
  466. FREE(chunk);
  467. chunk = next;
  468. }
  469. }
  470. /** Free all space held in <b>pool</b> This makes all pointers returned from
  471. * mp_pool_get(<b>pool</b>) invalid. */
  472. void
  473. mp_pool_destroy(mp_pool_t *pool)
  474. {
  475. destroy_chunks(pool->empty_chunks);
  476. destroy_chunks(pool->used_chunks);
  477. destroy_chunks(pool->full_chunks);
  478. memset(pool, 0xe0, sizeof(mp_pool_t));
  479. FREE(pool);
  480. }
  481. /** Helper: make sure that a given chunk list is not corrupt. */
  482. static int
  483. assert_chunks_ok(mp_pool_t *pool, mp_chunk_t *chunk, int empty, int full)
  484. {
  485. mp_allocated_t *allocated;
  486. int n = 0;
  487. if (chunk)
  488. ASSERT(chunk->prev == NULL);
  489. while (chunk) {
  490. n++;
  491. ASSERT(chunk->magic == MP_CHUNK_MAGIC);
  492. ASSERT(chunk->pool == pool);
  493. for (allocated = chunk->first_free; allocated;
  494. allocated = allocated->u.next_free) {
  495. ASSERT(allocated->in_chunk == chunk);
  496. }
  497. if (empty)
  498. ASSERT(chunk->n_allocated == 0);
  499. else if (full)
  500. ASSERT(chunk->n_allocated == chunk->capacity);
  501. else
  502. ASSERT(chunk->n_allocated > 0 && chunk->n_allocated < chunk->capacity);
  503. ASSERT(chunk->capacity == pool->new_chunk_capacity);
  504. ASSERT(chunk->mem_size ==
  505. pool->new_chunk_capacity * pool->item_alloc_size);
  506. ASSERT(chunk->next_mem >= chunk->mem &&
  507. chunk->next_mem <= chunk->mem + chunk->mem_size);
  508. if (chunk->next)
  509. ASSERT(chunk->next->prev == chunk);
  510. chunk = chunk->next;
  511. }
  512. return n;
  513. }
  514. /** Fail with an assertion if <b>pool</b> is not internally consistent. */
  515. void
  516. mp_pool_assert_ok(mp_pool_t *pool)
  517. {
  518. int n_empty;
  519. n_empty = assert_chunks_ok(pool, pool->empty_chunks, 1, 0);
  520. assert_chunks_ok(pool, pool->full_chunks, 0, 1);
  521. assert_chunks_ok(pool, pool->used_chunks, 0, 0);
  522. ASSERT(pool->n_empty_chunks == n_empty);
  523. }
  524. #ifdef TOR
  525. /** Dump information about <b>pool</b>'s memory usage to the Tor log at level
  526. * <b>severity</b>. */
  527. /*FFFF uses Tor logging functions. */
  528. void
  529. mp_pool_log_status(mp_pool_t *pool, int severity)
  530. {
  531. uint64_t bytes_used = 0;
  532. uint64_t bytes_allocated = 0;
  533. uint64_t bu = 0, ba = 0;
  534. mp_chunk_t *chunk;
  535. int n_full = 0, n_used = 0;
  536. ASSERT(pool);
  537. for (chunk = pool->empty_chunks; chunk; chunk = chunk->next) {
  538. bytes_allocated += chunk->mem_size;
  539. }
  540. log_fn(severity, LD_MM, U64_FORMAT" bytes in %d empty chunks",
  541. U64_PRINTF_ARG(bytes_used), pool->n_empty_chunks);
  542. for (chunk = pool->used_chunks; chunk; chunk = chunk->next) {
  543. ++n_used;
  544. bu += chunk->n_allocated * pool->item_alloc_size;
  545. ba += chunk->mem_size;
  546. log_fn(severity, LD_MM, " used chunk: %d items allocated",
  547. chunk->n_allocated);
  548. }
  549. log_fn(severity, LD_MM, U64_FORMAT"/"U64_FORMAT
  550. " bytes in %d partially full chunks",
  551. U64_PRINTF_ARG(bu), U64_PRINTF_ARG(ba), n_used);
  552. bytes_used += bu;
  553. bytes_allocated += ba;
  554. bu = ba = 0;
  555. for (chunk = pool->full_chunks; chunk; chunk = chunk->next) {
  556. ++n_full;
  557. bu += chunk->n_allocated * pool->item_alloc_size;
  558. ba += chunk->mem_size;
  559. }
  560. log_fn(severity, LD_MM, U64_FORMAT"/"U64_FORMAT
  561. " bytes in %d full chunks",
  562. U64_PRINTF_ARG(bu), U64_PRINTF_ARG(ba), n_full);
  563. bytes_used += bu;
  564. bytes_allocated += ba;
  565. log_fn(severity, LD_MM, "Total: "U64_FORMAT"/"U64_FORMAT" bytes allocated "
  566. "for cell pools are full.",
  567. U64_PRINTF_ARG(bytes_used), U64_PRINTF_ARG(bytes_allocated));
  568. #ifdef MEMPOOL_STATS
  569. log_fn(severity, LD_MM, U64_FORMAT" cell allocations ever; "
  570. U64_FORMAT" chunk allocations ever; "
  571. U64_FORMAT" chunk frees ever.",
  572. U64_PRINTF_ARG(pool->total_items_allocated),
  573. U64_PRINTF_ARG(pool->total_chunks_allocated),
  574. U64_PRINTF_ARG(pool->total_chunks_freed));
  575. #endif
  576. }
  577. #endif