Skip to content

Commit 48313cb

Browse files
authored
[NeoMathEngine] Remove IMathEngine from MemoryHandleVar (#1119)
Signed-off-by: Kirill Golikov <kirill.golikov@abbyy.com>
1 parent 76a18b1 commit 48313cb

File tree

2 files changed

+29
-48
lines changed

2 files changed

+29
-48
lines changed

NeoMathEngine/include/NeoMathEngine/MemoryHandle.h

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ class IMathEngine;
2525
class CMemoryHandleInternal;
2626

2727
// Wraps the pointer to memory allocated by a math engine
28+
// IMPORTANT: Do not use pointers to CMemoryHandle for children classes with fields, because of the non virtual dtor.
2829
class NEOMATHENGINE_API CMemoryHandle {
2930
public:
3031
constexpr CMemoryHandle() = default;
3132
// Be copied and moved by default
3233

33-
bool operator!=( const CMemoryHandle& other ) const { return !operator==( other ); }
34+
bool operator!=( const CMemoryHandle& other ) const { return !( *this == other ); }
3435
bool operator==( const CMemoryHandle& other ) const
3536
{ return MathEngine == other.MathEngine && Object == other.Object && Offset == other.Offset; }
3637

@@ -54,6 +55,7 @@ class NEOMATHENGINE_API CMemoryHandle {
5455
//---------------------------------------------------------------------------------------------------------------------
5556

5657
// Wraps the typed pointer to memory allocated by a math engine
58+
// IMPORTANT: Do not use pointers to CMemoryHandle for children classes with fields, because of the non virtual dtor.
5759
template <class T>
5860
class CTypedMemoryHandle : public CMemoryHandle {
5961
public:
@@ -130,37 +132,43 @@ class CTypedMemoryHandle : public CMemoryHandle {
130132
//---------------------------------------------------------------------------------------------------------------------
131133

132134
// CMemoryHandleVar is a variable or a fixed-size array for a math engine
135+
// IMPORTANT: Do not use pointers to CMemoryHandleVarBase for children with fields, because of the non virtual dtor.
133136
template<class T>
134137
class CMemoryHandleVarBase {
135138
public:
136-
void SetValueAt( int index, T value );
137-
T GetValueAt( int index ) const;
138-
void SetValue( T value );
139-
T GetValue() const;
139+
// Moveable only
140+
CMemoryHandleVarBase( CMemoryHandleVarBase&& other ) : Data( other.Data ), DataSize( other.DataSize )
141+
{ other.Data = CTypedMemoryHandle<T>{}; } // nullify to avoid double free
142+
CMemoryHandleVarBase& operator=( CMemoryHandleVarBase&& other )
143+
{ if( this != &other ) { std::swap( *this, other ); } return *this; }
144+
145+
void SetValueAt( int index, T value ) { Data.SetValueAt( index, value ); }
146+
T GetValueAt( int index ) const { return Data.GetValueAt( index ); }
147+
void SetValue( T value ) { Data.SetValue( value ); }
148+
T GetValue() const { return Data.GetValue(); }
140149

141150
const CTypedMemoryHandle<T>& GetHandle() const { return Data; }
142151

143152
// Operators for easier use
144-
operator const CTypedMemoryHandle<T>&( ) const { return GetHandle(); }
153+
operator const CTypedMemoryHandle<T>&() const { return GetHandle(); }
145154
operator CTypedMemoryHandle<const T>() const { return GetHandle(); }
146155
CTypedMemoryHandle<T> operator []( int index ) const { return GetHandle() + index; }
147156

148157
bool operator==( const CTypedMemoryHandle<const T>& other ) const { return GetHandle() == other; }
149-
bool operator!=( const CTypedMemoryHandle<const T>& other ) const { return GetHandle() != other; }
158+
bool operator!=( const CTypedMemoryHandle<const T>& other ) const { return !( *this == other ); }
150159

151160
CTypedMemoryHandle<T> operator+( ptrdiff_t shift ) const { return GetHandle() + shift; }
152161
CTypedMemoryHandle<T> operator-( ptrdiff_t shift ) const { return GetHandle() - shift; }
153162
int operator-( const CTypedMemoryHandle<T>& handle ) const { return GetHandle() - handle; }
154163

155164
int Size() const { return static_cast<int>( DataSize ); }
156-
IMathEngine* GetMathEngine() const { return &MathEngine; }
165+
IMathEngine* GetMathEngine() const { return Data.GetMathEngine(); }
157166

158167
protected:
159-
IMathEngine& MathEngine; // the math engine owner
160-
mutable CTypedMemoryHandle<T> Data; // the typed memory handler
168+
CTypedMemoryHandle<T> Data; // the typed memory handler
161169
const size_t DataSize; // the typed memory size
162170

163-
CMemoryHandleVarBase( IMathEngine& mathEngine, size_t size ) : MathEngine( mathEngine ), DataSize( size ) {}
171+
explicit CMemoryHandleVarBase( size_t size ) : DataSize( size ) {}
164172
~CMemoryHandleVarBase() = default;
165173

166174
private:
@@ -171,7 +179,7 @@ class CMemoryHandleVarBase {
171179

172180
//---------------------------------------------------------------------------------------------------------------------
173181

174-
// A variable or an array
182+
// A variable or an array on the heap
175183
template<class T>
176184
class CMemoryHandleVar : public CMemoryHandleVarBase<T> {
177185
public:

NeoMathEngine/include/NeoMathEngine/MemoryHandle.inl

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -51,70 +51,43 @@ inline T CTypedMemoryHandle<T>::GetValue() const
5151
return *value;
5252
}
5353

54-
//------------------------------------------------------------------------------------------------------------
55-
// CMemoryHandleVar is a variable or fixed-size array for a math engine
56-
57-
template<class T>
58-
inline void CMemoryHandleVarBase<T>::SetValueAt( int index, T value )
59-
{
60-
Data.SetValueAt( index, value );
61-
}
62-
63-
template<class T>
64-
inline T CMemoryHandleVarBase<T>::GetValueAt( int index ) const
65-
{
66-
return Data.GetValueAt( index );
67-
}
54+
//---------------------------------------------------------------------------------------------------------------------
6855

69-
template<class T>
70-
inline void CMemoryHandleVarBase<T>::SetValue( T value )
71-
{
72-
Data.SetValue( value );
73-
}
74-
75-
template<class T>
76-
inline T CMemoryHandleVarBase<T>::GetValue() const
77-
{
78-
return Data.GetValue();
79-
}
80-
81-
//------------------------------------------------------------------------------------------------------------
82-
// A variable or array
56+
// CMemoryHandleVar is a variable or fixed-size array for a math engine
8357

8458
template<class T>
8559
inline CMemoryHandleVar<T>::CMemoryHandleVar( IMathEngine& mathEngine, size_t size ) :
86-
CMemoryHandleVarBase<T>( mathEngine, size )
60+
CMemoryHandleVarBase<T>( size )
8761
{
8862
if( size != 0 ) {
89-
CMemoryHandleVarBase<T>::Data = CMemoryHandleVarBase<T>::MathEngine.template HeapAllocTyped<T>( size );
63+
CMemoryHandleVarBase<T>::Data = mathEngine.template HeapAllocTyped<T>( size );
9064
}
9165
}
9266

9367
template<class T>
9468
inline CMemoryHandleVar<T>::~CMemoryHandleVar()
9569
{
9670
if( !CMemoryHandleVarBase<T>::Data.IsNull() ) {
97-
CMemoryHandleVarBase<T>::MathEngine.HeapFree( CMemoryHandleVarBase<T>::Data );
71+
CMemoryHandleVarBase<T>::Data.GetMathEngine()->HeapFree( CMemoryHandleVarBase<T>::Data );
9872
}
9973
}
10074

101-
//------------------------------------------------------------------------------------------------------------
75+
//---------------------------------------------------------------------------------------------------------------------
10276

10377
template<class T>
10478
inline CMemoryHandleStackVar<T>::CMemoryHandleStackVar( IMathEngine& mathEngine, size_t size ) :
105-
CMemoryHandleVarBase<T>( mathEngine, size )
79+
CMemoryHandleVarBase<T>( size )
10680
{
10781
if( size != 0 ) {
108-
CMemoryHandleVarBase<T>::Data =
109-
CTypedMemoryHandle<T>( CMemoryHandleVarBase<T>::MathEngine.StackAlloc( size * sizeof( T ) ) );
82+
CMemoryHandleVarBase<T>::Data = CTypedMemoryHandle<T>( mathEngine.StackAlloc( size * sizeof( T ) ) );
11083
}
11184
}
11285

11386
template<class T>
11487
inline CMemoryHandleStackVar<T>::~CMemoryHandleStackVar()
11588
{
11689
if( !CMemoryHandleVarBase<T>::Data.IsNull() ) {
117-
CMemoryHandleVarBase<T>::MathEngine.StackFree( CMemoryHandleVarBase<T>::Data );
90+
CMemoryHandleVarBase<T>::Data.GetMathEngine()->StackFree( CMemoryHandleVarBase<T>::Data );
11891
}
11992
}
12093

0 commit comments

Comments
 (0)