handles.cpp File Reference

(r1785/r1467)

#include "camtypes.h"

Go to the source code of this file.

Classes

struct  HandleTableStruct

Defines

#define HandleTableSize   500
#define HandleTableElementSize   (sizeof( HandleTableElement ))
#define HandleTableByteSize   (HandleTableSize * HandleTableElementSize)

Typedefs

typedef HandleTableStruct HandleTableElement

Functions

BOOL InitHandles ()
 Starts up the Handles stuff. It sets all the handles in the table to sensible starting values and inits a count of the number of handles.
void DeinitHandles ()
 Closes down the Handles manager. Should free up any space allocated by the Handle Manager during it life.
ADDR DescribeHandle (MHANDLE Handle)
 To get an up to date location of the memory location.
BOOL ReleaseHandle (MHANDLE Handle)
 To free up a handle that is no longer required by its user Notes: There are 2 versions of this function below - one is used if USE_UNIQUE_IDS is defined and the other if it is not.
ADDR AlterHandle (MHANDLE Handle, ADDR Address)
 The change the location associated with the handle without having to release and then re-claim a new handle.
BOOL RelocateHandles (ADDR LowAddr, ADDR HighAddr, INT32 Shift)
 Finds all the memory locations in the supplied region that have handles associated with them and modifies the address. Usually called by the memory manager when it shifts a block of memory to a different location.
BOOL ReleaseRangeOfHandles (ADDR LowAddr, ADDR HighAddr)
 Find all the handles that refer to address in the the given range and release them.
MHANDLE ClaimHandle (ADDR Address)
 Allocates a handle to the supplied address. The handle should be used as the reference to the memory location from then on to allow the memory manager to move the allocation around.

Variables

MemoryBlock HandlesMemory
HandleTableElementHandlesTable
static UINT32 HandleTableTotalSize
static UINT32 ElementsInHandleTable
static UINT32 HandlesInUse


Define Documentation

#define HandleTableByteSize   (HandleTableSize * HandleTableElementSize)
 

Definition at line 130 of file handles.cpp.

#define HandleTableElementSize   (sizeof( HandleTableElement ))
 

Definition at line 127 of file handles.cpp.

#define HandleTableSize   500
 

Definition at line 126 of file handles.cpp.


Typedef Documentation

typedef struct HandleTableStruct HandleTableElement
 


Function Documentation

ADDR AlterHandle MHANDLE  Handle,
ADDR  Address
 

The change the location associated with the handle without having to release and then re-claim a new handle.

Author:
Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
Date:
27/04/93
Parameters:
Handle - A handle currently in use by the caller [INPUTS] Address - The new memory location to associate with the handle Size - The new size of the the block
Returns:
The old address that used to be associated with the handle

Definition at line 364 of file handles.cpp.

00365 {
00366     ADDR OldAddr;
00367     
00368     #ifdef USE_UNIQUE_IDS
00369         // Make sure its not garbage
00370         ENSURE( LOWORD(Handle) > BAD_MHANDLE, "AlterHandle: Handle was bad" );
00371         ENSURE( LOWORD(Handle) < ElementsInHandleTable, "AlterHandle: Handle too big" );
00372     
00373         // note old value and change it to the new one
00374         OldAddr = HandlesTable[LOWORD(Handle)].Address;
00375         HandlesTable[ LOWORD(Handle) ].Address = Address;
00376     #else
00377         // make sure its not garbage
00378         ENSURE( Handle > BAD_MHANDLE, "AlterHandle: Handle was bad" );
00379         ENSURE( Handle < ElementsInHandleTable, "AlterHandle: Handle was too big" );
00380     
00381         // note old value and change it to the new one
00382         OldAddr = HandlesTable[Handle].Address;
00383         HandlesTable[Handle].Address = Address;
00384     #endif
00385     
00386     // retrun back the old value
00387     return OldAddr;
00388 }

