Skip to content

Commit

Permalink
Merge branch 'master' into mbedtls3
Browse files Browse the repository at this point in the history
  • Loading branch information
Aidan63 committed Oct 12, 2024
2 parents 6e15219 + 66b8f88 commit d4bd8e4
Show file tree
Hide file tree
Showing 33 changed files with 555 additions and 230 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
40 changes: 24 additions & 16 deletions include/hx/Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,28 @@ struct HxMutex
pthread_mutexattr_t mta;
pthread_mutexattr_init(&mta);
pthread_mutexattr_settype(&mta, PTHREAD_MUTEX_RECURSIVE);
mValid = pthread_mutex_init(&mMutex,&mta) ==0;
mMutex = new pthread_mutex_t();
mValid = pthread_mutex_init(mMutex,&mta) ==0;
}
~HxMutex() { if (mValid) pthread_mutex_destroy(&mMutex); }
void Lock() { pthread_mutex_lock(&mMutex); }
void Unlock() { pthread_mutex_unlock(&mMutex); }
bool TryLock() { return !pthread_mutex_trylock(&mMutex); }
~HxMutex()
{
if (mValid)
pthread_mutex_destroy(mMutex);
delete mMutex;
}
void Lock() { pthread_mutex_lock(mMutex); }
void Unlock() { pthread_mutex_unlock(mMutex); }
bool TryLock() { return !pthread_mutex_trylock(mMutex); }
bool IsValid() { return mValid; }
void Clean()
{
if (mValid)
pthread_mutex_destroy(&mMutex);
pthread_mutex_destroy(mMutex);
mValid = 0;
}

bool mValid;
pthread_mutex_t mMutex;
pthread_mutex_t *mMutex;
};

