From 8a0efa53e44919bcf5ccb1d3353618a82afdf8bc Mon Sep 17 00:00:00 2001 From: Christopher Faylor Date: Thu, 17 Feb 2000 19:39:52 +0000 Subject: import newlib-2000-02-17 snapshot --- newlib/libc/stdlib/malloc.c | 206 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 newlib/libc/stdlib/malloc.c (limited to 'newlib/libc/stdlib/malloc.c') diff --git a/newlib/libc/stdlib/malloc.c b/newlib/libc/stdlib/malloc.c new file mode 100644 index 000000000..83453ab8d --- /dev/null +++ b/newlib/libc/stdlib/malloc.c @@ -0,0 +1,206 @@ +/* VxWorks provides its own version of malloc, and we can't use this + one because VxWorks does not provide sbrk. So we have a hook to + not compile this code. */ + +/* The routines here are simple cover fns to the routines that do the real + work (the reentrant versions). */ +/* FIXME: Does the warning below (see WARNINGS) about non-reentrancy still + apply? A first guess would be "no", but how about reentrancy in the *same* + thread? */ + +#ifdef MALLOC_PROVIDED + +int _dummy_malloc = 1; + +#else + +/* +FUNCTION +<>, <>, <>---manage memory + +INDEX + malloc +INDEX + realloc +INDEX + free +INDEX + memalign +INDEX + malloc_usable_size +INDEX + _malloc_r +INDEX + _realloc_r +INDEX + _free_r +INDEX + _memalign_r +INDEX + _malloc_usable_size_r + +ANSI_SYNOPSIS + #include + void *malloc(size_t <[nbytes]>); + void *realloc(void *<[aptr]>, size_t <[nbytes]>); + void free(void *<[aptr]>); + + void *memalign(size_t <[align]>, size_t <[nbytes]>); + + size_t malloc_usable_size(void *<[aptr]>); + + void *_malloc_r(void *<[reent]>, size_t <[nbytes]>); + void *_realloc_r(void *<[reent]>, + void *<[aptr]>, size_t <[nbytes]>); + void _free_r(void *<[reent]>, void *<[aptr]>); + + void *_memalign_r(void *<[reent]>, + size_t <[align]>, size_t <[nbytes]>); + + size_t _malloc_usable_size_r(void *<[reent]>, void *<[aptr]>); + +TRAD_SYNOPSIS + #include + char *malloc(<[nbytes]>) + size_t <[nbytes]>; + + char *realloc(<[aptr]>, <[nbytes]>) + char *<[aptr]>; + size_t <[nbytes]>; + + void free(<[aptr]>) + char *<[aptr]>; + + char *memalign(<[align]>, <[nbytes]>) + size_t <[align]>; + size_t <[nbytes]>; + + size_t malloc_usable_size(<[aptr]>) + char *<[aptr]>; + + char *_malloc_r(<[reent]>,<[nbytes]>) + char *<[reent]>; + size_t <[nbytes]>; + + char *_realloc_r(<[reent]>, <[aptr]>, <[nbytes]>) + char *<[reent]>; + char *<[aptr]>; + size_t <[nbytes]>; + + void _free_r(<[reent]>, <[aptr]>) + char *<[reent]>; + char *<[aptr]>; + + char *_memalign_r(<[reent]>, <[align]>, <[nbytes]>) + char *<[reent]>; + size_t <[align]>; + size_t <[nbytes]>; + + size_t malloc_usable_size(<[reent]>, <[aptr]>) + char *<[reent]>; + char *<[aptr]>; + +DESCRIPTION +These functions manage a pool of system memory. + +Use <> to request allocation of an object with at least +<[nbytes]> bytes of storage available. If the space is available, +<> returns a pointer to a newly allocated block as its result. + +If you already have a block of storage allocated by <>, but +you no longer need all the space allocated to it, you can make it +smaller by calling <> with both the object pointer and the +new desired size as arguments. <> guarantees that the +contents of the smaller object match the beginning of the original object. + +Similarly, if you need more space for an object, use <> to +request the larger size; again, <> guarantees that the +beginning of the new, larger object matches the contents of the +original object. + +When you no longer need an object originally allocated by <> +or <> (or the related function <>), return it to the +memory storage pool by calling <> with the address of the object +as the argument. You can also use <> for this purpose by +calling it with <<0>> as the <[nbytes]> argument. + +The <> function returns a block of size <[nbytes]> aligned +to a <[align]> boundary. The <[align]> argument must be a power of +two. + +The <> function takes a pointer to a block +allocated by <>. It returns the amount of space that is +available in the block. This may or may not be more than the size +requested from <>, due to alignment or minimum size +constraints. + +The alternate functions <<_malloc_r>>, <<_realloc_r>>, <<_free_r>>, +<<_memalign_r>>, and <<_malloc_usable_size_r>> are reentrant versions. +The extra argument <[reent]> is a pointer to a reentrancy structure. + +If you have multiple threads of execution which may call any of these +routines, or if any of these routines may be called reentrantly, then +you must provide implementations of the <<__malloc_lock>> and +<<__malloc_unlock>> functions for your system. See the documentation +for those functions. + +These functions operate by calling the function <<_sbrk_r>> or +<>, which allocates space. You may need to provide one of these +functions for your system. <<_sbrk_r>> is called with a positive +value to allocate more space, and with a negative value to release +previously allocated space if it is no longer required. +@xref{Stubs}. + +RETURNS +<> returns a pointer to the newly allocated space, if +successful; otherwise it returns <>. If your application needs +to generate empty objects, you may use <> for this purpose. + +<> returns a pointer to the new block of memory, or <> +if a new block could not be allocated. <> is also the result +when you use `<,0)>>' (which has the same effect as +`<)>>'). You should always check the result of +<>; successful reallocation is not guaranteed even when +you request a smaller object. + +<> does not return a result. + +<> returns a pointer to the newly allocated space. + +<> returns the usable size. + +PORTABILITY +<>, <>, and <> are specified by the ANSI C +standard, but other conforming implementations of <> may +behave differently when <[nbytes]> is zero. + +<> is part of SVR4. + +<> is not portable. + +Supporting OS subroutines required: <>. */ + +#include <_ansi.h> +#include +#include +#include + +#ifndef _REENT_ONLY + +_PTR +_DEFUN (malloc, (nbytes), + size_t nbytes) /* get a block */ +{ + return _malloc_r (_REENT, nbytes); +} + +void +_DEFUN (free, (aptr), + _PTR aptr) +{ + _free_r (_REENT, aptr); +} + +#endif + +#endif /* ! defined (MALLOC_PROVIDED) */ -- cgit v1.2.3