From 2eeefa35f9a1d9963e2451cd93d8e3725f540442 Mon Sep 17 00:00:00 2001 From: Walter Bright Date: Thu, 27 Jun 2013 16:35:17 -0700 Subject: [PATCH] new, but never delete --- src/root/rmem.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/src/root/rmem.c b/src/root/rmem.c index 595025ad0f40..aaae46309395 100644 --- a/src/root/rmem.c +++ b/src/root/rmem.c @@ -138,6 +138,57 @@ void Mem::addroots(char* pStart, char* pEnd) /* =================================================== */ +#if 1 + +/* Allocate, but never release + */ + +#define CHUNK_SIZE (4096 * 16) + +static size_t heapleft = 0; +static void *heapp; + +void * operator new(size_t m_size) +{ + // 16 byte alignment is better (and sometimes needed) for doubles + m_size = (m_size + 15) & ~15; + + // The layout of the code is selected so the most common case is straight through + if (m_size <= heapleft) + { + L1: + heapleft -= m_size; + void *p = heapp; + heapp = (void *)((char *)heapp + m_size); + return p; + } + + if (m_size > CHUNK_SIZE) + { + void *p = malloc(m_size); + if (p) + return p; + printf("Error: out of memory\n"); + exit(EXIT_FAILURE); + return p; + } + + heapleft = CHUNK_SIZE; + heapp = malloc(CHUNK_SIZE); + if (!heapp) + { + printf("Error: out of memory\n"); + exit(EXIT_FAILURE); + } + goto L1; +} + +void operator delete(void *p) +{ +} + +#else + void * operator new(size_t m_size) { void *p = malloc(m_size); @@ -153,4 +204,4 @@ void operator delete(void *p) free(p); } - +#endif