#define THREAD_FUNC_TYPE void *
Expand Down Expand Up @@ -196,13 +202,14 @@ struct HxSemaphore
{
mSet = false;
mValid = true;
pthread_cond_init(&mCondition,0);
mCondition = new pthread_cond_t();
pthread_cond_init(mCondition,0);
}
~HxSemaphore()
{
if (mValid)
{
pthread_cond_destroy(&mCondition);
pthread_cond_destroy(mCondition);
}
}
// For autolock
Expand All @@ -213,13 +220,13 @@ struct HxSemaphore
if (!mSet)
{
mSet = true;
pthread_cond_signal( &mCondition );
pthread_cond_signal( mCondition );
}
}
void QSet()
{
mSet = true;
pthread_cond_signal( &mCondition );
pthread_cond_signal( mCondition );
}
void Reset()
{
Expand All @@ -231,14 +238,14 @@ struct HxSemaphore
{
AutoLock lock(mMutex);
while( !mSet )
pthread_cond_wait( &mCondition, &mMutex.mMutex );
pthread_cond_wait( mCondition, mMutex.mMutex );
mSet = false;
}
// when we already hold the mMutex lock ...
void QWait()
{
while( !mSet )
pthread_cond_wait( &mCondition, &mMutex.mMutex );
pthread_cond_wait( mCondition, mMutex.mMutex );
mSet = false;
}
// Returns true if the wait was success, false on timeout.
Expand All @@ -262,7 +269,7 @@ struct HxSemaphore

int result = 0;
// Wait for set to be true...
while( !mSet && (result=pthread_cond_timedwait( &mCondition, &mMutex.mMutex, &spec )) != ETIMEDOUT)
while( !mSet && (result=pthread_cond_timedwait( mCondition, mMutex.mMutex, &spec )) != ETIMEDOUT)
{
if (result!=0)
{
Expand Down Expand Up @@ -290,13 +297,14 @@ struct HxSemaphore
if (mValid)
{
mValid = false;
pthread_cond_destroy(&mCondition);
pthread_cond_destroy(mCondition);
}
delete mCondition;
}


HxMutex mMutex;
pthread_cond_t mCondition;
pthread_cond_t *mCondition;
bool mSet;
bool mValid;
};
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
2 changes: 1 addition & 1 deletion src/Dynamic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class PointerData : public hx::Object
String toString()
{
char buf[100];
sprintf(buf,"Pointer(%p)", mValue);
snprintf(buf,sizeof(buf),"Pointer(%p)", mValue);
return String(buf);
}
String __ToString() const { return String(mValue); }
Expand Down
2 changes: 1 addition & 1 deletion src/hx/CFFI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Abstract_obj : public Object
return _hxcpp_toString( Dynamic(this) );

char buffer[40];
sprintf(buffer,"0x%p", mHandle);
snprintf(buffer,sizeof(buffer),"0x%p", mHandle);

return HX_CSTRING("Abstract(") +
__hxcpp_get_kind(this) +
Expand Down
7 changes: 5 additions & 2 deletions src/hx/Debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,15 @@ static void CriticalErrorHandler(String inErr, bool allowFixup)
return;
#endif

#ifdef HXCPP_STACK_TRACE
hx::StackContext *ctx = hx::StackContext::getCurrent();
ctx->beginCatch(true);
#endif

if (sCriticalErrorHandler!=null())
sCriticalErrorHandler(inErr);

#ifdef HXCPP_STACK_TRACE
hx::StackContext *ctx = hx::StackContext::getCurrent();
ctx->beginCatch(true);
ctx->dumpExceptionStack();
#endif

Expand Down
30 changes: 17 additions & 13 deletions src/hx/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -628,8 +628,8 @@ class hxCondition : public hx::Object {
CONDITION_VARIABLE cond;
#endif
#else
pthread_cond_t cond;
pthread_mutex_t mutex;
pthread_cond_t *cond;
pthread_mutex_t *mutex;
#endif
hx::InternalFinalizer *mFinalizer;
hxCondition() {
Expand All @@ -645,11 +645,13 @@ class hxCondition : public hx::Object {
#else
pthread_condattr_t cond_attr;
pthread_condattr_init(&cond_attr);
pthread_cond_init(&cond, &cond_attr);
cond = new pthread_cond_t();
pthread_cond_init(cond, &cond_attr);
pthread_condattr_destroy(&cond_attr);
pthread_mutexattr_t mutex_attr;
pthread_mutexattr_init(&mutex_attr);
pthread_mutex_init(&mutex, &mutex_attr);
mutex = new pthread_mutex_t();
pthread_mutex_init(mutex, &mutex_attr);
pthread_mutexattr_destroy(&mutex_attr);
#endif
}
Expand All @@ -667,8 +669,10 @@ class hxCondition : public hx::Object {
DeleteCriticalSection(&cond->cs);
#endif
#else
pthread_cond_destroy(&cond->cond);
pthread_mutex_destroy(&cond->mutex);
pthread_cond_destroy(cond->cond);
delete cond->cond;
pthread_mutex_destroy(cond->mutex);
delete cond->mutex;
#endif
}
}
Expand All @@ -679,7 +683,7 @@ class hxCondition : public hx::Object {
EnterCriticalSection(&cs);
#endif
#else
pthread_mutex_lock(&mutex);
pthread_mutex_lock(mutex);
#endif
}

Expand All @@ -691,7 +695,7 @@ class hxCondition : public hx::Object {
return false;
#endif
#else
return pthread_mutex_trylock(&mutex);
return pthread_mutex_trylock(mutex);
#endif
}

Expand All @@ -701,7 +705,7 @@ class hxCondition : public hx::Object {
LeaveCriticalSection(&cs);
#endif
#else
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(mutex);
#endif
}

Expand All @@ -711,7 +715,7 @@ class hxCondition : public hx::Object {
SleepConditionVariableCS(&cond,&cs,INFINITE);
#endif
#else
pthread_cond_wait(&cond, &mutex);
pthread_cond_wait(cond, mutex);
#endif
}

Expand All @@ -735,7 +739,7 @@ class hxCondition : public hx::Object {
delta -= idelta2 * 1e9;
t.tv_sec = tv.tv_sec + idelta + idelta2;
t.tv_nsec = (long)delta;
return pthread_cond_timedwait(&cond, &mutex, &t);
return pthread_cond_timedwait(cond, mutex, &t);
#endif
}
void Signal() {
Expand All @@ -744,7 +748,7 @@ class hxCondition : public hx::Object {
WakeConditionVariable(&cond);
#endif
#else
pthread_cond_signal(&cond);
pthread_cond_signal(cond);
#endif
}
void Broadcast() {
Expand All @@ -753,7 +757,7 @@ class hxCondition : public hx::Object {
WakeAllConditionVariable(&cond);
#endif
#else
pthread_cond_broadcast(&cond);
pthread_cond_broadcast(cond);
#endif
}
};
Expand Down
Loading

0 comments on commit d4bd8e4

Please sign in to comment.