From 7dbf07b87362bb883ce7d950371a6e5c1f355dd7 Mon Sep 17 00:00:00 2001 From: Rr42 Date: Tue, 5 Jun 2018 17:26:16 +0530 Subject: Added heap meamory handling (HeapCreate, HeapAlloc and HeepFree) --- ldmicro/CMakeLists.txt | 2 +- ldmicro/lib/linuxUI/linuxLD.cpp | 81 +++++++++++++++++++++++++++++++++++++++++ ldmicro/lib/linuxUI/linuxLD.h | 48 +++++++++++++++++++++--- ldmicro/miscutil.cpp | 4 +- 4 files changed, 127 insertions(+), 8 deletions(-) diff --git a/ldmicro/CMakeLists.txt b/ldmicro/CMakeLists.txt index 0868d83..9af4450 100644 --- a/ldmicro/CMakeLists.txt +++ b/ldmicro/CMakeLists.txt @@ -93,7 +93,7 @@ IF(UNIX) maincontrols.cpp ldmicro.cpp) - add_executable (LDMicro ${COMPILE_CPP_SOURCES})#windows.cpp arduino.cpp avr.cpp pic16.cpp interpreted.cpp ansic.cpp lang.cpp miscutil.cpp circuit.cpp draw_outputdev.cpp ldmicro.cpp + add_executable (LDMicro ${COMPILE_CPP_SOURCES}) install (TARGETS LDMicro DESTINATION bin) #add_executable (testMain testMain.cpp) target_link_libraries (LDMicro LinuxUI) 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 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; + +} + diff --git a/ldmicro/lib/linuxUI/linuxLD.h b/ldmicro/lib/linuxUI/linuxLD.h index 7878733..525f6a8 100644 --- a/ldmicro/lib/linuxUI/linuxLD.h +++ b/ldmicro/lib/linuxUI/linuxLD.h @@ -2,6 +2,8 @@ #define __LINUX_LD__ #include "linuxUI.h" +#include +#include /// common windows referances for linux @@ -10,12 +12,16 @@ /// CALLBACK or __stdcall os defined empty #define CALLBACK #define CONST const + +#define HEAP_ZERO_MEMORY 0x00000008 + /// typedefs //typedef int64_t __int64; typedef bool BOOL; typedef unsigned char BYTE; typedef unsigned short WORD; typedef unsigned int DWORD; +typedef size_t SIZE_T; typedef long LONG; typedef wchar_t WCHAR; typedef char CHAR; @@ -38,15 +44,16 @@ typedef CHAR *LPSTR; #endif /// all handles will hold a GtkWindow* type -typedef void* PVOID; +typedef void *PVOID; +typedef void *LPVOID; typedef PVOID HANDLE; typedef HANDLE HINSTANCE; typedef HANDLE HDC; -typedef GtkWidget* HWID; -typedef GtkWidget* HMENU; -typedef GtkWindow* HWND; -typedef GtkApplication* HAPP; +typedef GtkWidget *HWID; +typedef GtkWidget *HMENU; +typedef GtkWindow *HWND; +typedef GtkApplication *HAPP; /// Check if system is x64 or x86 #if defined(__UNIX64) @@ -77,5 +84,36 @@ class COLORREF : public GdkRGBA{ } }; +/// Custom structures +typedef struct HeapRecordChunckTag{ + PVOID Chunck; + SIZE_T dwSize; +} HEAPCHUNCK; +typedef struct HeapRecordTag{ + PVOID hHeap; + DWORD HeapID; + std::vector Element; + SIZE_T dwMaximumSize; + SIZE_T dwSize; + SIZE_T dwAllocatedSizeOffset; +} HEAPRECORD; +/// Variables +extern std::vector HeapRecord; + /// functions +HANDLE HeapCreate( + DWORD flOptions, + SIZE_T dwInitialSize, + SIZE_T dwMaximumSize); + +LPVOID HeapAlloc( + HANDLE hHeap, + DWORD dwFlags, + SIZE_T dwBytes); + +BOOL HeapFree( + HANDLE hHeap, + DWORD dwFlags, + LPVOID lpMem); + #endif diff --git a/ldmicro/miscutil.cpp b/ldmicro/miscutil.cpp index f84c3ca..08a4e78 100644 --- a/ldmicro/miscutil.cpp +++ b/ldmicro/miscutil.cpp @@ -159,13 +159,13 @@ void CheckHeap(char *file, int line) void *CheckMalloc(size_t n) { ok(); - // void *p = HeapAlloc(MainHeap, HEAP_ZERO_MEMORY, n); + void *p = HeapAlloc(MainHeap, HEAP_ZERO_MEMORY, n); return NULL; } void CheckFree(void *p) { ok(); - //HeapFree(MainHeap, 0, p); + HeapFree(MainHeap, 0, p); } -- cgit