File indexing completed on 2025-01-18 10:04:17
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #ifndef NCollection_AccAllocator_HeaderFile
0018 #define NCollection_AccAllocator_HeaderFile
0019
0020 #include <NCollection_BaseAllocator.hxx>
0021 #include <NCollection_DataMap.hxx>
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042 class NCollection_AccAllocator : public NCollection_BaseAllocator
0043 {
0044
0045 public:
0046
0047 static const Standard_Size Align = 4;
0048
0049
0050 static const Standard_Size DefaultBlockSize = 24600;
0051
0052
0053 static const Standard_Integer MaxLookupBlocks = 16;
0054
0055
0056 public:
0057
0058 Standard_EXPORT NCollection_AccAllocator(const size_t
0059 theBlockSize = DefaultBlockSize);
0060
0061
0062 Standard_EXPORT ~NCollection_AccAllocator();
0063
0064
0065 Standard_EXPORT virtual void* Allocate (const size_t theSize) Standard_OVERRIDE;
0066
0067
0068 void* AllocateOptimal(const size_t theSize) Standard_OVERRIDE
0069 {
0070 return Allocate(theSize);
0071 }
0072
0073
0074
0075 Standard_EXPORT virtual void Free (void* theAddress) Standard_OVERRIDE;
0076
0077
0078 protected:
0079
0080 class AlignedSize
0081 {
0082 Standard_Size myValue;
0083 public:
0084 AlignedSize() : myValue(0) {}
0085 AlignedSize(const Standard_Size theValue)
0086 : myValue((theValue + Align - 1) & ~(Align - 1)) {}
0087 operator Standard_Size() const {return myValue;}
0088 };
0089
0090
0091 class AlignedPtr
0092 {
0093 Standard_Byte* myValue;
0094 public:
0095 AlignedPtr() : myValue(0) {}
0096 AlignedPtr(const Standard_Address theValue)
0097 : myValue((Standard_Byte*)((Standard_Size)theValue & ~(Align - 1))) {}
0098 operator Standard_Address () const {return myValue;}
0099 operator Standard_Byte* () const {return myValue;}
0100 AlignedPtr operator -(const AlignedSize theValue) const
0101 {return myValue - theValue;}
0102 AlignedPtr operator +(const AlignedSize theValue) const
0103 {return myValue + theValue;}
0104 AlignedPtr operator -=(const AlignedSize theValue)
0105 {return myValue -= theValue;}
0106 AlignedPtr operator +=(const AlignedSize theValue)
0107 {return myValue += theValue;}
0108 };
0109
0110
0111 struct Key {Standard_Size Value;};
0112
0113
0114 class Hasher
0115 {
0116 public:
0117
0118
0119
0120 size_t operator() (const Key theKey) const noexcept
0121 {
0122 return theKey.Value;
0123 }
0124
0125 bool operator() (const Key theKey1, const Key theKey2) const noexcept
0126 {
0127 return theKey1.Value == theKey2.Value;
0128 }
0129 };
0130
0131
0132 struct Block
0133 {
0134 Standard_Address address;
0135 AlignedPtr allocStart;
0136 Block* prevBlock;
0137 Standard_Integer allocCount;
0138
0139 Block(const Standard_Address theAddress,
0140 const Standard_Size theSize,
0141 Block* thePrevBlock = 0L)
0142 : address(theAddress), prevBlock(thePrevBlock), allocCount(0)
0143 {SetFreeSize (theSize);}
0144
0145 void SetFreeSize(const Standard_Size theSize)
0146 {allocStart = (Standard_Byte*)address + theSize;}
0147
0148 Standard_Size FreeSize() const
0149 {return (Standard_Byte*)allocStart - (Standard_Byte*)address;}
0150
0151 AlignedPtr Allocate(const AlignedSize theSize)
0152 {allocCount++; return allocStart -= theSize;}
0153
0154 void Free()
0155 {allocCount--;}
0156
0157 Standard_Boolean IsEmpty() const
0158 {return allocCount == 0;}
0159 };
0160
0161
0162 protected:
0163
0164 inline Key getKey(const Standard_Address theAddress) const
0165 {
0166 Key aKey = {(Standard_Size)theAddress / myBlockSize};
0167 return aKey;
0168 }
0169
0170
0171 Standard_EXPORT Block* findBlock(const Standard_Address theAddress, Key& theKey);
0172
0173
0174 Standard_EXPORT Block* allocateNewBlock(const Standard_Size theSize);
0175
0176
0177 private:
0178 NCollection_AccAllocator (const NCollection_AccAllocator&);
0179 NCollection_AccAllocator& operator = (const NCollection_AccAllocator&);
0180
0181
0182 protected:
0183 AlignedSize myBlockSize;
0184 Block* mypLastBlock;
0185 NCollection_DataMap<Key, Block, Hasher> myBlocks;
0186
0187
0188 public:
0189 DEFINE_STANDARD_RTTIEXT(NCollection_AccAllocator,NCollection_BaseAllocator)
0190 };
0191
0192
0193 DEFINE_STANDARD_HANDLE (NCollection_AccAllocator, NCollection_BaseAllocator)
0194
0195
0196 #endif