new.cpp revision 151498
1208926Smjacob/* Copyright (C) 1989, 1990, 1991, 1992, 2001, 2003, 2004 2208926Smjacob Free Software Foundation, Inc. 3208926Smjacob Written by James Clark (jjc@jclark.com) 4208926Smjacob 5208926SmjacobThis file is part of groff. 6208926Smjacob 7208926Smjacobgroff is free software; you can redistribute it and/or modify it under 8208926Smjacobthe terms of the GNU General Public License as published by the Free 9208926SmjacobSoftware Foundation; either version 2, or (at your option) any later 10208926Smjacobversion. 11208926Smjacob 12208926Smjacobgroff is distributed in the hope that it will be useful, but WITHOUT ANY 13208926SmjacobWARRANTY; without even the implied warranty of MERCHANTABILITY or 14208926SmjacobFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15208926Smjacobfor more details. 16208926Smjacob 17208926SmjacobYou should have received a copy of the GNU General Public License along 18208926Smjacobwith groff; see the file COPYING. If not, write to the Free Software 19208926SmjacobFoundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */ 20208926Smjacob 21208926Smjacob#include "lib.h" 22208926Smjacob 23208926Smjacob#include <stddef.h> 24208926Smjacob#include <stdlib.h> 25208926Smjacob 26208926Smjacob#include "posix.h" 27208926Smjacob#include "nonposix.h" 28208926Smjacob 29208926Smjacobextern "C" const char *program_name; 30208926Smjacob 31208926Smjacobstatic void ewrite(const char *s) 32208926Smjacob{ 33208926Smjacob write(2, s, strlen(s)); 34208926Smjacob} 35208926Smjacob 36208926Smjacobvoid *operator new(size_t size) 37208926Smjacob{ 38208926Smjacob // Avoid relying on the behaviour of malloc(0). 39208926Smjacob if (size == 0) 40208926Smjacob size++; 41208926Smjacob#ifdef COOKIE_BUG 42208926Smjacob char *p = (char *)malloc(unsigned(size + 8)); 43208926Smjacob#else /* not COOKIE_BUG */ 44208926Smjacob char *p = (char *)malloc(unsigned(size)); 45208926Smjacob#endif /* not COOKIE_BUG */ 46208926Smjacob if (p == 0) { 47208926Smjacob if (program_name) { 48208926Smjacob ewrite(program_name); 49208926Smjacob ewrite(": "); 50208926Smjacob } 51208926Smjacob ewrite("out of memory\n"); 52208926Smjacob _exit(-1); 53208926Smjacob } 54208926Smjacob#ifdef COOKIE_BUG 55208926Smjacob ((unsigned *)p)[1] = 0; 56208926Smjacob return p + 8; 57208926Smjacob#else /* not COOKIE_BUG */ 58208926Smjacob return p; 59208926Smjacob#endif /* not COOKIE_BUG */ 60208926Smjacob} 61208926Smjacob 62208926Smjacobvoid operator delete(void *p) 63208926Smjacob{ 64208926Smjacob#ifdef COOKIE_BUG 65208926Smjacob if (p) 66208926Smjacob free((void *)((char *)p - 8)); 67208926Smjacob#else 68208926Smjacob if (p) 69208926Smjacob free(p); 70208926Smjacob#endif /* COOKIE_BUG */ 71208926Smjacob} 72208926Smjacob