-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxmalloc.c
163 lines (141 loc) · 3.9 KB
/
xmalloc.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
* This file is part of libsysperf
*
* Copyright (C) 2001, 2004-2007 by Nokia Corporation.
*
* Contact: Eero Tamminen <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* 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 St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
/* ========================================================================= *
* File: xmalloc.c
*
* Author: Simo Piiroinen
* ========================================================================= */
/* ========================================================================= *
* "Safer" versions for stdlib memory allocation functions
*
* History:
*
* 12-Apr-2001 Simo Piiroinen
* - initial version
* ========================================================================= */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "xmalloc.h"
#ifndef F_all
# define F_all 1
#endif
/* ------------------------------------------------------------------------- *
* xmalloc
* ------------------------------------------------------------------------- */
#if F_all || F_xmalloc
void *xmalloc(size_t size)
{
void *addr = malloc(size);
assert( addr != NULL );
return addr;
}
#endif
/* ------------------------------------------------------------------------- *
* xfree
* ------------------------------------------------------------------------- */
#if F_all || F_xfree
void xfree(void *addr)
{
if( addr != 0 ) { free(addr); }
}
#endif
/* ------------------------------------------------------------------------- *
* xcalloc
* ------------------------------------------------------------------------- */
#if F_all || F_xcalloc
void *xcalloc(size_t nelem, size_t size)
{
void *addr = calloc(nelem, size);
assert( addr != NULL );
return addr;
}
#endif
/* ------------------------------------------------------------------------- *
* xrealloc
* ------------------------------------------------------------------------- */
#if F_all || F_xrealloc
void *xrealloc(void *old_addr, size_t new_size)
{
void *new_addr = NULL;
if( new_size == 0 )
{
free(old_addr);
}
else if( old_addr == NULL )
{
new_addr = malloc(new_size);
assert( new_addr != NULL );
}
else
{
new_addr = realloc(old_addr, new_size);
assert( new_addr != NULL );
}
return new_addr;
}
#endif
/* ------------------------------------------------------------------------- *
* xstrdup
* ------------------------------------------------------------------------- */
#if F_all || F_xstrdup
#include <string.h>
char *xstrdup(const char *str)
{
char *res = 0;
if( str != 0 )
{
size_t cnt = strlen(str) + 1;
res = memcpy(xmalloc(cnt), str, cnt);
}
return res;
}
#endif
/* ------------------------------------------------------------------------- *
* xstrset
* ------------------------------------------------------------------------- */
#if F_all || F_xstrset
void xstrset(char **dst, const char *str)
{
free(*dst);
*dst = (str == 0) ? 0 : xstrdup(str);
}
#endif
/* ------------------------------------------------------------------------- *
* xstrdupn
* ------------------------------------------------------------------------- */
#if F_all || F_xstrdupn
#include <string.h>
char *xstrdupn(const char *str, size_t len)
{
char *res = 0;
if( str != 0 )
{
size_t cnt = strnlen(str, len);
res = xmalloc(cnt + 1);
memcpy(res, str, cnt);
res[cnt] = 0;
}
return res;
}
#endif