BentleyAllocator.h
Go to the documentation of this file.
1 /*--------------------------------------------------------------------------------------+
2 |
3 | Supplied under applicable software license agreement.
4 |
5 | Copyright (c) 2018 Bentley Systems, Incorporated. All rights reserved.
6 |
7 +---------------------------------------------------------------------------------------*/
8 #pragma once
9 
11 
12 #include "Bentley.h"
13 #include <memory>
14 
16 #define DEFINE_BENTLEY_NEW_DELETE_OPERATORS\
17  void* operator new(size_t size) { return bentleyAllocator_new(size); }\
18  void operator delete(void *rawMemory, size_t size) { bentleyAllocator_delete(rawMemory, size); }
19 
20 
21 #if !defined (BENTLEYALLOCATOR_EXPORT)
22 #define BENTLEYALLOCATOR_EXPORT IMPORT_ATTRIBUTE
23 #endif
24 
25 extern "C" BENTLEYALLOCATOR_EXPORT void* bentleyAllocator_new (size_t);
26 extern "C" BENTLEYALLOCATOR_EXPORT void bentleyAllocator_delete (void*, size_t);
27 extern "C" BENTLEYALLOCATOR_EXPORT void* bentleyAllocator_malloc (size_t);
28 extern "C" BENTLEYALLOCATOR_EXPORT void* bentleyAllocator_realloc (void*, size_t);
29 extern "C" BENTLEYALLOCATOR_EXPORT void bentleyAllocator_free (void*, size_t);
32 
34 
37 
38 /*=================================================================================**/
45 {
46 enum AllocationType {TopDown=1, Commit=2, Reserve=4, CommitTopDown=Commit|TopDown};
47 enum Protection {ReadWrite=1,ReadOnly=2};
48 enum FreeType {Release=1, Decommit=2};
49 
50  BENTLEYDLL_EXPORT static void* Alloc (void*, size_t numBytes, AllocationType, Protection);
51 
52  BENTLEYDLL_EXPORT static BentleyStatus Protect (Protection*, void*, size_t numBytes, Protection);
53 
54  BENTLEYDLL_EXPORT static BentleyStatus Free (void*, size_t numBytes, FreeType);
55 
56  BENTLEYDLL_EXPORT static Int32 GetPageSize();
57 
58 }; // BeVirtualMemory
59 
60 /*=================================================================================**/
67 template <class _Ty> class BentleyAllocator
68  {
69 public:
70  typedef _Ty value_type;
71  typedef _Ty* pointer;
72  typedef _Ty& reference;
73  typedef _Ty const* const_pointer;
74  typedef _Ty const& const_reference;
75  typedef std::size_t size_type;
76  typedef std::ptrdiff_t difference_type;
77 
78  template<class _Other> struct rebind
79  { // convert an allocator<_Ty> to an allocator <_Other>
81  };
82 
84  { // return address of mutable _Val
85  return (&_Val);
86  }
87 
89  { // return address of nonmutable _Val
90  return (&_Val);
91  }
92 
94  { // construct default allocator (do nothing)
95  }
96 
98  { // construct by copying (do nothing)
99  }
100 
101  template<class _Other>
103  { // construct from a related allocator (do nothing)
104  }
105 
106  template<class _Other>
108  { // assign from a related allocator (do nothing)
109  return (*this);
110  }
111 
112  void deallocate(pointer _Ptr, size_type _Size)
113  { // deallocate object at _Ptr, ignore size
114  bentleyAllocator_free (_Ptr, _Size * sizeof (_Ty));
115  }
116 
118  { // allocate array of _Count elements
119  return (pointer) bentleyAllocator_malloc (_Count * sizeof (_Ty));
120  }
121 
122  pointer allocate(size_type _Count, typename std::allocator<void>::const_pointer)
123  { // allocate array of _Count elements, ignore hint
124  return (allocate(_Count));
125  }
126 
127  void construct(pointer _Ptr, const _Ty& _Val)
128  { // construct object at _Ptr with value _Val
129  new((void*)_Ptr)_Ty(_Val);
130  }
131 
132  void destroy(pointer _Ptr)
133  { // destroy object at _Ptr
134  _Ptr->~_Ty();
135  }
136 
138  { // estimate maximum array size
139  size_type _Count = (size_type)(-1) / sizeof (_Ty);
140  return (0 < _Count ? _Count : 1);
141  }
142  };
143 
144 
145 template<class _Ty, class _Other>
147  { // test for allocator equality (always true)
148  return true;
149  }
150 
151 template <class _Ty, class _Other>
153  { // test for allocator inequality (always false)
154  return false;
155  }
156 
158 
void * bentleyAllocator_malloc(size_t)
#define BENTLEYDLL_EXPORT
Definition: Bentley.h:249
void bentleyAllocator_free(void *, size_t)
Protection
Definition: BentleyAllocator.h:47
#define BENTLEYALLOCATOR_EXPORT
Definition: BentleyAllocator.h:22
_Ty const * const_pointer
Definition: BentleyAllocator.h:73
pointer allocate(size_type _Count, typename std::allocator< void >::const_pointer)
Definition: BentleyAllocator.h:122
pointer address(reference _Val) const
Definition: BentleyAllocator.h:83
void bentleyAllocator_delete(void *, size_t)
size_type max_size() const
Definition: BentleyAllocator.h:137
BentleyAllocator(const BentleyAllocator< _Ty > &)
Definition: BentleyAllocator.h:97
void bentleyAllocator_enableLowFragmentationCRTHeap()
_Ty value_type
Definition: BentleyAllocator.h:70
BentleyAllocator()
Definition: BentleyAllocator.h:93
BentleyAllocator< _Ty > & operator=(const BentleyAllocator< _Other > &)
Definition: BentleyAllocator.h:107
void deallocate(pointer _Ptr, size_type _Size)
Definition: BentleyAllocator.h:112
pointer allocate(size_type _Count)
Definition: BentleyAllocator.h:117
void construct(pointer _Ptr, const _Ty &_Val)
Definition: BentleyAllocator.h:127
_Ty * pointer
Definition: BentleyAllocator.h:71
#define BEGIN_BENTLEY_NAMESPACE
Definition: Bentley.r.h:24
_Ty const & const_reference
Definition: BentleyAllocator.h:74
Definition: BentleyAllocator.h:78
void * bentleyAllocator_new(size_t)
std::ptrdiff_t difference_type
Definition: BentleyAllocator.h:76
BentleyStatus
Definition: Bentley.h:208
Open for both read and write.
int32_t Int32
Definition: Bentley.r.h:119
Defines typedefs and constants that can be used across other namespaces. All Bentley-authored C++ sou...
const_pointer address(const_reference _Val) const
Definition: BentleyAllocator.h:88
std::size_t size_type
Definition: BentleyAllocator.h:75
bool operator!=(const BentleyAllocator< _Ty > &, const BentleyAllocator< _Other > &)
Definition: BentleyAllocator.h:152
FreeType
Definition: BentleyAllocator.h:48
#define END_BENTLEY_NAMESPACE
Definition: Bentley.r.h:25
void destroy(pointer _Ptr)
Definition: BentleyAllocator.h:132
Utility functions for managing virtual memory.
Definition: BentleyAllocator.h:44
_Ty & reference
Definition: BentleyAllocator.h:72
BentleyAllocator< _Other > other
Definition: BentleyAllocator.h:80
void * bentleyAllocator_realloc(void *, size_t)
void * bentleyAllocator_getNullRefBuffer()
An STL-compliant allocator that calls bentleyAllocator_malloc and bentleyAllocator_free.
Definition: BentleyAllocator.h:67
BentleyAllocator(const BentleyAllocator< _Other > &)
Definition: BentleyAllocator.h:102
AllocationType
Definition: BentleyAllocator.h:46
bool operator==(const BentleyAllocator< _Ty > &, const BentleyAllocator< _Other > &)
Definition: BentleyAllocator.h:146

Copyright © 2017 Bentley Systems, Incorporated. All rights reserved.