Skip to content

Commit

Permalink
Merge pull request #2262 from WalterBright/never-delete
Browse files Browse the repository at this point in the history
new, but never delete
  • Loading branch information
Don Clugston committed Jun 28, 2013
2 parents ab3ae03 + 2eeefa3 commit 19a9003
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion src/root/rmem.c
Expand Up @@ -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);
Expand All @@ -153,4 +204,4 @@ void operator delete(void *p)
free(p);
}


#endif

0 comments on commit 19a9003

Please sign in to comment.