From 9a16546e07ee72e71fecfcf0db0ff45ca8fba136 Mon Sep 17 00:00:00 2001 From: servostar Date: Sat, 25 May 2024 16:38:26 +0200 Subject: [PATCH] added memory module and refactored filenames in stdlib --- lib/CMakeLists.txt | 2 ++ lib/def/{def.h => api.h} | 0 lib/def/mod.gem | 34 --------------------------- lib/io/{io.h => api.h} | 2 +- lib/io/{io.c => impl.c} | 3 +-- lib/mem.gem | 27 +++++++++++++++++++++ lib/mem/api.h | 17 ++++++++++++++ lib/mem/impl.c | 51 ++++++++++++++++++++++++++++++++++++++++ lib/std.gem | 4 ++++ 9 files changed, 103 insertions(+), 37 deletions(-) rename lib/def/{def.h => api.h} (100%) delete mode 100644 lib/def/mod.gem rename lib/io/{io.h => api.h} (94%) rename lib/io/{io.c => impl.c} (97%) create mode 100644 lib/mem.gem create mode 100644 lib/mem/api.h create mode 100644 lib/mem/impl.c diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index e5aa8cf..ac34f1f 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -18,3 +18,5 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}/../bin/std") file(GLOB_RECURSE STDLIB_IO_SOURCE_FILES io/*.c) add_library(io ${STDLIB_IO_SOURCE_FILES}) +file(GLOB_RECURSE STDLIB_MEM_SOURCE_FILES mem/*.c) +add_library(mem ${STDLIB_MEM_SOURCE_FILES}) diff --git a/lib/def/def.h b/lib/def/api.h similarity index 100% rename from lib/def/def.h rename to lib/def/api.h diff --git a/lib/def/mod.gem b/lib/def/mod.gem deleted file mode 100644 index 22bfd7f..0000000 --- a/lib/def/mod.gem +++ /dev/null @@ -1,34 +0,0 @@ -# Author: Sven Vogel -# Edited: 25.05.2024 -# License: GPL-2.0 - -# ,----------------------------------------. -# | Standard Type definitions | -# `----------------------------------------` - -# Unsigned integrals - -type unsgined half half int: u8 -type unsgined half int: u16 -type unsgined int: u32 -type unsgined double int: u64 -type unsgined double double int: u128 - -# Signed integrals - -type signed u8: i8 -type signed u16: i16 -type signed u32: i32 -type signed u64: i64 -type signed u128: i128 - -# IEEE-754 floating point - -type signed half float: f16 -type signed float: f32 -type signed double float: f64 -type signed double double float: f128 - -# String constant - -type ref u8: str diff --git a/lib/io/io.h b/lib/io/api.h similarity index 94% rename from lib/io/io.h rename to lib/io/api.h index 4e00856..c642185 100644 --- a/lib/io/io.h +++ b/lib/io/api.h @@ -2,7 +2,7 @@ #ifndef GEMSTONE_STD_LIB_IO_H_ #define GEMSTONE_STD_LIB_IO_H_ -#include +#include typedef ptr handle; diff --git a/lib/io/io.c b/lib/io/impl.c similarity index 97% rename from lib/io/io.c rename to lib/io/impl.c index f47dd6f..19fe716 100644 --- a/lib/io/io.c +++ b/lib/io/impl.c @@ -1,6 +1,5 @@ -#include -#include +#include #if defined(_WIN32) || defined (_WIN64) diff --git a/lib/mem.gem b/lib/mem.gem new file mode 100644 index 0000000..faecbc4 --- /dev/null +++ b/lib/mem.gem @@ -0,0 +1,27 @@ +# Author: Sven Vogel +# Edited: 25.05.2024 +# License: GPL-2.0 + +# ,----------------------------------------. +# | Memory Management | +# `----------------------------------------` + +import "def.gem" + +# Allocate `len` bytes of heap memory +# Returns a pointer to the memory as `ptr` +fun heap_alloc(in u32: len)(out ref u8: ptr) + +# Rellocate `len` bytes of heap memory +# Returns a pointer to the memory as `ptr` +fun heap_realloc(in u32: len, in out ref u8: ptr) + +# Free a block of memory +fun heap_free(in ref u8: ptr) + +# Copy `len` bytes from `dst` into `src` +fun copy(in ref u8: dst, in ref u8: src, in u32 len) + +# Fill `len` bytes of `dst` with `byte` +fun fill(in ref u8: dst, in u8: byte, in u32 len) + diff --git a/lib/mem/api.h b/lib/mem/api.h new file mode 100644 index 0000000..8ca69c0 --- /dev/null +++ b/lib/mem/api.h @@ -0,0 +1,17 @@ + +#ifndef GEMSTONE_STD_LIB_MEM_H_ +#define GEMSTONE_STD_LIB_MEM_H_ + +#include + +void heap_alloc(u32 len, u8** ptr); + +void heap_realloc(u32 len, u8** ptr); + +void heap_free(u8* ptr); + +void copy(u8* dst, u8* src, u32 len); + +void fill(u8* dst, u8 byte, u32 len); + +#endif // GEMSTONE_STD_LIB_MEM_H_ diff --git a/lib/mem/impl.c b/lib/mem/impl.c new file mode 100644 index 0000000..95863ac --- /dev/null +++ b/lib/mem/impl.c @@ -0,0 +1,51 @@ + +#include + +#if defined(_WIN32) || defined (_WIN64) + +#include + +#define HEAP_API_GLOBAL_FLAGS HEAP_ZERO_MEMORY | HEAP_GENERATE_EXCEPTIONS + +void heap_alloc(u32 len, u8** ptr) { + HANDLE heap = GetProcessHeap(); + *ptr = HeapAlloc(heap, HEAP_API_GLOBAL_FLAGS, len); +} + +void heap_realloc(u32 len, u8** ptr) { + HANDLE heap = GetProcessHeap(); + *ptr = HeapReAlloc(heap, HEAP_API_GLOBAL_FLAGS, *ptr, len); +} + +void heap_free(u8* ptr) { + HANDLE heap = GetProcessHeap(); + HeapFree(heap, ptr); +} + +#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__linux__) + +#include + +void heap_alloc(u32 len, u8** ptr) { + *ptr = malloc(len); +} + +void heap_realloc(u32 len, u8** ptr) { + *ptr = realloc(*ptr, len); +} + +void heap_free(u8* ptr) { + free(ptr); +} + +#endif + +#include + +void copy(u8* dst, u8* src, u32 len) { + memcpy(dst, src, len); +} + +void fill(u8* dst, u8 byte, u32 len) { + memset(dst, byte, len); +} \ No newline at end of file diff --git a/lib/std.gem b/lib/std.gem index 8f50d04..e135a13 100644 --- a/lib/std.gem +++ b/lib/std.gem @@ -9,4 +9,8 @@ # standard type definitions import "def.gem" +# I/O operations +import "io.gem" +# memory management +import "mem.gem"