feat(allocator): implemented basic free function to free allocated memory

This commit is contained in:
Mineplay 2025-04-11 18:21:20 -05:00
parent 3aabf00607
commit dd10dfd376
4 changed files with 100 additions and 1 deletions

View file

@ -24,7 +24,11 @@
#define HALLOCY_ALLOCATOR #define HALLOCY_ALLOCATOR
#include <stddef.h> #include <stddef.h>
#include <stdbool.h>
#include "Error.h"
void *hallocy_malloc(size_t size); void *hallocy_malloc(size_t size);
HallocyError hallocy_free(void *pointer);
#endif #endif

34
Include/Hallocy/Error.h Normal file
View file

@ -0,0 +1,34 @@
/*
* 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: Error.h
* Description:
* This file contains the ErrorCode enum that defines the error codes for the
* library.
*
* Author: Mineplay
* -----------------------------------------------------------------------------
*/
#ifndef HALLOCY_ERROR
#define HALLOCY_ERROR
typedef enum {
HALLOCY_ERROR_NONE = 0,
HALLOCY_ERROR_INVALID_POINTER = 1,
HALLOCY_ERROR_OUT_OF_MEMORY = 2,
HALLOCY_ERROR_UNKNOWN = 3,
HALLOCY_ERROR_INVALID_PARAM = 4
} HallocyError;
#endif

View file

@ -27,6 +27,7 @@
#elif defined(__linux__) #elif defined(__linux__)
#include <unistd.h> #include <unistd.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <errno.h>
#endif #endif
typedef struct HallocyMemoryHeader { typedef struct HallocyMemoryHeader {
@ -65,3 +66,52 @@ void *hallocy_malloc(size_t size) {
return (void*)(memory_pointer + 1); return (void*)(memory_pointer + 1);
} }
HallocyError hallocy_free(void *pointer) {
if (pointer == NULL) {
return HALLOCY_ERROR_INVALID_POINTER;
}
HallocyMemoryHeader *memory_header = ((HallocyMemoryHeader*)pointer) - 1;
#if defined(_WIN32)
bool result = VirtualFree(memory_header, 0, MEM_RELEASE);
if (result == false) {
DWORD error = GetLastError();
switch (error) {
case ERROR_INVALID_PARAMETER:
return HALLOCY_ERROR_INVALID_PARAM;
case ERROR_NOT_ENOUGH_MEMORY:
return HALLOCY_ERROR_OUT_OF_MEMORY;
case ERROR_INVALID_HANDLE:
return HALLOCY_ERROR_INVALID_POINTER;
case ERROR_INVALID_ADDRESS:
return HALLOCY_ERROR_INVALID_POINTER;
default:
return HALLOCY_ERROR_UNKNOWN;
}
}
#elif defined(__linux__)
int result = munmap(memory_header, memory_header->size);
if (result == -1) {
switch (errno) {
case EINVAL:
return HALLOCY_ERROR_INVALID_PARAM;
case ENOMEM:
return HALLOCY_ERROR_OUT_OF_MEMORY;
case EFAULT:
return HALLOCY_ERROR_INVALID_POINTER;
default:
return HALLOCY_ERROR_UNKNOWN;
}
}
#endif
return HALLOCY_ERROR_NONE;
}

View file

@ -3,10 +3,21 @@
int main() { int main() {
char *memory = (char *)hallocy_malloc(3); char *memory = (char *)hallocy_malloc(3);
if (memory == NULL) {
printf("Failed it allocate memory!");
return -1;
}
memory[0] = 'H'; memory[0] = 'H';
memory[1] = 'i'; memory[1] = 'i';
memory[2] = '\0'; memory[2] = '\0';
printf("%s\n", memory); printf("%s\n", memory);
int code = hallocy_free(memory);
if (code != HALLOCY_ERROR_NONE) {
printf("Failed to free memory error code (%d)!", code);
return -1;
}
return 0; return 0;
} }