malloc_sysv used before initialized, reported in PR4002 by

Dmitrij Tejblum <dima@tejblum.dnttm.rssi.ru>

Various cleanup from Keith Bostic

Reinstate calloc() as a separate funtion, in its own source/object file.
leave the manpage integrated with malloc.3 and friends.  Too many things
were broken in this respect.

PR:		4002
Reviewed by:	phk
Submitted by:	Dmitrij Tejblum <dima@tejblum.dnttm.rssi.ru>
Submitted by:	Keith Bostic <bostic@bostic.com>
This commit is contained in:
Poul-Henning Kamp 1997-07-01 18:39:38 +00:00
parent 5e17038f01
commit db7f90903d
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=27152
4 changed files with 88 additions and 40 deletions

View file

@ -1,10 +1,10 @@
# from @(#)Makefile.inc 8.3 (Berkeley) 2/4/95
# $Id: Makefile.inc,v 1.8 1997/05/03 03:50:04 jb Exp $
# $Id: Makefile.inc,v 1.9 1997/06/22 17:54:24 phk Exp $
# machine-independent stdlib sources
.PATH: ${.CURDIR}/../libc/${MACHINE}/stdlib ${.CURDIR}/../libc/stdlib
SRCS+= abort.c atexit.c atof.c atoi.c atol.c bsearch.c div.c \
SRCS+= abort.c atexit.c atof.c atoi.c atol.c bsearch.c calloc.c div.c \
exit.c getenv.c getopt.c getsubopt.c strhash.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 strtod.c strtol.c strtoq.c strtoul.c \

52
lib/libc/stdlib/calloc.c Normal file
View file

@ -0,0 +1,52 @@
/*-
* Copyright (c) 1990, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)calloc.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */
#include <stdlib.h>
#include <string.h>
void *
calloc(num, size)
size_t num;
register size_t size;
{
register void *p;
size *= num;
if ( (p = malloc(size)) )
bzero(p, size);
return(p);
}

View file

@ -34,7 +34,7 @@
.\" SUCH DAMAGE.
.\"
.\" @(#)malloc.3 8.1 (Berkeley) 6/4/93
.\" $Id: malloc.3,v 1.11 1997/06/12 12:45:45 phk Exp $
.\" $Id: malloc.3,v 1.12 1997/06/22 17:54:27 phk Exp $
.\"
.Dd August 27, 1996
.Dt MALLOC 3
@ -119,7 +119,7 @@ is NULL, no action occurs.
.Sh TUNING
Once, when the first call is made to one of these memory allocation
routines, various flags will be set or reset, which affect the
workings of this alloction implementation.
workings of this allocation implementation.
.Pp
The ``name'' of the file referenced by the symbolic link named
.Pa /etc/malloc.conf ,
@ -142,9 +142,16 @@ in these cases.
.It J
Each byte of new memory allocated by
.Fn malloc
and
or
.Fn realloc
as well as all memory returned by
.Fn free
or
.Fn realloc
will be initialized to 0xd0.
This options also sets the
.Dq R
option.
This is intended for debugging and will impact performance negatively.
.It H
Pass a hint to the kernel about pages unused by the allocation functions.
@ -183,8 +190,11 @@ extern char *malloc_options;
malloc_options = "X";
.Ed
.It Z
Initialize all allocated memory to nul bytes, and overwrite any
surrounding memory necessary for alignment reasons with 0xd0 bytes.
This option implicitly sets the
.Dq J
and
.Dq R
options, and then zeros out the bytes that were requested.
This is intended for debugging and will impact performance negatively.
.It <
Reduce the size of the cache by a factor of two.

View file

@ -6,7 +6,7 @@
* this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
* ----------------------------------------------------------------------------
*
* $Id: malloc.c,v 1.25 1997/06/12 12:45:45 phk Exp $
* $Id: malloc.c,v 1.26 1997/06/22 17:54:27 phk Exp $
*
*/
@ -58,7 +58,7 @@
# endif
#endif /* __FreeBSD__ */
#if defined(__sparc__) || defined(sun)
#if defined(__sparc__) && defined(sun)
# define malloc_pageshirt 12U
# define malloc_minsize 16U
# define MAP_ANON (0)
@ -74,7 +74,7 @@
#if defined(__FOOCPU__) && defined(__BAROS__)
# define malloc_pageshift 12U
# define malloc_minsize 16U
#endif /* __FOORCPU__ && __BAROS__ */
#endif /* __FOOCPU__ && __BAROS__ */
/*
@ -220,10 +220,11 @@ static int malloc_zero;
/* junk fill ? */
static int malloc_junk;
#ifdef HAS_UTRACE
/* utrace ? */
static int malloc_utrace;
#ifdef HAS_UTRACE
struct ut { void *p; size_t s; void *r; };
void utrace __P((struct ut *, int));
@ -233,7 +234,7 @@ void utrace __P((struct ut *, int));
{struct ut u; u.p=a; u.s = b; u.r=c; utrace(&u, sizeof u);}
#else /* !HAS_UTRACE */
#define UTRACE(a,b,c)
#endif
#endif /* HAS_UTRACE */
/* my last break. */
static void *malloc_brk;
@ -406,8 +407,10 @@ malloc_init ()
case 'R': malloc_realloc = 1; break;
case 'j': malloc_junk = 0; break;
case 'J': malloc_junk = 1; break;
#ifdef HAS_UTRACE
case 'u': malloc_utrace = 0; break;
case 'U': malloc_utrace = 1; break;
#endif
case 'v': malloc_sysv = 0; break;
case 'V': malloc_sysv = 1; break;
case 'x': malloc_xmalloc = 0; break;
@ -433,6 +436,13 @@ malloc_init ()
if (malloc_zero)
malloc_junk=1;
/*
* If we run with junk (or implicitly from above: zero), we want to
* force realloc() to get new storage, so we can DTRT with it.
*/
if (malloc_junk)
malloc_realloc=1;
/* Allocate one page for the page directory */
page_dir = (struct pginfo **) MMAP(malloc_pagesize);
@ -1051,8 +1061,6 @@ malloc(size_t size)
{
register void *r;
if (malloc_sysv && !size)
return (0);
malloc_func = " in malloc():";
THREAD_LOCK();
if (malloc_active++) {
@ -1060,7 +1068,10 @@ malloc(size_t size)
malloc_active--;
return (0);
}
r = imalloc(size);
if (malloc_sysv && !size)
r = 0;
else
r = imalloc(size);
UTRACE(0, size, r);
malloc_active--;
THREAD_UNLOCK();
@ -1114,28 +1125,3 @@ realloc(void *ptr, size_t size)
return (r);
}
void *
calloc(size_t num, size_t size)
{
register void *r;
size *= num;
if (malloc_sysv && !size)
return (0);
malloc_func = " in calloc():";
THREAD_LOCK();
if (malloc_active++) {
wrtwarning("recursive call.\n");
malloc_active--;
return (0);
}
r = imalloc(size);
UTRACE(0, size, r);
malloc_active--;
THREAD_UNLOCK();
if (malloc_xmalloc && !r)
wrterror("out of memory.\n");
if (r)
memset(r, 0, size);
return (r);
}