MHANDLE ClaimHandle ADDR  Address  ) 
 

Allocates a handle to the supplied address. The handle should be used as the reference to the memory location from then on to allow the memory manager to move the allocation around.

Author:
Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
Date:
27/04/93
Parameters:
Address - The address of a block or somewhere in a block of allocated [INPUTS] memory. Size - The size of the block of memory being stored. If zero then just a pointer stored. If this parameter is not passed, then zero assumed
Returns:
A handle to the address or BAD_MHANDLE

Definition at line 505 of file handles.cpp.

00506 {
00507     // Need to scan along the list of handles to find an unused one
00508     if (HandlesInUse == (ElementsInHandleTable-1))
00509     {
00510         // we have run out of handles
00511         TRACEUSER( "Rik", wxT("ClaimHandle - No more handles available. Trying to get some more\n") );
00512     
00513         // First off we had better try to extend our allocation of memory to the table
00514         // If it fails, return saying there are no more handles left!
00515         if (!HandlesMemory.Grow(HandleTableTotalSize, 
00516                                 HandleTableTotalSize+HandleTableByteSize, (LPVOID*)&HandlesTable ))
00517         {
00518             TRACE( wxT("ClaimHandle - Failed to get more handles!!!!   P A N I C\n") );
00519             return BAD_MHANDLE;
00520         }
00521 
00522         // grew handle table OK, Set all the new bits of mem to defaults
00523         for (INT32 i=0; i<HandleTableSize; i++)
00524             HandlesTable[ElementsInHandleTable+i].Address = PBYTE(BAD_MHANDLE);
00525 
00526         // increase the counters
00527         HandleTableTotalSize += HandleTableByteSize;
00528         ElementsInHandleTable += HandleTableSize;
00529     }
00530     
00531     // Ok, we must have some handles by now, so lets try and find one   
00532     UINT32 Handle = ElementsInHandleTable - 1;
00533     while (Handle>0)
00534     {
00535         if (HandlesTable[Handle].Address == BAD_MHANDLE )
00536         { 
00537             // Store the Addr
00538             HandlesTable[Handle].Address = Address;
00539             HandlesInUse ++;
00540 
00541             // if we are using unique IDs, do this
00542             #ifdef USE_UNIQUE_IDS
00543                 // Using Unique IDS, so get the next id and store it away
00544                 WORD UniqueID = CurrUniqueId++;
00545                 HandlesTable[ Handle ].UniqueID = UniqueID;
00546                 return MAKEINT32( Handle, UniqueID );
00547             #else
00548 //              TRACEUSER("Gerry", _T("ClaimHandle - %d"), Handle);
00549                 return Handle;
00550             #endif
00551         }
00552         
00553         Handle --;
00554     }
00555         
00556     // Should never get here
00557     ENSURE( FALSE, "ClaimHandle: We Should never have reached this bit!" );
00558     return BAD_MHANDLE;
00559 }

void DeinitHandles  ) 
 

Closes down the Handles manager. Should free up any space allocated by the Handle Manager during it life.

Author:
Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
Date:
27/04/93

Definition at line 204 of file handles.cpp.

00205 {   
00206 #ifdef _DEBUG
00207     // Tell me how many handles were in use at its peak
00208     TRACE( wxT("DeinitHandles: Max size of handle table = %ld"), ElementsInHandleTable );
00209 
00210     // Tell everyone about any handles that were not released
00211     UINT32 Waste = 0;
00212     for (UINT32 n=1; n<ElementsInHandleTable; n++)
00213     {   
00214         if (HandlesTable[n].Address != BAD_MHANDLE )
00215         {
00216             TRACE( wxT("Handle %d not released (0x%08x)"), n, HandlesTable[n].Address);
00217             Waste++;
00218         }
00219     }
00220     TRACE( wxT("DeinitHandles: %ld handles not released"), Waste );
00221 #endif
00222     
00223     // dealloc the memory we now have from the OS
00224     HandlesMemory.DeInit();
00225 }

