home *** CD-ROM | disk | FTP | other *** search
/ MegaDoom Adventures / PMWMEGADOOM.iso / doom / creators / deu521 / source / memory.c < prev    next >
C/C++ Source or Header  |  1994-04-24  |  4KB  |  157 lines

  1. /*
  2.    Doom Editor Utility, by Brendon Wyber and Raphaël Quinet.
  3.  
  4.    You are allowed to use any parts of this code in another program, as
  5.    long as you give credits to the authors in the documentation and in
  6.    the program itself.  Read the file README.1ST for more information.
  7.  
  8.    This program comes with absolutely no warranty.
  9.  
  10.    MEMORY.C - Memory allocation routines.
  11. */
  12.  
  13. /* the includes */
  14. #include "deu.h"
  15.  
  16. /*
  17.    Note from RQ:
  18.       To prevent memory fragmentation on large blocks (greater than 1K),
  19.       the size of all blocks is rounded up to 8K.  Thus, "realloc" will
  20.       move the block if and only if it has grown or shrunk enough to
  21.       cross a 8K boundary.
  22.       I don't do that for smaller blocks (smaller than 1K), because this
  23.       would waste too much space if these blocks were rounded up to 8K.
  24.       There are lots of "malloc"'s for very small strings (9 characters)
  25.       or filenames, etc.
  26.       Thanks to Craig Smith (bcs@cs.tamu.edu) for some of his ideas
  27.       about memory fragmentation.
  28. */
  29.  
  30. #define SIZE_THRESHOLD    1024
  31. #define SIZE_OF_BLOCK    4095  /* actually, this is (size - 1) */
  32.  
  33.  
  34. /*
  35.    allocate memory with error checking
  36. */
  37.  
  38. void *GetMemory( size_t size)
  39. {
  40.    void *ret;
  41.  
  42.    /* limit fragmentation on large blocks */
  43.    if (size >= (size_t) SIZE_THRESHOLD)
  44.       size = (size + (size_t) SIZE_OF_BLOCK) & ~((size_t) SIZE_OF_BLOCK);
  45.    ret = malloc( size);
  46.    if (!ret)
  47.    {
  48.       /* retry after having freed some memory, if possible */
  49.       FreeSomeMemory();
  50.       ret = malloc( size);
  51.    }
  52.    if (!ret)
  53.       ProgError( "out of memory (cannot allocate %u bytes)", size);
  54.    return ret;
  55. }
  56.  
  57.  
  58.  
  59. /*
  60.    reallocate memory with error checking
  61. */
  62.  
  63. void *ResizeMemory( void *old, size_t size)
  64. {
  65.    void *ret;
  66.  
  67.    /* limit fragmentation on large blocks */
  68.    if (size >= (size_t) SIZE_THRESHOLD)
  69.       size = (size + (size_t) SIZE_OF_BLOCK) & ~((size_t) SIZE_OF_BLOCK);
  70.    ret = realloc( old, size);
  71.    if (!ret)
  72.    {
  73.       FreeSomeMemory();
  74.       ret = realloc( old, size);
  75.    }
  76.    if (!ret)
  77.       ProgError( "out of memory (cannot reallocate %u bytes)", size);
  78.    return ret;
  79. }
  80.  
  81.  
  82.  
  83. /*
  84.    free memory
  85. */
  86.  
  87. void FreeMemory( void *ptr)
  88. {
  89.    /* just a wrapper around free(), but provide an entry point */
  90.    /* for memory debugging routines... */
  91.    free( ptr);
  92. }
  93.  
  94.  
  95. /*
  96.    allocate memory from the far heap with error checking
  97. */
  98.  
  99. void huge *GetFarMemory( unsigned long size)
  100. {
  101.    void huge *ret;
  102.  
  103.    /* limit fragmentation on large blocks */
  104.    if (size >= (unsigned long) SIZE_THRESHOLD)
  105.       size = (size + (unsigned long) SIZE_OF_BLOCK) & ~((unsigned long) SIZE_OF_BLOCK);
  106.    ret = farmalloc( size);
  107.    if (!ret)
  108.    {
  109.       /* retry after having freed some memory, if possible */
  110.       FreeSomeMemory();
  111.       ret = farmalloc( size);
  112.    }
  113.    if (!ret)
  114.       ProgError( "out of memory (cannot allocate %lu far bytes)", size);
  115.    return ret;
  116. }
  117.  
  118.  
  119.  
  120. /*
  121.    reallocate memory from the far heap with error checking
  122. */
  123.  
  124. void huge *ResizeFarMemory( void huge *old, unsigned long size)
  125. {
  126.    void huge *ret;
  127.  
  128.    /* limit fragmentation on large blocks */
  129.    if (size >= (unsigned long) SIZE_THRESHOLD)
  130.       size = (size + (unsigned long) SIZE_OF_BLOCK) & ~((unsigned long) SIZE_OF_BLOCK);
  131.    ret = farrealloc( old, size);
  132.    if (!ret)
  133.    {
  134.       FreeSomeMemory();
  135.       ret = farrealloc( old, size);
  136.    }
  137.    if (!ret)
  138.       ProgError( "out of memory (cannot reallocate %lu far bytes)", size);
  139.    return ret;
  140. }
  141.  
  142.  
  143.  
  144. /*
  145.    free memory from the far heap
  146. */
  147.  
  148. void FreeFarMemory( void huge *ptr)
  149. {
  150.    /* just a wrapper around farfree(), but provide an entry point */
  151.    /* for memory debugging routines... */
  152.    farfree( ptr);
  153. }
  154.  
  155.  
  156. /* end of file */
  157.