home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / GPTX01AS.ZIP / XMALLOC.C < prev   
Encoding:
C/C++ Source or Header  |  1992-02-22  |  3.3 KB  |  164 lines

  1. /* Malloc interface for C.
  2.    Copyright (C) 1990 Free Software Foundation, Inc.
  3.    Francois Pinard <pinard@iro.umontreal.ca>, September 1989.
  4.  
  5.    $Id$
  6. */
  7.  
  8. #include <stdio.h>
  9.  
  10. #ifndef appolo
  11. #include <malloc.h>
  12. #endif /* appolo */
  13.  
  14. #ifdef __STDC__
  15. #ifdef __GNUC__
  16. void volatile abort (void);
  17. #else /* not __GNUC__ */
  18. void abort (void);
  19. #endif /* not __GNUC__ */
  20. #else /* not __STDC__ */
  21. void abort ();
  22. #endif /* not __STDC__ */
  23.  
  24.  
  25. /* Allocates a block of WANTED_LENGTH bytes, aborts if not enough memory.
  26.    Uses malloc to allocate.  */
  27.  
  28. #ifdef __STDC__
  29. void *xmalloc (int wanted_length)
  30. #else
  31. void *
  32. xmalloc (wanted_length)
  33.      int wanted_length;
  34. #endif
  35. {
  36.   void *result_pointer = (void *) malloc (wanted_length);
  37.  
  38.   if (result_pointer == NULL)
  39.     {
  40.       fprintf (stderr, "Out of memory!\n");
  41.       abort ();
  42.     }
  43.   return result_pointer;
  44. }
  45.  
  46.  
  47. /* Reallocate a block of memory pointed to by PREVIOUS_POINTER, giving it a
  48.    new size of WANTED_LENGTH bytes, and aborts if not enough memory.
  49.    Uses realloc to reallocate.  */
  50.  
  51. #ifdef __STDC__
  52. void *xrealloc (void *previous_pointer, int wanted_length)
  53. #else
  54. void *
  55. xrealloc (previous_pointer, wanted_length)
  56.      void *previous_pointer;
  57.      int wanted_length;
  58. #endif
  59. {
  60.   void *result_pointer = (void *) realloc (previous_pointer, wanted_length);
  61.  
  62.   if (!result_pointer)
  63.     {
  64.       fprintf (stderr, "Out of memory!\n");
  65.       abort ();
  66.     }
  67.   return result_pointer;
  68. }
  69.  
  70. #ifdef MSDOS
  71.  
  72. #include <stdlib.h>
  73. #include <string.h>
  74. #include <io.h>
  75.  
  76. #if defined (_MSC_VER) && (_MSC_VER >= 600)
  77. #define HUGE _huge
  78. #else
  79. #define HUGE huge
  80. #endif
  81.  
  82.  
  83. /* malloc a block of memory, with fatal error message if we can't do it. */
  84.  
  85. #include <malloc.h>
  86. #include <dos.h>
  87.  
  88. void HUGE *
  89. xhalloc (long size)
  90. {
  91.   void HUGE *value = (void HUGE *) halloc (size, (size_t)1 );
  92.  
  93.   if (!value)
  94.     {
  95.       fprintf (stderr, "Out of memory!\n");
  96.       abort ();
  97.     }
  98.  
  99.   return value;
  100. }
  101.  
  102. /* Here we do a HUGE "realloc" by allocating a new block and
  103.    moving the old block afterwards.  This is *slow*, but should
  104.    be reliable.  */
  105.  
  106. void HUGE *
  107. xhrealloc (void HUGE *ptr, long new_size, long old_size)
  108. {
  109.   void HUGE *value = (void HUGE *) halloc (new_size, (size_t)1 );
  110.  
  111.   if (!value)
  112.     {
  113.       fprintf (stderr, "Out of memory!\n");
  114.       abort ();
  115.     }
  116.   else
  117.     {
  118.       char HUGE *dest = value;
  119.       char HUGE *src = ptr;
  120.  
  121.       while (old_size > 0L)
  122.     {
  123.       unsigned int bytes = (unsigned int) min (0x8000L, old_size);
  124.       memcpy (dest, src, bytes);
  125.       old_size -= (long) bytes;
  126.       dest += (long) bytes;
  127.       src += (long) bytes;
  128.     }
  129.     }
  130.   hfree (ptr);
  131.   return value;
  132. }
  133.  
  134. long
  135. hread (int fd, void HUGE *buffer, long bytes)
  136. {
  137.   long bytes_read = 0L;
  138.  
  139.   while (1)
  140.     {
  141.       int n = read (fd, buffer, (unsigned int) min (0x4000L, bytes));
  142.  
  143.       if (n > 0)
  144.     {
  145.       bytes_read += (long) n;
  146.       bytes -= (long) n;
  147.       /* we can't say buffer += n here, because we have to make
  148.          sure that the offset of BUFFER doesn't overflow during
  149.          the read() system call.  Therefore we add what we read
  150.          to the segment of BUFFER.  */
  151.       FP_SEG(buffer) += (n >> 4);
  152.       FP_OFF(buffer) += (n & 0x0F);
  153.     }
  154.       else if (n == 0)
  155.     return bytes_read;    /* done */
  156.       else
  157.     {
  158.       perror ("Out of memory");
  159.       abort ();
  160.     }
  161.     }
  162. }
  163. #endif /* MSDOS */
  164.