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