Lolly 1.4.28
Loading...
Searching...
No Matches
mi_malloc.cpp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : Fast memory allocation using mimalloc
4 * DESCRIPTION:
5 * COPYRIGHT : (C) 2023-2024 jingkaimori
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12#include "assert.h"
13#include "basic.hpp"
14#include "fast_alloc.hpp"
15#include "tm_ostream.hpp"
16#include <errno.h>
17#include <mimalloc.h>
18
19/*****************************************************************************/
20// General purpose fast allocation routines
21/*****************************************************************************/
22
23void*
24fast_alloc (size_t sz) {
25 return mi_malloc (sz);
26}
27
28void
29fast_free (void* ptr, size_t sz) {
30 mi_free (ptr);
31}
32
33void*
34fast_realloc (void* ptr, size_t, size_t new_size) {
35 return mi_realloc (ptr, new_size);
36}
37
38void*
39fast_new (size_t s) {
40 return mi_malloc (s);
41}
42
43void
45 mi_free (ptr);
46}
47
48/******************************************************************************
49 * Statistics
50 ******************************************************************************/
51
52bool
54 size_t blocksize, void* arg) {
55 int* count= (int*) arg;
56 *count+= heapinfo->used;
57 return true;
58}
59
60int
63 int count= 0;
65 return count;
66}
67
68void
70 cout << "\n---------------- memory statistics ----------------\n";
71 cout << "malloc overrided:" << mi_is_redirected () << "\n";
72}
73
74void
75mem_err_handler (int err, void* arg) {
76 switch (err) {
77 case ENOMEM:
78 cerr << "Fatal error: out of memory\n";
79 break;
80 case EINVAL:
81 cerr << "Fatal error: invalid pointer\n";
82 break;
83 case EFAULT:
84 cerr << "Corrupted heap. Please check whether there are objects used after "
85 "free.\n";
86 break;
87 case EOVERFLOW:
88 cerr << "Fatal error: input argument of allocator is too large\n";
89 break;
90 case EAGAIN:
91 cerr << "Double free detected\n";
92 break;
93 default:
94 break;
95 }
96 abort ();
97}
98
99void
The list class represents a linked list.
Definition list.hpp:48
void mem_info()
Definition mi_malloc.cpp:69
void fast_delete(void *ptr)
Definition mi_malloc.cpp:44
void fast_free(void *ptr, size_t sz)
Definition mi_malloc.cpp:29
int mem_used()
Definition mi_malloc.cpp:61
void * fast_new(size_t s)
Definition mi_malloc.cpp:39
void * fast_alloc(size_t sz)
Definition mi_malloc.cpp:24
void mem_init()
bool visit_mem(const mi_heap_t *heap, const mi_heap_area_t *heapinfo, void *block, size_t blocksize, void *arg)
Definition mi_malloc.cpp:53
void * fast_realloc(void *ptr, size_t, size_t new_size)
Definition mi_malloc.cpp:34
void mem_err_handler(int err, void *arg)
Definition mi_malloc.cpp:75
tm_ostream & cerr
tm_ostream & cout