diff options
author | Rr42 | 2018-06-05 17:26:16 +0530 |
---|---|---|
committer | Rr42 | 2018-06-05 17:26:16 +0530 |
commit | 7dbf07b87362bb883ce7d950371a6e5c1f355dd7 (patch) | |
tree | 40023b4e476f5d1de49b8bc52afeef0c4d14ee30 /ldmicro/lib/linuxUI/linuxLD.cpp | |
parent | 77fc5b2384f0d65d1cc47f7b2841f4649a90f4a4 (diff) | |
download | LDMicroGtk-7dbf07b87362bb883ce7d950371a6e5c1f355dd7.tar.gz LDMicroGtk-7dbf07b87362bb883ce7d950371a6e5c1f355dd7.tar.bz2 LDMicroGtk-7dbf07b87362bb883ce7d950371a6e5c1f355dd7.zip |
Added heap meamory handling (HeapCreate, HeapAlloc and HeepFree)
Diffstat (limited to 'ldmicro/lib/linuxUI/linuxLD.cpp')
-rw-r--r-- | ldmicro/lib/linuxUI/linuxLD.cpp | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/ldmicro/lib/linuxUI/linuxLD.cpp b/ldmicro/lib/linuxUI/linuxLD.cpp index 69c5f6e..487905c 100644 --- a/ldmicro/lib/linuxUI/linuxLD.cpp +++ b/ldmicro/lib/linuxUI/linuxLD.cpp @@ -1,2 +1,83 @@ #include "linuxUI.h" +std::vector<HEAPRECORD> HeapRecords; + +HANDLE HeapCreate(DWORD flOptions, SIZE_T dwInitialSize, SIZE_T dwMaximumSize) +{ + HANDLE hHeap = NULL; + HEAPRECORD hHeapRecord; + hHeapRecord.dwMaximumSize = dwMaximumSize; + hHeap = malloc(dwInitialSize); + + if (hHeap == NULL) + return NULL; + + hHeapRecord.dwSize = dwInitialSize; + hHeapRecord.hHeap = hHeap; + hHeapRecord.dwAllocatedSizeOffset = 0; + hHeapRecord.HeapID = hHeapRecords.size()+1; + HeapRecords.push_back(hHeapRecord); + + return hHeap; +} + +LPVOID HeapAlloc(HANDLE hHeap, DWORD dwFlags, SIZE_T dwBytes) +{ + auto it = std::find_if(HeapRecords.begin(), HeapRecords.end(), [&hHeap](HEAPRECORD &Record) { return Record.hHeap == hHeap; }); + + if (it == HeapRecords.end()) + return NULL; + + if ((*it).dwAllocatedSizeOffset + dwBytes > (*it).dwSize) + { + if ((*it).dwMaximumSize != 0) + if((*it).dwAllocatedSizeOffset + dwBytes > (*it).dwMaximumSize) + return NULL; + + (*it).hHeap = realloc((*it).hHeap, (*it).dwAllocatedSizeOffset + dwBytes); + hHeap = (*it).hHeap; + } + + /// HEAP_ZERO_MEMORY is set by default + DWORD flags = MAP_ANONYMOUS; + if ( (dwFlags & HEAP_ZERO_MEMORY) == HEAP_ZERO_MEMORY) + flags = MAP_ANONYMOUS | MAP_UNINITIALIZED; + //void * memset ( void * ptr, int value, size_t num ); + LPVOID p = mmap(hHeap + (*it).dwAllocatedSizeOffset, dwBytes, PROT_EXEC, flags, -1, 0); + + if (p == NULL) + return NULL; + + (*it).dwAllocatedSizeOffset += dwBytes; + HEAPCHUNCK chunck; + chunck.Chunck = p; + chunck.dwSize = dwBytes; + (*it).Element.push_back(chunck); + + return p; +} + +BOOL HeapFree(HANDLE hHeap, DWORD dwFlags, LPVOID lpMem) +{ + auto heap_it = std::find_if(HeapRecords.begin(), HeapRecords.end(), [&hHeap](HEAPRECORD &Record) { return Record.hHeap == hHeap; }); + + if (heap_it == HeapRecords.end()) + return NULL; + + auto chunck_it = std::find_if((*heap_it).Element.begin(), (*heap_it).Element.end(), [&lpMem](HEAPCHUNCK &Chunck) { return Chunck.Chunck == lpMem; }); + + if (chunck_it == (*heap_it).Element.end()) + return NULL; + + int result = munmap((*chunck_it).Chunck, (*chunck_it).dwSize); + + if (result == 0) + { + (*heap_it).Element.erase(chunck_it); + return TRUE; + } + else + return FALSE; + +} + |