diff options
author | 2014-07-10 19:33:16 +0000 | |
---|---|---|
committer | 2014-07-10 19:33:16 +0000 | |
commit | 2bb3378bd4d897f43207ad45da31601196e589d7 (patch) | |
tree | a41b858d3f2ef69c7e3542e752e99545be349214 | |
parent | Put boot signature always at end of 512 byte sector, even on disks with (diff) | |
download | wireguard-openbsd-2bb3378bd4d897f43207ad45da31601196e589d7.tar.xz wireguard-openbsd-2bb3378bd4d897f43207ad45da31601196e589d7.zip |
Add mallocarray(9)
While here, change malloc(9)'s size argument from "unsigned long" to
"size_t".
ok tedu
-rw-r--r-- | share/man/man9/Makefile | 4 | ||||
-rw-r--r-- | share/man/man9/malloc.9 | 23 | ||||
-rw-r--r-- | sys/kern/kern_malloc.c | 38 | ||||
-rw-r--r-- | sys/sys/malloc.h | 11 |
4 files changed, 62 insertions, 14 deletions
diff --git a/share/man/man9/Makefile b/share/man/man9/Makefile index c880323e6d8..e2a761d1faf 100644 --- a/share/man/man9/Makefile +++ b/share/man/man9/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.213 2014/07/09 18:00:09 jmc Exp $ +# $OpenBSD: Makefile,v 1.214 2014/07/10 19:33:16 matthew Exp $ # $NetBSD: Makefile,v 1.4 1996/01/09 03:23:01 thorpej Exp $ # Makefile for section 9 (kernel function and variable) manual pages. @@ -235,7 +235,7 @@ MLINKS+=ktrace.9 ktrcsw.9 ktrace.9 ktremul.9 ktrace.9 ktrgenio.9 \ ktrace.9 ktrsysret.9 ktrace.9 KTRPOINT.9 MLINKS+=lock.9 lockinit.9 lock.9 lockmgr.9 lock.9 lockstatus.9 MLINKS+=log.9 addlog.9 -MLINKS+=malloc.9 free.9 +MLINKS+=malloc.9 mallocarray.9 malloc.9 free.9 MLINKS+=membar_sync.9 membar_enter.9 membar_sync.9 membar_exit.9 \ membar_sync.9 membar_producer.9 membar_sync.9 membar_consumer.9 MLINKS+=mbuf.9 m_copym2.9 mbuf.9 m_copym.9 mbuf.9 m_free.9 mbuf.9 MFREE.9 \ diff --git a/share/man/man9/malloc.9 b/share/man/man9/malloc.9 index 78955677e2b..90737b7b43c 100644 --- a/share/man/man9/malloc.9 +++ b/share/man/man9/malloc.9 @@ -1,4 +1,4 @@ -.\" $OpenBSD: malloc.9,v 1.52 2014/04/03 04:10:34 lteo Exp $ +.\" $OpenBSD: malloc.9,v 1.53 2014/07/10 19:33:16 matthew Exp $ .\" $NetBSD: malloc.9,v 1.2 1996/10/30 05:29:54 lukem Exp $ .\" .\" Copyright (c) 1996 The NetBSD Foundation, Inc. @@ -28,18 +28,21 @@ .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGE. .\" -.Dd $Mdocdate: April 3 2014 $ +.Dd $Mdocdate: July 10 2014 $ .Dt MALLOC 9 .Os .Sh NAME .Nm malloc , +.Nm mallocarray , .Nm free .Nd kernel memory allocator .Sh SYNOPSIS .In sys/types.h .In sys/malloc.h .Ft void * -.Fn malloc "unsigned long size" "int type" "int flags" +.Fn malloc "size_t size" "int type" "int flags" +.Ft void * +.Fn malloc "size_t nmemb" "size_t size" "int type" "int flags" .Ft void .Fn free "void *addr" "int type" .Sh DESCRIPTION @@ -48,8 +51,17 @@ The function allocates uninitialized memory in kernel address space for an object whose size is specified by .Fa size . +The +.Fn mallocarray +function is the same as +.Fn malloc , +except it allocates space for an array of +.Fa nmemb +objects and checks for arithmetic overflow. +.Pp +The .Fn free -releases memory at address +function releases memory at address .Fa addr that was previously allocated by .Fn malloc @@ -60,7 +72,8 @@ is a null pointer, no action occurs. .Pp The .Fa flags -argument further qualifies malloc's +argument further qualifies +.Fn malloc Ns 's operational characteristics as follows: .Bl -tag -width xxx -offset indent .It Dv M_WAITOK diff --git a/sys/kern/kern_malloc.c b/sys/kern/kern_malloc.c index c519348d709..f071962cd9a 100644 --- a/sys/kern/kern_malloc.c +++ b/sys/kern/kern_malloc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_malloc.c,v 1.110 2014/07/10 10:53:45 deraadt Exp $ */ +/* $OpenBSD: kern_malloc.c,v 1.111 2014/07/10 19:33:16 matthew Exp $ */ /* $NetBSD: kern_malloc.c,v 1.15.4.2 1996/06/13 17:10:56 cgd Exp $ */ /* @@ -155,7 +155,7 @@ struct timeval malloc_lasterr; * Allocate a block of memory */ void * -malloc(unsigned long size, int type, int flags) +malloc(size_t size, int type, int flags) { struct kmembuckets *kbp; struct kmemusage *kup; @@ -697,3 +697,37 @@ malloc_printit( #endif } #endif /* DDB */ + +/* + * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX + * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW + */ +#define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4)) + +void * +mallocarray(size_t nmemb, size_t size, int type, int flags) +{ + if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && + nmemb > 0 && SIZE_MAX / nmemb < size) { + if (flags & M_CANFAIL) + return (NULL); + panic("mallocarray overflow: %zu * %zu", nmemb, size); + } + return (malloc(size * nmemb, type, flags)); +} diff --git a/sys/sys/malloc.h b/sys/sys/malloc.h index b04b6f712e4..717831266e6 100644 --- a/sys/sys/malloc.h +++ b/sys/sys/malloc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: malloc.h,v 1.108 2014/05/19 14:30:03 tedu Exp $ */ +/* $OpenBSD: malloc.h,v 1.109 2014/07/10 19:33:16 matthew Exp $ */ /* $NetBSD: malloc.h,v 1.39 1998/07/12 19:52:01 augustss Exp $ */ /* @@ -391,10 +391,11 @@ extern struct kmemusage *kmemusage; extern char *kmembase; extern struct kmembuckets bucket[]; -extern void *malloc(unsigned long size, int type, int flags); -extern void free(void *addr, int type); -extern int sysctl_malloc(int *, u_int, void *, size_t *, void *, size_t, - struct proc *); +void *malloc(size_t, int, int); +void *mallocarray(size_t, size_t, int, int); +void free(void *, int); +int sysctl_malloc(int *, u_int, void *, size_t *, void *, size_t, + struct proc *); size_t malloc_roundup(size_t); void malloc_printit(int (*)(const char *, ...)); |