Add reallocf to the library. This function is simliar to realloc, but
when it returns NULL to indicate failure, it will also free the memory that was passed to it, if that was non-null. This does not change the semantics of realloc. A second commit will be done to commit the conversion of those places in the code that can safely use this to avoid memory leaks when confronted with low memory situations. Beaten-to-death-but-finally-approved-in: -current
This commit is contained in:
parent
aed17b8379
commit
96fb246758
@ -163,6 +163,7 @@ int radixsort __P((const unsigned char **, int, const unsigned char *,
|
||||
int sradixsort __P((const unsigned char **, int, const unsigned char *,
|
||||
unsigned));
|
||||
long random __P((void));
|
||||
void *reallocf __P((void *, size_t));
|
||||
char *realpath __P((const char *, char resolved_path[]));
|
||||
char *setstate __P((char *));
|
||||
void srandom __P((unsigned long));
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
.Fn malloc ,
|
||||
.Fn realloc
|
||||
or
|
||||
.Fn realloc
|
||||
.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 .
|
||||
|
12
lib/libc/stdlib/reallocf.c
Normal file
12
lib/libc/stdlib/reallocf.c
Normal file
@ -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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user