summaryrefslogtreecommitdiff
path: root/ldmicro/lib
diff options
context:
space:
mode:
authorRr422018-06-05 17:26:16 +0530
committerRr422018-06-05 17:26:16 +0530
commit7dbf07b87362bb883ce7d950371a6e5c1f355dd7 (patch)
tree40023b4e476f5d1de49b8bc52afeef0c4d14ee30 /ldmicro/lib
parent77fc5b2384f0d65d1cc47f7b2841f4649a90f4a4 (diff)
downloadLDMicroGtk-7dbf07b87362bb883ce7d950371a6e5c1f355dd7.tar.gz
LDMicroGtk-7dbf07b87362bb883ce7d950371a6e5c1f355dd7.tar.bz2
LDMicroGtk-7dbf07b87362bb883ce7d950371a6e5c1f355dd7.zip
Added heap meamory handling (HeapCreate, HeapAlloc and HeepFree)
Diffstat (limited to 'ldmicro/lib')
-rw-r--r--ldmicro/lib/linuxUI/linuxLD.cpp81
-rw-r--r--ldmicro/lib/linuxUI/linuxLD.h48
2 files changed, 124 insertions, 5 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;
+
+}
+
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 <vector>
+#include <algorithm>
/// 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<HEAPCHUNCK> Element;
+ SIZE_T dwMaximumSize;
+ SIZE_T dwSize;
+ SIZE_T dwAllocatedSizeOffset;
+} HEAPRECORD;
+/// Variables
+extern std::vector<HEAPRECORD> 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