Main Page
Related Pages
Files
File List
Globals
avrmalloc.c
Go to the documentation of this file.
1
/*
2
* $Id: avrmalloc.c,v 1.7 2003/12/01 09:10:14 troth Exp $
3
*
4
****************************************************************************
5
*
6
* simulavr - A simulator for the Atmel AVR family of microcontrollers.
7
* Copyright (C) 2001, 2002, 2003 Theodore A. Roth
8
*
9
* This program is free software; you can redistribute it and/or modify
10
* it under the terms of the GNU General Public License as published by
11
* the Free Software Foundation; either version 2 of the License, or
12
* (at your option) any later version.
13
*
14
* This program is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
* GNU General Public License for more details.
18
*
19
* You should have received a copy of the GNU General Public License
20
* along with this program; if not, write to the Free Software
21
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
*
23
****************************************************************************
24
*/
25
26
/**
27
\file avrmalloc.c
28
\brief Memory Management Functions.
29
30
This module provides facilities for managing memory.
31
32
There is no need to check the returned values from any of these
33
functions. Any memory allocation failure is considered fatal and the
34
program is terminated.
35
36
We want to wrap all functions that allocate memory. This way we can
37
add secret code to track memory usage and debug memory leaks if we
38
want. Right now, I don't want to ;). */
39
40
#include <stdlib.h>
41
#include <string.h>
42
43
#include "avrerror.h"
44
#include "avrmalloc.h"
45
46
/* These macros are only here for documentation purposes. */
47
48
#if MACRO_DOCUMENTATION
49
50
/** \brief Macro for allocating memory.
51
\param type The C type of the memory to allocate.
52
\param count Allocate enough memory hold count types.
53
54
This macro is just a wrapper for avr_malloc() and should be used to avoid
55
the repetitive task of casting the returned pointer. */
56
57
#define avr_new(type, count) \
58
((type *) avr_malloc ((unsigned) sizeof (type) * (count)))
59
60
/** \brief Macro for allocating memory and initializing it to zero.
61
\param type The C type of the memory to allocate.
62
\param count Allocate enough memory hold count types.
63
64
This macro is just a wrapper for avr_malloc0() and should be used to avoid
65
the repetitive task of casting the returned pointer. */
66
67
#define avr_new0(type, count) \
68
((type *) avr_malloc0 ((unsigned) sizeof (type) * (count)))
69
70
/** \brief Macro for allocating memory.
71
\param type The C type of the memory to allocate.
72
\param mem Pointer to existing memory.
73
\param count Allocate enough memory hold count types.
74
75
This macro is just a wrapper for avr_malloc() and should be used to avoid
76
the repetitive task of casting the returned pointer. */
77
78
#define avr_renew(type, mem, count) \
79
((type *) avr_realloc (mem, (unsigned) sizeof (type) * (count)))
80
81
#endif
/* MACRO_DOCUMENTATION */
82
83
/** \brief Allocate memory and initialize to zero.
84
85
Use the avr_new() macro instead of this function.
86
87
There is no need to check the returned value, since this function will
88
terminate the program if the memory allocation fails.
89
90
No memory is allocated if passed a size of zero. */
91
92
void
*
93
avr_malloc
(
size_t
size)
94
{
95
if
(size)
96
{
97
void
*ptr;
98
ptr = malloc (size);
99
if
(ptr)
100
return
ptr;
101
102
avr_error
(
"malloc failed"
);
103
}
104
return
NULL;
105
}
106
107
/** \brief Allocate memory and initialize to zero.
108
109
Use the avr_new0() macro instead of this function.
110
111
There is no need to check the returned value, since this function will
112
terminate the program if the memory allocation fails.
113
114
No memory is allocated if passed a size of zero. */
115
116
void
*
117
avr_malloc0
(
size_t
size)
118
{
119
if
(size)
120
{
121
void
*ptr;
122
ptr = calloc (1, size);
123
if
(ptr)
124
return
ptr;
125
126
avr_error
(
"malloc0 failed"
);
127
}
128
return
NULL;
129
}
130
131
/** \brief Wrapper for realloc().
132
x
133
Resizes and possibly allocates more memory for an existing memory block.
134
135
Use the avr_renew() macro instead of this function.
136
137
There is no need to check the returned value, since this function will
138
terminate the program if the memory allocation fails.
139
140
No memory is allocated if passed a size of zero. */
141
142
void
*
143
avr_realloc
(
void
*ptr,
size_t
size)
144
{
145
if
(size)
146
{
147
ptr = realloc (ptr, size);
148
if
(ptr)
149
return
ptr;
150
151
avr_error
(
"realloc failed\n"
);
152
}
153
return
NULL;
154
}
155
156
/** \brief Wrapper for strdup().
157
158
Returns a copy of the passed in string. The returned copy must be
159
free'd.
160
161
There is no need to check the returned value, since this function will
162
terminate the program if the memory allocation fails.
163
164
It is safe to pass a NULL pointer. No memory is allocated if a NULL is
165
passed. */
166
167
char
*
168
avr_strdup
(
const
char
*s)
169
{
170
if
(s)
171
{
172
char
*ptr;
173
ptr = strdup (s);
174
if
(ptr)
175
return
ptr;
176
177
avr_error
(
"strdup failed"
);
178
}
179
return
NULL;
180
}
181
182
/** \brief Free malloc'd memory.
183
184
It is safe to pass a null pointer to this function. */
185
186
void
187
avr_free
(
void
*ptr)
188
{
189
if
(ptr)
190
free (ptr);
191
}
Automatically generated by Doxygen 1.8.2 on Thu Mar 10 2016.