Skip to content

Commit

Permalink
Implement Ram_Free to check amout of free RAM.
Browse files Browse the repository at this point in the history
Signed-off-by: Giuliano Belinassi <[email protected]>
  • Loading branch information
giulianobelinassi committed Jul 2, 2022
1 parent 15a5c1e commit 58c3443
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
73 changes: 71 additions & 2 deletions common/alloc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@

#include "wwmem.h"

#if defined(__unix__) || defined(__unix)
#include <sys/sysinfo.h>
#include <unistd.h>
#elif defined(__APPLE__)
#include <proc.h>
#elif defined(_WIN32)
#include <windows.h>
#endif

size_t Largest_Mem_Block(void);

/*=========================================================================*/
Expand Down Expand Up @@ -219,9 +228,69 @@ void* Resize_Alloc(void* original_ptr, size_t new_size_in_bytes)
* HISTORY: *
* 09/03/1991 JLB : Commented. *
*=========================================================================*/
int Ram_Free(MemoryFlagType)
size_t Ram_Free(MemoryFlagType)
{
return (64 * 1024 * 1024);
return Ram_Free();
}

size_t Ram_Free(void)
{
#if defined(__unix__) || defined(__unix)
// Get amount of memory available to program.
return sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE);
#elif defined(__APPLE__)
return os_proc_available_memory();
#elif defined(_WIN32)
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
return status.ullAvaillPhys;
#else
// Oh dear. We are running in some kind of baremetal machine.
// Assume malloc returns NULL if there is not enough memory.
//
// Let's try to allocate a huge chunk of memory first to check if
// malloc returns NULL on absurd cases.

void* p = malloc(1 * 1024 * 1024 * 1024);
if (p) {
// Either this machine has a lot of memory, or malloc doesn't return
// NULL when out of memory. Hence, returns a safe value.
free(p);
return 128 * 1024 * 1024;
}

// There is no way other than do a binary search with malloc to check.
// which is the maximum value that it returns something valid.

size_t x1 = 128 * 1024 * 1024;
size_t x0 = 0;

// Find rightmost element;
do {
p = malloc(x1);
if (p) {
free(p);
} else {
x0 = x1;
x1 *= 2;
}
} while (p != NULL);

// Binary search a value between x0 and x1;
while (x0 < x1) {
size_t m = (x1 - x0) / 2;
p = malloc(m);
if (p) {
free(p);
x0 = m;
} else {
x1 = m;
}
}

return x1;
#endif
}

/***************************************************************************
Expand Down
3 changes: 2 additions & 1 deletion common/memflag.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ void* operator new[](size_t size, MemoryFlagType flag);
void* Alloc(size_t bytes_to_alloc, MemoryFlagType flags);
void Free(void const* pointer);
void* Resize_Alloc(void* original_ptr, size_t new_size_in_bytes);
int Ram_Free(MemoryFlagType flag);
size_t Ram_Free(MemoryFlagType flag);
size_t Ram_Free(void);
int Heap_Size(MemoryFlagType flag);
int Total_Ram_Free(MemoryFlagType flag);

Expand Down

0 comments on commit 58c3443

Please sign in to comment.