mempool.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /* Copyright 2007 Nick Mathewson */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #define MEMPOOL_PRIVATE
  7. #include "mempool.h"
  8. /* OVERVIEW:
  9. *
  10. * This is an implementation of memory pools for Tor cells. It may be
  11. * useful for you too.
  12. *
  13. * Generally, a memory pool is an allocation strategy optimized for large
  14. * numbers of identically-sized objects. Rather than the elaborate arena
  15. * and coalescing strategeis you need to get good performance for a
  16. * general-purpose malloc(), pools use a series of large memory "chunks",
  17. * each of which is carved into a bunch of smaller "items" or
  18. * "allocations".
  19. *
  20. * To get decent performance, you need to:
  21. * - Minimize the number of times you hit the underlying allocator.
  22. * - Try to keep accesses as local in memory as possible.
  23. * - Try to keep the common case fast.
  24. *
  25. * Our implementation uses three lists of chunks per pool. Each chunk can
  26. * be either "full" (no more room for items); "empty" (no items); or
  27. * "used" (not full, not empty). There are independent doubly-linked
  28. * lists for each state.
  29. *
  30. * CREDIT:
  31. *
  32. * I wrote this after looking at 3 or 4 other pooling allocators, but
  33. * without copying. The strategy this most resembles (which is funny,
  34. * since that's the one I looked at longest ago) the pool allocator
  35. * underlying Python's obmalloc code. Major differences from obmalloc's
  36. * pools are:
  37. * - We don't even try to be threadsafe.
  38. * - We only handle objects of one size.
  39. * - Our list of empty chunks is doubly-linked, not singly-linked.
  40. * (This could change pretty easily; it's only doubly-linked for
  41. * consistency.)
  42. * - We keep a list of full chunks (so we can have a "nuke everything"
  43. * function). Obmalloc's pools leave full chunks to float unanchored.
  44. *
  45. * [XXXX020 Another way to support 'nuke everything' would be to keep
  46. * _all_ the chunks in a doubly-linked-list. This would have more
  47. * space overhead per chunk, but less pointer manipulation overhead
  48. * than the current approach.]
  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: redefign 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. * - probably, chunks should always be a power of 2.
  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. /* End Tor dependencies */
  74. #else
  75. /* If you're not building this as part of Tor, you'll want to define the
  76. * following macros. For now, these should do as defaults.
  77. */
  78. #include <assert.h>
  79. #define PREDICT_UNLIKELY(x) (x)
  80. #define PREDICT_LIKELY(x) (x)
  81. #define ALLOC(x) malloc(x)
  82. #define FREE(x) free(x)
  83. #define STRUCT_OFFSET(tp, member) \
  84. ((off_t) (((char*)&((tp*)0)->member)-(char*)0))
  85. #define ASSERT(x) assert(x)
  86. #define ALLOC_CAN_RETURN_NULL
  87. #endif
  88. /* Tuning parameters */
  89. /** Largest type that we need to ensure returned memory items are aligned to.
  90. * Change this to "double" if we need to be safe for structs with doubles. */
  91. #define ALIGNMENT_TYPE void *
  92. /** Increment that we need to align allocated */
  93. #define ALIGNMENT sizeof(ALIGNMENT_TYPE)
  94. /** Largest memory chunk that we should allocate. */
  95. #define MAX_CHUNK (8*(1L<<20))
  96. /** Smallest memory chunk size that we should allocate. */
  97. #define MIN_CHUNK 4096
  98. typedef struct mp_allocated_t mp_allocated_t;
  99. typedef struct mp_chunk_t mp_chunk_t;
  100. /** Holds a single allocated item, allocated as part of a chunk. */
  101. struct mp_allocated_t {
  102. /** The chunk that this item is allocated in. This adds overhead to each
  103. * allocated item, thus making this implementation inappropriate for
  104. * very small items. */
  105. mp_chunk_t *in_chunk;
  106. union {
  107. /** If this item is free, the next item on the free list. */
  108. mp_allocated_t *next_free;
  109. /** If this item is not free, the actual memory contents of this item.
  110. * (Not actual size.) */
  111. char mem[1];
  112. /** An extra element to the union to insure correct alignment. */
  113. ALIGNMENT_TYPE _dummy;
  114. } u;
  115. };
  116. /** 'Magic' value used to detect memory corruption. */
  117. #define MP_CHUNK_MAGIC 0x09870123
  118. /** A chunk of memory. Chunks come from malloc; we use them */
  119. struct mp_chunk_t {
  120. unsigned long magic; /**< Must be MP_CHUNK_MAGIC if this chunk is valid. */
  121. mp_chunk_t *next; /**< The next free, used, or full chunk in sequence. */
  122. mp_chunk_t *prev; /**< The previous free, used, or full chunk in sequence. */
  123. mp_pool_t *pool; /**< The pool that this chunk is part of */
  124. /** First free item in the freelist for this chunk. Note that this may be
  125. * NULL even if this chunk is not at capacity: if so, the free memory at
  126. * next_mem has not yet been carved into items.
  127. */
  128. mp_allocated_t *first_free;
  129. int n_allocated; /**< Number of currently allocated items in this chunk */
  130. int capacity; /**< Largest number of items that can be fit into this chunk */
  131. size_t mem_size; /**< Number of usable bytes in mem. */
  132. char *next_mem; /**< Pointer into part of <b>mem</b> not yet carved up. */
  133. char mem[1]; /**< Storage for this chunk. (Not actual size.) */
  134. };
  135. /** Number of extra bytes needed beyond mem_size to allocate a chunk. */
  136. #define CHUNK_OVERHEAD (sizeof(mp_chunk_t)-1)
  137. /** Given a pointer to a mp_allocated_t, return a pointer to the memory
  138. * item it holds. */
  139. #define A2M(a) (&(a)->u.mem)
  140. /** Given a pointer to a memory_item_t, return a pointer to its enclosing
  141. * mp_allocated_t. */
  142. #define M2A(p) ( ((char*)p) - STRUCT_OFFSET(mp_allocated_t, u.mem) )
  143. #ifdef ALLOC_CAN_RETURN_NULL
  144. /** If our ALLOC() macro can return NULL, check whether <b>x</b> is NULL,
  145. * and if so, return NULL. */
  146. #define CHECK_ALLOC(x) \
  147. if (PREDICT_UNLIKELY(!x)) { return NULL; }
  148. #else
  149. /** If our ALLOC() macro can't return NULL, do nothing. */
  150. #define CHECK_ALLOC(x)
  151. #endif
  152. /** Helper: Allocate and return a new memory chunk for <b>pool</b>. Does not
  153. * link the chunk into any list. */
  154. static mp_chunk_t *
  155. mp_chunk_new(mp_pool_t *pool)
  156. {
  157. size_t sz = pool->new_chunk_capacity * pool->item_alloc_size;
  158. mp_chunk_t *chunk = ALLOC(CHUNK_OVERHEAD + sz);
  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. /** Return an newly allocated item from <b>pool</b>. */
  169. void *
  170. mp_pool_get(mp_pool_t *pool)
  171. {
  172. mp_chunk_t *chunk;
  173. mp_allocated_t *allocated;
  174. if (PREDICT_LIKELY(pool->used_chunks != NULL)) {
  175. /* Common case: there is some chunk that is neither full nor empty. Use
  176. * that one. (We can't use the full ones, obviously, and we should fill
  177. * up the used ones before we start on any empty ones. */
  178. chunk = pool->used_chunks;
  179. } else if (pool->empty_chunks) {
  180. /* We have no used chunks, but we have an empty chunk that we haven't
  181. * freed yet: use that. (We pull from the front of the list, which should
  182. * get us the most recently emptied chunk.) */
  183. chunk = pool->empty_chunks;
  184. /* Remove the chunk from the empty list. */
  185. pool->empty_chunks = chunk->next;
  186. if (chunk->next)
  187. chunk->next->prev = NULL;
  188. /* Put the chunk on the 'used' list*/
  189. chunk->next = pool->used_chunks;
  190. if (chunk->next)
  191. chunk->next->prev = chunk;
  192. pool->used_chunks = chunk;
  193. ASSERT(!chunk->prev);
  194. --pool->n_empty_chunks;
  195. if (pool->n_empty_chunks < pool->min_empty_chunks)
  196. pool->min_empty_chunks = pool->n_empty_chunks;
  197. } else {
  198. /* We have no used or empty chunks: allocate a new chunk. */
  199. chunk = mp_chunk_new(pool);
  200. CHECK_ALLOC(chunk);
  201. /* Add the new chunk to the used list. */
  202. chunk->next = pool->used_chunks;
  203. if (chunk->next)
  204. chunk->next->prev = chunk;
  205. pool->used_chunks = chunk;
  206. ASSERT(!chunk->prev);
  207. }
  208. ASSERT(chunk->n_allocated < chunk->capacity);
  209. if (chunk->first_free) {
  210. /* If there's anything on the chunk's freelist, unlink it and use it. */
  211. allocated = chunk->first_free;
  212. chunk->first_free = allocated->u.next_free;
  213. allocated->u.next_free = NULL; /* For debugging; not really needed. */
  214. ASSERT(allocated->in_chunk == chunk);
  215. } else {
  216. /* Otherwise, the chunk had better have some free space left on it. */
  217. ASSERT(chunk->next_mem + pool->item_alloc_size <=
  218. chunk->mem + chunk->mem_size);
  219. /* Good, it did. Let's carve off a bit of that free space, and use
  220. * that. */
  221. allocated = (void*)chunk->next_mem;
  222. chunk->next_mem += pool->item_alloc_size;
  223. allocated->in_chunk = chunk;
  224. allocated->u.next_free = NULL; /* For debugging; not really needed. */
  225. }
  226. ++chunk->n_allocated;
  227. if (PREDICT_UNLIKELY(chunk->n_allocated == chunk->capacity)) {
  228. /* This chunk just became full. */
  229. ASSERT(chunk == pool->used_chunks);
  230. ASSERT(chunk->prev == NULL);
  231. /* Take it off the used list. */
  232. pool->used_chunks = chunk->next;
  233. if (chunk->next)
  234. chunk->next->prev = NULL;
  235. /* Put it on the full list. */
  236. chunk->next = pool->full_chunks;
  237. if (chunk->next)
  238. chunk->next->prev = chunk;
  239. pool->full_chunks = chunk;
  240. }
  241. /* And return the memory portion of the mp_allocated_t. */
  242. return A2M(allocated);
  243. }
  244. /** Return an allocated memory item to its memory pool. */
  245. void
  246. mp_pool_release(void *item)
  247. {
  248. mp_allocated_t *allocated = (void*) M2A(item);
  249. mp_chunk_t *chunk = allocated->in_chunk;
  250. ASSERT(chunk);
  251. ASSERT(chunk->magic == MP_CHUNK_MAGIC);
  252. ASSERT(chunk->n_allocated > 0);
  253. allocated->u.next_free = chunk->first_free;
  254. chunk->first_free = allocated;
  255. if (PREDICT_UNLIKELY(chunk->n_allocated == chunk->capacity)) {
  256. /* This chunk was full and is about to be used. */
  257. mp_pool_t *pool = chunk->pool;
  258. /* unlink from the full list */
  259. if (chunk->prev)
  260. chunk->prev->next = chunk->next;
  261. if (chunk->next)
  262. chunk->next->prev = chunk->prev;
  263. if (chunk == pool->full_chunks)
  264. pool->full_chunks = chunk->next;
  265. /* link to the used list. */
  266. chunk->next = pool->used_chunks;
  267. chunk->prev = NULL;
  268. if (chunk->next)
  269. chunk->next->prev = chunk;
  270. pool->used_chunks = chunk;
  271. } else if (PREDICT_UNLIKELY(chunk->n_allocated == 1)) {
  272. /* This was used and is about to be empty. */
  273. mp_pool_t *pool = chunk->pool;
  274. /* Unlink from the used list */
  275. if (chunk->prev)
  276. chunk->prev->next = chunk->next;
  277. if (chunk->next)
  278. chunk->next->prev = chunk->prev;
  279. if (chunk == pool->used_chunks)
  280. pool->used_chunks = chunk->next;
  281. /* Link to the empty list */
  282. chunk->next = pool->empty_chunks;
  283. chunk->prev = NULL;
  284. if (chunk->next)
  285. chunk->next->prev = chunk;
  286. pool->empty_chunks = chunk;
  287. /* Reset the guts of this chunk to defragment it, in case it gets
  288. * used again. */
  289. chunk->first_free = NULL;
  290. chunk->next_mem = chunk->mem;
  291. ++pool->n_empty_chunks;
  292. }
  293. --chunk->n_allocated;
  294. }
  295. /** Allocate a new memory pool to hold items of size <b>item_size</b>. We'll
  296. * try to fit about <b>chunk_capacity</b> bytes in each chunk. */
  297. mp_pool_t *
  298. mp_pool_new(size_t item_size, size_t chunk_capacity)
  299. {
  300. mp_pool_t *pool;
  301. size_t alloc_size;
  302. pool = ALLOC(sizeof(mp_pool_t));
  303. CHECK_ALLOC(pool);
  304. memset(pool, 0, sizeof(mp_pool_t));
  305. /* First, we figure out how much space to allow per item. We'll want to
  306. * use make sure we have enough for the overhead plus the item size. */
  307. alloc_size = STRUCT_OFFSET(mp_allocated_t, u.mem) + item_size;
  308. /* If the item_size is less than sizeof(next_free), we need to make
  309. * the allocation bigger. */
  310. if (alloc_size < sizeof(mp_allocated_t))
  311. alloc_size = sizeof(mp_allocated_t);
  312. /* If we're not an even multiple of ALIGNMENT, round up. */
  313. if (alloc_size % ALIGNMENT) {
  314. alloc_size = alloc_size + ALIGNMENT - (alloc_size % ALIGNMENT);
  315. }
  316. if (alloc_size < ALIGNMENT)
  317. alloc_size = ALIGNMENT;
  318. ASSERT((alloc_size % ALIGNMENT) == 0);
  319. /* Now we figure out how many items fit in each chunk. We need to fit at
  320. * least 2 items per chunk. No chunk can be more than MAX_CHUNK bytes long,
  321. * or less than MIN_CHUNK. */
  322. /* XXXX020 Try a bit harder here: we want to be a bit less than a power of
  323. 2, not a bit over. */
  324. if (chunk_capacity > MAX_CHUNK)
  325. chunk_capacity = MAX_CHUNK;
  326. if (chunk_capacity < alloc_size * 2 + CHUNK_OVERHEAD)
  327. chunk_capacity = alloc_size * 2 + CHUNK_OVERHEAD;
  328. if (chunk_capacity < MIN_CHUNK)
  329. chunk_capacity = MIN_CHUNK;
  330. pool->new_chunk_capacity = (chunk_capacity-CHUNK_OVERHEAD) / alloc_size;
  331. pool->item_alloc_size = alloc_size;
  332. return pool;
  333. }
  334. /** If there are more than <b>n</b> empty chunks in <b>pool</b>, free the
  335. * excess ones that have been empty for the longest. (If <b>n</b> is less
  336. * than zero, free only empty chunks that were not used since the last
  337. * call to mp_pool_clean(), leaving only -<b>n</b>.) */
  338. void
  339. mp_pool_clean(mp_pool_t *pool, int n)
  340. {
  341. mp_chunk_t *chunk, **first_to_free;
  342. if (n < 0) {
  343. n = pool->min_empty_chunks + (-n);
  344. if (n < pool->n_empty_chunks)
  345. pool->min_empty_chunks = n;
  346. }
  347. ASSERT(n>=0);
  348. first_to_free = &pool->empty_chunks;
  349. while (*first_to_free && n > 0) {
  350. first_to_free = &(*first_to_free)->next;
  351. --n;
  352. }
  353. if (!*first_to_free)
  354. return;
  355. chunk = *first_to_free;
  356. while (chunk) {
  357. mp_chunk_t *next = chunk->next;
  358. chunk->magic = 0xdeadbeef;
  359. FREE(chunk);
  360. --pool->n_empty_chunks;
  361. chunk = next;
  362. }
  363. *first_to_free = NULL;
  364. }
  365. /** Helper: Given a list of chunks, free all the chunks in the list. */
  366. static void
  367. destroy_chunks(mp_chunk_t *chunk)
  368. {
  369. mp_chunk_t *next;
  370. while (chunk) {
  371. chunk->magic = 0xd3adb33f;
  372. next = chunk->next;
  373. FREE(chunk);
  374. chunk = next;
  375. }
  376. }
  377. /** Free all space held in <b>pool</b> This makes all pointers returned from
  378. * mp_pool_get(<b>pool</b>) invalid. */
  379. void
  380. mp_pool_destroy(mp_pool_t *pool)
  381. {
  382. destroy_chunks(pool->empty_chunks);
  383. destroy_chunks(pool->used_chunks);
  384. destroy_chunks(pool->full_chunks);
  385. memset(pool, 0xe0, sizeof(mp_pool_t));
  386. FREE(pool);
  387. }
  388. /** Helper: make sure that a given chunk list is not corrupt. */
  389. static int
  390. assert_chunks_ok(mp_pool_t *pool, mp_chunk_t *chunk, int empty, int full)
  391. {
  392. mp_allocated_t *allocated;
  393. int n = 0;
  394. if (chunk)
  395. ASSERT(chunk->prev == NULL);
  396. while (chunk) {
  397. n++;
  398. ASSERT(chunk->magic == MP_CHUNK_MAGIC);
  399. ASSERT(chunk->pool == pool);
  400. for (allocated = chunk->first_free; allocated;
  401. allocated = allocated->u.next_free) {
  402. ASSERT(allocated->in_chunk == chunk);
  403. }
  404. if (empty)
  405. ASSERT(chunk->n_allocated == 0);
  406. else if (full)
  407. ASSERT(chunk->n_allocated == chunk->capacity);
  408. else
  409. ASSERT(chunk->n_allocated > 0 && chunk->n_allocated < chunk->capacity);
  410. ASSERT(chunk->capacity == pool->new_chunk_capacity);
  411. ASSERT(chunk->mem_size ==
  412. pool->new_chunk_capacity * pool->item_alloc_size);
  413. ASSERT(chunk->next_mem >= chunk->mem &&
  414. chunk->next_mem <= chunk->mem + chunk->mem_size);
  415. if (chunk->next)
  416. ASSERT(chunk->next->prev == chunk);
  417. chunk = chunk->next;
  418. }
  419. return n;
  420. }
  421. /** Fail with an assertion if <b>pool</b> is not internally consistent. */
  422. void
  423. mp_pool_assert_ok(mp_pool_t *pool)
  424. {
  425. int n_empty;
  426. n_empty = assert_chunks_ok(pool, pool->empty_chunks, 1, 0);
  427. assert_chunks_ok(pool, pool->full_chunks, 0, 1);
  428. assert_chunks_ok(pool, pool->used_chunks, 0, 0);
  429. ASSERT(pool->n_empty_chunks == n_empty);
  430. }
  431. #ifdef TOR
  432. /*FFFF uses Tor logging functions. */
  433. /**DOCDOC*/
  434. void
  435. mp_pool_log_status(mp_pool_t *pool, int severity)
  436. {
  437. uint64_t bytes_used = 0;
  438. uint64_t bytes_allocated = 0;
  439. uint64_t bu = 0, ba = 0;
  440. mp_chunk_t *chunk;
  441. int n_full = 0, n_used = 0;
  442. ASSERT(pool);
  443. for (chunk = pool->empty_chunks; chunk; chunk = chunk->next) {
  444. bytes_allocated += chunk->mem_size;
  445. }
  446. log_fn(severity, LD_MM, U64_FORMAT" bytes in %d empty chunks",
  447. U64_PRINTF_ARG(bytes_used), pool->n_empty_chunks);
  448. for (chunk = pool->used_chunks; chunk; chunk = chunk->next) {
  449. ++n_used;
  450. bu += chunk->n_allocated * pool->item_alloc_size;
  451. ba += chunk->mem_size;
  452. }
  453. log_fn(severity, LD_MM, U64_FORMAT"/"U64_FORMAT
  454. " bytes in %d partially full chunks",
  455. U64_PRINTF_ARG(bu), U64_PRINTF_ARG(ba), n_used);
  456. bytes_used += bu;
  457. bytes_allocated += ba;
  458. bu = ba = 0;
  459. for (chunk = pool->full_chunks; chunk; chunk = chunk->next) {
  460. ++n_full;
  461. bu += chunk->n_allocated * pool->item_alloc_size;
  462. ba += chunk->mem_size;
  463. }
  464. log_fn(severity, LD_MM, U64_FORMAT"/"U64_FORMAT
  465. " bytes in %d full chunks",
  466. U64_PRINTF_ARG(bu), U64_PRINTF_ARG(ba), n_full);
  467. bytes_used += bu;
  468. bytes_allocated += ba;
  469. log_fn(severity, LD_MM, "Total: "U64_FORMAT"/"U64_FORMAT" bytes allocated "
  470. "for cell pools are full.",
  471. U64_PRINTF_ARG(bytes_used), U64_PRINTF_ARG(bytes_allocated));
  472. }
  473. #endif