Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  Implementation of near malloc() and _nmalloc().
  28. *
  29. ****************************************************************************/
  30.  
  31.  
  32. //#include "dll.h"        // needs to be first
  33. #include "variety.h"
  34. #include <stddef.h>
  35. #include <stdlib.h>
  36. #include <malloc.h>
  37. #include "extfunc.h"
  38. #include "heapacc.h"
  39. #include "heap.h"
  40.  
  41. #if defined(_M_IX86)
  42.     #pragma aux (__outside_CLIB) __nmemneed;
  43. #endif
  44.  
  45. mheapptr _WCNEAR __nheapbeg = NULL;
  46.  
  47. struct miniheapblkp _WCNEAR *__MiniHeapRover = NULL;
  48. unsigned int   __LargestSizeB4MiniHeapRover = 0;
  49.  
  50.  
  51.  
  52. #if defined(__SMALL_DATA__)
  53.  
  54. _WCRTLINK void *malloc( size_t amount )
  55. {
  56.     return( _nmalloc( amount ) );
  57. }
  58.  
  59. #endif
  60.  
  61.  
  62. _WCRTLINK void _WCNEAR *_nmalloc( size_t amt )
  63. {
  64.     unsigned        largest;
  65.     unsigned        size;
  66.     unsigned        ptr;
  67.     unsigned char   expanded;
  68.     mheapptr        miniheap_ptr;
  69.  
  70.     if( (amt == 0) || (amt > -sizeof(struct heapblk)) ) {
  71.         return( (void _WCNEAR *)NULL );
  72.     }
  73.  
  74.     // Try to determine which miniheap to begin allocating from.
  75.     // first, round up the amount
  76.     size = (amt + TAG_SIZE + ROUND_SIZE) & ~ROUND_SIZE;
  77.     if( size < FRL_SIZE ) {
  78.         size = FRL_SIZE;
  79.     }
  80.  
  81.     _AccessNHeap();
  82.     ptr = NULL;
  83.     expanded = 0;
  84.     for(;;) {
  85.         if( size > __LargestSizeB4MiniHeapRover ) {
  86.             miniheap_ptr = __MiniHeapRover;
  87.             if( miniheap_ptr == NULL ) {
  88.                 __LargestSizeB4MiniHeapRover = 0;
  89.                 miniheap_ptr = __nheapbeg;
  90.             }
  91.         } else {
  92.             __LargestSizeB4MiniHeapRover = 0;   // force value to be updated
  93.             miniheap_ptr = __nheapbeg;
  94.         }
  95.         for(;;) {
  96.             if( miniheap_ptr == NULL ) {
  97.                 break;
  98.             }
  99.             __MiniHeapRover = miniheap_ptr;
  100.             largest = miniheap_ptr->largest_blk;
  101.             if( largest >= amt ) {
  102.                 ptr = __MemAllocator( amt, _DGroup(), (unsigned)miniheap_ptr );
  103.                 if( ptr != NULL ) {
  104.                     goto lbl_release_heap;
  105.                 }
  106.             }
  107.             if( largest > __LargestSizeB4MiniHeapRover ) {
  108.                 __LargestSizeB4MiniHeapRover = largest;
  109.             }
  110.             miniheap_ptr = miniheap_ptr->next;
  111.         }
  112.         if( expanded || !__ExpandDGROUP( amt ) ) {
  113.             if( !__nmemneed( amt ) ) {
  114.                 break;
  115.             }
  116.             expanded = 0;
  117.         } else {
  118.             expanded = 1;
  119.         }
  120.     }
  121. lbl_release_heap:
  122.     _ReleaseNHeap();
  123.     return( (void _WCNEAR *)ptr );
  124. }
  125.