-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem.c
131 lines (108 loc) · 2.98 KB
/
mem.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
This file is part of qbce-prepro.
Copyright 2018
Florian Lonsing, Vienna University of Technology, Austria.
qbce-prepro is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
qbce-prepro is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with qbce-prepro. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <stddef.h>
#include "mem.h"
#define ABORT_MEM(cond,msg) \
do { \
if (cond) \
{ \
fprintf (stderr, "[mem_man] %s at line %d: %s\n", __func__, \
__LINE__,msg); \
fflush (stderr); \
abort (); \
} \
} while (0)
MemMan *
mm_create ()
{
MemMan *mm = (MemMan *) malloc (sizeof (MemMan));
ABORT_MEM (!mm, "could not allocate memory!");
memset (mm, 0, sizeof (MemMan));
return mm;
}
void
mm_delete (MemMan * mm)
{
ABORT_MEM (!mm, "null pointer encountered!");
assert (mm->cur_allocated == 0);
free (mm);
}
void *
mm_malloc (MemMan * mm, size_t size)
{
/* Mem-limit is given in MB. */
if (mm->limit && mm->limit < (mm->cur_allocated + size) / 1024 / 1024)
{
fprintf (stderr,
"Attempted to allocate total %f MB (limit = %lu MB)\n",
((unsigned long) (mm->cur_allocated +
size)) / 1024 / (float) 1024,
(unsigned long) mm->limit);
ABORT_MEM (1, "mem-limit exceeded!");
}
void *r = malloc (size);
ABORT_MEM (!r, "could not allocate memory!");
memset (r, 0, size);
mm->cur_allocated += size;
if (mm->cur_allocated > mm->max_allocated)
mm->max_allocated = mm->cur_allocated;
return r;
}
void *
mm_realloc (MemMan * mm, void *ptr, size_t old_size, size_t new_size)
{
ptr = realloc (ptr, new_size);
ABORT_MEM (!ptr, "could not allocate memory!");
if (new_size > old_size)
memset (((char *) ptr) + old_size, 0, new_size - old_size);
mm->cur_allocated -= old_size;
mm->cur_allocated += new_size;
if (mm->cur_allocated > mm->max_allocated)
mm->max_allocated = mm->cur_allocated;
return ptr;
}
void
mm_free (MemMan * mm, void *ptr, size_t size)
{
ABORT_MEM (!mm, "null pointer encountered!");
free (ptr);
mm->cur_allocated -= size;
}
size_t
mm_max_allocated (MemMan * mm)
{
return mm->max_allocated;
}
size_t
mm_cur_allocated (MemMan * mm)
{
return mm->cur_allocated;
}
void
mm_set_mem_limit (MemMan * mm, size_t limit)
{
ABORT_MEM (limit <= 0, "mem-limit must be greater than 0!");
mm->limit = limit;
}
size_t
mm_get_mem_limit (MemMan * mm)
{
return mm->limit;
}