Merge pull request 'h4-advanced-allocators' (#9) from h4-advanced-allocators into main
Reviewed-on: #9
This commit is contained in:
commit
693fa06e81
3 changed files with 57 additions and 6 deletions
|
|
@ -24,10 +24,15 @@
|
||||||
#define HALLOCY_ALLOCATOR
|
#define HALLOCY_ALLOCATOR
|
||||||
|
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "../Utils/Error.h"
|
#include "../Utils/Error.h"
|
||||||
|
|
||||||
void *hallocy_malloc(size_t size);
|
void *hallocy_allocate(size_t size, bool zero_memory);
|
||||||
|
|
||||||
|
static inline void *hallocy_malloc(size_t size) { return hallocy_allocate(size, false); }
|
||||||
|
static inline void *hallocy_calloc(size_t size, size_t count) { return hallocy_allocate(size * count, true); }
|
||||||
|
void *hallocy_realloc(void *memory_pointer, size_t size);
|
||||||
HallocyError hallocy_free(void *pointer);
|
HallocyError hallocy_free(void *pointer);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
@ -21,8 +21,7 @@
|
||||||
* -----------------------------------------------------------------------------
|
* -----------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
#include "../../Include/Hallocy/Core/Allocator.h"
|
#include "../../Include/Hallocy/Core/Allocator.h"
|
||||||
|
#include "../../Include/Hallocy/Core/Memory.h"
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
@ -70,7 +69,7 @@ static BOOL CALLBACK hallocy_initialize_mutex(PINIT_ONCE init_once, PVOID parame
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void *hallocy_malloc(size_t size) {
|
void *hallocy_allocate(size_t size, bool zero_memory) {
|
||||||
if (page_size == 0) {
|
if (page_size == 0) {
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
SYSTEM_INFO system_info;
|
SYSTEM_INFO system_info;
|
||||||
|
|
@ -102,6 +101,10 @@ void *hallocy_malloc(size_t size) {
|
||||||
}
|
}
|
||||||
|
|
||||||
memory_pointer->next = NULL;
|
memory_pointer->next = NULL;
|
||||||
|
if (zero_memory) {
|
||||||
|
hallocy_set_memory(memory_pointer + 1, 0, aligned_size - sizeof(HallocyMemoryHeader));
|
||||||
|
}
|
||||||
|
|
||||||
return (void*)(memory_pointer + 1);
|
return (void*)(memory_pointer + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -114,7 +117,12 @@ void *hallocy_malloc(size_t size) {
|
||||||
hallocy_heap = GetProcessHeap();
|
hallocy_heap = GetProcessHeap();
|
||||||
}
|
}
|
||||||
|
|
||||||
memory_pointer = HeapAlloc(hallocy_heap, 0, aligned_size);
|
if (zero_memory) {
|
||||||
|
memory_pointer = HeapAlloc(hallocy_heap, HEAP_ZERO_MEMORY, aligned_size);
|
||||||
|
} else {
|
||||||
|
memory_pointer = HeapAlloc(hallocy_heap, 0, aligned_size);
|
||||||
|
}
|
||||||
|
|
||||||
if (memory_pointer == NULL) {
|
if (memory_pointer == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -162,6 +170,10 @@ void *hallocy_malloc(size_t size) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
memory_pointer->next = NULL;
|
memory_pointer->next = NULL;
|
||||||
|
if (zero_memory) {
|
||||||
|
hallocy_set_memory(memory_pointer + 1, 0, aligned_size - sizeof(HallocyMemoryHeader));
|
||||||
|
}
|
||||||
|
|
||||||
return (void*)(memory_pointer + 1);
|
return (void*)(memory_pointer + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -174,7 +186,12 @@ void *hallocy_malloc(size_t size) {
|
||||||
hallocy_heap = GetProcessHeap();
|
hallocy_heap = GetProcessHeap();
|
||||||
}
|
}
|
||||||
|
|
||||||
memory_pointer = HeapAlloc(hallocy_heap, 0, aligned_size);
|
if (zero_memory) {
|
||||||
|
memory_pointer = HeapAlloc(hallocy_heap, HEAP_ZERO_MEMORY, aligned_size);
|
||||||
|
} else {
|
||||||
|
memory_pointer = HeapAlloc(hallocy_heap, 0, aligned_size);
|
||||||
|
}
|
||||||
|
|
||||||
if (memory_pointer == NULL) {
|
if (memory_pointer == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -212,6 +229,28 @@ void *hallocy_malloc(size_t size) {
|
||||||
return (void*)(memory_pointer + 1);
|
return (void*)(memory_pointer + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void *hallocy_realloc(void *memory_pointer, size_t size) {
|
||||||
|
if (memory_pointer == NULL) {
|
||||||
|
return hallocy_allocate(size, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (size == 0) {
|
||||||
|
hallocy_free(memory_pointer);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
HallocyMemoryHeader *memory_header = ((HallocyMemoryHeader*)memory_pointer) - 1;
|
||||||
|
if (memory_header->size - sizeof(HallocyMemoryHeader) >= size) {
|
||||||
|
return memory_pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *new_memory = hallocy_allocate(size, false);
|
||||||
|
hallocy_copy_memory(new_memory, memory_pointer, memory_header->size);
|
||||||
|
hallocy_free(memory_pointer);
|
||||||
|
|
||||||
|
return new_memory;
|
||||||
|
}
|
||||||
|
|
||||||
HallocyError hallocy_free(void *pointer) {
|
HallocyError hallocy_free(void *pointer) {
|
||||||
if (pointer == NULL) {
|
if (pointer == NULL) {
|
||||||
return HALLOCY_ERROR_INVALID_POINTER;
|
return HALLOCY_ERROR_INVALID_POINTER;
|
||||||
|
|
|
||||||
|
|
@ -70,6 +70,13 @@
|
||||||
printf("text_copy_copy and text_copy are equal\n");
|
printf("text_copy_copy and text_copy are equal\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
text_copy_copy = (char*)hallocy_realloc(text_copy_copy, 40);
|
||||||
|
|
||||||
|
hallocy_set_memory(text_copy_copy + 35, 'q', 4);
|
||||||
|
text_copy_copy[39] = '\0';
|
||||||
|
|
||||||
|
printf("%s\n", text_copy_copy);
|
||||||
|
|
||||||
hallocy_free(text_copy_copy);
|
hallocy_free(text_copy_copy);
|
||||||
hallocy_free(text_copy);
|
hallocy_free(text_copy);
|
||||||
hallocy_free(text);
|
hallocy_free(text);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue