Skip to content

Commit

Permalink
Get the GC to work again and change the allocator
Browse files Browse the repository at this point in the history
Allocator subsystem works with a single contiguous chunk of memory.
Small object allocator uses fixed-size bins and never attempts to merge chunks,
and the large object allocator is a slightly-modified version of the SQLite
mem3.c allocator.
The GC uses the boundaries of this chunk of memory, plus a table of
flags, to determine if a Value refers to a heap allocation or not (we no longer have type information per-Value). The
current implementation requires storing the size of each heap object in
the GC header, but it should be possible to elide this requirement using flags (using 2 bits for each flag instead of 1, see golang GC implementation).
Should support expanding the heap via. sbrk or similar (will require
changing paw_Alloc to expand/contract the dynamic memory area, rather
than implement a realloc-like interface).
  • Loading branch information
andy-byers committed Aug 13, 2024
1 parent 6ee6a63 commit 2a0845c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ int pawZ_init(paw_Env *P, size_t heap_size)
void *heap = OS_MALLOC(P, heap_size);
if (heap == NULL) goto no_memory;
H->heap = heap;
printf("heap: %p-%p\n", heap,BUMP(heap,H->heap_size));

#define SKIP_CHUNK(z) (heap = BUMP(heap, aligned(z)), \
heap_size -= aligned(z))
H->a_block = heap;
Expand Down
2 changes: 1 addition & 1 deletion src/alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct GcFlag {
uint8_t value;
};

#define FAST_BIN_COUNT 1024
#define FAST_BIN_COUNT 64

struct FastBins {
struct BinInfo *info[FAST_BIN_COUNT];
Expand Down
9 changes: 6 additions & 3 deletions test/test_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static void alloc_and_free(paw_Env *P, size_t size)
pawZ_alloc(P, ptr, size, 0);
}

#define MAX_DEFER 16384
#define MAX_DEFER 32768
static struct DeferredAlloc {
void *ptr;
size_t size;
Expand Down Expand Up @@ -105,14 +105,17 @@ static void test_lots_of_allocations(paw_Env *P)
alloc_pattern(P, CAST_SIZE(rand() % 100 + 1));
}
for (size_t i = 0; i < 100; ++i) {
if(i==18){

}
alloc_pattern(P, CAST_SIZE(rand() % 10000 + 1));
}
free_deferred_ptrs(P);
}

int main(void)
{
driver(open_and_close);
driver(test_small_allocations);
// driver(open_and_close);
// driver(test_small_allocations);
driver(test_lots_of_allocations);
}

0 comments on commit 2a0845c

Please sign in to comment.