forked from servalproject/serval-dna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mem.c
89 lines (75 loc) · 2.2 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
/*
Serval DNA memory management
Copyright (C) 2012 Serval Project Inc.
This program 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 2
of the License, or (at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include "mem.h"
void *_emalloc(struct __sourceloc __whence, size_t bytes)
{
char *new = malloc(bytes);
if (!new) {
WHYF_perror("malloc(%lu)", (long)bytes);
return NULL;
}
return new;
}
void *_erealloc(struct __sourceloc __whence, void *ptr, size_t bytes)
{
char *new = realloc(ptr, bytes);
if (!new) {
WHYF_perror("realloc(%p, %lu)", ptr, (unsigned long)bytes);
return NULL;
}
return new;
}
void *_emalloc_zero(struct __sourceloc __whence, size_t bytes)
{
char *new = _emalloc(__whence, bytes);
if (new)
memset(new, 0, bytes);
return new;
}
char *_strn_edup(struct __sourceloc __whence, const char *str, size_t len)
{
char *new = _emalloc(__whence, len + 1);
if (new)
strncpy(new, str, len)[len] = '\0';
return new;
}
char *_str_edup(struct __sourceloc __whence, const char *str)
{
return _strn_edup(__whence, str, strlen(str));
}
#undef malloc
#undef calloc
#undef free
#undef realloc
#define SDM_GUARD_AFTER 16384
void *_serval_debug_malloc(unsigned int bytes, struct __sourceloc __whence)
{
void *r=malloc(bytes+SDM_GUARD_AFTER);
DEBUGF("malloc(%d) -> %p", bytes, r);
return r;
}
void *_serval_debug_calloc(unsigned int bytes, unsigned int count, struct __sourceloc __whence)
{
void *r=calloc((bytes*count)+SDM_GUARD_AFTER,1);
DEBUGF("calloc(%d,%d) -> %p", bytes, count, r);
return r;
}
void _serval_debug_free(void *p, struct __sourceloc __whence)
{
free(p);
DEBUGF("free(%p)", p);
}