feat(allocator): implemented basic malloc functionality

This commit is contained in:
Mineplay 2025-04-11 16:03:11 -05:00
parent 58fe77860a
commit c48200cd52
3 changed files with 78 additions and 1 deletions

View file

@ -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 <stddef.h>
void *hallocy_malloc(size_t size);
#endif

41
Src/Hallocy/Allocator.c Normal file
View file

@ -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 <windows.h>
#elif defined(__linux__)
#include <unistd.h>
#include <sys/mman.h>
#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;
}

View file

@ -1,6 +1,12 @@
#include <stdio.h>
#include <Hallocy/Allocator.h>
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;
}