1139749Simp/* Copyright (C) 1989, 1990, 1991, 1992, 2001, 2003, 2004 2113584Ssimokawa Free Software Foundation, Inc. 3103285Sikob Written by James Clark (jjc@jclark.com) 4103285Sikob 5103285SikobThis file is part of groff. 6103285Sikob 7103285Sikobgroff is free software; you can redistribute it and/or modify it under 8103285Sikobthe terms of the GNU General Public License as published by the Free 9103285SikobSoftware Foundation; either version 2, or (at your option) any later 10103285Sikobversion. 11103285Sikob 12103285Sikobgroff is distributed in the hope that it will be useful, but WITHOUT ANY 13103285SikobWARRANTY; without even the implied warranty of MERCHANTABILITY or 14103285SikobFITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 15103285Sikobfor more details. 16103285Sikob 17106802SsimokawaYou should have received a copy of the GNU General Public License along 18103285Sikobwith groff; see the file COPYING. If not, write to the Free Software 19103285SikobFoundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */ 20103285Sikob 21103285Sikob#include "lib.h" 22103285Sikob 23103285Sikob#include <stddef.h> 24103285Sikob#include <stdlib.h> 25103285Sikob 26103285Sikob#include "posix.h" 27103285Sikob#include "nonposix.h" 28103285Sikob 29103285Sikobextern "C" const char *program_name; 30103285Sikob 31103285Sikobstatic void ewrite(const char *s) 32103285Sikob{ 33103285Sikob write(2, s, strlen(s)); 34103285Sikob} 35103285Sikob 36103285Sikobvoid *operator new(size_t size) 37106802Ssimokawa{ 38103285Sikob // Avoid relying on the behaviour of malloc(0). 39103285Sikob if (size == 0) 40103285Sikob size++; 41103285Sikob#ifdef COOKIE_BUG 42103285Sikob char *p = (char *)malloc(unsigned(size + 8)); 43103285Sikob#else /* not COOKIE_BUG */ 44103285Sikob char *p = (char *)malloc(unsigned(size)); 45103285Sikob#endif /* not COOKIE_BUG */ 46103285Sikob if (p == 0) { 47103285Sikob if (program_name) { 48103285Sikob ewrite(program_name); 49103285Sikob ewrite(": "); 50169123Ssimokawa } 51103285Sikob ewrite("out of memory\n"); 52103285Sikob _exit(-1); 53103285Sikob } 54113584Ssimokawa#ifdef COOKIE_BUG 55170374Ssimokawa ((unsigned *)p)[1] = 0; 56103285Sikob return p + 8; 57103285Sikob#else /* not COOKIE_BUG */ 58103285Sikob return p; 59127468Ssimokawa#endif /* not COOKIE_BUG */ 60117067Ssimokawa} 61117067Ssimokawa 62117067Ssimokawavoid operator delete(void *p) 63127468Ssimokawa{ 64127468Ssimokawa#ifdef COOKIE_BUG 65127468Ssimokawa if (p) 66127468Ssimokawa free((void *)((char *)p - 8)); 67127468Ssimokawa#else 68127468Ssimokawa if (p) 69127468Ssimokawa free(p); 70127468Ssimokawa#endif /* COOKIE_BUG */ 71103285Sikob} 72103285Sikob