1010#elif defined(PLATFORM_LINUX)
1111 #include < cstring>
1212 #include < sys/resource.h>
13- #include < sys/sysinfo.h>
13+ #if defined(PLATFORM_EMSCRIPTEN)
14+ #include < malloc.h>
15+ #include < emscripten/heap.h>
16+ #include < emscripten/stack.h>
17+ #else
18+ #include < sys/sysinfo.h>
19+ #endif // defined(PLATFORM_EMSCRIPTEN)
1420 #include < unistd.h>
1521#elif defined(PLATFORM_MACOS)
1622 // #include <sys/types.h>
2228 // #include <mach/mach_host.h>
2329#else
2430 #error "Compilation platform could not be determined. Make sure platform_defines.h is included and up to date."
25- #endif // defined
31+ #endif
2632
2733namespace memorystorm {
2834
@@ -33,14 +39,14 @@ uint64_t get_stack_available() {
3339 VirtualQuery ((PVOID)&mbi, &mbi, sizeof (mbi)); // get range
3440 return (UINT_PTR)&mbi - (UINT_PTR)mbi.AllocationBase ; // subtract from top (stack grows downward on win)
3541 #elif defined(PLATFORM_LINUX) || defined(PLATFORM_MACOS)
36- #if defined(__EMSCRIPTEN__ )
37- return 0 ;
42+ #if defined(PLATFORM_EMSCRIPTEN )
43+ return emscripten_stack_get_free () ;
3844 #else
3945 rlimit limit; // hard limit and soft limit
4046 getrlimit (RLIMIT_STACK, &limit); // stack size availability
4147 return std::min (limit.rlim_cur , limit.rlim_max ); // return the smallest of the two
42- #endif // defined
43- #endif // defined
48+ #endif // defined(PLATFORM_EMSCRIPTEN)
49+ #endif
4450}
4551
4652uint64_t get_physical_total () {
@@ -51,8 +57,8 @@ uint64_t get_physical_total() {
5157 GlobalMemoryStatusEx (&status);
5258 return cast_if_required<uint64_t >(status.ullTotalPhys ); // physical memory
5359 #elif defined(PLATFORM_LINUX)
54- #if defined(__EMSCRIPTEN__ )
55- return 0 ;
60+ #if defined(PLATFORM_EMSCRIPTEN )
61+ return emscripten_get_heap_max () ;
5662 #else
5763 // uint64_t pages = sysconf(_SC_PHYS_PAGES);
5864 // uint64_t page_size = sysconf(_SC_PAGE_SIZE);
@@ -63,7 +69,7 @@ uint64_t get_physical_total() {
6369 uint64_t result = info.totalram ;
6470 result *= info.mem_unit ; // don't collapse this to avoid int overflow on rhs
6571 return result;
66- #endif // defined
72+ #endif // defined(PLATFORM_EMSCRIPTEN)
6773 #elif defined(PLATFORM_MACOS)
6874 int64_t result;
6975 size_t length = sizeof (result);
@@ -72,7 +78,7 @@ uint64_t get_physical_total() {
7278 mib[1 ] = HW_MEMSIZE;
7379 sysctl (mib, 2 , &result, &length, NULL , 0 );
7480 return result;
75- #endif // defined
81+ #endif
7682}
7783uint64_t get_physical_available () {
7884 // / Fetch the available memory of the system
@@ -82,15 +88,16 @@ uint64_t get_physical_available() {
8288 GlobalMemoryStatusEx (&status);
8389 return cast_if_required<uint64_t >(status.ullAvailPhys ); // physical memory
8490 #elif defined(PLATFORM_LINUX)
85- #if defined(__EMSCRIPTEN__)
86- return 0 ;
91+ #if defined(PLATFORM_EMSCRIPTEN)
92+ // return static_cast<uint64_t>(mallinfo().fordblks);
93+ return get_physical_total () - get_physical_usage ();
8794 #else
8895 struct sysinfo info;
8996 sysinfo (&info);
9097 uint64_t totalVirtualMem = info.freeram ;
9198 totalVirtualMem *= info.mem_unit ; // don't collapse this to avoid int overflow on rhs
9299 return totalVirtualMem;
93- #endif // defined
100+ #endif // defined(PLATFORM_EMSCRIPTEN)
94101 #elif defined(PLATFORM_MACOS)
95102 mach_port_t mach_port;
96103 mach_port = mach_host_self ();
@@ -103,7 +110,7 @@ uint64_t get_physical_available() {
103110 } else {
104111 return 0 ;
105112 }
106- #endif // defined
113+ #endif
107114}
108115uint64_t get_physical_usage () {
109116 // / Fetch the memory used by this process
@@ -112,13 +119,13 @@ uint64_t get_physical_usage() {
112119 GetProcessMemoryInfo (GetCurrentProcess (), &counters, sizeof (counters));
113120 return counters.WorkingSetSize ; // physical memory used
114121 #elif defined(PLATFORM_LINUX)
115- #if defined(__EMSCRIPTEN__ )
116- return 0 ;
122+ #if defined(PLATFORM_EMSCRIPTEN )
123+ return static_cast < uint64_t >( mallinfo (). uordblks ) ;
117124 #else
118125 FILE *file = fopen (" /proc/self/status" , " r" );
119126 uint64_t result = 0 ;
120127 char line[128 ];
121- while (fgets (line, 128 , file) != NULL ) {
128+ while (fgets (line, 128 , file) != nullptr ) {
122129 if (strncmp (line, " VmRSS:" , 6 ) == 0 ) {
123130 char *templine = line;
124131 size_t const length = strlen (templine);
@@ -132,15 +139,15 @@ uint64_t get_physical_usage() {
132139 }
133140 fclose (file);
134141 return result;
135- #endif // defined
142+ #endif // defined(PLATFORM_EMSCRIPTEN)
136143 #elif defined(PLATFORM_MACOS)
137144 struct task_basic_info info;
138145 mach_msg_type_number_t info_count = TASK_BASIC_INFO_COUNT;
139146 if (task_info (mach_task_self (), TASK_BASIC_INFO, (task_info_t )&info, &info_count) != KERN_SUCCESS) {
140147 return 0 ;
141148 }
142149 return info.resident_size ;
143- #endif // defined
150+ #endif
144151}
145152
146153uint64_t get_virtual_total () {
@@ -151,24 +158,24 @@ uint64_t get_virtual_total() {
151158 GlobalMemoryStatusEx (&status);
152159 return cast_if_required<uint64_t >(status.ullTotalPageFile ); // total virtual memory (including swapfiles)
153160 #elif defined(PLATFORM_LINUX)
154- #if defined(__EMSCRIPTEN__ )
155- return 0 ;
161+ #if defined(PLATFORM_EMSCRIPTEN )
162+ return emscripten_get_heap_max () ;
156163 #else
157164 struct sysinfo info;
158165 sysinfo (&info);
159166 uint64_t result = info.totalram ;
160167 result += info.totalswap ; // Add other values in next statement to avoid int overflow on right hand side...
161168 result *= info.mem_unit ;
162169 return result;
163- #endif // defined
170+ #endif // defined(PLATFORM_EMSCRIPTEN)
164171 #elif defined(PLATFORM_MACOS)
165172 xsw_usage xsu = {0 , 0 , 0 , 0 , 0 };
166173 size_t size = sizeof (xsu);
167174 if (sysctlbyname (" vm.swapusage" , &xsu, &size, NULL , 0 ) != 0 ) {
168175 perror (" unable to get swap usage by calling sysctlbyname(\" vm.swapusage\" ,...)" );
169176 }
170177 return xsu.xsu_total ;
171- #endif // defined
178+ #endif
172179}
173180uint64_t get_virtual_available () {
174181 // / Fetch the available memory of the system
@@ -178,24 +185,25 @@ uint64_t get_virtual_available() {
178185 GlobalMemoryStatusEx (&status);
179186 return cast_if_required<uint64_t >(status.ullAvailPageFile ); // available virtual memory (including swapfiles)
180187 #elif defined(PLATFORM_LINUX)
181- #if defined(__EMSCRIPTEN__)
182- return 0 ;
188+ #if defined(PLATFORM_EMSCRIPTEN)
189+ // return emscripten_get_heap_size();
190+ return get_virtual_total () - get_virtual_usage ();
183191 #else
184192 struct sysinfo info;
185193 sysinfo (&info);
186194 uint64_t result = info.freeram ;
187195 result += info.freeswap ; // Add other values in next statement to avoid int overflow on right hand side...
188196 result *= info.mem_unit ;
189197 return result;
190- #endif // defined
198+ #endif // defined(PLATFORM_EMSCRIPTEN)
191199 #elif defined(PLATFORM_MACOS)
192200 xsw_usage xsu = {0 , 0 , 0 , 0 , 0 };
193201 size_t size = sizeof (xsu);
194202 if (sysctlbyname (" vm.swapusage" , &xsu, &size, NULL , 0 ) != 0 ) {
195203 perror (" unable to get swap usage by calling sysctlbyname(\" vm.swapusage\" ,...)" );
196204 }
197205 return xsu.xsu_avail ;
198- #endif // defined
206+ #endif
199207}
200208uint64_t get_virtual_usage () {
201209 // / Fetch the memory used by this process
@@ -206,13 +214,13 @@ uint64_t get_virtual_usage() {
206214 GetProcessMemoryInfo (GetCurrentProcess (), (PROCESS_MEMORY_COUNTERS*)&counters, sizeof (counters));
207215 return counters.PrivateUsage ; // virtual memory used
208216 #elif defined(PLATFORM_LINUX)
209- #if defined(__EMSCRIPTEN__ )
210- return 0 ;
217+ #if defined(PLATFORM_EMSCRIPTEN )
218+ return static_cast < uint64_t >( mallinfo (). uordblks ) ;
211219 #else
212220 FILE *file = fopen (" /proc/self/status" , " r" );
213221 uint64_t result = 0 ;
214222 char line[128 ];
215- while (fgets (line, 128 , file) != NULL ) {
223+ while (fgets (line, 128 , file) != nullptr ) {
216224 if (strncmp (line, " VmSize:" , 7 ) == 0 ) {
217225 char *templine = line;
218226 size_t const length = strlen (templine);
@@ -225,15 +233,15 @@ uint64_t get_virtual_usage() {
225233 }
226234 fclose (file);
227235 return result;
228- #endif // defined
236+ #endif // defined(PLATFORM_EMSCRIPTEN)
229237 #elif defined(PLATFORM_MACOS)
230238 struct task_basic_info info;
231239 mach_msg_type_number_t info_count = TASK_BASIC_INFO_COUNT;
232240 if (task_info (mach_task_self (), TASK_BASIC_INFO, (task_info_t )&info, &info_count) != KERN_SUCCESS) {
233241 return 0 ;
234242 }
235243 return info.virtual_size ;
236- #endif // defined
244+ #endif
237245}
238246
239247std::string human_readable (uint64_t amount) {
0 commit comments