added memory module and refactored filenames in stdlib

This commit is contained in:
Sven Vogel 2024-05-25 16:38:26 +02:00
parent c3667474d7
commit 9a16546e07
9 changed files with 103 additions and 37 deletions

View File

@ -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})

View File

@ -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

View File

@ -2,7 +2,7 @@
#ifndef GEMSTONE_STD_LIB_IO_H_
#define GEMSTONE_STD_LIB_IO_H_
#include <def/def.h>
#include <def/api.h>
typedef ptr handle;

View File

@ -1,6 +1,5 @@
#include <io/io.h>
#include <stdio.h>
#include <io/api.h>
#if defined(_WIN32) || defined (_WIN64)

27
lib/mem.gem Normal file
View File

@ -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)

17
lib/mem/api.h Normal file
View File

@ -0,0 +1,17 @@
#ifndef GEMSTONE_STD_LIB_MEM_H_
#define GEMSTONE_STD_LIB_MEM_H_
#include <def/api.h>
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_

51
lib/mem/impl.c Normal file
View File

@ -0,0 +1,51 @@
#include <mem/api.h>
#if defined(_WIN32) || defined (_WIN64)
#include <Windows.h>
#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 <malloc.h>
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 <string.h>
void copy(u8* dst, u8* src, u32 len) {
memcpy(dst, src, len);
}
void fill(u8* dst, u8 byte, u32 len) {
memset(dst, byte, len);
}

View File

@ -9,4 +9,8 @@
# standard type definitions
import "def.gem"
# I/O operations
import "io.gem"
# memory management
import "mem.gem"