Changeset 502 for GPL/trunk/lib32/malloc.cpp
- Timestamp:
- Jul 11, 2010, 7:06:19 AM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GPL/trunk/lib32/malloc.cpp
r73 r502 38 38 #endif 39 39 40 #define DEFAULT_HEAP 256*102441 #define HEAP_SIZE 256*102442 43 40 #define SIGNATURE 0xBEEFDEAD // "DeadBeef" when view in hex in word order. 44 41 … … 46 43 typedef struct _MEMBLOCK { 47 44 ULONG ulSignature; 48 ULONG u Size;45 ULONG ulSize; 49 46 struct _MEMBLOCK NEAR *pmbNext; 50 47 #ifdef DEBUGHEAP … … 69 66 LINEAR acHeap = NULL; 70 67 68 ULONG ulnAlloc = 0; // Current number of allocated blocks. 69 ULONG ulcAlloc = 0; // Total memory in allocated blocks incl. headers. 70 ULONG ulnAllocCalls = 0; // Cumulative total, number of malloc() calls. 71 ULONG ulnFree = 0; // Current number of free blocks. 72 ULONG ulcFree = 0; // Total memory in free blocks incl. headers. 73 ULONG ulnFreeCalls = 0; // Cumulative total, number of free() calls. 74 ULONG ulnCompact = 0; // Num of times we've joined two adjacent free 75 // blocks into a single larger block. 76 ULONG ulcTotalHeap = HEAP_SIZE; 77 78 71 79 #pragma off (unreferenced) 72 80 … … 77 85 void dumpheap(void) 78 86 { 79 int i;87 SHORT sI; 80 88 PMEMBLOCK pmb; 81 unsigned u=0; 82 89 ULONG ulSize; 90 91 ulSize = 0; 83 92 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; 88 98 pmb=pmb->pmbNext; 89 99 if (!pmb) break; 90 100 } 91 dprintf((" Total used = % ui\n",u));92 93 u =0;101 dprintf((" Total used = %lx\n", ulSize)); 102 103 ulSize=0; 94 104 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; 99 109 pmb=pmb->pmbNext; 100 110 if (!pmb) break; 101 111 } 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 //***************************************************************************** 119 SHORT SignatureCheck ( PMEMBLOCK p, PSZ idText ) 120 { 121 short sGoodPointer; 123 122 124 123 // Set bGoodPointer to indicate whether or not 'p' points within heap. 125 bGoodPointer = ((LINEAR)p >= (acHeap))126 127 124 sGoodPointer = ((LINEAR)p >= (acHeap)) 125 && ((LINEAR)p <= (( acHeap) + HEAP_SIZE)) ; 126 //### heap might have less than HEAP_SIZE bytes 128 127 129 128 // 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)); 136 133 DebugInt3(); 137 134 } 138 return bGoodPointer; 139 } 140 //***************************************************************************** 141 //***************************************************************************** 142 #ifdef DEBUG 135 return sGoodPointer; 136 } 137 //***************************************************************************** 138 //***************************************************************************** 143 139 void HeapCheck ( PSZ idText ) 144 140 { 145 141 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; 148 145 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; 153 154 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 //***************************************************************************** 172 169 173 170 // Threshold for creating a new free block. 174 171 #define MIN_Leftover (HDR_SIZE + MIN_FragSize) 175 172 176 /* make_new_free()173 /* 177 174 Formats the back part of an existing free MEMBLOCK as a new, smaller 178 175 "Free" MEMBLOCK. Doesn't update Global vbls (caller responsibility). … … 227 224 We split the block into an allocated part to satisfy the allocation request, 228 225 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 275 228 * Update all data structures for allocation of one memory block. It's 276 229 * assumed, on entry, that the block is large enough for the requested … … 286 239 * suitable for return to malloc() caller. 287 240 */ 288 void NEAR *npvAllocateBlock( PMEMBLOCK pmb, ULONG uRequest, PMEMBLOCK pmbPrev ) 289 { 241 void NEAR *npvAllocateBlock( PMEMBLOCK pmb, ULONG ulRequest, PMEMBLOCK pmbPrev ) 242 { 243 290 244 //pmb points to the selected block. 291 245 //pmbPrev points to the block prior to the selected block, NULL if pmbFree. … … 294 248 295 249 // 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 } 297 263 298 264 // Update global memory counter. 299 uMemFree -= (pmb->u Size + HDR_SIZE);265 uMemFree -= (pmb->ulSize + HDR_SIZE); 300 266 301 267 // Add this block into the front of the Allocated list. … … 305 271 306 272 // 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; 320 280 } 321 281 … … 336 296 337 297 #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 u Request; // Request size after alignment.298 void NEAR *malloc(ULONG ulSize, const char *filename, int lineno) 299 #else 300 void NEAR *malloc( ULONG ulSize ) 301 #endif 302 { 303 ULONG ulRequest; // Request size after alignment. 344 304 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) { 364 311 DebugInt3(); 365 312 return 0; 366 313 } 367 314 368 cpuflags = DevPushfCli(); 369 ++nAllocCalls; // Diagnostics. 315 ulCpuFlags = DevPushfCli(); 316 npvReturn = 0; 317 ++ulnAllocCalls; // Diagnostics. 370 318 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 } 392 328 393 329 if (npvReturn) { … … 399 335 #endif 400 336 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")); 407 339 DebugInt3(); 408 340 } 409 341 410 342 HeapCheck((PSZ) "malloc() exit" ); 411 #ifdef DEBUG 412 // dprintf(("malloc(): malloc exit")); 413 #endif 414 415 DevPopf(cpuflags); 343 DevPopf(ulCpuFlags); 416 344 return npvReturn; 417 345 } … … 448 376 Also, the newly freed block can only be adjacent to at most two other 449 377 blocks. Therefore, the operation of combining two adjacent free blocks can 450 only happen at most twice. The variable nFreed counts the number of times451 two blocks are combined. The function exits if nFreed reaches two.nFreed378 only happen at most twice. The variable ulnFreed counts the number of times 379 two blocks are combined. The function exits if ulnFreed reaches two. ulnFreed 452 380 is initially 0. 453 381 … … 459 387 */ 460 388 461 #define after(pmb) ((PMEMBLOCK) ((char *) pmb + pmb->u Size + HDR_SIZE))389 #define after(pmb) ((PMEMBLOCK) ((char *) pmb + pmb->ulSize + HDR_SIZE)) 462 390 463 391 void remove(PMEMBLOCK pmb) … … 470 398 } 471 399 472 for (pmbPrev=pmbFree; pmbPrev; pmbPrev=pmbPrev->pmbNext) 400 for (pmbPrev=pmbFree; pmbPrev; pmbPrev=pmbPrev->pmbNext) { 473 401 if (pmbPrev->pmbNext == pmb) { 474 402 pmbPrev->pmbNext = pmb->pmbNext; 475 403 return; 476 404 } 405 } 477 406 } 478 407 //***************************************************************************** … … 481 410 { 482 411 PMEMBLOCK pmb; 483 int iFreed = 0; 484 412 SHORT sFreed; 413 414 sFreed = 0; 485 415 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; 489 426 } 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; 508 430 } 509 431 //***************************************************************************** … … 516 438 { 517 439 PMEMBLOCK pmb,pmbPrev,pmbBlock; 518 int fSentinel; 519 ULONG cpuflags; 440 ULONG ulCpuFlags; 520 441 521 442 if(acHeap == NULL) { … … 524 445 } 525 446 526 ++ nFreeCalls;447 ++ulnFreeCalls; 527 448 if (!pvBlock) return; // support freeing of NULL 528 449 529 cpuflags = DevPushfCli();450 ulCpuFlags = DevPushfCli(); 530 451 pmbBlock=(PMEMBLOCK) ((char NEAR *) pvBlock - HDR_SIZE); 531 452 … … 533 454 HeapCheck((PSZ) "free() entry" ); 534 455 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 562 477 HeapCheck((PSZ) "free() exit" ); 563 DevPopf( cpuflags);478 DevPopf(ulCpuFlags); 564 479 } 565 480 //***************************************************************************** … … 571 486 //***************************************************************************** 572 487 //***************************************************************************** 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 */ 490 unsigned _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 504 void NEAR *realloc(void NEAR *pvBlock, ULONG ulLength, const char *filename, int lineno) 505 #else 506 void NEAR *realloc(void NEAR *pvBlock, ULONG ulLength) 577 507 #endif 578 508 { … … 581 511 if (!pvBlock) // if ptr is null, alloc block 582 512 #ifdef DEBUGHEAP 583 return malloc(u sLength, filename, lineno);584 #else 585 return malloc(u sLength);586 #endif 587 588 if (!u sLength) { // if length is 0, free block513 return malloc(ulLength, filename, lineno); 514 #else 515 return malloc(ulLength); 516 #endif 517 518 if (!ulLength) { // if length is 0, free block 589 519 #ifdef DEBUGHEAP 590 520 free(pvBlock, filename, lineno); … … 596 526 597 527 #ifdef DEBUGHEAP 598 pv = malloc(u sLength, filename, lineno); // attempt to allocate the new block599 #else 600 pv = malloc(u sLength); // attempt to allocate the new block528 pv = malloc(ulLength, filename, lineno); // attempt to allocate the new block 529 #else 530 pv = malloc(ulLength); // attempt to allocate the new block 601 531 #endif 602 532 if (!pv) // can't do it? 603 533 return 0; // just fail. Version 2 will try harder 604 605 memcpy(pv, pvBlock, min( _msize(pvBlock), u sLength));534 535 memcpy(pv, pvBlock, min( _msize(pvBlock), ulLength)); 606 536 607 537 #ifdef DEBUGHEAP … … 614 544 //***************************************************************************** 615 545 //***************************************************************************** 616 BOOL IsHeapAddr(ULONG addr) 546 BOOL IsHeapAddr(ULONG addr) 617 547 { 618 548 int bGoodPointer; … … 626 556 extern "C" APIRET VMAlloc(ULONG size, ULONG flags, LINEAR *pAddr) ; 627 557 //***************************************************************************** 628 unsigned HeapInit(unsigned uSize)558 ULONG HeapInit(ULONG ulSize) 629 559 { 630 560 LINEAR addr; 631 561 SHORT sel; 632 562 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)) { 640 568 DebugInt3(); 641 569 return 0; 642 570 } 643 571 acHeap = addr; 644 #ifdef DEBUG 645 // dprintf(("HeapInit(): addr: %x", acHeap)); 646 #endif 572 //dprintf(("HeapInit(): addr: %x", acHeap)); 647 573 648 574 pmbFree = (PMEMBLOCK) acHeap; 649 pmbFree->u Size = uSize - HDR_SIZE;575 pmbFree->ulSize = ulSize - HDR_SIZE; 650 576 pmbFree->ulSignature = SIGNATURE; 651 577 pmbFree->pmbNext = 0; 652 uMemFree = pmbFree->u Size;653 return pmbFree->u Size;654 } 655 //***************************************************************************** 656 //***************************************************************************** 578 uMemFree = pmbFree->ulSize; 579 return pmbFree->ulSize; 580 } 581 //***************************************************************************** 582 //*****************************************************************************
Note:
See TracChangeset
for help on using the changeset viewer.