xref: /netbsd-src/external/gpl2/groff/dist/src/libs/libgroff/new.cpp (revision 89a07cf815a29524268025a1139fac4c5190f765)
1 /*	$NetBSD: new.cpp,v 1.1.1.1 2016/01/13 18:41:48 christos Exp $	*/
2 
3 /* Copyright (C) 1989, 1990, 1991, 1992, 2001, 2003, 2004
4    Free Software Foundation, Inc.
5      Written by James Clark (jjc@jclark.com)
6 
7 This file is part of groff.
8 
9 groff is free software; you can redistribute it and/or modify it under
10 the terms of the GNU General Public License as published by the Free
11 Software Foundation; either version 2, or (at your option) any later
12 version.
13 
14 groff is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17 for more details.
18 
19 You should have received a copy of the GNU General Public License along
20 with groff; see the file COPYING.  If not, write to the Free Software
21 Foundation, 51 Franklin St - Fifth Floor, Boston, MA 02110-1301, USA. */
22 
23 #include "lib.h"
24 
25 #include <stddef.h>
26 #include <stdlib.h>
27 
28 #include "posix.h"
29 #include "nonposix.h"
30 
31 extern "C" const char *program_name;
32 
ewrite(const char * s)33 static void ewrite(const char *s)
34 {
35   write(2, s, strlen(s));
36 }
37 
operator new(size_t size)38 void *operator new(size_t size)
39 {
40   // Avoid relying on the behaviour of malloc(0).
41   if (size == 0)
42     size++;
43 #ifdef COOKIE_BUG
44   char *p = (char *)malloc(unsigned(size + 8));
45 #else /* not COOKIE_BUG */
46   char *p = (char *)malloc(unsigned(size));
47 #endif /* not COOKIE_BUG */
48   if (p == 0) {
49     if (program_name) {
50       ewrite(program_name);
51       ewrite(": ");
52     }
53     ewrite("out of memory\n");
54     _exit(-1);
55   }
56 #ifdef COOKIE_BUG
57   ((unsigned *)p)[1] = 0;
58   return p + 8;
59 #else /* not COOKIE_BUG */
60   return p;
61 #endif /* not COOKIE_BUG */
62 }
63 
operator delete(void * p)64 void operator delete(void *p)
65 {
66 #ifdef COOKIE_BUG
67   if (p)
68     free((void *)((char *)p - 8));
69 #else
70   if (p)
71     free(p);
72 #endif /* COOKIE_BUG */
73 }
74