h1-basic-allocator #5

Merged
Mineplay merged 8 commits from h1-basic-allocator into main 2025-04-12 16:50:08 -05:00
3 changed files with 78 additions and 1 deletions
Showing only changes of commit c48200cd52 - Show all commits

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;
}