home *** CD-ROM | disk | FTP | other *** search
- /* Malloc interface for C.
- Copyright (C) 1990 Free Software Foundation, Inc.
- Francois Pinard <pinard@iro.umontreal.ca>, September 1989.
-
- $Id$
- */
-
- #include <stdio.h>
-
- #ifndef appolo
- #include <malloc.h>
- #endif /* appolo */
-
- #ifdef __STDC__
- #ifdef __GNUC__
- void volatile abort (void);
- #else /* not __GNUC__ */
- void abort (void);
- #endif /* not __GNUC__ */
- #else /* not __STDC__ */
- void abort ();
- #endif /* not __STDC__ */
-
-
- /* Allocates a block of WANTED_LENGTH bytes, aborts if not enough memory.
- Uses malloc to allocate. */
-
- #ifdef __STDC__
- void *xmalloc (int wanted_length)
- #else
- void *
- xmalloc (wanted_length)
- int wanted_length;
- #endif
- {
- void *result_pointer = (void *) malloc (wanted_length);
-
- if (result_pointer == NULL)
- {
- fprintf (stderr, "Out of memory!\n");
- abort ();
- }
- return result_pointer;
- }
-
-
- /* Reallocate a block of memory pointed to by PREVIOUS_POINTER, giving it a
- new size of WANTED_LENGTH bytes, and aborts if not enough memory.
- Uses realloc to reallocate. */
-
- #ifdef __STDC__
- void *xrealloc (void *previous_pointer, int wanted_length)
- #else
- void *
- xrealloc (previous_pointer, wanted_length)
- void *previous_pointer;
- int wanted_length;
- #endif
- {
- void *result_pointer = (void *) realloc (previous_pointer, wanted_length);
-
- if (!result_pointer)
- {
- fprintf (stderr, "Out of memory!\n");
- abort ();
- }
- return result_pointer;
- }
-
- #ifdef MSDOS
-
- #include <stdlib.h>
- #include <string.h>
- #include <io.h>
-
- #if defined (_MSC_VER) && (_MSC_VER >= 600)
- #define HUGE _huge
- #else
- #define HUGE huge
- #endif
-
-
- /* malloc a block of memory, with fatal error message if we can't do it. */
-
- #include <malloc.h>
- #include <dos.h>
-
- void HUGE *
- xhalloc (long size)
- {
- void HUGE *value = (void HUGE *) halloc (size, (size_t)1 );
-
- if (!value)
- {
- fprintf (stderr, "Out of memory!\n");
- abort ();
- }
-
- return value;
- }
-
- /* Here we do a HUGE "realloc" by allocating a new block and
- moving the old block afterwards. This is *slow*, but should
- be reliable. */
-
- void HUGE *
- xhrealloc (void HUGE *ptr, long new_size, long old_size)
- {
- void HUGE *value = (void HUGE *) halloc (new_size, (size_t)1 );
-
- if (!value)
- {
- fprintf (stderr, "Out of memory!\n");
- abort ();
- }
- else
- {
- char HUGE *dest = value;
- char HUGE *src = ptr;
-
- while (old_size > 0L)
- {
- unsigned int bytes = (unsigned int) min (0x8000L, old_size);
- memcpy (dest, src, bytes);
- old_size -= (long) bytes;
- dest += (long) bytes;
- src += (long) bytes;
- }
- }
- hfree (ptr);
- return value;
- }
-
- long
- hread (int fd, void HUGE *buffer, long bytes)
- {
- long bytes_read = 0L;
-
- while (1)
- {
- int n = read (fd, buffer, (unsigned int) min (0x4000L, bytes));
-
- if (n > 0)
- {
- bytes_read += (long) n;
- bytes -= (long) n;
- /* we can't say buffer += n here, because we have to make
- sure that the offset of BUFFER doesn't overflow during
- the read() system call. Therefore we add what we read
- to the segment of BUFFER. */
- FP_SEG(buffer) += (n >> 4);
- FP_OFF(buffer) += (n & 0x0F);
- }
- else if (n == 0)
- return bytes_read; /* done */
- else
- {
- perror ("Out of memory");
- abort ();
- }
- }
- }
- #endif /* MSDOS */
-