diff options
| author | Warner Losh <imp@FreeBSD.org> | 1998-09-14 20:34:34 +0000 | 
|---|---|---|
| committer | Warner Losh <imp@FreeBSD.org> | 1998-09-14 20:34:34 +0000 | 
| commit | 94ad719cf47118069c9d840583e10ee17b5dfe50 (patch) | |
| tree | 1cb89d88bd80c750c1346595db3af7715d4d5c26 /lib/libc | |
| parent | d6e5b04f9960ebfad47eb5442fb7acd671a0c58d (diff) | |
Notes
Diffstat (limited to 'lib/libc')
| -rw-r--r-- | lib/libc/stdlib/Makefile.inc | 6 | ||||
| -rw-r--r-- | lib/libc/stdlib/malloc.3 | 37 | ||||
| -rw-r--r-- | lib/libc/stdlib/reallocf.c | 12 | 
3 files changed, 42 insertions, 13 deletions
| diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc index 0d7ae27093ad..5ba11f905868 100644 --- a/lib/libc/stdlib/Makefile.inc +++ b/lib/libc/stdlib/Makefile.inc @@ -1,5 +1,5 @@  #	from @(#)Makefile.inc	8.3 (Berkeley) 2/4/95 -#	$Id: Makefile.inc,v 1.14 1998/02/20 08:41:46 jb Exp $ +#	$Id: Makefile.inc,v 1.15 1998/05/08 05:41:56 jb Exp $  # machine-independent stdlib sources  .PATH: ${.CURDIR}/../libc/${MACHINE_ARCH}/stdlib ${.CURDIR}/../libc/stdlib @@ -8,7 +8,7 @@ MISRCS+=abort.c abs.c atexit.c atof.c atoi.c atol.c bsearch.c calloc.c div.c \  	exit.c getenv.c getopt.c getsubopt.c heapsort.c labs.c ldiv.c \  	malloc.c merge.c putenv.c qsort.c radixsort.c rand.c random.c \  	realpath.c setenv.c strhash.c strtol.c strtoq.c strtoul.c \ -	strtouq.c system.c +	strtouq.c system.c reallocf.c  .if ${MACHINE_ARCH} == "alpha"  #  XXX Temporary until the assumption that a long is 32-bits is resolved @@ -35,5 +35,5 @@ MLINKS+=random.3 initstate.3 random.3 setstate.3 random.3 srandom.3 \  	random.3 srandomdev.3  MLINKS+=strtol.3 strtoq.3  MLINKS+=strtoul.3 strtouq.3 -MLINKS+=malloc.3 calloc.3 malloc.3 free.3 malloc.3 realloc.3 +MLINKS+=malloc.3 calloc.3 malloc.3 free.3 malloc.3 realloc.3 malloc.3 reallocf.3  .endif diff --git a/lib/libc/stdlib/malloc.3 b/lib/libc/stdlib/malloc.3 index a7d3240fd735..bb3d08c45b81 100644 --- a/lib/libc/stdlib/malloc.3 +++ b/lib/libc/stdlib/malloc.3 @@ -34,13 +34,13 @@  .\" SUCH DAMAGE.  .\"  .\"     @(#)malloc.3	8.1 (Berkeley) 6/4/93 -.\"     $Id: malloc.3,v 1.14 1997/08/27 06:40:34 phk Exp $ +.\"     $Id: malloc.3,v 1.15 1997/09/18 06:51:22 charnier Exp $  .\"  .Dd August 27, 1996  .Dt MALLOC 3  .Os FreeBSD 2  .Sh NAME -.Nm malloc, calloc, realloc, free +.Nm malloc, calloc, realloc, free, reallocf  .Nd general purpose memory allocation functions  .Sh SYNOPSIS  .Fd #include <stdlib.h> @@ -54,6 +54,8 @@  .Fn free "void *ptr"  .Ft char *  .Va malloc_options; +.Ft void * +.Fn reallocf "void *ptr" "size_t size"  .Sh DESCRIPTION  The  .Fn malloc @@ -109,6 +111,14 @@ function behaves identically to  for the specified size.  .Pp  The +.Fn reallocf +function call is identical to the realloc function call, except that it +will free the passed pointer when the requested memory cannot be allocated. +This is a FreeBSD +specific API designed to ease the problems with traditional coding styles +for realloc causing memory leaks in libraries. +.Pp +The  .Fn free  function causes the allocated memory referenced by  .Fa ptr @@ -141,13 +151,15 @@ The process will call  in these cases.  .It J  Each byte of new memory allocated by -.Fn malloc -or +.Fn malloc ,  .Fn realloc +or +.Fn freealloc  as well as all memory returned by -.Fn free -or  +.Fn free ,  .Fn realloc +or +.Fn reallocf  will be initialized to 0xd0.  This options also sets the  .Dq R @@ -158,9 +170,11 @@ Pass a hint to the kernel about pages unused by the allocation functions.  This will help performance if the system is paging excessively.  This  option is on by default.  .It R -Cause the +Causes the  .Fn realloc -function to always reallocate memory even if the initial allocation was +and +.Fn reallocf +functions to always reallocate memory even if the initial allocation was  sufficiently large.  This can substantially aid in compacting memory.  .It U @@ -247,7 +261,9 @@ a NULL pointer is returned.  .Pp  The  .Fn realloc -function returns a pointer, possibly identical to +and +.Fn reallocf +functions return a pointer, possibly identical to  .Fa ptr ,  to the allocated memory  if successful; otherwise a NULL pointer is returned, in which case the @@ -421,4 +437,5 @@ The present allocation implementation started out as a filesystem for a  drum attached to a 20bit binary challenged computer which was built   with discrete germanium transistors.  It has since graduated to   handle primary storage rather than secondary. -It first appeared in its new shape and ability in FreeBSD release 2.2. +It first appeared in its new shape and ability in +.Fx 2.2 . diff --git a/lib/libc/stdlib/reallocf.c b/lib/libc/stdlib/reallocf.c new file mode 100644 index 000000000000..728acc9af579 --- /dev/null +++ b/lib/libc/stdlib/reallocf.c @@ -0,0 +1,12 @@ +#include <stdlib.h> + +void * +reallocf(void *ptr, size_t size) +{ +    void *nptr; + +    nptr = realloc(ptr, size); +    if (!nptr && ptr) +        free(ptr); +    return (nptr); +} | 
