avrmalloc.c

Go to the documentation of this file.
00001 /*
00002  * $Id: avrmalloc.c,v 1.7 2003/12/01 09:10:14 troth Exp $
00003  *
00004  ****************************************************************************
00005  *
00006  * simulavr - A simulator for the Atmel AVR family of microcontrollers.
00007  * Copyright (C) 2001, 2002, 2003  Theodore A. Roth
00008  *
00009  * This program is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * This program is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License
00020  * along with this program; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  ****************************************************************************
00024  */
00025 
00026 /**
00027    \file avrmalloc.c
00028    \brief Memory Management Functions.
00029 
00030    This module provides facilities for managing memory.
00031 
00032    There is no need to check the returned values from any of these
00033    functions. Any memory allocation failure is considered fatal and the
00034    program is terminated.
00035 
00036    We want to wrap all functions that allocate memory. This way we can
00037    add secret code to track memory usage and debug memory leaks if we 
00038    want. Right now, I don't want to ;). */
00039 
00040 #include <stdlib.h>
00041 #include <string.h>
00042 
00043 #include "avrerror.h"
00044 #include "avrmalloc.h"
00045 
00046 /* These macros are only here for documentation purposes. */
00047 
00048 #if MACRO_DOCUMENTATION
00049 
00050 /** \brief Macro for allocating memory.
00051     \param type  The C type of the memory to allocate.
00052     \param count Allocate enough memory hold count types.
00053 
00054     This macro is just a wrapper for avr_malloc() and should be used to avoid
00055     the repetitive task of casting the returned pointer. */
00056 
00057 #define avr_new(type, count)          \
00058     ((type *) avr_malloc ((unsigned) sizeof (type) * (count)))
00059 
00060 /** \brief Macro for allocating memory and initializing it to zero.
00061     \param type  The C type of the memory to allocate.
00062     \param count Allocate enough memory hold count types.
00063 
00064     This macro is just a wrapper for avr_malloc0() and should be used to avoid
00065     the repetitive task of casting the returned pointer. */
00066 
00067 #define avr_new0(type, count)         \
00068     ((type *) avr_malloc0 ((unsigned) sizeof (type) * (count)))
00069 
00070 /** \brief Macro for allocating memory.
00071     \param type  The C type of the memory to allocate.
00072     \param mem   Pointer to existing memory.
00073     \param count Allocate enough memory hold count types.
00074 
00075     This macro is just a wrapper for avr_malloc() and should be used to avoid
00076     the repetitive task of casting the returned pointer. */
00077 
00078 #define avr_renew(type, mem, count)   \
00079    ((type *) avr_realloc (mem, (unsigned) sizeof (type) * (count)))
00080 
00081 #endif /* MACRO_DOCUMENTATION */
00082 
00083 /** \brief Allocate memory and initialize to zero.
00084 
00085     Use the avr_new() macro instead of this function.
00086 
00087     There is no need to check the returned value, since this function will
00088     terminate the program if the memory allocation fails.
00089 
00090     No memory is allocated if passed a size of zero. */
00091 
00092 void *
00093 avr_malloc (size_t size)
00094 {
00095     if (size)
00096     {
00097         void *ptr;
00098         ptr = malloc (size);
00099         if (ptr)
00100             return ptr;
00101 
00102         avr_error ("malloc failed");
00103     }
00104     return NULL;
00105 }
00106 
00107 /** \brief Allocate memory and initialize to zero.
00108 
00109     Use the avr_new0() macro instead of this function.
00110 
00111     There is no need to check the returned value, since this function will
00112     terminate the program if the memory allocation fails.
00113 
00114     No memory is allocated if passed a size of zero. */
00115 
00116 void *
00117 avr_malloc0 (size_t size)
00118 {
00119     if (size)
00120     {
00121         void *ptr;
00122         ptr = calloc (1, size);
00123         if (ptr)
00124             return ptr;
00125 
00126         avr_error ("malloc0 failed");
00127     }
00128     return NULL;
00129 }
00130 
00131 /** \brief Wrapper for realloc().
00132 x
00133     Resizes and possibly allocates more memory for an existing memory block.
00134 
00135     Use the avr_renew() macro instead of this function.
00136 
00137     There is no need to check the returned value, since this function will
00138     terminate the program if the memory allocation fails.
00139 
00140     No memory is allocated if passed a size of zero. */
00141 
00142 void *
00143 avr_realloc (void *ptr, size_t size)
00144 {
00145     if (size)
00146     {
00147         ptr = realloc (ptr, size);
00148         if (ptr)
00149             return ptr;
00150 
00151         avr_error ("realloc failed\n");
00152     }
00153     return NULL;
00154 }
00155 
00156 /** \brief Wrapper for strdup().
00157 
00158     Returns a copy of the passed in string. The returned copy must be
00159     free'd.
00160 
00161     There is no need to check the returned value, since this function will
00162     terminate the program if the memory allocation fails.
00163 
00164     It is safe to pass a NULL pointer. No memory is allocated if a NULL is
00165     passed. */
00166 
00167 char *
00168 avr_strdup (const char *s)
00169 {
00170     if (s)
00171     {
00172         char *ptr;
00173         ptr = strdup (s);
00174         if (ptr)
00175             return ptr;
00176 
00177         avr_error ("strdup failed");
00178     }
00179     return NULL;
00180 }
00181 
00182 /** \brief Free malloc'd memory.
00183 
00184     It is safe to pass a null pointer to this function. */
00185 
00186 void
00187 avr_free (void *ptr)
00188 {
00189     if (ptr)
00190         free (ptr);
00191 }