139322Simp/*-
239322Simp * Copyright (c) 1998, M. Warner Losh <imp@freebsd.org>
339322Simp * All rights reserved.
439322Simp *
539322Simp * Redistribution and use in source and binary forms, with or without
639322Simp * modification, are permitted provided that the following conditions
739322Simp * are met:
839322Simp * 1. Redistributions of source code must retain the above copyright
939322Simp *    notice, this list of conditions and the following disclaimer.
1039322Simp * 2. Redistributions in binary form must reproduce the above copyright
1139322Simp *    notice, this list of conditions and the following disclaimer in the
1239322Simp *    documentation and/or other materials provided with the distribution.
1339322Simp *
1439322Simp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1539322Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1639322Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1739322Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1839322Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1939322Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2039322Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2139322Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2239322Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2339322Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2439322Simp * SUCH DAMAGE.
2539322Simp */
2692986Sobrien
2792986Sobrien#include <sys/cdefs.h>
2892986Sobrien__FBSDID("$FreeBSD$");
2992986Sobrien
3039191Simp#include <stdlib.h>
3139191Simp
3239191Simpvoid *
3339191Simpreallocf(void *ptr, size_t size)
3439191Simp{
3539322Simp	void *nptr;
3639191Simp
3739322Simp	nptr = realloc(ptr, size);
38204636Sjh
39204636Sjh	/*
40204636Sjh	 * When the System V compatibility option (malloc "V" flag) is
41204636Sjh	 * in effect, realloc(ptr, 0) frees the memory and returns NULL.
42204636Sjh	 * So, to avoid double free, call free() only when size != 0.
43204636Sjh	 * realloc(ptr, 0) can't fail when ptr != NULL.
44204636Sjh	 */
45204636Sjh	if (!nptr && ptr && size != 0)
4639322Simp		free(ptr);
4739322Simp	return (nptr);
4839191Simp}
49