File: avrmalloc.c

package info (click to toggle)
simulavr 0.1.2.2-6.1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 2,756 kB
  • ctags: 3,179
  • sloc: ansic: 19,987; sh: 3,623; python: 3,528; makefile: 406; asm: 308; yacc: 145; lex: 48
file content (191 lines) | stat: -rw-r--r-- 5,313 bytes parent folder | download | duplicates (5)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * $Id: avrmalloc.c,v 1.7 2003/12/01 09:10:14 troth Exp $
 *
 ****************************************************************************
 *
 * simulavr - A simulator for the Atmel AVR family of microcontrollers.
 * Copyright (C) 2001, 2002, 2003  Theodore A. Roth
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 ****************************************************************************
 */

/**
   \file avrmalloc.c
   \brief Memory Management Functions.

   This module provides facilities for managing memory.

   There is no need to check the returned values from any of these
   functions. Any memory allocation failure is considered fatal and the
   program is terminated.

   We want to wrap all functions that allocate memory. This way we can
   add secret code to track memory usage and debug memory leaks if we 
   want. Right now, I don't want to ;). */

#include <stdlib.h>
#include <string.h>

#include "avrerror.h"
#include "avrmalloc.h"

/* These macros are only here for documentation purposes. */

#if MACRO_DOCUMENTATION

/** \brief Macro for allocating memory.
    \param type  The C type of the memory to allocate.
    \param count Allocate enough memory hold count types.

    This macro is just a wrapper for avr_malloc() and should be used to avoid
    the repetitive task of casting the returned pointer. */

#define avr_new(type, count)          \
    ((type *) avr_malloc ((unsigned) sizeof (type) * (count)))

/** \brief Macro for allocating memory and initializing it to zero.
    \param type  The C type of the memory to allocate.
    \param count Allocate enough memory hold count types.

    This macro is just a wrapper for avr_malloc0() and should be used to avoid
    the repetitive task of casting the returned pointer. */

#define avr_new0(type, count)         \
    ((type *) avr_malloc0 ((unsigned) sizeof (type) * (count)))

/** \brief Macro for allocating memory.
    \param type  The C type of the memory to allocate.
    \param mem   Pointer to existing memory.
    \param count Allocate enough memory hold count types.

    This macro is just a wrapper for avr_malloc() and should be used to avoid
    the repetitive task of casting the returned pointer. */

#define avr_renew(type, mem, count)   \
   ((type *) avr_realloc (mem, (unsigned) sizeof (type) * (count)))

#endif /* MACRO_DOCUMENTATION */

/** \brief Allocate memory and initialize to zero.

    Use the avr_new() macro instead of this function.

    There is no need to check the returned value, since this function will
    terminate the program if the memory allocation fails.

    No memory is allocated if passed a size of zero. */

void *
avr_malloc (size_t size)
{
    if (size)
    {
        void *ptr;
        ptr = malloc (size);
        if (ptr)
            return ptr;

        avr_error ("malloc failed");
    }
    return NULL;
}

/** \brief Allocate memory and initialize to zero.

    Use the avr_new0() macro instead of this function.

    There is no need to check the returned value, since this function will
    terminate the program if the memory allocation fails.

    No memory is allocated if passed a size of zero. */

void *
avr_malloc0 (size_t size)
{
    if (size)
    {
        void *ptr;
        ptr = calloc (1, size);
        if (ptr)
            return ptr;

        avr_error ("malloc0 failed");
    }
    return NULL;
}

/** \brief Wrapper for realloc().
x
    Resizes and possibly allocates more memory for an existing memory block.

    Use the avr_renew() macro instead of this function.

    There is no need to check the returned value, since this function will
    terminate the program if the memory allocation fails.

    No memory is allocated if passed a size of zero. */

void *
avr_realloc (void *ptr, size_t size)
{
    if (size)
    {
        ptr = realloc (ptr, size);
        if (ptr)
            return ptr;

        avr_error ("realloc failed\n");
    }
    return NULL;
}

/** \brief Wrapper for strdup().

    Returns a copy of the passed in string. The returned copy must be
    free'd.

    There is no need to check the returned value, since this function will
    terminate the program if the memory allocation fails.

    It is safe to pass a NULL pointer. No memory is allocated if a NULL is
    passed. */

char *
avr_strdup (const char *s)
{
    if (s)
    {
        char *ptr;
        ptr = strdup (s);
        if (ptr)
            return ptr;

        avr_error ("strdup failed");
    }
    return NULL;
}

/** \brief Free malloc'd memory.

    It is safe to pass a null pointer to this function. */

void
avr_free (void *ptr)
{
    if (ptr)
        free (ptr);
}