SophiaFramework UNIVERSE 5.3 |
SFXHeap Class and SFXClusterHeap Class
Both the SFXHeap class and the SFXClusterHeap class provide the dynamic heap management function.
The SFXHeap class provides the block allocation mechanism, but the SFXClusterHeap class does not.
If the heap size is updated a little bit at a time frequently, using the SFXClusterHeap class is recommended from the perspective of memory efficiency.
Attach Function and Detach Function
The SFXClusterHeap::Attach function delegates the control privilege of specified Void data to the SFXClusterHeap object. The SFXClusterHeap::Detach function has the reverse functionality.
What is delegation?: In object oriented programming, delegation means that an object have another deputy object behave like itself.
Example 805. How to Use the Attach Function
SFXClusterHeap<SInt16> heap; VoidPtr swap; // allocate memory swap = MemoryAllocate(10240); ... // delegate the control privilege of Void data(swap) to the SFXClusterHeap object(heap) heap.Attach(swap, 10240); // hereafter, the Void data(swap) will be handled as the SFXClusterHeap object(heap) ... // after used, the allocated memory will be released automatically
Example 806. How to Use the Detach Function
SFXClusterHeap heap; VoidPtr swap; UInt32 length; ... // delegate the control privilege of SFXClusterHeap object(heap) to the Void data(swap) swap = heap.Detach(&length); // hereafter, the SFXClusterHeap object(heap) will be handled as the Void data(swap) ... // after used, the Void data(swap) must be released explicitly MemoryFree(swap);
Constructor/Destructor |
---|
SFXClusterHeap( Void ) Constructor of the SFXClusterHeap class.
|
SFXClusterHeap(
UInt16 threshold
, UInt16 cluster
) Constructor of the SFXClusterHeap class.
|
Public Functions | |
---|---|
SFCError |
Attach(
VoidPtr heap
, UInt32 size
) Attach the specified area to this heap object.
|
Bool |
Contains(
VoidConstPtr heap
, UInt32 size
) Check whether or not this heap object contains some of the specified area.
|
VoidPtr |
Detach(
UInt32Ptr size = null
) Detach the heap area from this heap object.
|
static SFXClusterHeap< T > const & |
EmptyInstance( Void ) Get the empty heap object.
|
Void |
Free( Void ) Release the heap area which this heap object manages.
|
UInt16 |
GetCluster( Void ) Get the cluster size to increase or decrease the heap.
|
VoidPtr |
GetHeap( Void ) Get the pointer to the heap that the SFXClusterHeap object manages.
|
VoidConstPtr |
GetHeap( Void ) Get the pointer to the heap that the SFXClusterHeap object manages.
|
UInt32 |
GetSize( Void ) Get the heap size of this heap object.
|
UInt16 |
GetThreshold( Void ) Get the minimum heap size.
|
VoidConstPtr |
Protect(
VoidConstPtr heap
, UInt32 size
) Protect the specified area.
|
SFCError |
Resize(
UInt32 size
) Resize this heap.
|
Void |
SetCluster(
UInt16 cluster
) Set the cluster size to increase or decrease the heap.
|
Void |
SetThreshold(
UInt16 threshold
) Set the minimum heap size.
|
Void |
Unprotect(
VoidConstPtr protect
, VoidConstPtr heap
) Unprotect the protected area.
|
operator T *( Void )
Convert into the typr of "T *".
|
|
Bool |
operator==(
SFXClusterHeap< T > const & left
, SFXClusterHeap< M > const & right
) Check the relation of "==".
|
Bool |
operator==(
SFXClusterHeap< T > const & left
, VoidConstPtr right
) Check the relation of "==".
|
Bool |
operator==(
VoidConstPtr left
, SFXClusterHeap< T > const & right
) Check the relation of "==".
|
Bool |
operator>=(
SFXClusterHeap< T > const & left
, SFXClusterHeap< M > const & right
) Check the relation of ">=".
|
Bool |
operator>=(
SFXClusterHeap< T > const & left
, VoidConstPtr right
) Check the relation of ">=".
|
Bool |
operator>=(
VoidConstPtr left
, SFXClusterHeap< T > const & right
) Check the relation of ">=".
|
Bool |
operator>(
SFXClusterHeap< T > const & left
, SFXClusterHeap< M > const & right
) Check the relation of ">".
|
Bool |
operator>(
SFXClusterHeap< T > const & left
, VoidConstPtr right
) Check the relation of ">".
|
Bool |
operator>(
VoidConstPtr left
, SFXClusterHeap< T > const & right
) Check the relation of ">".
|
Bool |
operator<=(
SFXClusterHeap< T > const & left
, SFXClusterHeap< M > const & right
) Check the relation of "<=".
|
Bool |
operator<=(
SFXClusterHeap< T > const & left
, VoidConstPtr right
) Check the relation of "<=".
|
Bool |
operator<=(
VoidConstPtr left
, SFXClusterHeap< T > const & right
) Check the relation of "<=".
|
Bool |
operator<(
SFXClusterHeap< T > const & left
, SFXClusterHeap< M > const & right
) Check the relation of "<".
|
Bool |
operator<(
SFXClusterHeap< T > const & left
, VoidConstPtr right
) Check the relation of "<".
|
Bool |
operator<(
VoidConstPtr left
, SFXClusterHeap< T > const & right
) Check the relation of "<".
|
Bool |
operator!=(
SFXClusterHeap< T > const & left
, SFXClusterHeap< M > const & right
) Check the relation of "!=".
|
Bool |
operator!=(
SFXClusterHeap< T > const & left
, VoidConstPtr right
) Check the relation of "!=".
|
Bool |
operator!=(
VoidConstPtr left
, SFXClusterHeap< T > const & right
) Check the relation of "!=".
|
[ public, explicit ] SFXClusterHeap(Void);
[ public, explicit ] SFXClusterHeap( UInt16 threshold // minimum heap size (in bytes) UInt16 cluster // cluster size to increase or decrease heap (in bytes) );
As for arguments, the minimum size to allocate the heap memory is specified in bytes as the threshold argument, and the cluster size to increase or decrease the heap memory is specified in bytes as the cluster argument.
If no argument is specified, "threshold = 0" and "cluster = 1" are set by default.
This is an example of constructor with arguments.
SFXClusterHeap myheap(100, 10);
In this example, the minimum heap size is set to 100 bytes. When bigger than 100 bytes, it will be 110 bytes, 120 bytes, 130 bytes, and so on.
SFXClusterHeap::SetThreshold | SFXClusterHeap::SetCluster | SFXClusterHeap::Attach | SFXClusterHeap::Resize
[ public ] SFCError Attach( VoidPtr heap // pointer to the area to attach to this heap object UInt32 size // size of the area to attach to this heap object );
This function attaches the specified area to this heap object. After this function is executed, the specified area can be handled as the SFXClusterHeap object.
Note | |
---|---|
When this heap object is released, the specified area will be released automatically together with this heap object. |
SFXClusterHeap<Byte> heap; VoidPtr swap; // allocate the memory of 10240 bytes to swap swap = MemoryAllocate(10240); ... // attach the swap area to the heap object heap.Attach(swap, 10240); // hereafter, the swap area can be handled as the heap object ・・・ // after used, the swap area will be released automatically together with the heap object
[ public, const ] Bool Contains( VoidConstPtr heap // pointer to the area to compare with UInt32 size // size of the area to compare with (in bytes) );
This function checks whether or not this heap object contains some of the specified area.
SFXClusterHeap<Byte> heap(100, 10); heap.Resize(100); // set the size of the heap object to 100 bytes // area1 is contained by the heap object VoidConstPtr area1 = static_cast<BytePtr>(heap.GetHeap()) + 10; // area2 is not contained by the heap object VoidConstPtr area2 = static_cast<BytePtr>(heap.GetHeap()) + 100; if (heap.Contains(area1, 200)) { // since the heap object contains the area1 area, the statement below will be executed TRACE("The heap object contains area1."); } if (!heap.Contains(area2, 200)) { // since the heap object does not contain the area2 area, the statement below will be executed TRACE("The heap object does not contain area2."); }
[ public ] VoidPtr Detach( UInt32Ptr size = null // pointer to the variable where the size of the detached heap area will be stored );
Pointer to the detached heap area
This function detaches and returns the pointer to the heap area which this heap object has controled.
The size of the heap area will be returned into the size argument after this function is called.
Caution | |
---|---|
The detached heap area will not be copied. |
SFXClusterHeap<Byte> heap; VoidPtr swap; UInt32 length; ... // detach the heap area from the heap object and set the swap variable to this pointer // the size of the detached heap area will be stored into the length variable swap = heap.Detach(&length); // hereafter, the heap area of the heap object will be handled via the swap variable ... // after used, the detached heap area must be released explicitly MemoryFree(swap);
[ public, static ] SFXClusterHeap< T > const & EmptyInstance(Void);
This function gets the empty heap object.
[ public ] Void Free(Void);
This function releases the heap area which this heap object manages.
SFXClusterHeap<Byte> heap;
...
heap.Free(); // release the heap area which the heap object manages
[ public, const ] UInt16 GetCluster(Void);
Return the cluster size in bytes to increase or decrease the heap.
SFXClusterHeap<SInt16> heap; // set the cluster size when allocating the heap to 10 bytes heap.SetCluster(10); TRACE("cluster size = %d", heap.GetCluster()); // cluster size = 10
[ public ] VoidPtr GetHeap(Void);
[ public, const ] VoidConstPtr GetHeap(Void);
Return the pointer to the heap that the SFXClusterHeap object manages.
SFXClusterHeap<SInt16> heap; // set heap size if (heap.Resize(sizeof(SInt16) * 128) == SFERR_NO_ERROR) { TRACE("addr = 0x%08X", heap.GetHeap()); // addr = 0x0686FF80 }
[ public, const ] UInt32 GetSize(Void);
Heap size of this heap object.
This function gets the size of the heap area which this heap object manages.
SFXClusterHeap<Byte> heap; // set the heap size if (heap.Resize(0x200) == SFERR_NO_ERROR) { // get the heap size TRACE("size = 0x%x", heap.GetSize()); // size = 0x200 }
[ public, const ] UInt16 GetThreshold(Void);
Return the minimum heap size(in bytes).
SFXClusterHeap<SInt16> heap; // set the minimum heap size to 100 bytes heap.SetThreshold(100); TRACE("minimum heap size = %d", heap.GetThreshold()); // minimum heap size = 100
[ public, const ] VoidConstPtr Protect( VoidConstPtr heap // pointer to the area to protect UInt32 size // size of the area to protect );
Pointer to the protected area.
If this heap contains some of the specified area, this function will allocate another area outside of this heap area, where the contents of the specified area will be copied. Otherwise, this function will only return the pointer to the specified area.
After you perform some processings regarding to the area protected by the SFXClusterHeap::Protect function, you have to call the SFXClusterHeap::Unprotect function.
Note | |
---|---|
The SFXClusterHeap::Unprotect function will release the area which is allocated by the SFXClusterHeap::Protect function. If the SFXClusterHeap::Protect function has allocated no area, nothing will happen. |
SFXClusterHeap<Byte> heap; VoidConstPtr protect; heap.Resize(0x200); // set the heap size to 0x200 bytes // _area1 = _heap + 0x100 // _area2 = _heap + 0x200 VoidConstPtr _heap(heap.GetHeap()); VoidConstPtr _area1(static_cast<BytePtr>(heap.GetHeap()) + 0x100); VoidConstPtr _area2(static_cast<BytePtr>(heap.GetHeap()) + 0x200); TRACE("addr of _heap = 0x%08X", _heap); // addr of _heap = 0x0585BD50 TRACE("addr of _area1 = 0x%08X", _area1); // addr of _area1 = 0x0585BE50 TRACE("addr of _area2 = 0x%08X", _area2); // addr of _area2 = 0x0585BF50 // HEAP: [ _heap, _heap + 0x200 ) // AREA_1: [ _area1, _area1 + 0x100 ) // AREA_2: [ _area2, _area2 + 0x100 ) // HEAP ∩ AREA_1 ≠ Φ // protect AREA_1 if ((protect = heap.Protect(_area1, 0x100)) != null) { // the [ protect, protect + 0x100 ) area will be allocated outside of HEAP TRACE("addr of protect = 0x%08X", protect); // addr of protect = 0x0585BF80 // unprotect: release the [ protect, protect + 0x100 ) area which Protect() has allocated heap.Unprotect(protect, _area1); } // HEAP ∩ AREA_2 = Φ // protect AREA_2 if ((protect = heap.Protect(_area2, 0x100)) != null) { // since AREA_2 and HEAP have no common area, the value of protect will equal that of _area2 TRACE("addr of protect = 0x%08X", protect); // addr of protect = 0x0585BF50 // unprotect: since Protect() has allocated no area, nothing will happen heap.Unprotect(protect, _area2); }
The heap size is determined by the value of the minimun heap size and the cluster size. The minimun heap size is set with the threshold argument of the SFXClusterHeap::SFXClusterHeap constructor or the SFXClusterHeap::SetThreshold function. And the cluster size is specified with the threshold argument of the SFXClusterHeap::SFXClusterHeap constructor or the SFXClusterHeap::SetCluster function.
If the value of size is smaller than or equals that of the minimun heap size, the allocated heap size equals the value of the minimun heap size in effect.
Otherwise, the heap bigger than the minimun heap size is allocated by the cluster size.
Caution | |
---|---|
If this heap is resized, the pointer to the heap which the SFXClusterHeap::GetHeap function returns will be changed too. |
SFXClusterHeap<Byte> heap;
heap.Resize(0x200); // set the heap size to 0x200 bytes
SFXClusterHeap::SFXClusterHeap | SFXClusterHeap::SetCluster | SFXClusterHeap::SetThreshold | SFXClusterHeap::Attach | SFXClusterHeap::GetHeap | SFXClusterHeap::GetSize
[ public ] Void SetCluster( UInt16 cluster // the cluster size in bytes to increase or decrease the heap );
This function sets the cluster size to increase or decrease the heap.
Default: Value specified in the cluster argument of the SFXClusterHeap::SFXClusterHeap constructor[Unit: byte]. 1 byte if not specified.
SFXClusterHeap<SInt16> heap; // set the cluster size when allocating the heap to 10 bytes heap.SetCluster(10); TRACE("cluster = %d", heap.GetCluster()); // cluster = 10
This function sets the minimum heap size.
Default: Value specified in the threshold argument of the SFXClusterHeap::SFXClusterHeap constructor[Unit: byte]. 0 byte if not specified.
SFXClusterHeap<SInt16> heap; // set the minimum heap size to 100 bytes heap.SetThreshold(100); TRACE("minimum heap size = %d", heap.GetThreshold()); // minimum heap size = 100
[ public, const ] Void Unprotect( VoidConstPtr protect // pointer to the protected area, which SFXClusterHeap::Protect() has returned VoidConstPtr heap // pointer to the area to protect, which has been specified in the heap argument of SFXClusterHeap::Protect() );
This function unprotects the area protected by the SFXClusterHeap::Protect function.
If this heap contains some of the area to protect, this function will release the area outside of this heap area which has been allocated by the SFXClusterHeap::Protect function. Otherwise, this function will do nothing.
Note | |
---|---|
If this heap contains some of the area to protect, the value of the protect argument will be different from that of the heap argument. Otherwise, both values of these two arguments are the same. |
[ public, const ] operator T *(Void);
If heap is allocated, return a pointer to the heap. Otherwise return a null pointer.
[ public, friend ] Bool operator==( SFXClusterHeap< T > const & left // SFXClusterHeap object to compare with SFXClusterHeap< M > const & right // SFXClusterHeap object to compare with );
[ public, friend ] Bool operator==( SFXClusterHeap< T > const & left // SFXClusterHeap object to compare with VoidConstPtr right // pointer to the heap to compare with );
[ public, friend ] Bool operator==( VoidConstPtr left // pointer to the heap to compare with SFXClusterHeap< T > const & right // SFXClusterHeap object to compare with );
Both head address of left heap and that of right heap are compared with.
[ public, friend ] Bool operator>=( SFXClusterHeap< T > const & left // SFXClusterHeap object to compare with SFXClusterHeap< M > const & right // SFXClusterHeap object to compare with );
[ public, friend ] Bool operator>=( SFXClusterHeap< T > const & left // SFXClusterHeap object to compare with VoidConstPtr right // pointer to the heap to compare with );
[ public, friend ] Bool operator>=( VoidConstPtr left // pointer to the heap to compare with SFXClusterHeap< T > const & right // SFXClusterHeap object to compare with );
Both head address of left heap and that of right heap are compared with.
[ public, friend ] Bool operator>( SFXClusterHeap< T > const & left // SFXClusterHeap object to compare with SFXClusterHeap< M > const & right // SFXClusterHeap object to compare with );
[ public, friend ] Bool operator>( SFXClusterHeap< T > const & left // SFXClusterHeap object to compare with VoidConstPtr right // pointer to the heap to compare with );
[ public, friend ] Bool operator>( VoidConstPtr left // pointer to the heap to compare with SFXClusterHeap< T > const & right // SFXClusterHeap object to compare with );
Both head address of left heap and that of right heap are compared with.
[ public, friend ] Bool operator<=( SFXClusterHeap< T > const & left // SFXClusterHeap object to compare with SFXClusterHeap< M > const & right // SFXClusterHeap object to compare with );
[ public, friend ] Bool operator<=( SFXClusterHeap< T > const & left // SFXClusterHeap object to compare with VoidConstPtr right // pointer to the heap to compare with );
[ public, friend ] Bool operator<=( VoidConstPtr left // SFXClusterHeap object to compare with SFXClusterHeap< T > const & right // pointer to the heap to compare with );
Both head address of left heap and that of right heap are compared with.
[ public, friend ] Bool operator<( SFXClusterHeap< T > const & left // SFXClusterHeap object to compare with SFXClusterHeap< M > const & right // SFXClusterHeap object to compare with );
[ public, friend ] Bool operator<( SFXClusterHeap< T > const & left // SFXClusterHeap object to compare with VoidConstPtr right // pointer to the heap to compare with );
[ public, friend ] Bool operator<( VoidConstPtr left // pointer to the heap to compare with SFXClusterHeap< T > const & right // SFXClusterHeap object to compare with );
Both head address of left heap and that of right heap are compared with.
[ public, friend ] Bool operator!=( SFXClusterHeap< T > const & left // SFXClusterHeap object to compare with SFXClusterHeap< M > const & right // SFXClusterHeap object to compare with );
[ public, friend ] Bool operator!=( SFXClusterHeap< T > const & left // SFXClusterHeap object to compare with VoidConstPtr right // pointer to the heap to compare with );
[ public, friend ] Bool operator!=( VoidConstPtr left // pointer to the heap to compare with SFXClusterHeap< T > const & right // SFXClusterHeap object to compare with );
Both head address of left heap and that of right heap are compared with.
Copyright(c) 2002 - 2024 Sophia Cradle Incorporated All Rights Reserved. |