• R/O
  • SSH
  • HTTPS

eirrepo: Commit


Commit MetaInfo

Revision127 (tree)
Time2018-09-13 00:47:30
Authorquiret

Log Message

- removed STL types dependencies from MemoryUtils.h and DynamicTypeSystem.h (pretty complicated stuff)

Change Summary

Incremental Difference

--- common/sdk/DynamicTypeSystem.h (revision 126)
+++ common/sdk/DynamicTypeSystem.h (revision 127)
@@ -102,7 +102,12 @@
102102 // This class manages runtime type information.
103103 // It allows for dynamic C++ class extension depending on runtime conditions.
104104 // Main purpose for its creation are tight memory requirements.
105-template <typename allocatorType, typename systemPointer, typename lockProvider_t = dtsDefaultLockProvider, template <typename genericType> typename structFlavorDispatchTypeTemplate = cachedMinimalStructRegistryFlavor>
105+#define DTS_TEMPLARGS \
106+ template <typename allocatorType, typename systemPointer, typename lockProvider_t = dtsDefaultLockProvider, template <typename genericType> typename structFlavorDispatchTypeTemplate = cachedMinimalStructRegistryFlavor>
107+#define DTS_TEMPLUSE \
108+ <allocatorType, systemPointer, lockProvider_t, structFlavorDispatchTypeTemplate>
109+
110+DTS_TEMPLARGS
106111 struct DynamicTypeSystem
107112 {
108113 // REQUIREMENTS FOR allocatorType STRUCT INTERFACE:
@@ -288,8 +293,13 @@
288293 }
289294 };
290295
291- typedef AnonymousPluginStructRegistry <GenericRTTI, pluginDescriptor, structFlavorDispatchType, systemPointer_t*> structRegistry_t;
296+private:
297+ // Need to redirect the typeInfoBase structRegistry allocations to the DynamicTypeSystem allocator.
298+ DEFINE_HEAP_REDIR_ALLOC( structRegRedirAlloc );
292299
300+public:
301+ typedef AnonymousPluginStructRegistry <GenericRTTI, pluginDescriptor, structFlavorDispatchType, structRegRedirAlloc, systemPointer_t*> structRegistry_t;
302+
293303 // Localize important struct details.
294304 typedef typename pluginDescriptor::pluginOffset_t pluginOffset_t;
295305 typedef typename structRegistry_t::pluginInterface pluginInterface;
@@ -1701,15 +1711,50 @@
17011711 return type_iterator( *this );
17021712 }
17031713
1714+private:
1715+ struct dtsAllocLink
1716+ {
1717+ inline dtsAllocLink( DynamicTypeSystem *refMem )
1718+ {
1719+ this->refMem = refMem;
1720+ }
1721+
1722+ inline dtsAllocLink( const dtsAllocLink& right ) = default;
1723+
1724+ inline void* Allocate( void *_, size_t memSize, size_t alignment )
1725+ {
1726+ return allocatorType::Allocate( this>refMem, memSize, alignment );
1727+ }
1728+
1729+ inline bool Resize( void *_, void *memPtr, size_t reqSize )
1730+ {
1731+ return allocatorType::Resize( this->refMem, memPtr, reqSize );
1732+ }
1733+
1734+ inline void Free( void *_, void *memPtr )
1735+ {
1736+ allocatorType::Free( this->refMem, memPtr );
1737+ }
1738+
1739+ DynamicTypeSystem *refMem;
1740+
1741+ struct is_object {};
1742+ };
1743+
1744+public:
1745+ typedef eir::String <char, dtsAllocLink> dtsString;
1746+
17041747 // THREAD-SAFE, because it only consists of THREAD-SAFE local operations.
17051748 struct type_resolution_iterator
17061749 {
1750+ DynamicTypeSystem *typeSys;
17071751 const char *typePath;
17081752 const char *type_iter_ptr;
17091753 size_t tokenLen;
17101754
1711- inline type_resolution_iterator( const char *typePath )
1755+ inline type_resolution_iterator( DynamicTypeSystem *typeSys, const char *typePath )
17121756 {
1757+ this->typeSys = typeSys;
17131758 this->typePath = typePath;
17141759 this->type_iter_ptr = typePath;
17151760 this->tokenLen = 0;
@@ -1717,13 +1762,13 @@
17171762 this->Increment();
17181763 }
17191764
1720- inline std::string Resolve( void ) const
1765+ inline dtsString Resolve( void ) const
17211766 {
17221767 const char *returnedPathToken = this->typePath;
17231768
17241769 size_t nameLength = this->tokenLen;
17251770
1726- return std::string( returnedPathToken, nameLength );
1771+ return dtsString( returnedPathToken, nameLength, this->typeSys );
17271772 }
17281773
17291774 inline void Increment( void )
@@ -1810,11 +1855,11 @@
18101855 typeInfoBase *currentType = baseTypeInfo;
18111856
18121857 // Find a base type.
1813- type_resolution_iterator type_path_iter( typePath );
1858+ type_resolution_iterator type_path_iter( this, typePath );
18141859
18151860 while ( type_path_iter.IsEnd() == false )
18161861 {
1817- std::string curToken = type_path_iter.Resolve();
1862+ dtsString curToken = type_path_iter.Resolve();
18181863
18191864 currentType = FindTypeInfo( curToken.c_str(), currentType );
18201865
@@ -1832,4 +1877,24 @@
18321877 }
18331878 };
18341879
1880+// Redirect from structRegistry to typeInfoBase.
1881+DTS_TEMPLARGS
1882+IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_ALLOCATE_RETURN DynamicTypeSystem DTS_TEMPLUSE::structRegRedirAlloc::Allocate IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_ALLOCATE_ARGS
1883+{
1884+ typeInfoBase *hostStruct = LIST_GETITEM( typeInfoBase, refMem, structRegistry );
1885+ return allocatorType::Allocate( hostStruct->typeSys, memSize, alignment );
1886+}
1887+DTS_TEMPLARGS
1888+IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_RESIZE_RETURN DynamicTypeSystem DTS_TEMPLUSE::structRegRedirAlloc::Resize IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_RESIZE_ARGS
1889+{
1890+ typeInfoBase *hostStruct = LIST_GETITEM( typeInfoBase, refMem, structRegistry );
1891+ return allocatorType::Resize( hostStruct->typeSys, objMem, reqNewSize );
1892+}
1893+DTS_TEMPLARGS
1894+IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_FREE_RETURN DynamicTypeSystem DTS_TEMPLUSE::structRegRedirAlloc::Free IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_FREE_ARGS
1895+{
1896+ typeInfoBase *hostStruct = LIST_GETITEM( typeInfoBase, refMem, structRegistry );
1897+ allocatorType::Free( hostStruct->typeSys, memPtr );
1898+}
1899+
18351900 #endif //_DYN_TYPE_ABSTRACTION_SYSTEM_
--- common/sdk/MemoryUtils.h (revision 126)
+++ common/sdk/MemoryUtils.h (revision 127)
@@ -14,8 +14,6 @@
1414 #define _GLOBAL_MEMORY_UTILITIES_
1515
1616 #include <assert.h>
17-#include <list>
18-#include <vector>
1917 #include "rwlist.hpp"
2018 #include "MemoryRaw.h"
2119 #include "DataUtil.h"
@@ -24,6 +22,9 @@
2422 #include <type_traits>
2523 #include <tuple>
2624
25+// Prefer our own SDK types, because they are not tied to ridiculous allocator semantics.
26+#include "Vector.h"
27+
2728 template <typename numberType, typename managerType>
2829 struct FirstPassAllocationSemantics
2930 {
@@ -1221,7 +1222,12 @@
12211222 };
12221223
12231224 // Class used to register anonymous structs that can be placed on top of a C++ type.
1224-template <typename abstractionType, typename pluginDescriptorType_meta, typename pluginAvailabilityDispatchType, typename... AdditionalArgs>
1225+#define APSR_TEMPLARGS \
1226+ template <typename abstractionType, typename pluginDescriptorType_meta, typename pluginAvailabilityDispatchType, typename allocatorType, typename... AdditionalArgs>
1227+#define APSR_TEMPLUSE \
1228+ <abstractionType, pluginDescriptorType_meta, pluginAvailabilityDispatchType, allocatorType, AdditionalArgs...>
1229+
1230+APSR_TEMPLARGS
12251231 struct AnonymousPluginStructRegistry
12261232 {
12271233 // Export for DTS.
@@ -1333,9 +1339,10 @@
13331339 pluginInterface *descriptor;
13341340 };
13351341
1342+ DEFINE_HEAP_REDIR_ALLOC( regPluginsRedirAlloc );
1343+
13361344 // Container that holds plugin information.
1337- // TODO: Using STL types is crap (?). Use a custom type instead!
1338- typedef std::vector <registered_plugin> registeredPlugins_t;
1345+ typedef eir::Vector <registered_plugin, regPluginsRedirAlloc> registeredPlugins_t;
13391346
13401347 registeredPlugins_t regPlugins;
13411348
@@ -1381,7 +1388,7 @@
13811388 // Register the pointer to the registered plugin.
13821389 pluginRegions.PutBlock( &info, blockAllocInfo );
13831390
1384- regPlugins.push_back( std::move( info ) );
1391+ regPlugins.AddToBack( std::move( info ) );
13851392
13861393 // Notify the flavor to update.
13871394 this->regBoundFlavor.UpdatePluginRegion( this );
@@ -1394,9 +1401,11 @@
13941401 bool hasDeleted = false;
13951402
13961403 // Get the plugin information that represents this plugin offset.
1397- for ( typename registeredPlugins_t::iterator iter = regPlugins.begin(); iter != regPlugins.end(); iter++ )
1404+ size_t numPlugins = regPlugins.GetCount();
1405+
1406+ for ( size_t n = 0; n < numPlugins; n++ )
13981407 {
1399- registered_plugin& thePlugin = *iter;
1408+ registered_plugin& thePlugin = regPlugins[ n ];
14001409
14011410 if ( thePlugin.pluginOffset == pluginOffset )
14021411 {
@@ -1406,7 +1415,7 @@
14061415
14071416 pluginRegions.RemoveBlock( &thePlugin );
14081417
1409- regPlugins.erase( iter );
1418+ regPlugins.RemoveByIndex( n );
14101419
14111420 hasDeleted = true;
14121421 break; // there can be only one.
@@ -1591,6 +1600,16 @@
15911600 }
15921601 };
15931602
1603+APSR_TEMPLARGS
1604+IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_ALLOCATE_RETURN AnonymousPluginStructRegistry APSR_TEMPLUSE::regPluginsRedirAlloc::Allocate IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_ALLOCATE_ARGS
1605+IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_ALLOCATE_BODY( regPluginsRedirAlloc, AnonymousPluginStructRegistry, regPlugins, allocatorType )
1606+APSR_TEMPLARGS
1607+IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_RESIZE_RETURN AnonymousPluginStructRegistry APSR_TEMPLUSE::regPluginsRedirAlloc::Resize IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_RESIZE_ARGS
1608+IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_RESIZE_BODY( regPluginsRedirAlloc, AnonymousPluginStructRegistry, regPlugins, allocatorType )
1609+APSR_TEMPLARGS
1610+IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_FREE_RETURN AnonymousPluginStructRegistry APSR_TEMPLUSE::regPluginsRedirAlloc::Free IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_FREE_ARGS
1611+IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_FREE_BODY( regPluginsRedirAlloc, AnonymousPluginStructRegistry, regPlugins, allocatorType )
1612+
15941613 struct copy_assignment_exception {};
15951614
15961615 // Helper struct for common plugin system functions.
@@ -1814,7 +1833,12 @@
18141833 // This one is inspired by the RenderWare plugin system.
18151834 // This container is NOT MULTI-THREAD SAFE.
18161835 // All operations are expected to be ATOMIC.
1817-template <typename classType, typename flavorType = cachedMinimalStructRegistryFlavor <classType>>
1836+#define SPCF_TEMPLARGS \
1837+ template <typename classType, typename allocatorType, typename flavorType = cachedMinimalStructRegistryFlavor <classType>>
1838+#define SPCF_TEMPLUSE \
1839+ <classType, allocatorType, flavorType>
1840+
1841+SPCF_TEMPLARGS
18181842 struct StaticPluginClassFactory
18191843 {
18201844 typedef classType hostType_t;
@@ -1876,8 +1900,12 @@
18761900 unsigned int pluginId;
18771901 };
18781902
1879- typedef AnonymousPluginStructRegistry <classType, pluginDescriptor, flavorType> structRegistry_t;
1903+private:
1904+ DEFINE_HEAP_REDIR_ALLOC( spcfAllocRedir );
18801905
1906+public:
1907+ typedef AnonymousPluginStructRegistry <classType, pluginDescriptor, flavorType, spcfAllocRedir> structRegistry_t;
1908+
18811909 structRegistry_t structRegistry;
18821910
18831911 // Localize certain plugin registry types.
@@ -1912,7 +1940,7 @@
19121940 AINLINE static classType* BACK_RESOLVE_STRUCT( void *pluginObj, pluginOffset_t offset )
19131941 {
19141942 if ( IsOffsetValid( offset ) == false )
1915- return NULL;
1943+ return nullptr;
19161944
19171945 return (classType*)( (char*)pluginObj - ( sizeof( classType ) + offset ) );
19181946 }
@@ -2320,6 +2348,16 @@
23202348 }
23212349 };
23222350
2351+SPCF_TEMPLARGS
2352+IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_ALLOCATE_RETURN StaticPluginClassFactory SPCF_TEMPLUSE::spcfAllocRedir::Allocate IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_ALLOCATE_ARGS
2353+IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_ALLOCATE_BODY( spcfAllocRedir, StaticPluginClassFactory, structRegistry, allocatorType )
2354+SPCF_TEMPLARGS
2355+IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_RESIZE_RETURN StaticPluginClassFactory SPCF_TEMPLUSE::spcfAllocRedir::Resize IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_RESIZE_ARGS
2356+IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_RESIZE_BODY( spcfAllocRedir, StaticPluginClassFactory, structRegistry, allocatorType )
2357+SPCF_TEMPLARGS
2358+IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_FREE_RETURN StaticPluginClassFactory SPCF_TEMPLUSE::spcfAllocRedir::Free IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_FREE_ARGS
2359+IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_FREE_BODY( spcfAllocRedir, StaticPluginClassFactory, structRegistry, allocatorType )
2360+
23232361 // Array implementation that extends on concepts found inside GTA:SA
23242362 // NOTE: This array type is a 'trusted type'.
23252363 // -> Use it whenever necessary.
--- common/sdk/MetaHelpers.h (revision 126)
+++ common/sdk/MetaHelpers.h (revision 127)
@@ -88,4 +88,38 @@
8888 hostStruct->allocNode.Free( memPtr ); \
8989 }
9090
91+// Complicated macro template programming.
92+#define IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_ALLOCATE_ARGS ( void *refMem, size_t memSize, size_t alignment )
93+#define IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_ALLOCATE_RETURN void*
94+#define IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_ALLOCATE_BODY( allocTypeName, hostStructTypeName, redirNode, directAllocTypeName ) \
95+ { \
96+ hostStructTypeName *hostStruct = LIST_GETITEM( hostStructTypeName, refMem, redirNode ); \
97+ return directAllocTypeName::Allocate( hostStruct, memSize, alignment ); \
98+ }
99+
100+#define IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_RESIZE_ARGS ( void *refMem, void *objMem, size_t reqNewSize )
101+#define IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_RESIZE_RETURN bool
102+#define IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_RESIZE_BODY( allocTypeName, hostStructTypeName, redirNode, directAllocTypeName ) \
103+ { \
104+ hostStructTypeName *hostStruct = LIST_GETITEM( hostStructTypeName, refMem, redirNode ); \
105+ return directAllocTypeName::Resize( hostStruct, objMem, reqNewSize ); \
106+ }
107+
108+#define IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_FREE_ARGS ( void *refMem, void *memPtr )
109+#define IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_FREE_RETURN void
110+#define IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_FREE_BODY( allocTypeName, hostStructTypeName, redirNode, directAllocTypeName ) \
111+ { \
112+ hostStructTypeName *hostStruct = LIST_GETITEM( hostStructTypeName, refMem, redirNode ); \
113+ directAllocTypeName::Free( hostStruct, memPtr ); \
114+ }
115+
116+// A simple redirector for allocators.
117+#define IMPL_HEAP_REDIR_DIRECT_ALLOC( allocTypeName, hostStructTypeName, redirNode, directAllocTypeName ) \
118+ IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_ALLOCATE_RETURN allocTypeName::Allocate IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_ALLOCATE_ARGS \
119+ IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_ALLOCATE_BODY( allocTypeName, hostStructTypeName, redirNode, directAllocTypeName ) \
120+ IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_RESIZE_RETURN allocTypeName::Resize IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_RESIZE_ARGS \
121+ IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_RESIZE_BODY( allocTypeName, hostStructTypeName, redirNode, directAllocTypeName ) \
122+ IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_FREE_RETURN allocTypeName::Free IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_FREE_ARGS \
123+ IMPL_HEAP_REDIR_DIRECT_ALLOC_METH_FREE_BODY( allocTypeName, hostStructTypeName, redirNode, directAllocTypeName )
124+
91125 #endif //_COMMON_META_PROGRAMMING_HELPERS_
\ No newline at end of file
--- common/sdk/UniChar.h (revision 126)
+++ common/sdk/UniChar.h (revision 127)
@@ -18,6 +18,7 @@
1818
1919 #include <locale>
2020 #include <type_traits>
21+#include <string>
2122
2223 #include <limits>
2324
--- common/sdk/UniChar.strmodel.h (revision 126)
+++ common/sdk/UniChar.strmodel.h (revision 127)
@@ -15,6 +15,8 @@
1515
1616 #include "MacroUtils.h"
1717
18+#include <string>
19+
1820 //TODO: find ways to share as much code as possible between the different string model functions!
1921
2022 // IMPORTANT: this function is designed to terminate on '\0' inside any string aswell.
--- common/sdk/Vector.h (revision 126)
+++ common/sdk/Vector.h (revision 127)
@@ -277,6 +277,41 @@
277277
278278 public:
279279 // Simple modification methods.
280+ inline void AddToBack( structType&& item )
281+ {
282+ size_t oldCount = ( this->data.data_count );
283+ size_t newCount = ( oldCount + 1 );
284+ size_t newRequiredSize = newCount * sizeof(structType);
285+
286+ if ( structType *use_data = this->data.data_entries )
287+ {
288+ bool gotToResize = this->data.allocData.Resize( this, use_data, newRequiredSize );
289+
290+ if ( gotToResize )
291+ {
292+ // We just have to add at back.
293+ new ( use_data + oldCount ) structType( std::move( item ) );
294+
295+ this->data.data_count = newCount;
296+
297+ return;
298+ }
299+ }
300+
301+ recast_memory( newCount, newRequiredSize,
302+ [&]( void *memPtr, structType *old_item, size_t idx )
303+ {
304+ if ( idx == oldCount )
305+ {
306+ new (memPtr) structType( std::move( item ) );
307+ }
308+ else
309+ {
310+ new (memPtr) structType( std::move( *old_item ) );
311+ }
312+ });
313+ }
314+
280315 inline void AddToBack( const structType& item )
281316 {
282317 size_t oldCount = ( this->data.data_count );
@@ -299,7 +334,7 @@
299334 }
300335
301336 recast_memory( newCount, newRequiredSize,
302- [&]( void *memPtr, const structType *old_item, size_t idx )
337+ [&]( void *memPtr, structType *old_item, size_t idx )
303338 {
304339 if ( idx == oldCount )
305340 {
@@ -307,11 +342,52 @@
307342 }
308343 else
309344 {
310- new (memPtr) structType( *old_item );
345+ new (memPtr) structType( std::move( *old_item ) );
311346 }
312347 });
313348 }
314349
350+ inline void RemoveByIndex( size_t removeIdx )
351+ {
352+ size_t oldCount = this->data.data_count;
353+
354+ if ( oldCount == 0 )
355+ return;
356+
357+ size_t newCount = ( oldCount - 1 );
358+
359+ // We can always shrink memory, so go for it.
360+ structType *use_data = this->data.data_entries;
361+
362+ use_data[ newCount ].~structType();
363+
364+ // We move down all items to squash the item that was removed.
365+ {
366+ size_t canBeSquashedCount = newCount;
367+
368+ for ( size_t n = removeIdx; n < canBeSquashedCount; n++ )
369+ {
370+ *( use_data + n ) = std::move( *( use_data + n + 1 ) );
371+ }
372+ }
373+
374+ if ( newCount == 0 )
375+ {
376+ this->data.allocData.Free( this, use_data );
377+
378+ // Gotta do this.
379+ this->data.data_entries = nullptr;
380+ }
381+ else
382+ {
383+ bool resizeSuccess = this->data.allocData.Resize( this, use_data, sizeof(structType) * newCount );
384+
385+ assert( resizeSuccess == true );
386+ }
387+
388+ this->data.data_count = newCount;
389+ }
390+
315391 inline void RemoveFromBack( void )
316392 {
317393 size_t oldCount = this->data.data_count;
--- unittests/src/impltests.cpp (revision 126)
+++ unittests/src/impltests.cpp (revision 127)
@@ -7,6 +7,22 @@
77
88 #include <sdk/MetaHelpers.h>
99
10+// Test if you can define a function body of a substruct inside of a template struct.
11+template <typename structType>
12+struct templStruct
13+{
14+ struct meow
15+ {
16+ static void* Test( void );
17+ };
18+};
19+
20+template <typename structType>
21+void* templStruct <structType>::meow::Test( void )
22+{
23+ return nullptr;
24+}
25+
1026 struct class_a
1127 {
1228 int value = -1;
@@ -176,4 +192,9 @@
176192
177193 static_assert( sizeof( one_allocTest ) == sizeof(int) );
178194 }
195+
196+ // Test for a test above.
197+ {
198+ templStruct <int>::meow::Test();
199+ }
179200 }
Show on old repository browser