📄 malloc.c
字号:
/* 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_PROVIDEDint _dummy_malloc = 1;#else/*FUNCTION<<malloc>>, <<realloc>>, <<free>>---manage memoryINDEX mallocINDEX reallocINDEX freeINDEX _malloc_rINDEX _realloc_rINDEX _free_rANSI_SYNOPSIS #include <stdlib.h> void *malloc(size_t <[nbytes]>); void *realloc(void *<[aptr]>, size_t <[nbytes]>); void free(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]>);TRAD_SYNOPSIS #include <stdlib.h> char *malloc(<[nbytes]>) size_t <[nbytes]>; char *realloc(<[aptr]>, <[nbytes]>) char *<[aptr]>; size_t <[nbytes]>; void free(<[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]>;DESCRIPTIONThese functions manage a pool of system memory.Use <<malloc>> to request allocation of an object with at least<[nbytes]> bytes of storage available. If the space is available,<<malloc>> returns a pointer to a newly allocated block as its result.If you already have a block of storage allocated by <<malloc>>, butyou no longer need all the space allocated to it, you can make itsmaller by calling <<realloc>> with both the object pointer and thenew desired size as arguments. <<realloc>> guarantees that thecontents of the smaller object match the beginning of the original object.Similarly, if you need more space for an object, use <<realloc>> torequest the larger size; again, <<realloc>> guarantees that thebeginning of the new, larger object matches the contents of theoriginal object.When you no longer need an object originally allocated by <<malloc>>or <<realloc>> (or the related function <<calloc>>), return it to thememory storage pool by calling <<free>> with the address of the objectas the argument. You can also use <<realloc>> for this purpose bycalling it with <<0>> as the <[nbytes]> argument.The alternate functions <<_malloc_r>>, <<_realloc_r>>, and <<_free_r>>are reentrant versions. The extra argument <[reent]> is a pointer toa reentrancy structure.RETURNS<<malloc>> returns a pointer to the newly allocated space, ifsuccessful; otherwise it returns <<NULL>>. If your application needsto generate empty objects, you may use <<malloc(0)>> for this purpose.<<realloc>> returns a pointer to the new block of memory, or <<NULL>>if a new block could not be allocated. <<NULL>> is also the resultwhen you use `<<realloc(<[aptr]>,0)>>' (which has the same effect as`<<free(<[aptr]>)>>'). You should always check the result of<<realloc>>; successful reallocation is not guaranteed even whenyou request a smaller object.<<free>> does not return a result.PORTABILITY<<malloc>>, <<realloc>>, and <<free>> are specified by the ANSI Cstandard, but other conforming implementations of <<malloc>> maybehave differently when <[nbytes]> is zero.Supporting OS subroutines required: <<sbrk>>, <<write>> (if WARN_VLIMIT).*/#include <_ansi.h>#include <reent.h>#include <stdlib.h>#include "malloc.h"#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);}_PTR_DEFUN (realloc, (ap, nbytes), _PTR ap _AND size_t nbytes){ return _realloc_r (_REENT, ap, nbytes);}#endif#endif /* ! defined (MALLOC_PROVIDED) */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -