diff --git a/Include/Hallocy/Allocator.h b/Include/Hallocy/Allocator.h index 80d6249..a8f0805 100644 --- a/Include/Hallocy/Allocator.h +++ b/Include/Hallocy/Allocator.h @@ -24,7 +24,11 @@ #define HALLOCY_ALLOCATOR #include +#include + +#include "Error.h" void *hallocy_malloc(size_t size); +HallocyError hallocy_free(void *pointer); #endif \ No newline at end of file diff --git a/Include/Hallocy/Error.h b/Include/Hallocy/Error.h new file mode 100644 index 0000000..3e65784 --- /dev/null +++ b/Include/Hallocy/Error.h @@ -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 \ No newline at end of file diff --git a/Src/Hallocy/Allocator.c b/Src/Hallocy/Allocator.c index 1f96cb9..79151e5 100644 --- a/Src/Hallocy/Allocator.c +++ b/Src/Hallocy/Allocator.c @@ -27,6 +27,7 @@ #elif defined(__linux__) #include #include +#include #endif typedef struct HallocyMemoryHeader { @@ -64,4 +65,53 @@ void *hallocy_malloc(size_t size) { memory_pointer->next = NULL; 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; } \ No newline at end of file diff --git a/Src/Main.c b/Src/Main.c index e78593e..3b99def 100644 --- a/Src/Main.c +++ b/Src/Main.c @@ -3,10 +3,21 @@ int main() { char *memory = (char *)hallocy_malloc(3); + if (memory == NULL) { + printf("Failed it allocate memory!"); + return -1; + } + memory[0] = 'H'; memory[1] = 'i'; memory[2] = '\0'; - + 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; } \ No newline at end of file