From c48200cd526704bffbc6888bcf86bb81b2df669d Mon Sep 17 00:00:00 2001 From: Mineplay Date: Fri, 11 Apr 2025 16:03:11 -0500 Subject: [PATCH] feat(allocator): implemented basic malloc functionality --- Include/Hallocy/Allocator.h | 30 +++++++++++++++++++++++++++ Src/Hallocy/Allocator.c | 41 +++++++++++++++++++++++++++++++++++++ Src/Main.c | 8 +++++++- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 Include/Hallocy/Allocator.h create mode 100644 Src/Hallocy/Allocator.c diff --git a/Include/Hallocy/Allocator.h b/Include/Hallocy/Allocator.h new file mode 100644 index 0000000..80d6249 --- /dev/null +++ b/Include/Hallocy/Allocator.h @@ -0,0 +1,30 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: Allocator.h + * Description: + * This file contains the functions for allocating, freeing and managing memory. + * It includes functions to allocate, reallocate, free, copy, move and set memory. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#ifndef HALLOCY_ALLOCATOR +#define HALLOCY_ALLOCATOR + +#include + +void *hallocy_malloc(size_t size); + +#endif \ No newline at end of file diff --git a/Src/Hallocy/Allocator.c b/Src/Hallocy/Allocator.c new file mode 100644 index 0000000..17bbc6a --- /dev/null +++ b/Src/Hallocy/Allocator.c @@ -0,0 +1,41 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ----------------------------------------------------------------------------- + * File: Allocator.h + * Description: + * This file implements the functions for allocating, freeing and managing memory. + * It includes functions to allocate, reallocate, free, copy, move and set memory. + * + * Author: Mineplay + * ----------------------------------------------------------------------------- + */ +#include "../../Include/Hallocy/Allocator.h" + +#if defined(_WIN32) +#include +#elif defined(__linux__) +#include +#include +#endif + +void *hallocy_malloc(size_t size) { + void *memory_pointer = NULL; + #if defined(_WIN32) + memory_pointer = VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); + #elif defined(__linux__) + memory_pointer = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + #endif + + return memory_pointer; +} \ No newline at end of file diff --git a/Src/Main.c b/Src/Main.c index 8321e25..e78593e 100644 --- a/Src/Main.c +++ b/Src/Main.c @@ -1,6 +1,12 @@ #include +#include int main() { - printf("Hello, World!\n"); + char *memory = (char *)hallocy_malloc(3); + memory[0] = 'H'; + memory[1] = 'i'; + memory[2] = '\0'; + + printf("%s\n", memory); return 0; } \ No newline at end of file