Skip to content

Commit

Permalink
Merge pull request #4535 from yebblies/rmemsplit
Browse files Browse the repository at this point in the history
[DDMD] Split C++ operator new declaration into a new file
  • Loading branch information
WalterBright committed Apr 1, 2015
2 parents 9ea13cf + 8a4fb01 commit d777143
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 43 deletions.
5 changes: 3 additions & 2 deletions src/posix.mak
Expand Up @@ -149,7 +149,8 @@ DMD_OBJS = \
ROOT_OBJS = \
rmem.o port.o man.o stringtable.o response.o \
aav.o speller.o outbuffer.o object.o \
filename.o file.o async.o checkedint.o
filename.o file.o async.o checkedint.o \
newdelete.o

GLUE_OBJS = \
glue.o msc.o s2ir.o todt.o e2ir.o tocsym.o \
Expand Down Expand Up @@ -208,7 +209,7 @@ SRC = win32.mak posix.mak osmodel.mak \
ROOT_SRC = $(ROOT)/root.h \
$(ROOT)/array.h \
$(ROOT)/rmem.h $(ROOT)/rmem.c $(ROOT)/port.h $(ROOT)/port.c \
$(ROOT)/man.c \
$(ROOT)/man.c $(ROOT)/newdelete.c \
$(ROOT)/checkedint.h $(ROOT)/checkedint.c \
$(ROOT)/stringtable.h $(ROOT)/stringtable.c \
$(ROOT)/response.c $(ROOT)/async.h $(ROOT)/async.c \
Expand Down
54 changes: 54 additions & 0 deletions src/root/newdelete.c
@@ -0,0 +1,54 @@

/* Copyright (c) 2000-2014 by Digital Mars
* All Rights Reserved, written by Walter Bright
* http://www.digitalmars.com
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
* https://github.com/D-Programming-Language/dmd/blob/master/src/root/rmem.c
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined(__has_feature)
#if __has_feature(address_sanitizer)
#define USE_ASAN_NEW_DELETE
#endif
#endif

#if !defined(USE_ASAN_NEW_DELETE)

#if 1

void *allocmemory(size_t m_size);

void * operator new(size_t m_size)
{
return allocmemory(m_size);
}

void operator delete(void *p)
{
}

#else

void * operator new(size_t m_size)
{
void *p = malloc(m_size);
if (p)
return p;
printf("Error: out of memory\n");
exit(EXIT_FAILURE);
return p;
}

void operator delete(void *p)
{
free(p);
}

#endif

#endif
37 changes: 1 addition & 36 deletions src/root/rmem.c
Expand Up @@ -117,16 +117,6 @@ void Mem::error()

/* =================================================== */

#if defined(__has_feature)
#if __has_feature(address_sanitizer)
#define USE_ASAN_NEW_DELETE
#endif
#endif

#if !defined(USE_ASAN_NEW_DELETE)

#if 1

/* Allocate, but never release
*/

Expand All @@ -137,7 +127,7 @@ void Mem::error()
static size_t heapleft = 0;
static void *heapp;

void * operator new(size_t m_size)
void *allocmemory(size_t m_size)
{
// 16 byte alignment is better (and sometimes needed) for doubles
m_size = (m_size + 15) & ~15;
Expand Down Expand Up @@ -171,28 +161,3 @@ void * operator new(size_t m_size)
}
goto L1;
}

void operator delete(void *p)
{
}

#else

void * operator new(size_t m_size)
{
void *p = malloc(m_size);
if (p)
return p;
printf("Error: out of memory\n");
exit(EXIT_FAILURE);
return p;
}

void operator delete(void *p)
{
free(p);
}

#endif

#endif
10 changes: 5 additions & 5 deletions src/win32.mak
Expand Up @@ -167,13 +167,10 @@ BACKOBJ= go.obj gdag.obj gother.obj gflow.obj gloop.obj var.obj el.obj \


# Root package
GCOBJS=rmem.obj
# Removed garbage collector (look in history)
#GCOBJS=dmgcmem.obj bits.obj win32.obj gc.obj
ROOTOBJS= man.obj port.obj checkedint.obj \
stringtable.obj response.obj async.obj speller.obj aav.obj outbuffer.obj \
object.obj filename.obj file.obj \
$(GCOBJS)
rmem.obj newdelete.obj

# D front end
SRCS= mars.c enum.c struct.c dsymbol.c import.c idgen.d impcnvgen.c utf.h \
Expand Down Expand Up @@ -232,7 +229,7 @@ TKSRC= $(TK)\filespec.h $(TK)\mem.h $(TK)\list.h $(TK)\vec.h $(TKSRCC)
ROOTSRCC=$(ROOT)\rmem.c $(ROOT)\stringtable.c \
$(ROOT)\man.c $(ROOT)\port.c $(ROOT)\async.c $(ROOT)\response.c \
$(ROOT)\speller.c $(ROOT)\aav.c $(ROOT)\longdouble.c \
$(ROOT)\checkedint.c \
$(ROOT)\checkedint.c $(ROOT)\newdelete.c \
$(ROOT)\outbuffer.c $(ROOT)\object.c $(ROOT)\filename.c $(ROOT)\file.c
ROOTSRC= $(ROOT)\root.h \
$(ROOT)\rmem.h $(ROOT)\port.h \
Expand Down Expand Up @@ -638,6 +635,9 @@ man.obj : $(ROOT)\man.c
rmem.obj : $(ROOT)\rmem.c
$(CC) -c $(CFLAGS) $(ROOT)\rmem.c

newdelete.obj : $(ROOT)\newdelete.c
$(CC) -c $(CFLAGS) $(ROOT)\newdelete.c

port.obj : $(ROOT)\port.c
$(CC) -c $(CFLAGS) $(ROOT)\port.c

Expand Down

0 comments on commit d777143

Please sign in to comment.