VOID CheckHeap(VOID) { USHORT usSel; BYTE _huge * pHeapStart; BYTE _huge * pHeapEnd; BYTE _huge * pWork; ULONG ulTotal = 0; for (usSel = 0; usSel < MAX_SELECTORS; usSel++) { pHeapStart = rgpSegment[usSel]; if (!pHeapStart) continue; pHeapEnd = pHeapStart + HEAP_SIZE; pWork = pHeapStart; while (pWork < pHeapEnd) { if (!IsBlockFree(pWork)) ulTotal += BlockSize(pWork); pWork += BlockSize(pWork) + sizeof (ULONG); } if (pWork != pHeapEnd) errmsg("Heap corruption found!"); } if (ulTotal != ulMem) { fflush(stdout); errmsg("Memory lost! %lu %lu %s", ulMem, ulTotal, szLast); } }
//***************************************************************************** // // Report to the user the amount of free space and used space in the data // storage area. // //***************************************************************************** void FlashStoreReport(void) { unsigned long ulAddr; unsigned long ulFreeBlocks = 0; unsigned long ulUsedBlocks = 0; static char cBufFree[16]; static char cBufUsed[16]; // // Loop through each block of the storage area and count how many blocks // are free and non-free. // for(ulAddr = FLASH_STORE_START_ADDR; ulAddr < FLASH_STORE_END_ADDR; ulAddr += 0x400) { if(IsBlockFree(ulAddr)) { ulFreeBlocks++; } else { ulUsedBlocks++; } } // // Report the result to the user via a status display screen. // usnprintf(cBufFree, sizeof(cBufFree), "FREE: %3uK", ulFreeBlocks); usnprintf(cBufUsed, sizeof(cBufUsed), "USED: %3uK", ulUsedBlocks); SetStatusText("FREE FLASH", cBufFree, cBufUsed, "PRESS <"); }
VOID SetBlockSize(PBYTE pMCB, ULONG ulSize) { BOOL fFree = IsBlockFree(pMCB); *((PULONG)pMCB) = ulSize; if (fFree) SetFree(pMCB); }
/********************************************************************* * FindFreeSpace *********************************************************************/ void * FindFreeSpace(void * pStart, size_t tSize) { BYTE _huge * pHeapStart; BYTE _huge * pHeapEnd; BYTE _huge * pWork; BYTE _huge * pNext; USHORT rc; pHeapStart = pStart; pHeapEnd = pHeapStart + HEAP_SIZE; rc = MY_PROBEBUF(PB_OPREAD, (PBYTE)pHeapStart, HEAP_SIZE); if (rc) { CritMessage("FAT32: Protection VIOLATION in FindFreeSpace (SYS%d)", rc); Message("FAT32: Protection VIOLATION in FindFreeSpace (SYS%d)", rc); return NULL; } pWork = pHeapStart; while (pWork < pHeapEnd) { if (BlockSize(pWork) >= tSize && IsBlockFree(pWork)) { ULONG ulRemaining = BlockSize(pWork) - tSize; if (ulRemaining > sizeof (ULONG)) { pNext = pWork + sizeof (ULONG) + tSize; SetBlockSize(pNext, BlockSize(pWork) - tSize - sizeof (ULONG)); SetFree(pNext); SetBlockSize(pWork, tSize); } SetInUse(pWork); return pWork + sizeof (ULONG); } pWork += BlockSize(pWork) + sizeof (ULONG); } return NULL; }
/********************************************************************* * FindFreeSpace *********************************************************************/ void * FindFreeSpace(void * pStart, size_t tSize) { BYTE _huge * pHeapStart; BYTE _huge * pHeapEnd; BYTE _huge * pWork; BYTE _huge * pNext; pHeapStart = pStart; pHeapEnd = pHeapStart + HEAP_SIZE; pWork = pHeapStart; while (pWork < pHeapEnd) { if (BlockSize(pWork) >= tSize && IsBlockFree(pWork)) { ULONG ulRemaining = BlockSize(pWork) - tSize; if (ulRemaining > sizeof (ULONG)) { pNext = pWork + sizeof (ULONG) + tSize; SetBlockSize(pNext, BlockSize(pWork) - tSize - sizeof (ULONG)); SetFree(pNext); SetBlockSize(pWork, tSize); } else { printf("Remaining %lu bytes\n", ulRemaining); ulMem += ulRemaining; } SetInUse(pWork); return pWork + sizeof (ULONG); } pWork += BlockSize(pWork) + sizeof (ULONG); } return NULL; }
//***************************************************************************** // // Report to the user the amount of free space and used space in the data // storage area. // //***************************************************************************** void FlashStoreReport(void) { uint32_t ui32Addr, ui32FreeBlocks, ui32UsedBlocks = 0; static char pcBufFree[16], pcBufUsed[16]; // // Initialize locals. // ui32FreeBlocks = 0; ui32UsedBlocks = 0; // // Loop through each block of the storage area and count how many blocks // are free and non-free. // for(ui32Addr = FLASH_STORE_START_ADDR; ui32Addr < FLASH_STORE_END_ADDR; ui32Addr += 0x400) { if(IsBlockFree(ui32Addr)) { ui32FreeBlocks++; } else { ui32UsedBlocks++; } } // // Report the result to the user via a status display screen. // usnprintf(pcBufFree, sizeof(pcBufFree), "FREE: %3uK", ui32FreeBlocks); usnprintf(pcBufUsed, sizeof(pcBufUsed), "USED: %3uK", ui32UsedBlocks); SetStatusText("FREE FLASH", pcBufFree, pcBufUsed, "PRESS <"); }
void cdecl Free(void * pntr) { USHORT usSel; BYTE _huge * pHeapStart; BYTE _huge * pHeapEnd; BYTE _huge * pWork; BYTE _huge * pToFree = pntr; BYTE _huge * pPrev; BYTE _huge * pNext; #if 0 if (OFFSETOF(pntr) == 0) return Freeseg(pntr); #endif for (usSel = 0; usSel < MAX_SELECTORS; usSel++) { if (SELECTOROF(pntr) == SELECTOROF(rgpSegment[usSel])) break; } if (usSel == MAX_SELECTORS) errmsg("FAT32: %lX not found in Free!", pntr); ulMem -= *(PULONG)((PBYTE)pntr - sizeof (ULONG)); sprintf(szLast, "Free %lu bytes", *(PULONG)((PBYTE)pntr - sizeof (ULONG))); printf("%s\n", szLast); pHeapStart = rgpSegment[usSel]; pHeapEnd = pHeapStart + HEAP_SIZE; pWork = pHeapStart; pPrev = NULL; while (pWork < pHeapEnd) { if (pWork + sizeof (ULONG) == pToFree) { if (pPrev && IsBlockFree(pPrev)) { SetBlockSize(pPrev, BlockSize(pPrev) + BlockSize(pWork) + sizeof (ULONG)); pWork = pPrev; } pNext = pWork + BlockSize(pWork) + sizeof (ULONG); if (pNext < pHeapEnd && IsBlockFree(pNext)) SetBlockSize(pWork, BlockSize(pWork) + BlockSize(pNext) + sizeof (ULONG)); SetFree(pWork); break; } else pPrev = pWork; pWork += BlockSize(pWork) + sizeof (ULONG); } if (pWork >= pHeapEnd) { errmsg("FAT32: ERROR: Address not found in Free"); return; } /* Free selector if no longer needed */ if (usSel > 0 && BlockSize(rgpSegment[usSel]) == (HEAP_SIZE - sizeof (ULONG)) && IsBlockFree(rgpSegment[usSel])) { Freeseg(rgpSegment[usSel]); rgpSegment[usSel] = NULL; } }
void cdecl free(void * pntr) #endif { USHORT usSel; BYTE _huge * pHeapStart; BYTE _huge * pHeapEnd; BYTE _huge * pWork; BYTE _huge * pToFree = pntr; BYTE _huge * pPrev; BYTE _huge * pNext; USHORT rc; if (f32Parms.fMessageActive & LOG_MEM) Message("free %lX", pntr); // CheckHeap(); if (OFFSETOF(pntr) == 0) { freeseg(pntr); return; } GetMemAccess(); for (usSel = 0; usSel < MAX_SELECTORS; usSel++) { if (SELECTOROF(pntr) == SELECTOROF(rgpSegment[usSel])) break; } if (usSel == MAX_SELECTORS) { CritMessage("FAT32: %lX not found in free!", pntr); Message("FAT32: %lX not found in free!", pntr); ReleaseMemAccess(); return; } pHeapStart = rgpSegment[usSel]; pHeapEnd = pHeapStart + HEAP_SIZE; rc = MY_PROBEBUF(PB_OPREAD, (PBYTE)pHeapStart, HEAP_SIZE); if (rc) { CritMessage("FAT32: Protection VIOLATION in free (SYS%d)", rc); Message("FAT32: Protection VIOLATION in free (SYS%d)", rc); ReleaseMemAccess(); return; } pWork = pHeapStart; pPrev = NULL; while (pWork < pHeapEnd) { if (pWork + sizeof (ULONG) == pToFree) { if (pPrev && IsBlockFree(pPrev)) { SetBlockSize(pPrev, BlockSize(pPrev) + BlockSize(pWork) + sizeof (ULONG)); pWork = pPrev; } pNext = pWork + BlockSize(pWork) + sizeof (ULONG); if (pNext < pHeapEnd && IsBlockFree(pNext)) SetBlockSize(pWork, BlockSize(pWork) + BlockSize(pNext) + sizeof (ULONG)); SetFree(pWork); break; } else pPrev = pWork; pWork += BlockSize(pWork) + sizeof (ULONG); } if (pWork >= pHeapEnd) { CritMessage("FAT32: ERROR: Address not found in free"); Message("ERROR: Address not found in free"); ReleaseMemAccess(); return; } /* free selector if no longer needed */ if (usSel > 0 && BlockSize(rgpSegment[usSel]) == (HEAP_SIZE - sizeof (ULONG)) && IsBlockFree(rgpSegment[usSel])) { PBYTE p = rgpSegment[usSel]; rgpSegment[usSel] = NULL; freeseg(p); } ReleaseMemAccess(); }
bool MemoryManager::IsPreviousBlockFree(char * const ptr) const { return IsBlockFree(GetPreviousBlock(ptr)); }
bool MemoryManager::IsNextBlockFree(char * const ptr) const { return IsBlockFree(GetNextBlock(ptr)); }