-
Notifications
You must be signed in to change notification settings - Fork 0
/
d_memory.c
65 lines (53 loc) · 1.65 KB
/
d_memory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "d_memory.h"
#include "d_types.h"
#include <malloc.h>
#include <stdio.h>
#include "d_logger.h"
static boolean g_exit_on_malloc_failure = TRUE;
void* d_malloc(size_t size){
void* mem = malloc(size);
if ( mem == NULL){
printf("DLib:CRITICAL: d_malloc() failed !!!\n");
if ( g_exit_on_malloc_failure )
exit(EXIT_FAILURE);
return NULL;
}
return mem;
}
void d_set_malloc_exit_on_failure(boolean b){
g_exit_on_malloc_failure = b;
}
static void* memory_pool= 0;
static void* memory_pool_next_available_memory = 0;
static int memory_pool_remaining_memory = 0;
#define POOL_SIZE 8192000
void* d_mpool_alloc(size_t size){
if ( memory_pool == 0){
memory_pool = d_malloc(POOL_SIZE);
memory_pool_next_available_memory = memory_pool;
memory_pool_remaining_memory = POOL_SIZE;
}
if ( size > memory_pool_remaining_memory){
printf("Memory pool out of memory");
exit(EXIT_FAILURE);
}
void* allocate = memory_pool_next_available_memory;
memory_pool_remaining_memory -= size;
memory_pool_next_available_memory += size;
return allocate;
}
void d_mpool_release(){
memory_pool_next_available_memory = memory_pool;
memory_pool_remaining_memory = POOL_SIZE;
}
void d_mpool_free(){
free(memory_pool);
}
void d_memory_print_memusage(){
#if __WIN32__ == 1
struct mallinfo info = mallinfo();
DLOGI("used memory %.2f Mo, %.2f Mo with mmap\n",info.arena / 1000000.0f,info.hblkhd / 1000000.0f);
#else
DLOGV("function mallinfo() not available on this platform");
#endif
}