#include "camtypes.h"
#include <stdio.h>
Go to the source code of this file.
Classes | |
class | DynamicHeap |
Defines | |
#define | new CAM_DEBUG_NEW |
Functions | |
DECLARE_SOURCE ("$Revision: 1492 $") | |
static DynamicHeap * | FindHeap (MHANDLE Handle) |
This function works out which heap the block associated with the handle is in. | |
BOOL | InitMemory () |
Initialises the dynamic memory manager. This involves obtaining a block of memory from the OS. This is then used as a pool to satisfy allocation requests from clients of this module. | |
void | DeinitMemory () |
Returns the memory pool to the OS. | |
MHANDLE | ClaimBlock (size_t Size, BOOL Heavy) |
Allocates a block of 'size' bytes from the free pool, and obtains a handle or it. Blocks which are zero bytes long are permitted, but blocks must be word-sized (i.e. a multiple of 4 bytes in size). | |
BOOL | ReleaseBlock (MHANDLE Handle) |
Given a handle to a memory block, releases this memory block from use, and returns the block to the free pool. Subsequent accesses via this handle or via the block's address cause undefined behaviour. | |
BOOL | DescribeBlock (MHANDLE Handle, ADDR *Address, size_t *Size) |
Given a handle, returns the size and location of the associated block of memory. | |
BOOL | SplitBlock (MHANDLE Handle, INT32 SplitSize, UINT32 Offset) |
NB. Do not use this function! Use on of the following instead: IncreaseBlock, DecreaseBlock. | |
BOOL | IncreaseBlock (MHANDLE Handle, UINT32 NumBytes) |
Increase the size of the specified block by the specified amount. No memory is moved within the block - the block is simply extended. NB. This operation may cause the block to move, so the handle should be realised into an address again (via DescribeBlock()) after this function returns. | |
BOOL | DecreaseBlock (MHANDLE Handle, UINT32 NumBytes) |
Decrease the size of the specified block by the specified amount. No memory is moved within the block - the block is simply reduced in size, and the memory is chopped off the end. NB. This operation may cause the block to move, so the handle should be realised into an address again (via DescribeBlock()) after this function returns. | |
BOOL | InsertMemory (MHANDLE Handle, UINT32 Offset, UINT32 NumBytes) |
Insert memory into the middle of a block at the position specified by the 'Offset' parameter. The memory above the offset is shuffled up (i.e. it is not overwritten). i.e. the 'B' block is moved up in order to insert the new memory. The new memory (the '?' block) is uninitialised, so you must initialise it yourself. | |
BOOL | RemoveMemory (MHANDLE Handle, UINT32 Offset, UINT32 NumBytes) |
Remove memory from the middle of a block at the position specified by the 'Offset' parameter. The memory contents above the offset is shuffled down (i.e. it is not lost). i.e. the 'B' block is moved down in order to preserve its contents. The 'A' block is truncated, and the last 'NumBytes' of it are lost. | |
BOOL | TidyMemory () |
Removes one hole from the heap (if any exist). It always removes the hole which is lowest in memory in an attempt to avoid redundant memory moves. Intended to be called on idle events to sanitise the heap when nothing much else is going on. | |
void | GetMemoryStatus (UINT64 *pPhysRam, UINT32 *pLoadPercent) |
Read the current state of the memory environment for Camelot. | |
Variables | |
static DynamicHeap * | NormalHeap = NULL |
|
Definition at line 123 of file cammemory.cpp. |
|
Allocates a block of 'size' bytes from the free pool, and obtains a handle or it. Blocks which are zero bytes long are permitted, but blocks must be word-sized (i.e. a multiple of 4 bytes in size).
Definition at line 670 of file cammemory.cpp. 00671 { 00672 // Make sure we have a heap 00673 if (NormalHeap==NULL) 00674 { 00675 ERROR3("The Heap is not there!"); 00676 return BAD_MHANDLE; 00677 } 00678 00679 // claim some memory from it 00680 return NormalHeap->ClaimBlock(Size); 00681 }
|
|
|
|
Decrease the size of the specified block by the specified amount. No memory is moved within the block - the block is simply reduced in size, and the memory is chopped off the end. NB. This operation may cause the block to move, so the handle should be realised into an address again (via DescribeBlock()) after this function returns.
Definition at line 825 of file cammemory.cpp. 00826 { 00827 // Make sure we have a heap 00828 if (NormalHeap==NULL) 00829 { 00830 ERROR3("The Heap is not there!"); 00831 return FALSE; 00832 } 00833 00834 // Decrease the block 00835 return NormalHeap->DecreaseBlock(Handle, NumBytes); 00836 }
|
|
Returns the memory pool to the OS.
Definition at line 647 of file cammemory.cpp. 00648 { 00649 if (NormalHeap != NULL) 00650 delete NormalHeap; 00651 }
|
|
Given a handle, returns the size and location of the associated block of memory.
Definition at line 727 of file cammemory.cpp. 00728 { 00729 // Get details associated with this handle 00730 *Address = DescribeHandle(Handle); 00731 00732 if (*Address == NULL) 00733 { 00734 // Handle was invalid - return error 00735 TRACE( wxT("DescribeBlock: Handle is invalid\n") ); 00736 *Address = NULL; 00737 *Size = 0; 00738 return FALSE; 00739 } 00740 00741 // Extract the size from the block 00742 *Size = CCGetBlockSize(*Address); 00743 00744 // Everything went ok 00745 return TRUE; 00746 }
|
|
This function works out which heap the block associated with the handle is in.
Definition at line 592 of file cammemory.cpp. 00593 { 00594 ADDR Address = DescribeHandle(Handle); 00595 00596 if (Address != BAD_MHANDLE) 00597 { 00598 // return the heap 00599 return NormalHeap; 00600 } 00601 00602 // Couldn't find the correct heap 00603 return NULL; 00604 }
|
|
Read the current state of the memory environment for Camelot.
Definition at line 981 of file cammemory.cpp. 00982 { 00983 #if defined(__WXMSW__) 00984 // Windows: 00985 // It's important to call GlobalMemoryStatusEX if it's available 00986 // because GlobalMemoryStatus has issues in older OSs that can cause it to return 00987 // duff information. 00988 GETMEMORYSTATUSEX_FUNC pExFunc; 00989 pExFunc = (GETMEMORYSTATUSEX_FUNC) GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "GlobalMemoryStatusEx"); 00990 00991 if (pExFunc) 00992 { 00993 MEMORYSTATUSEX memStatus; 00994 memStatus.dwLength=sizeof(MEMORYSTATUSEX); 00995 pExFunc(&memStatus); 00996 00997 if (pLoadPercent) *pLoadPercent = memStatus.dwMemoryLoad; 00998 if (pPhysRam) *pPhysRam = memStatus.ullTotalPhys; 00999 } 01000 else 01001 { 01002 MEMORYSTATUS memStatus; 01003 memStatus.dwLength=sizeof(MEMORYSTATUS); 01004 GlobalMemoryStatus(&memStatus); 01005 01006 if (pLoadPercent) *pLoadPercent = memStatus.dwMemoryLoad; 01007 if (pPhysRam) *pPhysRam = memStatus.dwTotalPhys; 01008 } 01009 #elsif defined(__WXGTK__) 01010 // Assume Linux - there should really be some FreeBSD code here 01011 01012 /* Linux: read memory information from the kernel's /proc/meminfo interface */ 01013 FILE *fp; 01014 unsigned long /*TYPENOTE: Correct*/ memTotalKb, memFreeKb; 01015 int /*TYPENOTE: Correct*/ haveMemTotal = 0, haveMemFree = 0; 01016 char lineBuf[256]; 01017 01018 fp = fopen("/proc/meminfo", "r"); 01019 01020 if (fp != NULL) 01021 { 01022 while (!haveMemTotal || !haveMemFree) 01023 { 01024 if (fgets(lineBuf, 256, fp) == NULL) 01025 break; 01026 01027 if (sscanf(lineBuf, "MemTotal: %lu", &memTotalKb) == 1) 01028 haveMemTotal = 1; 01029 if (sscanf(lineBuf, "MemFree: %lu", &memFreeKb) == 1) 01030 haveMemFree = 1; 01031 } 01032 fclose(fp); 01033 } 01034 01035 if (!haveMemTotal) 01036 memTotalKb = 512UL * 1024; /* guess 512MB */ 01037 if (!haveMemFree) 01038 memFreeKb = memTotalKb / 2; /* guess 50% free */ 01039 01040 if (pPhysRam != NULL) 01041 *pPhysRam = (UINT64)memTotalKb * 1024; 01042 if (pLoadPercent != NULL) 01043 *pLoadPercent = (UINT32)(100UL - ((memFreeKb * 100UL) / memTotalKb)); 01044 #else 01045 PORTNOTETRACE("other", "GetMemoryStatus is not implemented on this architecture"); 01046 if (pPhysRam) *pPhysRam = 512L * 1024 * 1024; // Guess 512M 01047 if (pLoadPercent) *pLoadPercent = 50; // Guess 50% free 01048 #endif 01049 }
|
|
Increase the size of the specified block by the specified amount. No memory is moved within the block - the block is simply extended. NB. This operation may cause the block to move, so the handle should be realised into an address again (via DescribeBlock()) after this function returns.
Definition at line 792 of file cammemory.cpp. 00793 { 00794 // Make sure we have a heap 00795 if (NormalHeap==NULL) 00796 { 00797 ERROR3("The Heap is not there!"); 00798 return FALSE; 00799 } 00800 00801 // Increase the block 00802 return NormalHeap->IncreaseBlock(Handle, NumBytes); 00803 }
|
|
Initialises the dynamic memory manager. This involves obtaining a block of memory from the OS. This is then used as a pool to satisfy allocation requests from clients of this module.
Definition at line 620 of file cammemory.cpp. 00621 { 00622 // Initialise the two heaps - one for normal objects, one for hevy objects (bitmaps etc) 00623 NormalHeap = new DynamicHeap; 00624 if (NormalHeap==NULL) 00625 return FALSE; 00626 00627 if (!NormalHeap->Init()) 00628 // report failure of heap initialisation 00629 return FALSE; 00630 00631 // Everything went ok 00632 return TRUE; 00633 }
|
|
Insert memory into the middle of a block at the position specified by the 'Offset' parameter. The memory above the offset is shuffled up (i.e. it is not overwritten). i.e. the 'B' block is moved up in order to insert the new memory. The new memory (the '?' block) is uninitialised, so you must initialise it yourself.
Definition at line 862 of file cammemory.cpp. 00863 { 00864 ERROR3("InsertMemory function is now dead. Use IncreaseBlock or DecreaseBlock instead\n"); 00865 return FALSE; 00866 }
|
|
Given a handle to a memory block, releases this memory block from use, and returns the block to the free pool. Subsequent accesses via this handle or via the block's address cause undefined behaviour.
Definition at line 697 of file cammemory.cpp. 00698 { 00699 // Make sure we have a heap 00700 if (NormalHeap==NULL) 00701 { 00702 ERROR3("The Heap is not there!"); 00703 return FALSE; 00704 } 00705 00706 // Release the memory 00707 return NormalHeap->ReleaseBlock(Handle); 00708 }
|
|
Remove memory from the middle of a block at the position specified by the 'Offset' parameter. The memory contents above the offset is shuffled down (i.e. it is not lost). i.e. the 'B' block is moved down in order to preserve its contents. The 'A' block is truncated, and the last 'NumBytes' of it are lost.
Definition at line 891 of file cammemory.cpp. 00892 { 00893 ERROR3("RemoveMemory function is now dead. Use IncreaseBlock or DecreaseBlock instead\n"); 00894 return FALSE; 00895 }
|
|
NB. Do not use this function! Use on of the following instead: IncreaseBlock, DecreaseBlock.
Definition at line 765 of file cammemory.cpp. 00766 { 00767 ERROR3("SplitBlock function is now dead. Use IncreaseBlock or DecreaseBlock instead\n"); 00768 return FALSE; 00769 }
|
|
Removes one hole from the heap (if any exist). It always removes the hole which is lowest in memory in an attempt to avoid redundant memory moves. Intended to be called on idle events to sanitise the heap when nothing much else is going on.
Definition at line 911 of file cammemory.cpp. 00912 { 00913 return FALSE; 00914 }
|
|
Definition at line 325 of file cammemory.cpp. |