@@ -268,27 +268,66 @@ all partially used pools holding small blocks with "size class idx" i. So
268268usedpools[0] corresponds to blocks of size 8, usedpools[2] to blocks of size
26926916, and so on: index 2*i <-> blocks of size (i+1)<<ALIGNMENT_SHIFT.
270270
271- The partially used pools for a given index are linked together via their
272- pool_header's prevpool and nextpool members. "Partially used" means at least
273- one block in the pool is currently allocated, *and* at least one block in the
274- pool is not currently allocated.
275-
276- When all blocks in a pool are allocated, the pool is unlinked from the list,
277- and isn't linked to from anything anymore (you can't find it then from
278- anything obmalloc.c knows about); the pool's own prevpool and nextpool
279- pointers are set to point to itself. The comments say the pool "is full" then.
280-
281- When a small block is returned to pymalloc, there are two cases. If its pool
282- was full, its pool is relinked into the appropriate usedpools[] list, at the
283- front (so the next allocation of the same size class will be taken from this
284- pool). Else its pool was not full, the pool is already in a usedpools[]
285- list, and isn't moved. Instead the block is just linked to the front of the
286- pool's own freeblock singly-linked list. However, if that makes the pool
287- entirely free of allocated blocks (the comments say the pool "is empty" then),
288- the pool is unlinked from usedpools[], and inserted at the front of the
289- (file static) singly-linked freepools list, via the pool header's nextpool
290- member; prevpool is meaningless in this case. Pools put on the freepools
291- list can be changed to belong to a different size class.
271+ Pools are carved off the current arena highwater mark (file static arenabase)
272+ as needed. Once carved off, a pool is in one of three states forever after:
273+
274+ used == partially used, neither empty nor full
275+ At least one block in the pool is currently allocated, and at least one
276+ block in the pool is not currently allocated (note this implies a pool
277+ has room for at least two blocks).
278+ This is a pool's initial state, as a pool is created only when malloc
279+ needs space.
280+ The pool holds blocks of a fixed size, and is in the circular list headed
281+ at usedpools[i] (see above). It's linked to the other used pools of the
282+ same size class via the pool_header's nextpool and prevpool members.
283+ If all but one block is currently allocated, a malloc can cause a
284+ transition to the full state. If all but one block is not currently
285+ allocated, a free can cause a transition to the empty state.
286+
287+ full == all the pool's blocks are currently allocated
288+ On transition to full, a pool is unlinked from its usedpools[] list.
289+ It's not linked to from anything then anymore, and its nextpool and
290+ prevpool members are meaningless until it transitions back to used.
291+ A free of a block in a full pool puts the pool back in the used state.
292+ Then it's linked in at the front of the appropriate usedpools[] list, so
293+ that the next allocation for its size class will reuse the freed block.
294+
295+ empty == all the pool's blocks are currently available for allocation
296+ On transition to empty, a pool is unlinked from its usedpools[] list,
297+ and linked to the front of the (file static) singly-linked freepools list,
298+ via its nextpool member. The prevpool member has no meaning in this case.
299+ Empty pools have no inherent size class: the next time a malloc finds
300+ an empty list in usedpools[], it takes the first pool off of freepools.
301+ If the size class needed happens to be the same as the size class the pool
302+ last had, some expensive initialization can be skipped (including an
303+ integer division -- XXX since the value
304+
305+ pool->capacity = (POOL_SIZE - POOL_OVERHEAD) / size;
306+
307+ is invariant across all pools of a given size class, it may make more
308+ sense to compute those at compile-time into a const vector indexed by
309+ size class, and lose the pool->capacity member and the runtime divisions).
310+
311+
312+ Block Management
313+
314+ Blocks within pools are again carved out as needed. pool->freeblock points to
315+ the start of a singly-linked list of free blocks within the pool. When a
316+ block is freed, it's inserted at the front of its pool's freeblock list. Note
317+ that the available blocks in a pool are *not* linked all together when a pool
318+ is initialized. Instead only "the first" (lowest address) block is set up,
319+ setting pool->freeblock to NULL. This is consistent with that pymalloc
320+ strives at all levels (arena, pool, and block) never to touch a piece of
321+ memory until it's actually needed. So long as a pool is in the used state,
322+ we're certain there *is* a block available for allocating. If pool->freeblock
323+ is NULL then, that means we simply haven't yet gotten to one of the higher-
324+ address blocks. The address of "the next" available block can be computed
325+ then from pool->ref.count (the number of currently allocated blocks). This
326+ computation can be expensive, because it requires an integer multiply.
327+ However, so long as the pool's size class doesn't change, it's a one-time cost
328+ for that block; the computation could be made cheaper via adding a highwater
329+ pointer to the pool_header, but the tradeoff is murky.
330+
292331
293332Major obscurity: While the usedpools vector is declared to have poolp
294333entries, it doesn't really. It really contains two pointers per (conceptual)
@@ -510,6 +549,7 @@ new_arena(void)
510549 */
511550#define ADDRESS_IN_RANGE (P , I ) \
512551 ((I) < narenas && (uptr)(P) - arenas[I] < (uptr)ARENA_SIZE)
552+
513553/*==========================================================================*/
514554
515555/* malloc */
0 commit comments