1515#include < memory>
1616#include < sys/mman.h>
1717using namespace OCLRT ;
18- constexpr uintptr_t maxMmap32BitAddress = 0x80000000 ;
19- constexpr uintptr_t lowerRangeStart = 0x10000000 ;
2018
2119class Allocator32bit ::OsInternals {
2220 public:
23- uintptr_t upperRangeAddress = maxMmap32BitAddress;
24- uintptr_t lowerRangeAddress = lowerRangeStart;
2521 decltype (&mmap) mmapFunction = mmap;
2622 decltype (&munmap) munmapFunction = munmap;
2723 void *heapBasePtr = nullptr ;
2824 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 ;
7625};
7726
7827bool OCLRT::is32BitOsAllocatorAvailable = true ;
@@ -85,65 +34,48 @@ OCLRT::Allocator32bit::Allocator32bit() : Allocator32bit(new OsInternals) {
8534}
8635
8736OCLRT::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 );
8839
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 );
9243
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);
9645
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 ;
10349 }
50+ }
10451
105- DebugManager.log (DebugManager.flags .PrintDebugMessages .get (), __FUNCTION__, " Allocator ptr == " , ptr);
52+ DebugManager.log (DebugManager.flags .PrintDebugMessages .get (), __FUNCTION__, " Allocator ptr == " , ptr);
10653
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;
11158
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));
11660}
11761
11862OCLRT::Allocator32bit::~Allocator32bit () {
11963 if (this ->osInternals .get () != nullptr ) {
12064 if (this ->osInternals ->heapBasePtr != nullptr )
12165 this ->osInternals ->munmapFunction (this ->osInternals ->heapBasePtr , this ->osInternals ->heapSize );
122-
123- if (this ->osInternals ->drmAllocator != nullptr )
124- delete this ->osInternals ->drmAllocator ;
12566 }
12667}
12768
12869uint64_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);
13671}
13772
13873int 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))
14075 return 0 ;
14176
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+
14779 return 0 ;
14880}
14981
0 commit comments