Skip to content
Garrett Berg edited this page Mar 11, 2015 · 6 revisions

Implementation

The current memory manager is in py/gc.c and is almost a textbook implementation of mark-sweep GC.

Resources:

User Level Functions

The main functions that a user needs are: gc_alloc -- malloc with gc gc_free -- free with gc

These are built on top of a memory structure using Allocation Table Bytes (ATBs)

Memory Structure

Memory is split up into 4 Allocation Tables. Every Allocation Table has a ATB which stands for "Allocation Table Byte". This is a single byte containing 4 sets of the following

// 0b00 = FREE -- free block
// 0b01 = HEAD -- head of a chain of blocks
// 0b10 = TAIL -- in the tail of a chain of blocks
// 0b11 = MARK -- marked head block

These are known as ATB_0 through ATB_3 and have several C methods (i.e. functions and macros) to access their attributes. These include:

BLOCKS_PER_ATB -- The number of ATB's that fit in an Allocation Table ATB_MASK_0 -- Get the relevant bytes for ATB_0 ... ATB_MASK_3

ATB_0_IS_FREE(a) -- Determine whether table is currently free ... ATB_3_IS_FREE(a)

What do these do? Why are they useful??? BLOCK_SHIFT(block) ATB_GET_KIND(block) ATB_ANY_TO_FREE(block) ATB_FREE_TO_HEAD(block) ATB_FREE_TO_TAIL(block) ATB_HEAD_TO_MARK(block) ATB_MARK_TO_HEAD(block)

BLOCK_FROM_PTR(ptr) PTR_FROM_BLOCK(block) ATB_FROM_BLOCK(bl)

Questions

  • Is there documentation for the above methods? I think that would help me understand what they do
  • How is the memory structured? How is it gotten? Does python take out an array of data, or does it take out single elements at at time? I am having trouble understanding how this memory manager is supposed to be used.
  • How many pointers can the memory manager handle at once (on your reference implementation, I believe 128k of memory)? Does the size of the data being requested matter? How does it deal with fragmentation? For instance, if the system asks for a few 100byte arrays and a few 5byte structs one after another, and then frees the 100byte arrays -- how does it handle the fragmentation? Is there a memory defragmenter?
Clone this wiki locally