00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00040 #include <stdlib.h>
00041 #include <string.h>
00042
00043 #include "avrerror.h"
00044 #include "avrmalloc.h"
00045
00046
00047
00048 #if MACRO_DOCUMENTATION
00049
00057 #define avr_new(type, count) \
00058 ((type *) avr_malloc ((unsigned) sizeof (type) * (count)))
00059
00067 #define avr_new0(type, count) \
00068 ((type *) avr_malloc0 ((unsigned) sizeof (type) * (count)))
00069
00078 #define avr_renew(type, mem, count) \
00079 ((type *) avr_realloc (mem, (unsigned) sizeof (type) * (count)))
00080
00081 #endif
00082
00092 void *avr_malloc(size_t size)
00093 {
00094 if (size)
00095 {
00096 void *ptr;
00097 ptr = malloc( size );
00098 if (ptr)
00099 return ptr;
00100
00101 avr_error( "malloc failed" );
00102 }
00103 return NULL;
00104 }
00105
00115 void *avr_malloc0(size_t size)
00116 {
00117 if (size)
00118 {
00119 void *ptr;
00120 ptr = calloc( 1, size );
00121 if (ptr)
00122 return ptr;
00123
00124 avr_error( "malloc0 failed" );
00125 }
00126 return NULL;
00127 }
00128
00140 void *avr_realloc(void *ptr, size_t size)
00141 {
00142 if (size)
00143 {
00144 ptr = realloc( ptr, size );
00145 if (ptr)
00146 return ptr;
00147
00148 avr_error( "realloc failed\n" );
00149 }
00150 return NULL;
00151 }
00152
00164 char *avr_strdup(const char *s)
00165 {
00166 if (s)
00167 {
00168 char *ptr;
00169 ptr = strdup(s);
00170 if (ptr)
00171 return ptr;
00172
00173 avr_error( "strdup failed" );
00174 }
00175 return NULL;
00176 }
00177
00182 void avr_free(void *ptr)
00183 {
00184 if (ptr)
00185 free(ptr);
00186 }
00187