memory.h File Reference

Functions for dealing with garbage collection and memory allocation. More...

#include "kuroko.h"
#include "object.h"
#include "table.h"
Include dependency graph for memory.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define KRK_GROW_CAPACITY(c)   ((c) < 8 ? 8 : (c) * 2)
 
#define KRK_GROW_ARRAY(t, p, o, n)   (t*)krk_reallocate(p,sizeof(t)*o,sizeof(t)*n)
 
#define KRK_FREE_ARRAY(t, a, c)   krk_reallocate(a,sizeof(t) * c, 0)
 
#define KRK_ALLOCATE(type, count)   (type*)krk_reallocate(NULL,0,sizeof(type)*(count))
 

Functions

void * krk_reallocate (void *ptr, size_t old, size_t new)
 Resize an allocated heap object. More...
 
void krk_freeObjects (void)
 Release all objects. More...
 
size_t krk_collectGarbage (void)
 Run a cycle of the garbage collector. More...
 
void krk_markValue (KrkValue value)
 During a GC scan cycle, mark a value as used. More...
 
void krk_markObject (KrkObj *object)
 During a GC scan cycle, mark an object as used. More...
 
void krk_markTable (KrkTable *table)
 During a GC scan cycle, mark the contents of a table as used. More...
 
void krk_gcTakeBytes (const void *ptr, size_t size)
 Assume ownership of size bytes at ptr. More...
 

Detailed Description

Functions for dealing with garbage collection and memory allocation.

Definition in file memory.h.

Function Documentation

◆ krk_collectGarbage()

size_t krk_collectGarbage ( void  )

Run a cycle of the garbage collector.

Runs one scan-sweep cycle of the garbage collector, potentially freeing unused resources and advancing potentially-unused resources to the next stage of removal.

Returns
The number of bytes released by this collection cycle.

The GC scheduling is in need of some improvement. The strategy at the moment is to schedule the next collect at double the current post-collection byte allocation size, up until that reaches 128MiB (64*2). Beyond that point, the next collection is scheduled for 64MiB after the current value.

Previously, we always doubled as that was what Lox did, but this rather quickly runs into issues when memory allocation climbs into the GiB range. 64MiB seems to be a good switchover point.

Definition at line 514 of file memory.c.

◆ krk_freeObjects()

void krk_freeObjects ( void  )

Release all objects.

Generally called automatically by krk_freeVM(); releases all of the GC-tracked heap objects.

Definition at line 277 of file memory.c.

◆ krk_gcTakeBytes()

void krk_gcTakeBytes ( const void *  ptr,
size_t  size 
)

Assume ownership of size bytes at ptr.

Future memory allocation operations with this ptr must be through krk_reallocate - size will be added to the VM allocation count, and if extensively memory debugging is enabled then the pointer will be marked as owned.

Parameters
ptrPointer to take ownership of
sizeSize of data at ptr

Definition at line 149 of file memory.c.

◆ krk_markObject()

void krk_markObject ( KrkObj object)

During a GC scan cycle, mark an object as used.

Equivalent to krk_markValue but operates directly on an object.

Parameters
objectThe object to mark.

Definition at line 321 of file memory.c.

◆ krk_markTable()

void krk_markTable ( KrkTable table)

During a GC scan cycle, mark the contents of a table as used.

Marks all keys and values in a table as used. Generally applied to the internal storage of mapping types.

Parameters
tableThe table to mark.

Definition at line 444 of file memory.c.

◆ krk_markValue()

void krk_markValue ( KrkValue  value)

During a GC scan cycle, mark a value as used.

When defining a new type in a C extension, this function should be used by the type's _ongcscan callback to mark any values not already tracked by the garbage collector.

Parameters
valueThe value to mark.

Definition at line 334 of file memory.c.

◆ krk_reallocate()

void* krk_reallocate ( void *  ptr,
size_t  old,
size_t  new 
)

Resize an allocated heap object.

Allocates or reallocates the heap object 'ptr', tracking changes in sizes from 'old' to 'new'. If 'ptr' is NULL, 'old' should be 0, and a new pointer will be allocated of size 'new'.

Parameters
ptrHeap object to resize.
oldCurrent size of the object.
newNew size of the object.
Returns
New pointer for heap object.

Definition at line 157 of file memory.c.