Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new, but never delete #2262

Merged
merged 1 commit into from
Jun 28, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 52 additions & 1 deletion src/root/rmem.c
Original file line number Diff line number Diff line change
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no alignment considered?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops! Good catch.

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return there was because some compiler complained about no return value.

}

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