1/*	$NetBSD$	*/
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
7This file is part of groff.
8
9groff is free software; you can redistribute it and/or modify it under
10the terms of the GNU General Public License as published by the Free
11Software Foundation; either version 2, or (at your option) any later
12version.
13
14groff is distributed in the hope that it will be useful, but WITHOUT ANY
15WARRANTY; without even the implied warranty of MERCHANTABILITY or
16FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
17for more details.
18
19You should have received a copy of the GNU General Public License along
20with groff; see the file COPYING.  If not, write to the Free Software
21Foundation, 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
31extern "C" const char *program_name;
32
33static void ewrite(const char *s)
34{
35  write(2, s, strlen(s));
36}
37
38void *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
64void 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