Changeset 502


Ignore:
Timestamp:
Jul 11, 2010, 7:06:19 AM (15 years ago)
Author:
David Azarewicz
Message:

malloc changes

Location:
GPL/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • GPL/trunk/drv32/init.c

    r492 r502  
    3636#include <ossidc32.h>
    3737#include <dbgos2.h>
    38 #include <printfos2.h>
    3938#include <irqos2.h>
    4039//#include <bsedos16.h>
     
    4342#endif
    4443#include "parse.h"
     44#include "malloc.h"
    4545
    4646const char ERR_ERROR[]   = "ERROR: ";
     
    6868extern "C" int sprintf (char *buffer, const char *format, ...);
    6969
    70 extern "C" int wrOffset;
    71 extern "C" char *szprintBuf;
    7270extern "C" APIRET APIENTRY DOS16OPEN(PSZ pszFileName, PHFILE phf, PULONG pulAction, ULONG cbFile, ULONG ulAttribute, ULONG fsOpenFlags, ULONG fsOpenMode, PEAOP2 peaop2 );
    7371extern "C" APIRET APIENTRY DOS16CLOSE(HFILE hFile);
    7472extern "C" APIRET APIENTRY DOS16WRITE(HFILE hFile, PVOID pBuffer, ULONG cbWrite, PULONG pcbActual);
    7573extern "C" void SaveBuffer(void);
     74
     75#define VMDHA_FIXED             0x0002
     76
     77extern "C" APIRET VMAlloc(ULONG size, ULONG flags, char NEAR* *pAddr);
    7678
    7779//Print messages with DosWrite when init is done or has failed (see startup.asm)
     
    9193}
    9294
    93 void _cdecl printfos2(char *string)
    94 {
    95    DevSaveMessage(string, strlen(string));
    96 }
    97 
    9895//SvL: Lock our 32 bits data & code segments
    99 int LockSegments(void) 
     96int LockSegments(void)
    10097{
    10198#ifdef KEE
     
    194191#endif
    195192// Initialize device driver
    196 WORD32 DiscardableInit(RPInit __far* rp) 
     193WORD32 DiscardableInit(RPInit __far* rp)
    197194{
    198195    char        debugmsg[64];
     
    209206    }
    210207
     208        DebugLevel = 1;
    211209    rp->Out.FinalCS = 0;
    212210    rp->Out.FinalDS = 0;
     211
     212        if ( szprintBuf == 0 ) {
     213                VMAlloc( DBG_MAX_BUF_SIZE, VMDHA_FIXED, &szprintBuf );
     214                if (szprintBuf) {
     215                        memset( szprintBuf, 0, DBG_MAX_BUF_SIZE );
     216                        wrOffset= 0;
     217                }
     218        }
     219        if (!HeapInit(HEAP_SIZE)) {
     220                dprintf(("HeapInit failed!"));
     221        }
    213222
    214223    args = MAKE_FARPTR32(rp->In.Args);
     
    268277#if 0
    269278        for(int i=1;i<OSS32_MAX_AUDIOCARDS;i++) {
    270             if(OSS32_QueryNames(i, szDeviceName, sizeof(szDeviceName), szMixerName, sizeof(szMixerName)) == OSSERR_SUCCESS) 
     279            if(OSS32_QueryNames(i, szDeviceName, sizeof(szDeviceName), szMixerName, sizeof(szMixerName)) == OSSERR_SUCCESS)
    271280            {
    272281                WriteString(szDeviceName, strlen(szDeviceName));
     
    281290        WriteString(szEOL, sizeof(szEOL)-1);
    282291    }
    283 #ifdef DEBUG
    284     dprintf(("DiscardableInit. cp1"));
    285 #endif
    286292    // Complete the installation
    287293    rp->Out.FinalCS = _OffsetFinalCS16;
    288294    rp->Out.FinalDS = _OffsetFinalDS16;
    289 #ifdef DEBUG
    290     dprintf(("DiscardableInit. cp2"));
    291 #endif
    292 
    293 //    SaveBuffer();
     295
     296        //SaveBuffer();
    294297
    295298    // Confirm a successful installation
  • GPL/trunk/include/malloc.h

    r32 r502  
    2525#define MALLOC_INCLUDED
    2626
     27#define  DEFAULT_HEAP   256*1024
     28#define  HEAP_SIZE      256*1024
     29
    2730// Standard malloc.h functions
    2831
     
    3033//      put in seperate private segments)
    3134#ifdef DEBUGHEAP
    32 void NEAR *malloc(unsigned size, const char *filename, int lineno);
     35void NEAR *malloc(ULONG size, const char *filename, int lineno);
    3336void       free(void *NEAR ptr, const char *filename, int lineno);
    3437void NEAR *realloc(void *NEAR ptr, unsigned newsize, const char *filename, int lineno);
    3538#else
    36 void NEAR *malloc(unsigned);
     39void NEAR *malloc(ULONG size);
    3740void       free(void NEAR *);
    3841void NEAR *realloc(void NEAR *, unsigned);
     
    4952
    5053// Specialized routines
    51 unsigned HeapInit(unsigned);        // initializes the heap manager
     54ULONG HeapInit(ULONG size);        // initializes the heap manager
    5255void dumpheap(void);
    5356
  • GPL/trunk/lib32/malloc.cpp

    r73 r502  
    3838#endif
    3939
    40 #define  DEFAULT_HEAP   256*1024
    41 #define  HEAP_SIZE      256*1024
    42 
    4340#define  SIGNATURE      0xBEEFDEAD      // "DeadBeef" when view in hex in word order.
    4441
     
    4643typedef struct _MEMBLOCK {
    4744    ULONG                  ulSignature;
    48     ULONG                  uSize;
     45    ULONG                  ulSize;
    4946    struct _MEMBLOCK NEAR *pmbNext;
    5047#ifdef DEBUGHEAP
     
    6966LINEAR     acHeap   = NULL;
    7067
     68ULONG ulnAlloc = 0;           // Current number of allocated blocks.
     69ULONG ulcAlloc = 0;           // Total memory in allocated blocks incl. headers.
     70ULONG ulnAllocCalls = 0;      // Cumulative total, number of malloc() calls.
     71ULONG ulnFree  = 0;           // Current number of free blocks.
     72ULONG ulcFree  = 0;           // Total memory in free blocks incl. headers.
     73ULONG ulnFreeCalls  = 0;      // Cumulative total, number of free() calls.
     74ULONG ulnCompact = 0;         // Num of times we've joined two adjacent free
     75                             // blocks into a single larger block.
     76ULONG ulcTotalHeap = HEAP_SIZE;
     77
     78
    7179#pragma off (unreferenced)
    7280
     
    7785void dumpheap(void)
    7886{
    79     int i;
     87    SHORT sI;
    8088    PMEMBLOCK pmb;
    81     unsigned u=0;
    82 
     89    ULONG ulSize;
     90
     91        ulSize = 0;
    8392    pmb=pmbUsed;
    84     dprintf(("HEAP: Heap Dump - Used blocks\n"));
    85     for (i=0; i<10; i++) {
    86         dprintf(("  pmb=%p, length=%ui\n",(void __far *) pmb, pmb->uSize));
    87         u+=pmb->uSize;
     93        dprintf(("HEAP:Heap Dump acHeap=%lx Used=%p Free=%p\n", (void __far *)acHeap, pmbUsed, pmbFree));
     94    dprintf(("  Used blocks\n"));
     95    for (sI=0; sI<10; sI++) {
     96        dprintf(("  pmb=%p, length=%lx\n",(void __far *) pmb, pmb->ulSize));
     97        ulSize+=pmb->ulSize;
    8898        pmb=pmb->pmbNext;
    8999        if (!pmb) break;
    90100    }
    91     dprintf(("  Total used = %ui\n",u));
    92 
    93     u=0;
     101    dprintf(("  Total used = %lx\n", ulSize));
     102
     103    ulSize=0;
    94104    pmb=pmbFree;
    95     dprintf(("HEAP: Heap Dump - Free blocks\n"));
    96     for (i=0; i<50; i++) {
    97         dprintf(("  pmb=%p, length=%ui\n",(void __far *) pmb, pmb->uSize));
    98         u+=pmb->uSize;
     105    dprintf(("  Free blocks\n"));
     106    for (sI=0; sI<50; sI++) {
     107        dprintf(("  pmb=%p, length=%lx\n",(void __far *) pmb, pmb->ulSize));
     108        ulSize+=pmb->ulSize;
    99109        pmb=pmb->pmbNext;
    100110        if (!pmb) break;
    101111    }
    102     dprintf(("  Total free = %ui\n",u));
    103 }
    104 //*****************************************************************************
    105 //*****************************************************************************
    106 
    107 ULONG fMemoryDoc = 0;
    108 ULONG nAlloc = 0;           // Current number of allocated blocks.
    109 ULONG cAlloc = 0;           // Total memory in allocated blocks incl. headers.
    110 ULONG nAllocCalls = 0;      // Cumulative total, number of malloc() calls.
    111 ULONG nFree  = 0;           // Current number of free blocks.
    112 ULONG cFree  = 0;           // Total memory in free blocks incl. headers.
    113 ULONG nFreeCalls  = 0;      // Cumulative total, number of free() calls.
    114 ULONG nCompact = 0;         // Num of times we've joined two adjacent free
    115                              // blocks into a single larger block.
    116 ULONG cTotalHeap = HEAP_SIZE;
    117 
    118 //*****************************************************************************
    119 //*****************************************************************************
    120 BOOL SignatureCheck ( PMEMBLOCK p, PSZ idText )
    121 {
    122     int bGoodPointer;
     112    dprintf(("  Total free = %lx\n", ulSize));
     113}
     114//*****************************************************************************
     115//*****************************************************************************
     116
     117//*****************************************************************************
     118//*****************************************************************************
     119SHORT SignatureCheck ( PMEMBLOCK p, PSZ idText )
     120{
     121    short sGoodPointer;
    123122
    124123    // Set bGoodPointer to indicate whether or not 'p' points within heap.
    125     bGoodPointer = ((LINEAR)p >= (acHeap))
    126                     && ((LINEAR)p <= (( acHeap) + HEAP_SIZE)) ;
    127                               //### heap might have less than HEAP_SIZE bytes
     124    sGoodPointer = ((LINEAR)p >= (acHeap))
     125                                        && ((LINEAR)p <= (( acHeap) + HEAP_SIZE)) ;
     126                                        //### heap might have less than HEAP_SIZE bytes
    128127
    129128    // Check for correct signature.
    130     if (bGoodPointer)
    131         bGoodPointer = (p->ulSignature == SIGNATURE) && p->uSize < HEAP_SIZE;
    132 
    133     if (! bGoodPointer)
    134     {
    135 //      ddprintf( "Heap pointer out of range, or signature exception: %p %s\n", p, idText );
     129    if (sGoodPointer) sGoodPointer = (p->ulSignature == SIGNATURE) && p->ulSize < HEAP_SIZE;
     130
     131    if (!sGoodPointer) {
     132                dprintf(("SignatureCheck: Bad pointer %p %s\n", p, idText));
    136133        DebugInt3();
    137134    }
    138     return bGoodPointer;
    139 }
    140 //*****************************************************************************
    141 //*****************************************************************************
    142 #ifdef DEBUG
     135    return sGoodPointer;
     136}
     137//*****************************************************************************
     138//*****************************************************************************
    143139void HeapCheck ( PSZ idText )
    144140{
    145141    PMEMBLOCK p;
    146     for ( nAlloc = 0, cAlloc = 0, p = pmbUsed; p; p = p->pmbNext ) {
    147         ++nAlloc;
     142    for ( ulnAlloc = 0, ulcAlloc = 0, p = pmbUsed; p; p = p->pmbNext ) {
     143        ++ulnAlloc;
     144        ulcAlloc += p->ulSize + HDR_SIZE;
    148145        SignatureCheck( p,(PSZ) "HeapCheck() Used list" );
    149         cAlloc += p->uSize + HDR_SIZE;
    150     }
    151     for ( nFree = 0, cFree = 0, p = pmbFree; p; p = p->pmbNext ) {
    152         ++nFree;
     146                if (p == p->pmbNext) {
     147                        dprintf(("HeapCheck: used %p points to itself\n", p));
     148                        DebugInt3();
     149                }
     150    }
     151    for ( ulnFree = 0, ulcFree = 0, p = pmbFree; p; p = p->pmbNext ) {
     152        ++ulnFree;
     153        ulcFree += p->ulSize + HDR_SIZE;
    153154        SignatureCheck( p,(PSZ) "HeapCheck() Free list" );
    154         cFree += p->uSize + HDR_SIZE;
    155     }
    156     if (fMemoryDoc & 1) {
    157         if (cAlloc + cFree != cTotalHeap) {
    158 //         ddprintf( "Heap Alloc + Free != Total\n" );
    159 //       dumpheap();
    160             DebugInt3();
    161         }
    162     }
    163 }
    164 #else
    165 #define HeapCheck(a)
    166 #endif
    167 
    168 //*****************************************************************************
    169 //*****************************************************************************
    170 
    171 
     155                if (p == p->pmbNext) {
     156                        dprintf(("HeapCheck: free %p points to itself\n", p));
     157                        DebugInt3();
     158                }
     159    }
     160        if (ulcAlloc + ulcFree != ulcTotalHeap) {
     161                dprintf(("HeapCheck: Alloc+Free != Total\n"));
     162                //dumpheap();
     163                DebugInt3();
     164        }
     165}
     166
     167//*****************************************************************************
     168//*****************************************************************************
    172169
    173170// Threshold for creating a new free block.
    174171#define  MIN_Leftover  (HDR_SIZE + MIN_FragSize)
    175172
    176 /* make_new_free()
     173/*
    177174   Formats the back part of an existing free MEMBLOCK as a new, smaller
    178175   "Free" MEMBLOCK.  Doesn't update Global vbls (caller responsibility).
     
    227224  We split the block into an allocated part to satisfy the allocation request,
    228225  and a free block which can be allocated in a subsequent request.
    229 */
    230 
    231 PMEMBLOCK make_new_free(PMEMBLOCK pmbOldFree, unsigned uRequest)
    232 {
    233     PMEMBLOCK pmbNewFree;     // If we create a new free block, it goes here.
    234 
    235     // Which of the two cases (as described in function header) have we got?
    236     // We know we're big enough to satisfy request, is there enough leftover
    237     // for a new free block?
    238 
    239     if ((uRequest + MIN_Leftover) > pmbOldFree->uSize) {
    240         // Not enough leftover, allocate the entire free block.  Don't
    241         // change pmbOldFree->uSize.
    242         pmbNewFree = 0;
    243     }
    244     else {
    245         // We've got enough leftover to make a new free block.
    246         pmbNewFree = (PMEMBLOCK) ((char *) pmbOldFree + HDR_SIZE + uRequest );
    247         pmbNewFree->ulSignature = SIGNATURE;
    248         pmbNewFree->uSize = pmbOldFree->uSize - (uRequest + HDR_SIZE);
    249         pmbNewFree->pmbNext = pmbOldFree->pmbNext;
    250 
    251         // Update the size of the free block that was passed in.
    252         pmbOldFree->uSize -= (pmbNewFree->uSize + HDR_SIZE);
    253     }
    254 
    255     return pmbNewFree;
    256 }
    257 
    258 
    259 /**@internal _msize
    260  */
    261 unsigned _msize(void NEAR *pvBlock)
    262 {
    263     PMEMBLOCK pmb;
    264 
    265     if (!pvBlock)
    266         return 0;
    267 
    268     pmb = (PMEMBLOCK) ((char NEAR *) pvBlock - HDR_SIZE);
    269    
    270     return pmb->uSize;
    271 }
    272 
    273 
    274 /**@internal pmbAllocateBlock
     226
     227 @internal pmbAllocateBlock
    275228 *  Update all data structures for allocation of one memory block.  It's
    276229 *  assumed, on entry, that the block is large enough for the requested
     
    286239 *  suitable for return to malloc() caller.
    287240 */
    288 void NEAR *npvAllocateBlock( PMEMBLOCK pmb, ULONG uRequest, PMEMBLOCK pmbPrev )
    289 {
     241void NEAR *npvAllocateBlock( PMEMBLOCK pmb, ULONG ulRequest, PMEMBLOCK pmbPrev )
     242{
     243
    290244    //pmb points to the selected block.
    291245    //pmbPrev points to the block prior to the selected block, NULL if pmbFree.
     
    294248
    295249    // Split this block into an allocated + free part if it's big enough.
    296     pmbNewFree = make_new_free( pmb, uRequest );
     250        pmbNewFree = 0;
     251        if (pmb->ulSize >= (ulRequest + MIN_Leftover)) {
     252                // We've got enough leftover to make a new free block.
     253
     254                /* create the new free block */
     255                pmbNewFree = (PMEMBLOCK) ((char __near *) pmb + HDR_SIZE + ulRequest );
     256                pmbNewFree->ulSignature = SIGNATURE;
     257                pmbNewFree->ulSize = pmb->ulSize - (ulRequest + HDR_SIZE);
     258                pmbNewFree->pmbNext = pmb->pmbNext;
     259
     260                // Update the size of the original free block that was passed in.
     261                pmb->ulSize -= (pmbNewFree->ulSize + HDR_SIZE);
     262        }
    297263
    298264    // Update global memory counter.
    299     uMemFree -= (pmb->uSize + HDR_SIZE);
     265    uMemFree -= (pmb->ulSize + HDR_SIZE);
    300266
    301267    // Add this block into the front of the Allocated list.
     
    305271
    306272    // Remove the new allocation from the Free list.
    307     if (pmbNewFree) {                // If we split off a new free block
    308         pmbNewFree->pmbNext = pmbOldNext;
    309         if (pmbPrev)                  // If we're not at head of Free list
    310             pmbPrev->pmbNext = pmbNewFree;
    311         else
    312             pmbFree = pmbNewFree;
    313     }
    314     else {                           // We allocated the entire free block.
    315         if (pmbPrev)                  // If we're not at head of Free list
    316             pmbPrev->pmbNext = pmbOldNext;
    317         else
    318             if (pmbOldNext)
    319                 pmbFree = pmbOldNext;
     273    if (pmbNewFree) { // If we split off a new free block
     274        pmbNewFree->pmbNext = pmbOldNext; // If we're not at head of Free list
     275        if (pmbPrev) pmbPrev->pmbNext = pmbNewFree;
     276        else pmbFree = pmbNewFree;
     277    } else { // We allocated the entire free block.
     278        if (pmbPrev) pmbPrev->pmbNext = pmbOldNext; // If we're not at head of Free list
     279        else pmbFree = pmbOldNext;
    320280    }
    321281
     
    336296
    337297#ifdef DEBUGHEAP
    338 void NEAR *malloc(unsigned uSize, const char *filename, int lineno)
    339 #else
    340 void NEAR *malloc( unsigned uSize )
    341 #endif
    342 {
    343     ULONG uRequest;                    // Request size after alignment.
     298void NEAR *malloc(ULONG ulSize, const char *filename, int lineno)
     299#else
     300void NEAR *malloc( ULONG ulSize )
     301#endif
     302{
     303    ULONG ulRequest;                    // Request size after alignment.
    344304    PMEMBLOCK pmb, pmbPrev;             // Use to walk free lists.
    345     void NEAR *npvReturn = 0;         // Return value.
    346     ULONG cpuflags;
    347 
    348 #ifdef DEBUG
    349 //    dprintf(("malloc(). size: %d",uSize));
    350 #endif
    351     if(acHeap == NULL) {
    352         if (!HeapInit(HEAP_SIZE))
    353         {
    354 #ifdef DEBUG
    355             dprintf(("malloc(): error heap initing"));
    356 #endif
    357             return 0;
    358         }
    359     }
    360 #ifdef DEBUG
    361 //    dprintf(("malloc(): heap inited"));
    362 #endif
    363     if (!uSize || uSize > HEAP_SIZE) {
     305    void NEAR *npvReturn;         // Return value.
     306    ULONG ulCpuFlags;
     307
     308    //dprintf(("malloc(). size: %d", ulSize));
     309    if(acHeap == NULL) return 0;
     310    if (!ulSize || ulSize > HEAP_SIZE) {
    364311        DebugInt3();
    365312        return 0;
    366313    }
    367314
    368     cpuflags = DevPushfCli();
    369     ++nAllocCalls;                      // Diagnostics.
     315    ulCpuFlags = DevPushfCli();
     316        npvReturn = 0;
     317    ++ulnAllocCalls;                      // Diagnostics.
    370318    HeapCheck((PSZ) "malloc() entry" );
    371 #ifdef DEBUG
    372 //    dprintf(("malloc(): heap checked. pmbFree %x",pmbFree));
    373 #endif
    374 
    375     uRequest = (uSize + 3) & -4;        // Force DWORD alignment.
    376 
    377     if (pmbFree->uSize >= uRequest)
    378         npvReturn = npvAllocateBlock( pmbFree, uRequest, NULL );
    379     else {
    380         pmbPrev = pmbFree;
    381         for ( pmb=pmbFree->pmbNext; pmb; pmbPrev=pmb, pmb=pmb->pmbNext)
    382         {
    383 #ifdef DEBUG
    384 //    dprintf(("malloc(): for. pmb %x",pmb));
    385 #endif
    386             if (pmb->uSize >= uRequest) {
    387                 npvReturn = npvAllocateBlock( pmb, uRequest, pmbPrev );
    388                 break;
    389             }
    390         }
    391     }
     319
     320    ulRequest = (ulSize + 3) & -4;        // Force DWORD alignment.
     321
     322        for (pmbPrev=NULL, pmb=pmbFree; pmb; pmbPrev=pmb, pmb=pmb->pmbNext) {
     323                if (pmb->ulSize >= ulRequest) {
     324                        npvReturn = npvAllocateBlock(pmb, ulRequest, pmbPrev);
     325                        break;
     326                }
     327        }
    392328
    393329    if (npvReturn) {
     
    399335#endif
    400336        SignatureCheck( (PMEMBLOCK) (((PUCHAR) npvReturn) - HDR_SIZE), (PSZ) "malloc() exit, allocated block" );
    401     }
    402     else {
    403 #ifdef DEBUG
    404     dprintf(("malloc(): out of memory"));
    405 #endif
    406         // Out of Memory !!!
     337    } else {
     338                dprintf(("malloc(): out of memory"));
    407339        DebugInt3();
    408340    }
    409341
    410342    HeapCheck((PSZ) "malloc() exit" );
    411 #ifdef DEBUG
    412 //    dprintf(("malloc(): malloc exit"));
    413 #endif
    414 
    415     DevPopf(cpuflags);
     343    DevPopf(ulCpuFlags);
    416344    return npvReturn;
    417345}
     
    448376Also, the newly freed block can only be adjacent to at most two other
    449377blocks.  Therefore, the operation of combining two adjacent free blocks can
    450 only happen at most twice.  The variable nFreed counts the number of times
    451 two blocks are combined.  The function exits if nFreed reaches two.  nFreed
     378only happen at most twice.  The variable ulnFreed counts the number of times
     379two blocks are combined.  The function exits if ulnFreed reaches two.  ulnFreed
    452380is initially 0.
    453381
     
    459387*/
    460388
    461 #define after(pmb) ((PMEMBLOCK) ((char *) pmb + pmb->uSize + HDR_SIZE))
     389#define after(pmb) ((PMEMBLOCK) ((char *) pmb + pmb->ulSize + HDR_SIZE))
    462390
    463391void remove(PMEMBLOCK pmb)
     
    470398    }
    471399
    472     for (pmbPrev=pmbFree; pmbPrev; pmbPrev=pmbPrev->pmbNext)
     400    for (pmbPrev=pmbFree; pmbPrev; pmbPrev=pmbPrev->pmbNext) {
    473401        if (pmbPrev->pmbNext == pmb) {
    474402            pmbPrev->pmbNext = pmb->pmbNext;
    475403            return;
    476404        }
     405        }
    477406}
    478407//*****************************************************************************
     
    481410{
    482411    PMEMBLOCK pmb;
    483     int iFreed = 0;
    484 
     412    SHORT sFreed;
     413
     414        sFreed = 0;
    485415    for (pmb=pmbFree->pmbNext; pmb; pmb=pmb->pmbNext) {
    486         if (pmb == pmb->pmbNext) {
    487 //         ddprintf("HEAP: heap loop, %p points to itself\n", (void __far *) pmb);
    488             DebugInt3();
     416        if (after(pmb)  == pmbFree) {
     417                        // ddprintf("HEAP: compact found pointer %p (size=%ui) before pmbFree %p\n", (void __far *) pmb, pmb->uSize, (void __far *) pmbFree);
     418            pmb->ulSize += HDR_SIZE + pmbFree->ulSize;
     419            remove(pmbFree);
     420            if (++sFreed == 2) break;
     421        } else if (after(pmbFree) == pmb) {
     422                        // ddprintf("HEAP: compact found pointer %p (size=%ui) after pmbFree %p\n", (void __far *) pmb, pmb->uSize, (void __far *) pmbFree);
     423            pmbFree->ulSize += HDR_SIZE + pmb->ulSize;
     424            remove(pmb);
     425            if (++sFreed == 2) break;
    489426        }
    490 
    491         if (after(pmb)  == pmbFree) {
    492 // ddprintf("HEAP: compact found pointer %p (size=%ui) before pmbFree %p\n", (void __far *) pmb, pmb->uSize, (void __far *) pmbFree);
    493             pmb->uSize += HDR_SIZE + pmbFree->uSize;
    494             remove(pmbFree);
    495             if (++iFreed == 2) goto exit;
    496         }
    497         else
    498         if (after(pmbFree) == pmb) {
    499 // ddprintf("HEAP: compact found pointer %p (size=%ui) after pmbFree %p\n", (void __far *) pmb, pmb->uSize, (void __far *) pmbFree);
    500             pmbFree->uSize += HDR_SIZE + pmb->uSize;
    501             remove(pmb);
    502             if (++iFreed == 2) goto exit;
    503         }
    504     }
    505 
    506 exit:
    507     nCompact += iFreed;
     427    }
     428
     429    ulnCompact += sFreed;
    508430}
    509431//*****************************************************************************
     
    516438{
    517439    PMEMBLOCK pmb,pmbPrev,pmbBlock;
    518     int fSentinel;
    519     ULONG cpuflags;
     440    ULONG ulCpuFlags;
    520441
    521442    if(acHeap == NULL) {
     
    524445    }
    525446
    526     ++nFreeCalls;
     447    ++ulnFreeCalls;
    527448    if (!pvBlock) return;     // support freeing of NULL
    528449
    529     cpuflags = DevPushfCli();
     450    ulCpuFlags = DevPushfCli();
    530451    pmbBlock=(PMEMBLOCK) ((char NEAR *) pvBlock - HDR_SIZE);
    531452
     
    533454    HeapCheck((PSZ) "free() entry" );
    534455
    535     uMemFree += pmbBlock->uSize + HDR_SIZE;
    536 
    537     if (pmbBlock == pmbUsed) {       // are we freeing the first block?
    538         pmbUsed = pmbUsed->pmbNext;   // the 2nd block is now the 1st
    539         pmbBlock->pmbNext = pmbFree;  // this block is now free, so it points to 1st free block
    540         pmbFree = pmbBlock;           // this is now the 1st free block
    541         compact();
    542         goto exit;
    543     }
    544 
    545     pmbPrev=pmbUsed;
    546     fSentinel = FALSE;
    547     for (pmb=pmbUsed->pmbNext; pmb; pmbPrev=pmb, pmb=pmb->pmbNext) {
    548         if (pmb == pmbBlock) {
    549             if (fSentinel) {
    550                 dprintf(("HEAP: free sentinel triggered, pmb=%p\n", (void __far *) pmb));
    551                 DebugInt3();
    552             }
    553             pmbPrev->pmbNext = pmb->pmbNext;   // delete this block from the chain
    554             pmbBlock->pmbNext = pmbFree;
    555             pmbFree = pmbBlock;
    556             compact();
    557             fSentinel = TRUE;
    558         }
    559     }
    560 
    561 exit: //--- Check things are still intact.
     456    uMemFree += pmbBlock->ulSize + HDR_SIZE;
     457
     458        /* find the block on the used chain */
     459        for (pmbPrev=NULL, pmb=pmbUsed; pmb; pmbPrev=pmb, pmb=pmb->pmbNext) {
     460                if (pmb == pmbBlock) { /* found the block */
     461                        if (pmbPrev) { /* it is in the middle of the chain */
     462                                pmbPrev->pmbNext = pmb->pmbNext; /* delete this block from the chain */
     463                        } else { /* it is the first block on the used list */
     464                                pmbUsed = pmbUsed->pmbNext;     // the 2nd block is now the head
     465                        }
     466                        pmbBlock->pmbNext = pmbFree; /* This block is now free, so it points to the first free block */
     467                        pmbFree = pmbBlock; /* This is now the first free block */
     468                        break;
     469                }
     470        }
     471        if (pmb == 0) {
     472                dprintf(("free: block not found %x\n", pmb));
     473                DebugInt3();
     474        }
     475        compact();
     476
    562477    HeapCheck((PSZ) "free() exit" );
    563     DevPopf(cpuflags);
     478    DevPopf(ulCpuFlags);
    564479}
    565480//*****************************************************************************
     
    571486//*****************************************************************************
    572487//*****************************************************************************
    573 #ifdef DEBUGHEAP
    574 void NEAR *realloc(void NEAR *pvBlock, unsigned usLength, const char *filename, int lineno)
    575 #else
    576 void NEAR *realloc(void NEAR *pvBlock, unsigned usLength)
     488/**@internal _msize
     489 */
     490unsigned _msize(void NEAR *pvBlock)
     491{
     492    PMEMBLOCK pmb;
     493
     494    if (!pvBlock)
     495        return 0;
     496
     497    pmb = (PMEMBLOCK) ((char NEAR *) pvBlock - HDR_SIZE);
     498
     499    return pmb->ulSize;
     500}
     501
     502
     503#ifdef DEBUGHEAP
     504void NEAR *realloc(void NEAR *pvBlock, ULONG ulLength, const char *filename, int lineno)
     505#else
     506void NEAR *realloc(void NEAR *pvBlock, ULONG ulLength)
    577507#endif
    578508{
     
    581511    if (!pvBlock)                 // if ptr is null, alloc block
    582512#ifdef DEBUGHEAP
    583         return malloc(usLength, filename, lineno);
    584 #else
    585         return malloc(usLength);
    586 #endif
    587 
    588     if (!usLength) {              // if length is 0, free block
     513        return malloc(ulLength, filename, lineno);
     514#else
     515        return malloc(ulLength);
     516#endif
     517
     518    if (!ulLength) {              // if length is 0, free block
    589519#ifdef DEBUGHEAP
    590520        free(pvBlock, filename, lineno);
     
    596526
    597527#ifdef DEBUGHEAP
    598     pv = malloc(usLength, filename, lineno);          // attempt to allocate the new block
    599 #else
    600     pv = malloc(usLength);          // attempt to allocate the new block
     528    pv = malloc(ulLength, filename, lineno);          // attempt to allocate the new block
     529#else
     530    pv = malloc(ulLength);          // attempt to allocate the new block
    601531#endif
    602532    if (!pv)                      // can't do it?
    603533        return 0;                  // just fail.  Version 2 will try harder
    604    
    605     memcpy(pv, pvBlock, min( _msize(pvBlock), usLength));
     534
     535    memcpy(pv, pvBlock, min( _msize(pvBlock), ulLength));
    606536
    607537#ifdef DEBUGHEAP
     
    614544//*****************************************************************************
    615545//*****************************************************************************
    616 BOOL IsHeapAddr(ULONG addr) 
     546BOOL IsHeapAddr(ULONG addr)
    617547{
    618548    int bGoodPointer;
     
    626556extern "C" APIRET VMAlloc(ULONG size, ULONG flags, LINEAR *pAddr) ;
    627557//*****************************************************************************
    628 unsigned HeapInit(unsigned uSize)
     558ULONG HeapInit(ULONG ulSize)
    629559{
    630560    LINEAR addr;
    631561    SHORT sel;
    632562
    633     if (!uSize)
    634         uSize = DEFAULT_HEAP;
    635 
    636     if (uSize > HEAP_SIZE)
    637         uSize = HEAP_SIZE;
    638 
    639     if(VMAlloc(uSize, VMDHA_FIXED, &addr)) {
     563    if (!ulSize) ulSize = DEFAULT_HEAP;
     564
     565    if (ulSize > HEAP_SIZE) ulSize = HEAP_SIZE;
     566
     567    if(VMAlloc(ulSize, VMDHA_FIXED, &addr)) {
    640568            DebugInt3();
    641569            return 0;
    642570    }
    643571    acHeap = addr;
    644 #ifdef DEBUG
    645 //    dprintf(("HeapInit(): addr: %x", acHeap));
    646 #endif
     572        //dprintf(("HeapInit(): addr: %x", acHeap));
    647573
    648574    pmbFree = (PMEMBLOCK) acHeap;
    649     pmbFree->uSize = uSize - HDR_SIZE;
     575    pmbFree->ulSize = ulSize - HDR_SIZE;
    650576    pmbFree->ulSignature = SIGNATURE;
    651577    pmbFree->pmbNext = 0;
    652     uMemFree = pmbFree->uSize;
    653     return pmbFree->uSize;
    654 }
    655 //*****************************************************************************
    656 //*****************************************************************************
     578    uMemFree = pmbFree->ulSize;
     579    return pmbFree->ulSize;
     580}
     581//*****************************************************************************
     582//*****************************************************************************
Note: See TracChangeset for help on using the changeset viewer.