Merge from vendor branch LIBPCAP:
[dragonfly.git] / contrib / gperf / src / new.cc
1 /* Defines a buffered memory allocation abstraction that reduces calls to
2    malloc.
3    Copyright (C) 1989-1998 Free Software Foundation, Inc.
4    written by Douglas C. Schmidt (schmidt@ics.uci.edu)
5
6 This file is part of GNU GPERF.
7
8 GNU GPERF is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 1, or (at your option)
11 any later version.
12
13 GNU GPERF is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU GPERF; see the file COPYING.  If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111, USA.  */
21
22 #include "config.h"
23 #include <stdio.h>
24 #include <stdlib.h> /* declares malloc(), exit() */
25 #include "trace.h"
26
27 /* Determine default alignment.  If your C++ compiler does not
28    like this then try something like #define DEFAULT_ALIGNMENT 8. */
29 struct fooalign {char x; double d;};
30 const int ALIGNMENT = ((char *)&((struct fooalign *) 0)->d - (char *)0);
31
32 /* Provide an abstraction that cuts down on the number of
33    calls to NEW by buffering the memory pool from which
34    strings are allocated. */
35
36 void *
37 operator new (size_t size)
38 {
39   T (Trace t ("operator new");)
40   static char *buf_start = 0;          /* Large array used to reduce calls to NEW. */
41   static char *buf_end = 0;            /* Indicates end of BUF_START. */
42   static size_t buf_size = 4096;       /* Size of buffer pointed to by BUF_START. */
43          char *temp;
44
45   /* Align this on correct boundaries, just to be safe... */
46   size = ((size + ALIGNMENT - 1) / ALIGNMENT) * ALIGNMENT;
47
48   /* If we are about to overflow our buffer we'll just grab another
49      chunk of memory.  Since we never free the original memory it
50      doesn't matter that no one points to the beginning of that
51      chunk. Note we use a heuristic that grows the buffer either by
52      size of the request or by twice the previous size, whichever is
53      larger. */
54
55   if (buf_start + size >= buf_end)
56     {
57       buf_size *= 2;
58       if (buf_size < size)
59         buf_size = size;
60       if ((buf_start = (char *)malloc (buf_size)) != (char *)0)
61         buf_end = buf_start + buf_size;
62       else
63         {
64           fprintf (stderr, "Virtual memory exhausted in `operator new'\n");
65           exit (1);
66         }
67     }
68
69   temp = buf_start;
70   buf_start += size;
71   return temp;
72 }
73
74 /* We need this deletion operator in order to make the linker happy.
75    Because `operator new' and `operator delete' always come together.  */
76
77 void
78 operator delete (void *ptr)
79 #ifdef HAVE_THROW_DECL
80   throw()
81 #endif
82 {
83   T (Trace t ("operator delete");)
84   // We cannot call free here, as it doesn't match the mallocs.
85   // free ((char *) ptr);
86   (void) ptr;
87 }