ADDR DescribeHandle MHANDLE  Handle  ) 
 

To get an up to date location of the memory location.

Author:
Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
Date:
27/04/93
Parameters:
A Handle to a memory location [INPUTS]
Returns:
The address associated with the handle supplied

Definition at line 242 of file handles.cpp.

00243 {
00244     #ifdef USE_UNIQUE_IDS
00245         ENSURE( (Handle & 0xFFFF) > BAD_MHANDLE, "DescribeHandle: Handle was Bad" );
00246         ENSURE( (Handle & 0xFFFF) < ElementsInHandleTable, "DescribeHandle: Handle out of range" );
00247     #else
00248         ENSURE(Handle > BAD_MHANDLE, "DescribeHandle: Handle was Bad" );
00249         ENSURE(Handle < ElementsInHandleTable, "DescribeHandle: Handle out of range" );
00250     #endif
00251     
00252     // passed all the assertions, but make no checks that the addr being returned
00253     // is valid. If it is not then they will be passed BAD_MHANDLE
00254 
00255     // Must also check that the Unique ID in the Handle is the same as the one in the
00256     // Handle table, but only if USE_UNIQUE_IDS is defined
00257     
00258     
00259     #ifdef USE_UNIQUE_IDS
00260         // Have to decode the handles
00261         if ((HIWORD(Handle)) != (HandlesTable[LOWORD(Handle)].UniqueID))
00262             return BAD_MHANDLE;
00263         else
00264             return HandlesTable[LOWORD(Handle)].Address;
00265             
00266     #else
00267         // the more efficent version, with no unique ID passing
00268         return HandlesTable[Handle].Address;        
00269     #endif
00270 }

BOOL InitHandles  ) 
 

Starts up the Handles stuff. It sets all the handles in the table to sensible starting values and inits a count of the number of handles.

Author:
Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
Date:
27/04/93
Returns:
TRUE if all went OK, FALSE otherwise

Definition at line 159 of file handles.cpp.

00160 {
00161     // Try and get some memory from the OS for my lovely table
00162     HandleTableTotalSize  = HandleTableByteSize;
00163     ElementsInHandleTable = HandleTableSize;
00164     HandlesInUse = 0;
00165     HandlesTable = (HandleTableElement*)
00166                     HandlesMemory.Init( HandleTableTotalSize, TRUE, MEMORYBLOCK_RELOCHEAP); 
00167 
00168     // check to see if we got the ram needed
00169     if (HandlesTable==NULL)
00170         return FALSE;
00171 
00172     // init all the elements in the table to a non-existent handle
00173     for (UINT32 n=0; n<ElementsInHandleTable; n++)
00174     {
00175         // Set the Address to Bad
00176         HandlesTable[n].Address  = PBYTE(BAD_MHANDLE);
00177         
00178         // if we are using unique IDs, set it to zero
00179         #ifdef USE_UNIQUE_IDS
00180             HandlesTable[n].UniqueID = 0;
00181         #endif
00182     }
00183 
00184     // All worked
00185     return TRUE;    
00186 }

BOOL ReleaseHandle MHANDLE  Handle  ) 
 

To free up a handle that is no longer required by its user Notes: There are 2 versions of this function below - one is used if USE_UNIQUE_IDS is defined and the other if it is not.

Author:
Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
Date:
27/04/93
Parameters:
A Handle that is currently in use [INPUTS]
Returns:
A BOOL to say if the release of the Handle was succesful

Definition at line 323 of file handles.cpp.

