Skip to content

Commit

Permalink
fix: fix memory allocation blocking on Mac (#151)
Browse files Browse the repository at this point in the history
* fix memory allocation blocking on Mac by checking the used memory against the total RAM
* Update DLL/Bundle

Others:
* Don't copy/deploy DLL/Bundle file if we are building unit tests

(cherry picked from commit 5c68824)
  • Loading branch information
sindharta authored and Sindharta Tanuwijaya committed Aug 13, 2020
1 parent 0bcebfe commit b4b9792
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Plugins~/Build/cmake_modules/AddPlugin.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ function(add_plugin name)
set_target_properties(${name} PROPERTIES PREFIX "")
endif()

if(ENABLE_DEPLOY)
# Don't deploy if we are building unit tests
if(ENABLE_DEPLOY AND NOT BUILD_UNIT_TESTS)
if(WIN32)

# Win: Visual Studio Settings
Expand Down
14 changes: 11 additions & 3 deletions Plugins~/Src/Loader/ImageMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ const uint64_t DEFAULT_MAX_MEMORY = UNLIMITED_MEMORY;

ImageMemoryAllocator::ImageMemoryAllocator() : m_usedMemory(0)
, m_maxMemory(DEFAULT_MAX_MEMORY)
, m_totalRAM(MemoryUtility::GetTotalRAM())
, m_inverseTotalRAM(1.0f / MemoryUtility::GetTotalRAM())
{
#ifdef MAX_IMAGE_MEMORY
#ifdef MAX_IMAGE_MEMORY //overwrite for testing
m_maxMemory = MAX_IMAGE_MEMORY;
#endif

Expand All @@ -35,12 +36,19 @@ bool ImageMemoryAllocator::Allocate(uint8_t ** rawDataPtr, const uint32_t w, con
if (m_maxMemory != UNLIMITED_MEMORY && (m_usedMemory + dataSize) > m_maxMemory)
return false;

const float MIN_AVAILABLE_RAM_RATIO = 0.1f;
#if OSX
//Mac (10.9+) compresses inactive memory automatically to free memory to be used by other application, and therefore
//we can't directly use the amount of free RAM returned by the OS (the value is often near zero).
//For now, we simply check if the number of used memory has exceeded the total RAM.
if (m_usedMemory + dataSize > m_totalRAM)
return false;

#else
const float MIN_AVAILABLE_RAM_RATIO = 0.1f;
const float availableRAMRatio = MemoryUtility::GetAvailableRAM() * m_inverseTotalRAM;
if (availableRAMRatio <= MIN_AVAILABLE_RAM_RATIO)
return false;

#endif

uint8_t* buffer = static_cast<uint8_t*>(malloc(dataSize));
if (nullptr == buffer) {
Expand Down
1 change: 1 addition & 0 deletions Plugins~/Src/Loader/ImageMemoryAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class ImageMemoryAllocator {

uint64_t m_usedMemory;
uint64_t m_maxMemory;
float m_totalRAM;
float m_inverseTotalRAM;

};
Expand Down
10 changes: 5 additions & 5 deletions Runtime/Plugins/OSX/Loader.bundle/Contents/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>BuildMachineOSBuild</key>
<string>18G5033</string>
<string>19G73</string>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleExecutable</key>
Expand All @@ -27,17 +27,17 @@
<key>DTCompiler</key>
<string>com.apple.compilers.llvm.clang.1_0</string>
<key>DTPlatformBuild</key>
<string>11C504</string>
<string>11E708</string>
<key>DTPlatformVersion</key>
<string>GM</string>
<key>DTSDKBuild</key>
<string>19B90</string>
<string>19G68</string>
<key>DTSDKName</key>
<string>macosx10.15</string>
<key>DTXcode</key>
<string>1131</string>
<string>1160</string>
<key>DTXcodeBuild</key>
<string>11C504</string>
<string>11E708</string>
<key>LSMinimumSystemVersion</key>
<string>10.12</string>
<key>NSHumanReadableCopyright</key>
Expand Down
Binary file modified Runtime/Plugins/OSX/Loader.bundle/Contents/MacOS/Loader
Binary file not shown.
Binary file modified Runtime/Plugins/Win64/Loader.dll
Binary file not shown.

0 comments on commit b4b9792

Please sign in to comment.