home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / ramfs102.zip / src / nearheap.c < prev    next >
C/C++ Source or Header  |  2002-09-28  |  3KB  |  127 lines

  1. #include "includes.h"
  2.  
  3.  
  4.  
  5. #define HEAPSIZE    49152u        /* must be a multiple of 32 */
  6.  
  7.  
  8. struct block
  9. {
  10.   USHORT cbSize;
  11.   char fFree;
  12.   char data[1];
  13. };
  14.  
  15. char near nearheap [HEAPSIZE];
  16.  
  17.  
  18.  
  19.  
  20. void NearInit (void)
  21. {
  22.   struct block near *blk;
  23.  
  24.   blk = (struct block near *) nearheap;
  25.   blk->cbSize = HEAPSIZE;
  26.   blk->fFree = TRUE;
  27. }
  28.  
  29.  
  30.  
  31.  
  32. int NearAlloc (PNEARBLOCK _ss *ppBlock, USHORT cbSize)
  33. {
  34.   struct block near *cur;
  35.   struct block near *best_block;
  36.   struct block near *new_free;
  37.   USHORT best_size;
  38.  
  39.   cbSize = (cbSize+0x0022) & ~0x001F;
  40.   best_size = 0xFFFF;
  41.   cur = (struct block near *) &nearheap[0];
  42.  
  43.   while ((USHORT)cur < (USHORT) &nearheap[HEAPSIZE])
  44.   {
  45.     if (cur->fFree)
  46.     {
  47.       if (cur->cbSize == cbSize)
  48.       {
  49.     /* found a free block of the right size, take it */
  50.     cur->fFree = FALSE;
  51.     *ppBlock = &cur->data[0];
  52.     return NO_ERROR;
  53.       }
  54.       else if (cur->cbSize > cbSize  &&  cur->cbSize < best_size)
  55.       {
  56.     /* remember the smallest free block that is large enough */
  57.     best_block = cur;
  58.     best_size = cur->cbSize;
  59.       }
  60.     }
  61.  
  62.     cur = (struct block near *) ((USHORT)cur + cur->cbSize);
  63.   }
  64.  
  65.   if (best_size == 0xFFFF)
  66.     return ERROR_OUT_OF_STRUCTURES;    /* no free block large enough */
  67.  
  68.   /* take a part of the smallest free block that was large enough */
  69.   new_free = (struct block near *) ((USHORT)best_block + cbSize);
  70.   new_free->cbSize = best_size - cbSize;
  71.   new_free->fFree = TRUE;
  72.   best_block->cbSize = cbSize;
  73.   best_block->fFree = FALSE;
  74.   *ppBlock = &best_block->data[0];
  75.   return NO_ERROR;
  76. }
  77.  
  78.  
  79.  
  80.  
  81. void NearFree (PNEARBLOCK pBlock)
  82. {
  83.   struct block near *blk;
  84.   struct block near *prev;
  85.   struct block near *cur;
  86.   struct block near *next;
  87.  
  88.   blk = (struct block near *) ((USHORT)pBlock - 3);
  89.   prev = 0;
  90.  
  91.   cur = (struct block near *) &nearheap[0];
  92.   while ((USHORT)cur < (USHORT) &nearheap[HEAPSIZE])
  93.   {
  94.     if (blk == cur)
  95.     {
  96. #ifdef DEBUG
  97.       if (cur->fFree)
  98.       {
  99.     debugging = TRUE;
  100.     DEBUG_PRINTF1 ("\r\n!!! NearFree (0x%04X) -- block already free\r\n", pBlock);
  101.     INT3;
  102.     return;
  103.       }
  104. #endif
  105.       cur->fFree = TRUE;
  106.       next = (struct block near *) ((USHORT)cur + cur->cbSize);
  107.       if ((USHORT)next < (USHORT) &nearheap[HEAPSIZE]  &&  next->fFree)
  108.     /* next block is also free, join us with it */
  109.     cur->cbSize += next->cbSize;
  110.  
  111.       if (prev  &&  prev->fFree)
  112.     /* previous block is also free, join us with it */
  113.     prev->cbSize += cur->cbSize;
  114.       return;
  115.     }
  116.  
  117.     prev = cur;
  118.     cur = (struct block near *) ((USHORT)cur + cur->cbSize);
  119.   }
  120.  
  121. #ifdef DEBUG
  122.   debugging = TRUE;
  123.   DEBUG_PRINTF1 ("\r\n!!! NearFree (0x%04) -- invalid block\r\n", pBlock);
  124.   INT3;
  125. #endif
  126. }
  127.