summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi.dcr@tutanota.com>2021-10-24 02:15:50 +0200
committerXavier Del Campo Romero <xavi.dcr@tutanota.com>2021-10-24 02:15:50 +0200
commit15e2ff6bb683138878c376a84882b1a6da63cb94 (patch)
tree5bac4719b3bf9a5931414a5bed8ead79fda70fe0
parent6989798d87d3e5eded4fbc2e1d19a5b40a4e8051 (diff)
Lazy-initialize heap
-rw-r--r--libpsx/src/memory.c11
-rw-r--r--libpsx/src/memory.h6
-rw-r--r--libpsx/src/setup.c6
3 files changed, 10 insertions, 13 deletions
diff --git a/libpsx/src/memory.c b/libpsx/src/memory.c
index 9b18556..1d0e6de 100644
--- a/libpsx/src/memory.c
+++ b/libpsx/src/memory.c
@@ -12,6 +12,7 @@ extern int __bss_start[];
extern int __bss_end[];
static unsigned int first_free_page;
+static int init;
// 1K granularity and "pages", so more allocations which are small can be done
@@ -22,7 +23,7 @@ static unsigned int alloc_size[2048];
// 0x80000000 - 0x8000FFFF RAM used by the BIOS
// 0x80010000 - 0x801FFFFF Program memory
-void malloc_setup()
+static void malloc_setup()
{
int x;
@@ -53,6 +54,14 @@ void malloc_setup()
void *malloc(size_t size)
{
+ if (!init)
+ {
+ // Setup memory allocation functions
+ malloc_setup();
+ dprintf("Finished setting up memory allocation functions.\n");
+ init = 1;
+ }
+
dprintf("malloc(%d)\n", size);
int x, y;
diff --git a/libpsx/src/memory.h b/libpsx/src/memory.h
deleted file mode 100644
index 39985b2..0000000
--- a/libpsx/src/memory.h
+++ /dev/null
@@ -1,6 +0,0 @@
-#ifndef _PSXSDK_MEMORY_H
-#define _PSXSDK_MEMORY_H
-
-void malloc_setup();
-
-#endif \ No newline at end of file
diff --git a/libpsx/src/setup.c b/libpsx/src/setup.c
index d77f062..1a28685 100644
--- a/libpsx/src/setup.c
+++ b/libpsx/src/setup.c
@@ -1,6 +1,5 @@
#include <psx.h>
#include <stdio.h>
-#include "memory.h"
extern int __bss_start[];
extern int __bss_end[];
@@ -38,11 +37,6 @@ void psxsdk_setup()
*((unsigned char*)x) = 0;
dprintf("BSS space cleared.\n");
-
-// Setup memory allocation functions
- malloc_setup();
-
- dprintf("Finished setting up memory allocation functions.\n");
// Call static constructors
call_ctors();