15
15
#include < memory>
16
16
#include < sys/mman.h>
17
17
using namespace OCLRT ;
18
- constexpr uintptr_t maxMmap32BitAddress = 0x80000000 ;
19
- constexpr uintptr_t lowerRangeStart = 0x10000000 ;
20
18
21
19
class Allocator32bit ::OsInternals {
22
20
public:
23
- uintptr_t upperRangeAddress = maxMmap32BitAddress;
24
- uintptr_t lowerRangeAddress = lowerRangeStart;
25
21
decltype (&mmap) mmapFunction = mmap;
26
22
decltype (&munmap) munmapFunction = munmap;
27
23
void *heapBasePtr = nullptr ;
28
24
size_t heapSize = 0 ;
29
-
30
- class Drm32BitAllocator {
31
- protected:
32
- Allocator32bit::OsInternals &outer;
33
-
34
- public:
35
- Drm32BitAllocator (Allocator32bit::OsInternals &outer) : outer(outer) {
36
- }
37
-
38
- void *allocate (size_t size) {
39
- auto ptr = outer.mmapFunction (nullptr , size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1 , 0 );
40
-
41
- // In case we failed, retry with address provided as a hint
42
- if (ptr == MAP_FAILED) {
43
- ptr = outer.mmapFunction ((void *)outer.upperRangeAddress , size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1 , 0 );
44
- if (((uintptr_t )ptr + alignUp (size, 4096 )) >= max32BitAddress || ptr == MAP_FAILED) {
45
- outer.munmapFunction (ptr, size);
46
-
47
- // Try to use lower range
48
- ptr = outer.mmapFunction ((void *)outer.lowerRangeAddress , size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1 , 0 );
49
- if ((uintptr_t )ptr >= max32BitAddress) {
50
- outer.munmapFunction (ptr, size);
51
- return nullptr ;
52
- }
53
-
54
- outer.lowerRangeAddress = (uintptr_t )ptr + alignUp (size, 4096 );
55
- return ptr;
56
- }
57
-
58
- outer.upperRangeAddress = (uintptr_t )ptr + alignUp (size, 4096 );
59
- }
60
- return ptr;
61
- }
62
-
63
- int free (void *ptr, uint64_t size) {
64
- auto alignedSize = alignUp (size, 4096 );
65
- auto offsetedPtr = (uintptr_t )ptrOffset (ptr, alignedSize);
66
-
67
- if (offsetedPtr == outer.upperRangeAddress ) {
68
- outer.upperRangeAddress -= alignedSize;
69
- } else if (offsetedPtr == outer.lowerRangeAddress ) {
70
- outer.lowerRangeAddress -= alignedSize;
71
- }
72
- return outer.munmapFunction (ptr, size);
73
- }
74
- };
75
- Drm32BitAllocator *drmAllocator = nullptr ;
76
25
};
77
26
78
27
bool OCLRT::is32BitOsAllocatorAvailable = true ;
@@ -85,65 +34,48 @@ OCLRT::Allocator32bit::Allocator32bit() : Allocator32bit(new OsInternals) {
85
34
}
86
35
87
36
OCLRT::Allocator32bit::Allocator32bit (Allocator32bit::OsInternals *osInternalsIn) : osInternals(osInternalsIn) {
37
+ size_t sizeToMap = getSizeToMap ();
38
+ void *ptr = this ->osInternals ->mmapFunction (nullptr , sizeToMap, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1 , 0 );
88
39
89
- if (DebugManager. flags . UseNewHeapAllocator . get () ) {
90
- size_t sizeToMap = getSizeToMap () ;
91
- void * ptr = this ->osInternals ->mmapFunction (nullptr , sizeToMap, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1 , 0 );
40
+ if (ptr == MAP_FAILED ) {
41
+ sizeToMap -= sizeToMap / 4 ;
42
+ ptr = this ->osInternals ->mmapFunction (nullptr , sizeToMap, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1 , 0 );
92
43
93
- if (ptr == MAP_FAILED) {
94
- sizeToMap -= sizeToMap / 4 ;
95
- ptr = this ->osInternals ->mmapFunction (nullptr , sizeToMap, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1 , 0 );
44
+ DebugManager.log (DebugManager.flags .PrintDebugMessages .get (), __FUNCTION__, " Allocator RETRY ptr == " , ptr);
96
45
97
- DebugManager.log (DebugManager.flags .PrintDebugMessages .get (), __FUNCTION__, " Allocator RETRY ptr == " , ptr);
98
-
99
- if (ptr == MAP_FAILED) {
100
- ptr = nullptr ;
101
- sizeToMap = 0 ;
102
- }
46
+ if (ptr == MAP_FAILED) {
47
+ ptr = nullptr ;
48
+ sizeToMap = 0 ;
103
49
}
50
+ }
104
51
105
- DebugManager.log (DebugManager.flags .PrintDebugMessages .get (), __FUNCTION__, " Allocator ptr == " , ptr);
52
+ DebugManager.log (DebugManager.flags .PrintDebugMessages .get (), __FUNCTION__, " Allocator ptr == " , ptr);
106
53
107
- osInternals->heapBasePtr = ptr;
108
- osInternals->heapSize = sizeToMap;
109
- base = reinterpret_cast <uint64_t >(ptr);
110
- size = sizeToMap;
54
+ osInternals->heapBasePtr = ptr;
55
+ osInternals->heapSize = sizeToMap;
56
+ base = reinterpret_cast <uint64_t >(ptr);
57
+ size = sizeToMap;
111
58
112
- heapAllocator = std::unique_ptr<HeapAllocator>(new HeapAllocator (base, sizeToMap));
113
- } else {
114
- this ->osInternals ->drmAllocator = new Allocator32bit::OsInternals::Drm32BitAllocator (*this ->osInternals );
115
- }
59
+ heapAllocator = std::unique_ptr<HeapAllocator>(new HeapAllocator (base, sizeToMap));
116
60
}
117
61
118
62
OCLRT::Allocator32bit::~Allocator32bit () {
119
63
if (this ->osInternals .get () != nullptr ) {
120
64
if (this ->osInternals ->heapBasePtr != nullptr )
121
65
this ->osInternals ->munmapFunction (this ->osInternals ->heapBasePtr , this ->osInternals ->heapSize );
122
-
123
- if (this ->osInternals ->drmAllocator != nullptr )
124
- delete this ->osInternals ->drmAllocator ;
125
66
}
126
67
}
127
68
128
69
uint64_t OCLRT::Allocator32bit::allocate (size_t &size) {
129
- uint64_t ptr = 0llu;
130
- if (DebugManager.flags .UseNewHeapAllocator .get ()) {
131
- ptr = this ->heapAllocator ->allocate (size);
132
- } else {
133
- ptr = reinterpret_cast <uint64_t >(this ->osInternals ->drmAllocator ->allocate (size));
134
- }
135
- return ptr;
70
+ return this ->heapAllocator ->allocate (size);
136
71
}
137
72
138
73
int Allocator32bit::free (uint64_t ptr, size_t size) {
139
- if (ptr == reinterpret_cast <uint64_t >(MAP_FAILED) || ptr == 0llu )
74
+ if (ptr == reinterpret_cast <uint64_t >(MAP_FAILED))
140
75
return 0 ;
141
76
142
- if (DebugManager.flags .UseNewHeapAllocator .get ()) {
143
- this ->heapAllocator ->free (ptr, size);
144
- } else {
145
- return this ->osInternals ->drmAllocator ->free (reinterpret_cast <void *>(ptr), size);
146
- }
77
+ this ->heapAllocator ->free (ptr, size);
78
+
147
79
return 0 ;
148
80
}
149
81
0 commit comments