home *** CD-ROM | disk | FTP | other *** search
/ GEMini Atari / GEMini_Atari_CD-ROM_Walnut_Creek_December_1993.iso / zip / gnu / gperf.lzh / GPERF / NEW.CC < prev    next >
C/C++ Source or Header  |  1993-07-30  |  3KB  |  77 lines

  1. /* Defines a buffered memory allocation abstraction that reduces calls to
  2.    malloc.
  3.    Copyright (C) 1989 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
  20. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include <stdio.h>
  23. #include <std.h>
  24. #include "std-err.h"
  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 (long 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 int   buf_size = 4 * BUFSIZ; /* 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_start = malloc (buf_size >?= size))
  59.         buf_end = buf_start + buf_size;
  60.       else
  61.         Std_Err::report_error ("Virtual memory failed at %s, %s in function %s\n%a", __FILE__, __LINE__, "operator new", 1);
  62.     }
  63.  
  64.   temp = buf_start;
  65.   buf_start += size;
  66.   return temp;
  67. }
  68.  
  69. /* We need this deletion operator in order to make the linker happy. */
  70.  
  71. void 
  72. operator delete (void *ptr)
  73. {
  74.   T (Trace t ("operator delete");)
  75.   free ((char *) ptr);
  76. }
  77.