1 |
148 |
jeremybenn |
/* VxWorks provides its own version of malloc, and we can't use this
|
2 |
|
|
one because VxWorks does not provide sbrk. So we have a hook to
|
3 |
|
|
not compile this code. */
|
4 |
|
|
|
5 |
|
|
/* The routines here are simple cover fns to the routines that do the real
|
6 |
|
|
work (the reentrant versions). */
|
7 |
|
|
/* FIXME: Does the warning below (see WARNINGS) about non-reentrancy still
|
8 |
|
|
apply? A first guess would be "no", but how about reentrancy in the *same*
|
9 |
|
|
thread? */
|
10 |
|
|
|
11 |
|
|
#ifdef MALLOC_PROVIDED
|
12 |
|
|
|
13 |
|
|
int _dummy_malloc = 1;
|
14 |
|
|
|
15 |
|
|
#else
|
16 |
|
|
|
17 |
|
|
/*
|
18 |
|
|
FUNCTION
|
19 |
|
|
<<malloc>>, <<realloc>>, <<free>>---manage memory
|
20 |
|
|
|
21 |
|
|
INDEX
|
22 |
|
|
malloc
|
23 |
|
|
INDEX
|
24 |
|
|
realloc
|
25 |
|
|
INDEX
|
26 |
|
|
reallocf
|
27 |
|
|
INDEX
|
28 |
|
|
free
|
29 |
|
|
INDEX
|
30 |
|
|
memalign
|
31 |
|
|
INDEX
|
32 |
|
|
malloc_usable_size
|
33 |
|
|
INDEX
|
34 |
|
|
_malloc_r
|
35 |
|
|
INDEX
|
36 |
|
|
_realloc_r
|
37 |
|
|
INDEX
|
38 |
|
|
_reallocf_r
|
39 |
|
|
INDEX
|
40 |
|
|
_free_r
|
41 |
|
|
INDEX
|
42 |
|
|
_memalign_r
|
43 |
|
|
INDEX
|
44 |
|
|
_malloc_usable_size_r
|
45 |
|
|
|
46 |
|
|
ANSI_SYNOPSIS
|
47 |
|
|
#include <stdlib.h>
|
48 |
|
|
void *malloc(size_t <[nbytes]>);
|
49 |
|
|
void *realloc(void *<[aptr]>, size_t <[nbytes]>);
|
50 |
|
|
void *reallocf(void *<[aptr]>, size_t <[nbytes]>);
|
51 |
|
|
void free(void *<[aptr]>);
|
52 |
|
|
|
53 |
|
|
void *memalign(size_t <[align]>, size_t <[nbytes]>);
|
54 |
|
|
|
55 |
|
|
size_t malloc_usable_size(void *<[aptr]>);
|
56 |
|
|
|
57 |
|
|
void *_malloc_r(void *<[reent]>, size_t <[nbytes]>);
|
58 |
|
|
void *_realloc_r(void *<[reent]>,
|
59 |
|
|
void *<[aptr]>, size_t <[nbytes]>);
|
60 |
|
|
void *_reallocf_r(void *<[reent]>,
|
61 |
|
|
void *<[aptr]>, size_t <[nbytes]>);
|
62 |
|
|
void _free_r(void *<[reent]>, void *<[aptr]>);
|
63 |
|
|
|
64 |
|
|
void *_memalign_r(void *<[reent]>,
|
65 |
|
|
size_t <[align]>, size_t <[nbytes]>);
|
66 |
|
|
|
67 |
|
|
size_t _malloc_usable_size_r(void *<[reent]>, void *<[aptr]>);
|
68 |
|
|
|
69 |
|
|
TRAD_SYNOPSIS
|
70 |
|
|
#include <stdlib.h>
|
71 |
|
|
char *malloc(<[nbytes]>)
|
72 |
|
|
size_t <[nbytes]>;
|
73 |
|
|
|
74 |
|
|
char *realloc(<[aptr]>, <[nbytes]>)
|
75 |
|
|
char *<[aptr]>;
|
76 |
|
|
size_t <[nbytes]>;
|
77 |
|
|
|
78 |
|
|
char *reallocf(<[aptr]>, <[nbytes]>)
|
79 |
|
|
char *<[aptr]>;
|
80 |
|
|
size_t <[nbytes]>;
|
81 |
|
|
|
82 |
|
|
void free(<[aptr]>)
|
83 |
|
|
char *<[aptr]>;
|
84 |
|
|
|
85 |
|
|
char *memalign(<[align]>, <[nbytes]>)
|
86 |
|
|
size_t <[align]>;
|
87 |
|
|
size_t <[nbytes]>;
|
88 |
|
|
|
89 |
|
|
size_t malloc_usable_size(<[aptr]>)
|
90 |
|
|
char *<[aptr]>;
|
91 |
|
|
|
92 |
|
|
char *_malloc_r(<[reent]>,<[nbytes]>)
|
93 |
|
|
char *<[reent]>;
|
94 |
|
|
size_t <[nbytes]>;
|
95 |
|
|
|
96 |
|
|
char *_realloc_r(<[reent]>, <[aptr]>, <[nbytes]>)
|
97 |
|
|
char *<[reent]>;
|
98 |
|
|
char *<[aptr]>;
|
99 |
|
|
size_t <[nbytes]>;
|
100 |
|
|
|
101 |
|
|
char *_reallocf_r(<[reent]>, <[aptr]>, <[nbytes]>)
|
102 |
|
|
char *<[reent]>;
|
103 |
|
|
char *<[aptr]>;
|
104 |
|
|
size_t <[nbytes]>;
|
105 |
|
|
|
106 |
|
|
void _free_r(<[reent]>, <[aptr]>)
|
107 |
|
|
char *<[reent]>;
|
108 |
|
|
char *<[aptr]>;
|
109 |
|
|
|
110 |
|
|
char *_memalign_r(<[reent]>, <[align]>, <[nbytes]>)
|
111 |
|
|
char *<[reent]>;
|
112 |
|
|
size_t <[align]>;
|
113 |
|
|
size_t <[nbytes]>;
|
114 |
|
|
|
115 |
|
|
size_t malloc_usable_size(<[reent]>, <[aptr]>)
|
116 |
|
|
char *<[reent]>;
|
117 |
|
|
char *<[aptr]>;
|
118 |
|
|
|
119 |
|
|
DESCRIPTION
|
120 |
|
|
These functions manage a pool of system memory.
|
121 |
|
|
|
122 |
|
|
Use <<malloc>> to request allocation of an object with at least
|
123 |
|
|
<[nbytes]> bytes of storage available. If the space is available,
|
124 |
|
|
<<malloc>> returns a pointer to a newly allocated block as its result.
|
125 |
|
|
|
126 |
|
|
If you already have a block of storage allocated by <<malloc>>, but
|
127 |
|
|
you no longer need all the space allocated to it, you can make it
|
128 |
|
|
smaller by calling <<realloc>> with both the object pointer and the
|
129 |
|
|
new desired size as arguments. <<realloc>> guarantees that the
|
130 |
|
|
contents of the smaller object match the beginning of the original object.
|
131 |
|
|
|
132 |
|
|
Similarly, if you need more space for an object, use <<realloc>> to
|
133 |
|
|
request the larger size; again, <<realloc>> guarantees that the
|
134 |
|
|
beginning of the new, larger object matches the contents of the
|
135 |
|
|
original object.
|
136 |
|
|
|
137 |
|
|
When you no longer need an object originally allocated by <<malloc>>
|
138 |
|
|
or <<realloc>> (or the related function <<calloc>>), return it to the
|
139 |
|
|
memory storage pool by calling <<free>> with the address of the object
|
140 |
|
|
as the argument. You can also use <<realloc>> for this purpose by
|
141 |
|
|
calling it with <<0>> as the <[nbytes]> argument.
|
142 |
|
|
|
143 |
|
|
The <<reallocf>> function behaves just like <<realloc>> except if the
|
144 |
|
|
function is required to allocate new storage and this fails. In this
|
145 |
|
|
case <<reallocf>> will free the original object passed in whereas
|
146 |
|
|
<<realloc>> will not.
|
147 |
|
|
|
148 |
|
|
The <<memalign>> function returns a block of size <[nbytes]> aligned
|
149 |
|
|
to a <[align]> boundary. The <[align]> argument must be a power of
|
150 |
|
|
two.
|
151 |
|
|
|
152 |
|
|
The <<malloc_usable_size>> function takes a pointer to a block
|
153 |
|
|
allocated by <<malloc>>. It returns the amount of space that is
|
154 |
|
|
available in the block. This may or may not be more than the size
|
155 |
|
|
requested from <<malloc>>, due to alignment or minimum size
|
156 |
|
|
constraints.
|
157 |
|
|
|
158 |
|
|
The alternate functions <<_malloc_r>>, <<_realloc_r>>, <<_reallocf_r>>,
|
159 |
|
|
<<_free_r>>, <<_memalign_r>>, and <<_malloc_usable_size_r>> are reentrant
|
160 |
|
|
versions. The extra argument <[reent]> is a pointer to a reentrancy structure.
|
161 |
|
|
|
162 |
|
|
If you have multiple threads of execution which may call any of these
|
163 |
|
|
routines, or if any of these routines may be called reentrantly, then
|
164 |
|
|
you must provide implementations of the <<__malloc_lock>> and
|
165 |
|
|
<<__malloc_unlock>> functions for your system. See the documentation
|
166 |
|
|
for those functions.
|
167 |
|
|
|
168 |
|
|
These functions operate by calling the function <<_sbrk_r>> or
|
169 |
|
|
<<sbrk>>, which allocates space. You may need to provide one of these
|
170 |
|
|
functions for your system. <<_sbrk_r>> is called with a positive
|
171 |
|
|
value to allocate more space, and with a negative value to release
|
172 |
|
|
previously allocated space if it is no longer required.
|
173 |
|
|
@xref{Stubs}.
|
174 |
|
|
|
175 |
|
|
RETURNS
|
176 |
|
|
<<malloc>> returns a pointer to the newly allocated space, if
|
177 |
|
|
successful; otherwise it returns <<NULL>>. If your application needs
|
178 |
|
|
to generate empty objects, you may use <<malloc(0)>> for this purpose.
|
179 |
|
|
|
180 |
|
|
<<realloc>> returns a pointer to the new block of memory, or <<NULL>>
|
181 |
|
|
if a new block could not be allocated. <<NULL>> is also the result
|
182 |
|
|
when you use `<<realloc(<[aptr]>,0)>>' (which has the same effect as
|
183 |
|
|
`<<free(<[aptr]>)>>'). You should always check the result of
|
184 |
|
|
<<realloc>>; successful reallocation is not guaranteed even when
|
185 |
|
|
you request a smaller object.
|
186 |
|
|
|
187 |
|
|
<<free>> does not return a result.
|
188 |
|
|
|
189 |
|
|
<<memalign>> returns a pointer to the newly allocated space.
|
190 |
|
|
|
191 |
|
|
<<malloc_usable_size>> returns the usable size.
|
192 |
|
|
|
193 |
|
|
PORTABILITY
|
194 |
|
|
<<malloc>>, <<realloc>>, and <<free>> are specified by the ANSI C
|
195 |
|
|
standard, but other conforming implementations of <<malloc>> may
|
196 |
|
|
behave differently when <[nbytes]> is zero.
|
197 |
|
|
|
198 |
|
|
<<memalign>> is part of SVR4.
|
199 |
|
|
|
200 |
|
|
<<malloc_usable_size>> is not portable.
|
201 |
|
|
|
202 |
|
|
Supporting OS subroutines required: <<sbrk>>. */
|
203 |
|
|
|
204 |
|
|
#include <_ansi.h>
|
205 |
|
|
#include <reent.h>
|
206 |
|
|
#include <stdlib.h>
|
207 |
|
|
#include <malloc.h>
|
208 |
|
|
|
209 |
|
|
#ifndef _REENT_ONLY
|
210 |
|
|
|
211 |
|
|
_PTR
|
212 |
|
|
_DEFUN (malloc, (nbytes),
|
213 |
|
|
size_t nbytes) /* get a block */
|
214 |
|
|
{
|
215 |
|
|
return _malloc_r (_REENT, nbytes);
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
void
|
219 |
|
|
_DEFUN (free, (aptr),
|
220 |
|
|
_PTR aptr)
|
221 |
|
|
{
|
222 |
|
|
_free_r (_REENT, aptr);
|
223 |
|
|
}
|
224 |
|
|
|
225 |
|
|
#endif
|
226 |
|
|
|
227 |
|
|
#endif /* ! defined (MALLOC_PROVIDED) */
|