Skip to content

Commit

Permalink
refactor: Synchronize naming of GC public API (#3652)
Browse files Browse the repository at this point in the history
* Normalize names of public GC types/functions, use `scalanative_GC_` prefix instead of `scalanative_`
Merge `GCScalaNative.h` into `ScalaNativeGC.h` defining common interface
* Synchronize reamning env variable and preprocesor definitions
* Reanem define `DEBUG_ASSERT` to `GC_ASSERTIONS`
  • Loading branch information
WojciechMazur committed Jan 5, 2024
1 parent 5279a63 commit 8655125
Show file tree
Hide file tree
Showing 40 changed files with 232 additions and 232 deletions.
3 changes: 3 additions & 0 deletions docs/changelog/0.5.0.draft.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ are used on all GCs. The last one works on Boehm and Commix.
* GC_INITIAL_HEAP_SIZE (was SCALANATIVE_MIN_HEAP_SIZE)
* GC_MAXIMUM_HEAP_SIZE (was SCALANATIVE_MAX_HEAP_SIZE)
* GC_NPROCS (was SCALANATIVE_GC_THREADS)
* GC_TIME_RATIO (was SCALANATIVE_TIME_RATIO)
* GC_FREE_RATION (was SCALANATIVE_FREE_RATIO)
* GC_STATS_FILE (was SCALANATIVE_STATS_FILE)

## New features

Expand Down
4 changes: 2 additions & 2 deletions docs/contrib/ides.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ certain simple setups which is probably fine for most other projects. The follow
-DSCALANATIVE_GC_EXPERIMENTAL
# Other defines to allow analysis of code
-DSCALANATIVE_MULTITHREADING_ENABLED
-DENABLE_GC_STATS
-DENABLE_GC_STATS_SYNC
-DGC_ENABLE_STATS
-DGC_ENABLE_STATS_SYNC
-DDEBUG_PRINT
-DDEBUG_ASSERT
# end GC
10 changes: 5 additions & 5 deletions docs/user/runtime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Immix GC

The Immix GC uses the two variables shown above as well as the following variable.

* SCALANATIVE_STATS_FILE (set to the file name)
* GC_STATS_FILE (set to the file name)

Commix GC
---------
Expand All @@ -59,11 +59,11 @@ variable shared with Boehm.

Commix also adds a few more variables which do not match the Boehm settings yet.

* SCALANATIVE_TIME_RATIO (default is .05)
* SCALANATIVE_FREE_RATIO (default is .5)
* GC_TIME_RATIO (default is .05)
* GC_FREE_RATIO (default is .5)

Note: STATS_FILE_SETTING shared with Immix is only available if the compiler defines
-DENABLE_GC_STATS for Commix.
Note: GC_STATS_FILE shared with Immix is only honored if the compiler defines
-DGC_ENABLE_STATS for Commix.

Examples
--------
Expand Down
40 changes: 20 additions & 20 deletions nativelib/src/main/resources/scala-native/gc/boehm/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,42 @@
#include <stdio.h>
#include <string.h>
#include "shared/Parsing.h"
#include "shared/GCScalaNative.h"

// At the moment we rely on the conservative
// mode of Boehm GC as our garbage collector.

void scalanative_init() { GC_INIT(); }
void scalanative_GC_init() { GC_INIT(); }

void *scalanative_alloc(void *info, size_t size) {
void *scalanative_GC_alloc(void *info, size_t size) {
void **alloc = (void **)GC_malloc(size);
*alloc = info;
return (void *)alloc;
}

void *scalanative_alloc_small(void *info, size_t size) {
void *scalanative_GC_alloc_small(void *info, size_t size) {
void **alloc = (void **)GC_malloc(size);
*alloc = info;
return (void *)alloc;
}

void *scalanative_alloc_large(void *info, size_t size) {
void *scalanative_GC_alloc_large(void *info, size_t size) {
void **alloc = (void **)GC_malloc(size);
*alloc = info;
return (void *)alloc;
}

void *scalanative_alloc_atomic(void *info, size_t size) {
void *scalanative_GC_alloc_atomic(void *info, size_t size) {
void **alloc = (void **)GC_malloc_atomic(size);
memset(alloc, 0, size);
*alloc = info;
return (void *)alloc;
}

size_t scalanative_get_init_heapsize() {
size_t scalanative_GC_get_init_heapsize() {
return Parse_Env_Or_Default("GC_INITIAL_HEAP_SIZE", 0L);
}

size_t scalanative_get_max_heapsize() {
size_t scalanative_GC_get_max_heapsize() {
struct GC_prof_stats_s *stats =
(struct GC_prof_stats_s *)malloc(sizeof(struct GC_prof_stats_s));
GC_get_prof_stats(stats, sizeof(struct GC_prof_stats_s));
Expand All @@ -55,36 +54,37 @@ size_t scalanative_get_max_heapsize() {
return Parse_Env_Or_Default("GC_MAXIMUM_HEAP_SIZE", heap_sz);
}

void scalanative_collect() { GC_gcollect(); }
void scalanative_GC_collect() { GC_gcollect(); }

void scalanative_register_weak_reference_handler(void *handler) {}
void scalanative_GC_register_weak_reference_handler(void *handler) {}

#ifdef SCALANATIVE_MULTITHREADING_ENABLED
#ifdef _WIN32
HANDLE scalanative_CreateThread(LPSECURITY_ATTRIBUTES threadAttributes,
SIZE_T stackSize, ThreadStartRoutine routine,
RoutineArgs args, DWORD creationFlags,
DWORD *threadId) {
HANDLE scalanative_GC_CreateThread(LPSECURITY_ATTRIBUTES threadAttributes,
SIZE_T stackSize, ThreadStartRoutine routine,
RoutineArgs args, DWORD creationFlags,
DWORD *threadId) {
return GC_CreateThread(threadAttributes, stackSize, routine, args,
creationFlags, threadId);
}
#else
int scalanative_pthread_create(pthread_t *thread, pthread_attr_t *attr,
ThreadStartRoutine routine, RoutineArgs args) {
int scalanative_GC_pthread_create(pthread_t *thread, pthread_attr_t *attr,
ThreadStartRoutine routine,
RoutineArgs args) {
return GC_pthread_create(thread, attr, routine, args);
}
#endif
#endif // SCALANATIVE_MULTITHREADING_ENABLED

// ScalaNativeGC interface stubs. Boehm GC relies on STW using signal handlers
void scalanative_gc_set_mutator_thread_state(MutatorThreadState unused){};
void scalanative_gc_yield(){};
void scalanative_GC_set_mutator_thread_state(GC_MutatorThreadState unused){};
void scalanative_GC_yield(){};

void scalanative_add_roots(void *addr_low, void *addr_high) {
void scalanative_GC_add_roots(void *addr_low, void *addr_high) {
GC_add_roots(addr_low, addr_high);
}

void scalanative_remove_roots(void *addr_low, void *addr_high) {
void scalanative_GC_remove_roots(void *addr_low, void *addr_high) {
GC_remove_roots(addr_low, addr_high);
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ bool Allocator_newBlock(Allocator *allocator) {
fflush(stdout);
#endif
assert(block->debugFlag == dbg_partial_free);
#ifdef DEBUG_ASSERT
#ifdef GC_ASSERTIONS
block->debugFlag = dbg_in_use;
#endif
blockStart = BlockMeta_GetBlockStart(blockMetaStart,
Expand Down Expand Up @@ -254,7 +254,7 @@ NOINLINE word_t *Allocator_allocSlow(Allocator *allocator, Heap *heap,
assert(object != NULL);
memset(object, 0, size);
ObjectMeta *objectMeta = Bytemap_Get(allocator->bytemap, object);
#ifdef DEBUG_ASSERT
#ifdef GC_ASSERTIONS
ObjectMeta_AssertIsValidAllocation(objectMeta, size);
#endif
ObjectMeta_SetAllocated(objectMeta);
Expand Down Expand Up @@ -310,7 +310,7 @@ INLINE word_t *Allocator_Alloc(Heap *heap, uint32_t size) {

word_t *object = start;
ObjectMeta *objectMeta = Bytemap_Get(heap->bytemap, object);
#ifdef DEBUG_ASSERT
#ifdef GC_ASSERTIONS
ObjectMeta_AssertIsValidAllocation(objectMeta, size);
#endif
ObjectMeta_SetAllocated(objectMeta);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void BlockAllocator_Init(BlockAllocator *blockAllocator, word_t *blockMetaStart,

mutex_init(&blockAllocator->allocationLock);

#ifdef DEBUG_ASSERT
#ifdef GC_ASSERTIONS
BlockMeta *limit = sCursor + blockCount;
for (BlockMeta *current = sCursor; current < limit; current++) {
current->debugFlag = dbg_free_in_collection;
Expand Down Expand Up @@ -110,7 +110,7 @@ BlockAllocator_getFreeBlockSlow(BlockAllocator *blockAllocator) {
blockAllocator->smallestSuperblock.limit = superblock + size;
assert(BlockMeta_IsFree(superblock));
assert(superblock->debugFlag == dbg_free_in_collection);
#ifdef DEBUG_ASSERT
#ifdef GC_ASSERTIONS
superblock->debugFlag = dbg_in_use;
#endif
BlockMeta_SetFlag(superblock, block_simple);
Expand Down Expand Up @@ -140,7 +140,7 @@ BlockAllocator_getFreeBlockSlow(BlockAllocator *blockAllocator) {
if (block != NULL) {
assert(BlockMeta_IsFree(block));
assert(block->debugFlag == dbg_free_in_collection);
#ifdef DEBUG_ASSERT
#ifdef GC_ASSERTIONS
block->debugFlag = dbg_in_use;
#endif
BlockMeta_SetFlag(block, block_simple);
Expand All @@ -161,7 +161,7 @@ INLINE BlockMeta *BlockAllocator_GetFreeBlock(BlockAllocator *blockAllocator) {
block = blockAllocator->smallestSuperblock.cursor;
assert(BlockMeta_IsFree(block));
assert(block->debugFlag == dbg_free_in_collection);
#ifdef DEBUG_ASSERT
#ifdef GC_ASSERTIONS
block->debugFlag = dbg_in_use;
#endif
BlockMeta_SetFlag(block, block_simple);
Expand Down Expand Up @@ -227,7 +227,7 @@ BlockMeta *BlockAllocator_GetFreeSuperblock(BlockAllocator *blockAllocator,

assert(BlockMeta_IsFree(superblock));
assert(superblock->debugFlag == dbg_free_in_collection);
#ifdef DEBUG_ASSERT
#ifdef GC_ASSERTIONS
superblock->debugFlag = dbg_in_use;
#endif
BlockMeta_SetFlagAndSuperblockSize(superblock, block_superblock_start,
Expand All @@ -236,7 +236,7 @@ BlockMeta *BlockAllocator_GetFreeSuperblock(BlockAllocator *blockAllocator,
for (BlockMeta *current = superblock + 1; current < limit; current++) {
assert(BlockMeta_IsFree(current));
assert(current->debugFlag == dbg_free_in_collection);
#ifdef DEBUG_ASSERT
#ifdef GC_ASSERTIONS
current->debugFlag = dbg_in_use;
#endif
BlockMeta_SetFlag(current, block_superblock_tail);
Expand Down Expand Up @@ -311,7 +311,7 @@ void BlockAllocator_AddFreeSuperblockLocal(BlockAllocator *blockAllocator,
// check for double sweeping
assert(current->debugFlag == dbg_free);
BlockMeta_Clear(current);
#ifdef DEBUG_ASSERT
#ifdef GC_ASSERTIONS
current->debugFlag = dbg_free_in_collection;
#endif
}
Expand All @@ -337,7 +337,7 @@ void BlockAllocator_AddFreeSuperblock(BlockAllocator *blockAllocator,
// check for double sweeping
assert(current->debugFlag == dbg_free);
BlockMeta_Clear(current);
#ifdef DEBUG_ASSERT
#ifdef GC_ASSERTIONS
current->debugFlag = dbg_free_in_collection;
#endif
}
Expand All @@ -363,7 +363,7 @@ void BlockAllocator_AddFreeBlocks(BlockAllocator *blockAllocator,
assert(current->debugFlag == dbg_free);
assert(!BlockMeta_IsSuperblockStartMe(current));
BlockMeta_Clear(current);
#ifdef DEBUG_ASSERT
#ifdef GC_ASSERTIONS
current->debugFlag = dbg_free_in_collection;
#endif
}
Expand Down Expand Up @@ -425,7 +425,7 @@ void BlockAllocator_ReserveBlocks(BlockAllocator *blockAllocator) {
}
}

#ifdef DEBUG_ASSERT
#ifdef GC_ASSERTIONS
BlockMeta *limit = superblock + SWEEP_RESERVE_BLOCKS;
for (BlockMeta *current = superblock; current < limit; current++) {
assert(BlockMeta_IsFree(current));
Expand Down

0 comments on commit 8655125

Please sign in to comment.