home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 March / PCWK3A99.iso / Linux / DDD331 / DDD-3_1_.000 / DDD-3_1_ / ddd-3.1.1 / libiberty / xmalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-30  |  3.4 KB  |  134 lines

  1. /* memory allocation routines with error checking.
  2.    Copyright 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
  3.    
  4. This file is part of the libiberty library.
  5. Libiberty is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9.  
  10. Libiberty is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. Library General Public License for more details.
  14.  
  15. You should have received a copy of the GNU Library General Public
  16. License along with libiberty; see the file COPYING.LIB.  If
  17. not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. Boston, MA 02111-1307, USA.  */
  19.  
  20. #include "ansidecl.h"
  21. #include "libiberty.h"
  22.  
  23. #include <stdio.h>
  24.  
  25. #ifdef __STDC__
  26. #include <stddef.h>
  27. #else
  28. #define size_t unsigned long
  29. #define ptrdiff_t long
  30. #endif
  31.  
  32. #if VMS
  33. #include <stdlib.h>
  34. #include <unixlib.h>
  35. #else
  36. /* For systems with larger pointers than ints, these must be declared.  */
  37. PTR malloc PARAMS ((size_t));
  38. PTR realloc PARAMS ((PTR, size_t));
  39. PTR sbrk PARAMS ((ptrdiff_t));
  40. #endif
  41.  
  42. /* The program name if set.  */
  43. static const char *name = "";
  44.  
  45. #if ! defined (_WIN32) || defined (__CYGWIN32__)
  46. /* The initial sbrk, set when the program name is set. Not used for win32
  47.    ports other than cygwin32.  */
  48. static char *first_break = NULL;
  49. #endif
  50.  
  51. void
  52. xmalloc_set_program_name (s)
  53.      const char *s;
  54. {
  55.   name = s;
  56. #if ! defined (_WIN32) || defined (__CYGWIN32__)
  57.   /* Win32 ports other than cygwin32 don't have brk() */
  58.   if (first_break == NULL)
  59.     first_break = (char *) sbrk (0);
  60. #endif /* ! _WIN32 || __CYGWIN32 __ */
  61. }
  62.  
  63. PTR
  64. xmalloc (size)
  65.     size_t size;
  66. {
  67.   PTR newmem;
  68.  
  69.   if (size == 0)
  70.     size = 1;
  71.   newmem = malloc (size);
  72.   if (!newmem)
  73.     {
  74. #if ! defined (_WIN32) || defined (__CYGWIN32__)
  75.       extern char **environ;
  76.       size_t allocated;
  77.  
  78.       if (first_break != NULL)
  79.     allocated = (char *) sbrk (0) - first_break;
  80.       else
  81.     allocated = (char *) sbrk (0) - (char *) &environ;
  82.       fprintf (stderr,
  83.            "\n%s%sCan not allocate %lu bytes after allocating %lu bytes\n",
  84.            name, *name ? ": " : "",
  85.            (unsigned long) size, (unsigned long) allocated);
  86. #else
  87.       fprintf (stderr,
  88.               "\n%s%sCan not allocate %lu bytes\n",
  89.               name, *name ? ": " : "",
  90.               (unsigned long) size);
  91. #endif /* ! _WIN32 || __CYGWIN32 __ */
  92.       xexit (1);
  93.     }
  94.   return (newmem);
  95. }
  96.  
  97. PTR
  98. xrealloc (oldmem, size)
  99.     PTR oldmem;
  100.     size_t size;
  101. {
  102.   PTR newmem;
  103.  
  104.   if (size == 0)
  105.     size = 1;
  106.   if (!oldmem)
  107.     newmem = malloc (size);
  108.   else
  109.     newmem = realloc (oldmem, size);
  110.   if (!newmem)
  111.     {
  112. #ifndef __MINGW32__
  113.       extern char **environ;
  114.       size_t allocated;
  115.  
  116.       if (first_break != NULL)
  117.     allocated = (char *) sbrk (0) - first_break;
  118.       else
  119.     allocated = (char *) sbrk (0) - (char *) &environ;
  120.       fprintf (stderr,
  121.            "\n%s%sCan not reallocate %lu bytes after allocating %lu bytes\n",
  122.            name, *name ? ": " : "",
  123.            (unsigned long) size, (unsigned long) allocated);
  124. #else
  125.       fprintf (stderr,
  126.               "\n%s%sCan not reallocate %lu bytes\n",
  127.               name, *name ? ": " : "",
  128.               (unsigned long) size);
  129. #endif /* __MINGW32__ */
  130.       xexit (1);
  131.     }
  132.   return (newmem);
  133. }
  134.