Skip to content

Commit

Permalink
Fix an old crash that happen randomly especially at startup
Browse files Browse the repository at this point in the history
Using lock_guard to let the compiler decide when its time to unlock seem to fix the issue.
  • Loading branch information
cyberium committed Sep 7, 2023
1 parent ef565dd commit 2fa08e3
Showing 1 changed file with 52 additions and 59 deletions.
111 changes: 52 additions & 59 deletions dep/g3dlite/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -987,13 +987,6 @@ class BufferPool {

std::mutex m_lock;

void lock() {
m_lock.lock();
}

void unlock() {
m_lock.unlock();
}

#if 0 //-----------------------------------------------old mutex
# ifdef G3D_WIN32
Expand Down Expand Up @@ -1222,50 +1215,49 @@ class BufferPool {


UserPtr malloc(size_t bytes) {
lock();
++totalMallocs;
{
std::lock_guard lock(m_lock);
++totalMallocs;

if (bytes <= tinyBufferSize) {
if (bytes <= tinyBufferSize) {

UserPtr ptr = tinyMalloc(bytes);
UserPtr ptr = tinyMalloc(bytes);

if (ptr) {
++mallocsFromTinyPool;
return ptr;
}

if (ptr) {
++mallocsFromTinyPool;
unlock();
return ptr;
}

}
// Failure to allocate a tiny buffer is allowed to flow
// through to a small buffer
if (bytes <= smallBufferSize) {

// Failure to allocate a tiny buffer is allowed to flow
// through to a small buffer
if (bytes <= smallBufferSize) {
UserPtr ptr = malloc(smallPool, smallPoolSize, bytes);

UserPtr ptr = malloc(smallPool, smallPoolSize, bytes);
if (ptr) {
++mallocsFromSmallPool;
return ptr;
}

if (ptr) {
++mallocsFromSmallPool;
unlock();
return ptr;
}
else if (bytes <= medBufferSize) {
// Note that a small allocation failure does *not* fall
// through into a medium allocation because that would
// waste the medium buffer's resources.

} else if (bytes <= medBufferSize) {
// Note that a small allocation failure does *not* fall
// through into a medium allocation because that would
// waste the medium buffer's resources.
UserPtr ptr = malloc(medPool, medPoolSize, bytes);

UserPtr ptr = malloc(medPool, medPoolSize, bytes);

if (ptr) {
++mallocsFromMedPool;
unlock();
debugAssertM(ptr != NULL, "BufferPool::malloc returned NULL");
return ptr;
if (ptr) {
++mallocsFromMedPool;
debugAssertM(ptr != NULL, "BufferPool::malloc returned NULL");
return ptr;
}
}
}

bytesAllocated += USERSIZE_TO_REALSIZE(bytes);
unlock();
bytesAllocated += USERSIZE_TO_REALSIZE(bytes);
}

// Heap allocate

Expand Down Expand Up @@ -1318,35 +1310,36 @@ class BufferPool {
assert(isValidPointer(ptr));

if (inTinyHeap(ptr)) {
lock();
tinyFree(ptr);
unlock();
{
std::lock_guard lock(m_lock);
tinyFree(ptr);
}
return;
}

uint32 bytes = USERSIZE_FROM_USERPTR(ptr);

lock();
if (bytes <= smallBufferSize) {
if (smallPoolSize < maxSmallBuffers) {
smallPool[smallPoolSize] = MemBlock(ptr, bytes);
++smallPoolSize;
unlock();
return;
{
std::lock_guard lock(m_lock);
if (bytes <= smallBufferSize) {
if (smallPoolSize < maxSmallBuffers) {
smallPool[smallPoolSize] = MemBlock(ptr, bytes);
++smallPoolSize;
return;
}
}
} else if (bytes <= medBufferSize) {
if (medPoolSize < maxMedBuffers) {
medPool[medPoolSize] = MemBlock(ptr, bytes);
++medPoolSize;
unlock();
return;
else if (bytes <= medBufferSize) {
if (medPoolSize < maxMedBuffers) {
medPool[medPoolSize] = MemBlock(ptr, bytes);
++medPoolSize;
return;
}
bytesAllocated -= USERSIZE_TO_REALSIZE(bytes);

// Free; the buffer pools are full or this is too big to store.
::free(USERPTR_TO_REALPTR(ptr));
}
}
bytesAllocated -= USERSIZE_TO_REALSIZE(bytes);
unlock();

// Free; the buffer pools are full or this is too big to store.
::free(USERPTR_TO_REALPTR(ptr));
}

std::string performance() const {
Expand Down

0 comments on commit 2fa08e3

Please sign in to comment.