00324 {
00325 //  TRACEUSER("Gerry", _T("ReleaseHandle - %d"), Handle);
00326 
00327     // make sure its not garbage
00328     ENSURE( Handle > BAD_MHANDLE, "ReleaseHandle: Handle was BAD_MHANDLE" );
00329     ENSURE( Handle < ElementsInHandleTable, "ReleaseHandle: Handle was too big" );
00330                 
00331     if ( HandlesTable[Handle].Address != BAD_MHANDLE )
00332     {
00333         // mark it as available
00334         HandlesTable[Handle].Address = PBYTE(BAD_MHANDLE);
00335         HandlesInUse --;
00336         return TRUE;
00337     }
00338 
00339     return FALSE;                               // if we got here, return false
00340 }

BOOL ReleaseRangeOfHandles ADDR  LowAddr,
ADDR  HighAddr
 

Find all the handles that refer to address in the the given range and release them.

Author:
Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
Date:
28/04/93
Parameters:
LowAddr - The Low address in the range [INPUTS] HighAddr - The High address in the range to be searched
Returns:
TRUE

Definition at line 455 of file handles.cpp.

00456 {
00457 //  TRACEUSER("Gerry", _T("ReleaseRange - 0x%08x to 0x%08x"), LowAddr, HighAddr);
00458 
00459     // Make sure the params are not rubbish
00460     ENSURE( HighAddr > LowAddr, "ReleaseRangeOfHandles: Low Addr is Higher the High Addr" );
00461     
00462     // loop through the table
00463     ADDR Address;   
00464     for (UINT32 n=1; n<ElementsInHandleTable; n++)
00465     {
00466         // First out get the address from the array
00467         Address = HandlesTable[ n ].Address;
00468 
00469         // if it is ok
00470         if (Address != BAD_MHANDLE)
00471         {
00472             if( Address >= LowAddr &&
00473                 Address < HighAddr )
00474             {
00475                 // If the address is in the range, release it
00476 //              TRACEUSER("Gerry", _T("ReleaseRange - releasing %d"), n);
00477                 HandlesTable[n].Address = PBYTE(BAD_MHANDLE);
00478                 HandlesInUse --;
00479             }
00480         }
00481     }
00482 
00483     return TRUE;
00484 }

BOOL RelocateHandles ADDR  LowAddr,
ADDR  HighAddr,
INT32  Shift
 

Finds all the memory locations in the supplied region that have handles associated with them and modifies the address. Usually called by the memory manager when it shifts a block of memory to a different location.

Author:
Rik_Heywood (Xara Group Ltd) <camelotdev@xara.com>
Date:
27/04/93
Parameters:
LowAddr - The low address in the region supplied [INPUTS] HighAddr - The high address in the region supplied Shift - The amount to alter all the address in the region by.
Returns:
TRUE

Definition at line 411 of file handles.cpp.

00412 {
00413     // Make sure that the params are not rubbish
00414     ENSURE( HighAddr > LowAddr, "RelocateHandles: Low Addr is Higher the High Addr" );
00415 
00416     // Check if nothing needs to be done    
00417     if (Shift == 0)
00418         return TRUE;
00419 
00420     // Loop through the handles
00421     ADDR Address;       
00422     for (UINT32 n=1; n<ElementsInHandleTable; n++)
00423     {
00424         // First out get the address from the array     
00425         Address = HandlesTable[n].Address;
00426 
00427         if (Address != BAD_MHANDLE)
00428         {
00429             if (Address >= LowAddr  &&  Address < HighAddr)
00430                 // If the address is in the range, shift it and store it back in the array
00431                 HandlesTable[n].Address = Address + Shift;
00432         }
00433     }
00434     
00435     return TRUE;
00436 }


Variable Documentation

UINT32 ElementsInHandleTable [static]
 

Definition at line 136 of file handles.cpp.

UINT32 HandlesInUse [static]
 

Definition at line 137 of file handles.cpp.

MemoryBlock HandlesMemory
 

Definition at line 132 of file handles.cpp.

HandleTableElement* HandlesTable
 

Definition at line 134 of file handles.cpp.

UINT32 HandleTableTotalSize [static]
 

Definition at line 135 of file handles.cpp.


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