simulavr  1.1.0
avrmalloc.cpp
Go to the documentation of this file.
1 /*
2  *
3  ****************************************************************************
4  *
5  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
6  * Copyright (C) 2001, 2002 Theodore A. Roth
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  ****************************************************************************
23  *
24  * $Id$
25  */
26 
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 
57 #define avr_new(type, count) \
58  ((type *) avr_malloc ((unsigned) sizeof (type) * (count)))
59 
67 #define avr_new0(type, count) \
68  ((type *) avr_malloc0 ((unsigned) sizeof (type) * (count)))
69 
78 #define avr_renew(type, mem, count) \
79  ((type *) avr_realloc (mem, (unsigned) sizeof (type) * (count)))
80 
81 #endif /* MACRO_DOCUMENTATION */
82 
92 void *avr_malloc(size_t size)
93 {
94  if (size)
95  {
96  void *ptr;
97  ptr = malloc( size );
98  if (ptr)
99  return ptr;
100 
101  avr_error( "malloc failed" );
102  }
103  return NULL;
104 }
105 
115 void *avr_malloc0(size_t size)
116 {
117  if (size)
118  {
119  void *ptr;
120  ptr = calloc( 1, size );
121  if (ptr)
122  return ptr;
123 
124  avr_error( "malloc0 failed" );
125  }
126  return NULL;
127 }
128 
140 void *avr_realloc(void *ptr, size_t size)
141 {
142  if (size)
143  {
144  ptr = realloc( ptr, size );
145  if (ptr)
146  return ptr;
147 
148  avr_error( "realloc failed\n" );
149  }
150  return NULL;
151 }
152 
164 char *avr_strdup(const char *s)
165 {
166  if (s)
167  {
168  char *ptr;
169  ptr = strdup(s);
170  if (ptr)
171  return ptr;
172 
173  avr_error( "strdup failed" );
174  }
175  return NULL;
176 }
177 
182 void avr_free(void *ptr)
183 {
184  if (ptr)
185  free(ptr);
186 }
187 
void * avr_malloc(size_t size)
Memory Management Functions.
Definition: avrmalloc.cpp:92
void avr_free(void *ptr)
Free malloc&#39;d memory.
Definition: avrmalloc.cpp:182
#define avr_error(...)
Definition: avrerror.h:135
void * avr_malloc0(size_t size)
Allocate memory and initialize to zero.
Definition: avrmalloc.cpp:115
void * avr_realloc(void *ptr, size_t size)
Wrapper for realloc().
Definition: avrmalloc.cpp:140
char * avr_strdup(const char *s)
Wrapper for strdup().
Definition: avrmalloc.cpp:164