Skip to content

Commit

Permalink
Fix HXCPP_GC_GENERATIONAL in combination with HXCPP_ALIGN_ALLOC
Browse files Browse the repository at this point in the history
  • Loading branch information
hughsando committed Sep 17, 2024
1 parent 61f3911 commit 8e19a09
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 95 deletions.
81 changes: 46 additions & 35 deletions include/hx/GC.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,12 @@ class ImmixAllocator
{
#ifdef HXCPP_GC_NURSERY

#ifdef HXCPP_ALIGN_ALLOC
// make sure buffer is 8-byte aligned
unsigned char *buffer = alloc->spaceFirst + ( (size_t)alloc->spaceFirst & 4 );
#else
unsigned char *buffer = alloc->spaceFirst;
#endif
unsigned char *end = buffer + (inSize + 4);

if ( end > alloc->spaceOversize )
Expand All @@ -381,48 +386,54 @@ class ImmixAllocator
((unsigned int *)buffer)[-1] = inSize;
}

#if defined(HXCPP_GC_CHECK_POINTER) && defined(HXCPP_GC_DEBUG_ALWAYS_MOVE)
hx::GCOnNewPointer(buffer);
#endif

#ifdef HXCPP_TELEMETRY
__hxt_gc_new((hx::StackContext *)alloc,buffer, inSize, inName);
#endif

return buffer;

#else
#ifndef HXCPP_ALIGN_ALLOC
// Inline the fast-path if we can
// We know the object can hold a pointer (vtable) and that the size is int-aligned
int start = alloc->spaceStart;
int end = start + sizeof(int) + inSize;

if ( end <= alloc->spaceEnd )
{
alloc->spaceStart = end;

unsigned int *buffer = (unsigned int *)(alloc->allocBase + start);

int startRow = start>>IMMIX_LINE_BITS;

alloc->allocStartFlags[ startRow ] |= gImmixStartFlag[start&127];

if (inContainer)
*buffer++ = (( (end+(IMMIX_LINE_LEN-1))>>IMMIX_LINE_BITS) -startRow) |
(inSize<<IMMIX_ALLOC_SIZE_SHIFT) |
hx::gMarkIDWithContainer;
else
*buffer++ = (( (end+(IMMIX_LINE_LEN-1))>>IMMIX_LINE_BITS) -startRow) |
(inSize<<IMMIX_ALLOC_SIZE_SHIFT) |
hx::gMarkID;

#if defined(HXCPP_GC_CHECK_POINTER) && defined(HXCPP_GC_DEBUG_ALWAYS_MOVE)
hx::GCOnNewPointer(buffer);
#endif

#ifdef HXCPP_TELEMETRY
__hxt_gc_new((hx::StackContext *)alloc,buffer, inSize, inName);
#endif
return buffer;
}
#endif // HXCPP_ALIGN_ALLOC
// Inline the fast-path if we can
// We know the object can hold a pointer (vtable) and that the size is int-aligned
int start = alloc->spaceStart;
#ifdef HXCPP_ALIGN_ALLOC
// Ensure odd alignment in 8 bytes
start += 4 - (start & 4);
#endif
int end = start + sizeof(int) + inSize;

if ( end <= alloc->spaceEnd )
{
alloc->spaceStart = end;

unsigned int *buffer = (unsigned int *)(alloc->allocBase + start);

int startRow = start>>IMMIX_LINE_BITS;

alloc->allocStartFlags[ startRow ] |= gImmixStartFlag[start&127];

if (inContainer)
*buffer++ = (( (end+(IMMIX_LINE_LEN-1))>>IMMIX_LINE_BITS) -startRow) |
(inSize<<IMMIX_ALLOC_SIZE_SHIFT) |
hx::gMarkIDWithContainer;
else
*buffer++ = (( (end+(IMMIX_LINE_LEN-1))>>IMMIX_LINE_BITS) -startRow) |
(inSize<<IMMIX_ALLOC_SIZE_SHIFT) |
hx::gMarkID;

#if defined(HXCPP_GC_CHECK_POINTER) && defined(HXCPP_GC_DEBUG_ALWAYS_MOVE)
hx::GCOnNewPointer(buffer);
#endif

#ifdef HXCPP_TELEMETRY
__hxt_gc_new((hx::StackContext *)alloc,buffer, inSize, inName);
#endif
return buffer;
}

// Fall back to external method
void *result = alloc->CallAlloc(inSize, inContainer ? IMMIX_ALLOC_IS_CONTAINER : 0);
Expand Down
2 changes: 1 addition & 1 deletion include/hxcpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

// Must allign allocs to 8 bytes to match floating point requirement?
// Ints must br read on 4-byte boundary
#if defined(EMSCRIPTEN) || defined(GCW0)
#if (!defined(HXCPP_ALIGN_FLOAT) && (defined(EMSCRIPTEN) || defined(GCW0)) )
#define HXCPP_ALIGN_ALLOC
#endif

Expand Down
Loading

0 comments on commit 8e19a09

Please sign in to comment.