1
- /* Copyright © 2017-2020 ABBYY Production LLC
1
+ /* Copyright © 2017-2023 ABBYY
2
2
3
3
Licensed under the Apache License, Version 2.0 (the "License");
4
4
you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@ limitations under the License.
16
16
#pragma once
17
17
18
18
#include < NeoMathEngine/NeoMathEngineDefs.h>
19
+ #include < NeoMathEngine/NeoMathEngineException.h>
19
20
#include < cstddef>
20
21
#include < type_traits>
21
22
@@ -24,47 +25,77 @@ namespace NeoML {
24
25
class IMathEngine ;
25
26
class CMemoryHandleInternal ;
26
27
28
+ // Get pointer to IMathEngine by the given CurrentEntity
29
+ inline IMathEngine* GetMathEngineByIndex ( size_t currentEntity );
30
+ // Get current entity from the given pointer to IMathEngine
31
+ inline size_t GetIndexOfMathEngine ( const IMathEngine* mathEngine );
32
+
27
33
// Wraps the pointer to memory allocated by a math engine
28
34
class NEOMATHENGINE_API CMemoryHandle {
35
+ private:
36
+ #if FINE_PLATFORM( FINE_64_BIT )
37
+ static constexpr int MathEngineCountWidth = 10 ; // compress to bitfield
38
+ static constexpr int MathEngineCountShift = ( sizeof ( size_t ) * 8 /* bits*/ ) - MathEngineCountWidth;
39
+ static constexpr size_t MathEngineEntityInvalid = ( ( size_t ( 1 ) << MathEngineCountWidth ) - 1 );
40
+ static constexpr size_t MathEngineMaxOffset = size_t ( 1 ) << MathEngineCountShift;
41
+ #else // FINE_32_BIT
42
+ // only for bitfield compiles correct. no compress
43
+ static constexpr int MathEngineCountWidth = sizeof ( size_t ) * 8 ;
44
+ static constexpr int MathEngineCountShift = sizeof ( size_t ) * 8 ;
45
+ static constexpr size_t MathEngineEntityInvalid = size_t ( -1 );
46
+ #endif // FINE_32_BIT
47
+
29
48
public:
30
- CMemoryHandle () : mathEngine( 0 ), object( 0 ), offset( 0 ) {}
31
- CMemoryHandle ( const CMemoryHandle& other ) : mathEngine( other.mathEngine ), object( other.object ), offset( other.offset ) {}
49
+ // Any possible number of all mathEngines
50
+ static constexpr int MaxMathEngineEntities = 1024 ;
51
+
52
+ CMemoryHandle () : object( nullptr ), offset( 0 ), entity( MathEngineEntityInvalid ) {}
53
+ CMemoryHandle ( const CMemoryHandle& other ) : object( other.object ), offset( other.offset ), entity( other.entity ) {}
32
54
33
55
CMemoryHandle& operator =( const CMemoryHandle& other )
34
56
{
35
- mathEngine = other.mathEngine ;
36
- object = other.object ;
37
- offset = other.offset ;
57
+ if ( this != &other ) {
58
+ object = other.object ;
59
+ offset = other.offset ;
60
+ entity = other.entity ;
61
+ }
38
62
return *this ;
39
63
}
40
64
41
65
bool operator ==( const CMemoryHandle& other ) const
42
- {
43
- return mathEngine == other.mathEngine && object == other.object && offset == other.offset ;
44
- }
66
+ { return object == other.object && offset == other.offset && entity == other.entity ; }
45
67
46
- bool operator !=( const CMemoryHandle& other ) const
47
- {
48
- return !operator ==( other );
49
- }
68
+ bool operator !=( const CMemoryHandle& other ) const { return !operator ==( other ); }
50
69
51
- bool IsNull () const
52
- {
53
- return mathEngine == 0 && object == 0 && offset == 0 ;
54
- }
70
+ bool IsNull () const { return object == nullptr && offset == 0 && entity == MathEngineEntityInvalid; }
55
71
56
- IMathEngine* GetMathEngine () const { return mathEngine ; }
72
+ IMathEngine* GetMathEngine () const { return GetMathEngineByIndex ( entity ) ; }
57
73
58
74
protected:
59
- IMathEngine* mathEngine; // the math engine
75
+ // struct of (16 bytes size for x64 and arm-x64) and (12 bytes size for x86 and arm-x32)
60
76
const void * object; // the base object
61
- std::ptrdiff_t offset; // the offset in the base object, in bytes
77
+ size_t offset : MathEngineCountShift; // (x64) the less significant bits of size_t stores offset in the base object, in bytes
78
+ size_t entity : MathEngineCountWidth; // (x64) the most significant bits of size_t stores the number of IMathEngine entity
62
79
63
80
friend class CMemoryHandleInternal ;
64
81
65
- explicit CMemoryHandle ( IMathEngine* _mathEngine, const void * _object, ptrdiff_t _offset ) : mathEngine( _mathEngine ), object( _object ), offset( _offset ) {}
82
+ explicit CMemoryHandle ( IMathEngine* _mathEngine, const void * _object, ptrdiff_t _offset ) :
83
+ CMemoryHandle( _object , _offset, GetIndexOfMathEngine( _mathEngine ) )
84
+ {}
85
+
86
+ CMemoryHandle CopyMemoryHandle ( ptrdiff_t shift ) const { return CMemoryHandle ( object, offset + shift, entity ); }
66
87
67
- CMemoryHandle CopyMemoryHandle ( ptrdiff_t shift ) const { return CMemoryHandle ( mathEngine, object, offset + shift ); }
88
+ private:
89
+ explicit CMemoryHandle ( const void * _object, ptrdiff_t _offset, size_t _entity ) :
90
+ object( _object ), offset( _offset ), entity( _entity )
91
+ {
92
+ #if FINE_PLATFORM( FINE_64_BIT )
93
+ static_assert ( MaxMathEngineEntities == ( 1 << MathEngineCountWidth ), " Invalid MaxMathEngineEntities" );
94
+ // Checks that the most significant bits do not interfere the result
95
+ ASSERT_EXPR ( 0 <= _offset && size_t ( _offset ) < MathEngineMaxOffset );
96
+ #endif // FINE_64_BIT
97
+ ASSERT_EXPR ( _entity < MaxMathEngineEntities );
98
+ }
68
99
};
69
100
70
101
// ------------------------------------------------------------------------------------------------------------
0 commit comments