cammemory.cpp File Reference

(r1785/r1492)

#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 DynamicHeapFindHeap (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 DynamicHeapNormalHeap = NULL


Define Documentation

#define new   CAM_DEBUG_NEW
 

Definition at line 123 of file cammemory.cpp.


Function Documentation

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).

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
26/4/93
Parameters:
Size,: The size in bytes of the block being requested [INPUTS] Heavy: If TRUE, then the block is treated differently to other (non-heavy) blocks, in that heavy blocks are the last to be moved. Large objects (e.g. bitmaps etc) should be declared heavy.
Returns:
The handle that refers to the newly allocated block.

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 }

DECLARE_SOURCE "$Revision: 1492 $"   ) 
 

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.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
06/12/93
Parameters:
Handle - the handle of the block to resize [INPUTS] NumBytes - the number of bytes to remove from this block's allocation (must be word-sized, and >=12 bytes).
Returns:
TRUE if the resize worked, FALSE if it failed.
See also:
IncreaseBlock; InsertMemory; RemoveMemory; DescribeHandle

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 }

void DeinitMemory  ) 
 

Returns the memory pool to the OS.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
26/4/93

Definition at line 647 of file cammemory.cpp.

00648 {
00649     if (NormalHeap != NULL)
00650         delete NormalHeap;
00651 }

BOOL DescribeBlock MHANDLE  Handle,
ADDR Address,
size_t *  Size
 

Given a handle, returns the size and location of the associated block of memory.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
26/4/93
Parameters:
Handle,: The handle of the block to describe [INPUTS]
Address,: The real address of the block [OUTPUTS] Size: The size in bytes of the block
Returns:
TRUE if the handle was valid

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 }

static DynamicHeap* FindHeap MHANDLE  Handle  )  [inline, static]
 

This function works out which heap the block associated with the handle is in.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
09/02/94
Parameters:
Handle - the handle of the block to look for. [INPUTS]
Returns:
Pointer to the heap containing the block identified by 'Handle', or NULL if the block cannot be found.

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 }

void GetMemoryStatus UINT64 pPhysRam,
UINT32 pLoadPercent
 

Read the current state of the memory environment for Camelot.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
09/March/2006
Parameters:
- [INPUTS] Ouputs: PhysRam - Amount of physical RAM (in bytes) CurrentLoadPercent - Percentage of physical RAM in use at the time of this call
Returns:
-

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 }

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.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
06/12/93
Parameters:
Handle - the handle of the block to resize [INPUTS] NumBytes - the number of bytes to add to this block's allocation (must be word-sized, and >=12 bytes).
Returns:
TRUE if the resize worked, FALSE if it failed.
See also:
DecreaseBlock; InsertMemory; RemoveMemory; DescribeHandle

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 }

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.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
26/4/93
Returns:
TRUE if global memory was obtained and initialised successfully.

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 }

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.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
06/12/93
Parameters:
Handle - the handle of the block to resize. [INPUTS] Offset - the position within the block to insert the memory at (must be word-aligned). NumBytes - the number of bytes to add to this block's allocation (must be word-sized, and >=12 bytes).
Returns:
TRUE, if the insertion was successful, FALSE if not.
See also:
IncreaseBlock; DecreaseBlock; RemoveMemory; DescribeHandle; DynamicHeap::InsertMemory

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 }

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.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
26/4/93
Parameters:
Handle,: The handle of the block to be released. [INPUTS]
Returns:
TRUE if the handle was valid

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 }

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.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
06/12/93
Parameters:
Handle - the handle of the block to resize. [INPUTS] Offset - the position within the block to remove the memory at (must be word-aligned). NumBytes - the number of bytes to remove to this block's allocation (must be word-sized, and >=12 bytes).
Returns:
TRUE, if the removal was successful, FALSE if not.
See also:
IncreaseBlock; DecreaseBlock; InsertMemory; DescribeHandle; DynamicHeap::RemoveMemory

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 }

BOOL SplitBlock MHANDLE  Handle,
INT32  SplitSize,
UINT32  Offset
 

NB. Do not use this function! Use on of the following instead: IncreaseBlock, DecreaseBlock.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
26/4/93
Parameters:
Handle,: The handle of the block to be shrunk/enlarged. [INPUTS] SplitSize: Amount of memory to insert/delete. Offset: The position to insert/delete memory at.
Returns:
FALSE
See also:
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 }

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.

Author:
Tim_Browse (Xara Group Ltd) <camelotdev@xara.com>
Date:
09/02/94
Returns:
TRUE if a hole was removed, FALSE if there were no holes left to remove.

Definition at line 911 of file cammemory.cpp.

00912 {
00913     return FALSE;
00914 }


Variable Documentation

DynamicHeap* NormalHeap = NULL [static]
 

Definition at line 325 of file cammemory.cpp.


Generated on Sat Nov 10 03:49:28 2007 for Camelot by  doxygen 1.4.4