home *** CD-ROM | disk | FTP | other *** search
/ IRIS Development Option 6.2 / IRIS_Development_Option_6.2_814-0478-001.iso / dist / c_dev.idb / usr / share / src / customalloc / qf-part2.c.z / qf-part2.c
Text File  |  1996-03-14  |  1KB  |  66 lines

  1. /*
  2.  * Quick fit
  3.  */
  4.  
  5. MallocPtrType realloc(MallocPtrType mem, MallocArgType bytes)
  6. {
  7.    if ( mem == 0 ) {
  8.      return( malloc(bytes) );
  9.    } else {
  10.      if ( is_sbrk(mem) ) {
  11.         return( __sbrk_realloc(mem,bytes));
  12.      } else {
  13.         return( __internal_realloc(mem,bytes));
  14.      }
  15.    }
  16. }
  17.  
  18. MallocPtrType malloc(MallocArgType bytes)
  19. {
  20.   if ( bytes <= 32 ) {
  21.     /*
  22.      * Round size up by 4
  23.      */
  24.     int index = (bytes+3) >> 2;
  25.     int size = index << 2;
  26.     if (__customalloc_FreeList[index]) 
  27.       return(__customalloc_unlink(&(__customalloc_FreeList[index])));
  28.     else
  29.       /*
  30.        * create it of appropriate size to fit in this bin..
  31.        */
  32.       return( __sbrk_malloc(size) ); 
  33.   } else {
  34.     return __internal_malloc(bytes);
  35.   }
  36. }
  37.  
  38. FreeRetType free(FreePtrType p)
  39. {
  40.   if ( p ) {
  41.      MallocChunk *chunk = external_to_malloc(p);
  42.      int bytes = malloc_size(chunk) - (2 * SIZE_SZ);
  43.  
  44.      if ( bytes <= 32 ) {
  45.        /*
  46.     * Truncate size
  47.     */
  48.        int index = bytes >> 2;
  49.        __customalloc_link(&(__customalloc_FreeList[index]), p);
  50.      } else {
  51.        /*
  52.     * If it's an sbrk, it must be returned to a freelist
  53.     */
  54.        if (is_sbrk(p)) {
  55.      int index = bytes >> 2;
  56.      if ( index >= __customalloc_SizeClasses ) {
  57.        index = __customalloc_SizeClasses - 1;
  58.      }
  59.      __customalloc_link(&(__customalloc_FreeList[index]), p);
  60.        } else {
  61.      __internal_free(p);
  62.        }
  63.      }
  64.    }
  65. }
  66.