• R/O
  • SSH
  • HTTPS

eirrepo: Commit


Commit MetaInfo

Revision115 (tree)
Time2018-09-06 04:13:36
Authorquiret

Log Message

- added alignment handling of C++ structs to the eir::Vector object
- refactored some code

Change Summary

Incremental Difference

--- common/sdk/Vector.h (revision 114)
+++ common/sdk/Vector.h (revision 115)
@@ -38,7 +38,7 @@
3838 private:
3939 static AINLINE structType* make_data_copy( void *objPtr, const structType *right_data, size_t right_count )
4040 {
41- structType *data_entries = (structType*)allocatorType::Allocate( objPtr, right_count * sizeof(structType) );
41+ structType *data_entries = (structType*)allocatorType::Allocate( objPtr, right_count * sizeof(structType), alignof(structType) );
4242
4343 if ( data_entries == nullptr )
4444 {
@@ -145,7 +145,7 @@
145145 AINLINE void recast_memory( size_t new_item_count, size_t newRequiredSize, const callbackType& cb )
146146 {
147147 // Try to fetch new memory.
148- void *new_data_ptr = allocatorType::Allocate( this, newRequiredSize );
148+ void *new_data_ptr = allocatorType::Allocate( this, newRequiredSize, alignof(structType) );
149149
150150 if ( new_data_ptr == nullptr )
151151 {
@@ -222,7 +222,7 @@
222222 }
223223 else
224224 {
225- use_data = (structType*)allocatorType::Allocate( this, newRequiredSize );
225+ use_data = (structType*)allocatorType::Allocate( this, newRequiredSize, alignof(structType) );
226226
227227 this->data_entries = use_data;
228228 }
--- common/sdk/OSUtils.memheap.h (revision 114)
+++ common/sdk/OSUtils.memheap.h (revision 115)
@@ -173,11 +173,14 @@
173173 return dataPtr;
174174 }
175175
176- inline void Free( void *memPtr )
176+private:
177+ // Forward declaration for the methods here.
178+ struct VMemAllocation;
179+
180+ AINLINE VMemAllocation* get_mem_block_from_ptr( void *memPtr )
177181 {
178- // We guarrantee that this operation is O(1) in Release mode with all optimizations.
179-
180182 // TODO: add debug to this code, so that memory corruption can be combatted.
183+ // it could iterate over all memory pointers to verify that memPtr really belongs to us.
181184
182185 size_t header_size = sizeof(VMemAllocation);
183186
@@ -185,8 +188,27 @@
185188
186189 size_t memHandleOff = UINT_DOWNPUSH( memOff - header_size, VMemIsland::HEADER_ALIGNMENT );
187190
188- VMemAllocation *memHandle = (VMemAllocation*)memHandleOff;
191+ return (VMemAllocation*)memHandleOff;
192+ }
189193
194+ AINLINE const VMemAllocation* get_mem_block_from_ptr( const void *memPtr ) const
195+ {
196+ size_t header_size = sizeof(VMemAllocation);
197+
198+ size_t memOff = (size_t)memPtr;
199+
200+ size_t memHandleOff = UINT_DOWNPUSH( memOff - header_size, VMemIsland::HEADER_ALIGNMENT );
201+
202+ return (const VMemAllocation*)memHandleOff;
203+ }
204+
205+public:
206+ inline void Free( void *memPtr )
207+ {
208+ // We guarrantee that this operation is O(1) in Release mode with all optimizations.
209+
210+ VMemAllocation *memHandle = get_mem_block_from_ptr( memPtr );
211+
190212 // Release the memory.
191213 VMemIsland *manager = memHandle->manager;
192214
@@ -214,14 +236,8 @@
214236 if ( newSize == 0 )
215237 return false;
216238
217- size_t header_size = sizeof(VMemAllocation);
239+ VMemAllocation *memHandle = get_mem_block_from_ptr( memPtr );
218240
219- size_t memOff = (size_t)memPtr;
220-
221- size_t memHandleOff = UINT_DOWNPUSH( memOff - header_size, VMemIsland::HEADER_ALIGNMENT );
222-
223- VMemAllocation *memHandle = (VMemAllocation*)memHandleOff;
224-
225241 // We do not have to update anything, so bail.
226242 size_t oldDataSize = memHandle->dataSize;
227243
@@ -275,17 +291,20 @@
275291 }
276292
277293 // Returns the data size of an allocation.
278- inline size_t GetAllocationSize( void *memPtr )
294+ inline size_t GetAllocationSize( const void *memPtr ) const
279295 {
280- size_t header_size = sizeof(VMemAllocation);
296+ const VMemAllocation *memHandle = get_mem_block_from_ptr( memPtr );
281297
282- size_t memOff = (size_t)memPtr;
298+ return memHandle->dataSize;
299+ }
283300
284- size_t memHandleOff = UINT_DOWNPUSH( memOff - header_size, VMemIsland::HEADER_ALIGNMENT );
301+ // Returns the whole size of this allocation.
302+ // This includes the meta-data header as well as the alignment.
303+ inline size_t GetAllocationMetaSize( const void *memPtr ) const
304+ {
305+ const VMemAllocation *memHandle = get_mem_block_from_ptr( memPtr );
285306
286- VMemAllocation *memHandle = (VMemAllocation*)memHandleOff;
287-
288- return memHandle->dataSize;
307+ return ( memHandle->dataOff + memHandle->dataSize );
289308 }
290309
291310 // Simple realloc helper just because it is being exposed in the CRT aswell.
--- unittests/src/vector.cpp (revision 114)
+++ unittests/src/vector.cpp (revision 115)
@@ -2,13 +2,14 @@
22 #include <sdk/Vector.h>
33 #include <sdk/OSUtils.memheap.h>
44
5+// The heap allocator that should be used for our tests.
56 static NativeHeapAllocator heapAlloc;
67
78 struct VectorHeapAllocator
89 {
9- static void* Allocate( void *objPtr, size_t memSize )
10+ static void* Allocate( void *objPtr, size_t memSize, size_t alignment )
1011 {
11- return heapAlloc.Allocate( memSize );
12+ return heapAlloc.Allocate( memSize, alignment );
1213 }
1314
1415 static bool Resize( void *objPtr, void *memPtr, size_t memSize )
@@ -123,4 +124,24 @@
123124 assert( vh2.GetCount() == 1 );
124125 }
125126 printf( "ok.\n" );
127+
128+ printf( "testing vector alignment guarantees..." );
129+ {
130+ eir::Vector <char, VectorHeapAllocator> byteVector;
131+ eir::Vector <short, VectorHeapAllocator> wordVector;
132+ eir::Vector <int, VectorHeapAllocator> dwordVector;
133+ eir::Vector <long long, VectorHeapAllocator> qwordVector;
134+
135+ byteVector.AddToBack( 1 );
136+ wordVector.AddToBack( 1 );
137+ assert( (size_t)&wordVector.GetBack() % sizeof(short) == 0 );
138+ wordVector.RemoveFromBack();
139+ dwordVector.AddToBack( 2 );
140+ assert( (size_t)&dwordVector.GetBack() % sizeof(int) == 0 );
141+ dwordVector.RemoveFromBack();
142+ qwordVector.AddToBack( 3 );
143+ assert( (size_t)&qwordVector.GetBack() % sizeof(long long) == 0 );
144+ qwordVector.RemoveFromBack();
145+ }
146+ printf( "ok.\n" );
126147 }
\ No newline at end of file
Show on old repository browser