Copyright 2014 Nexenta Systems, Inc. All rights reserved.
Copyright 2022 Joyent, Inc.
Copyright 1989 AT&T
Copyright (c) 2006, Sun Microsystems, Inc., All Rights Reserved
The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
#include <sys/types.h> #include <sys/kmem.h> void *kmem_alloc(size_t size, int flag);
void *kmem_zalloc(size_t size, int flag);
void kmem_free(void *buf, size_t size);
Number of bytes to allocate.
Determines whether caller can sleep for memory. Possible flags are KM_SLEEP to allow sleeping until memory is available, KM_NOSLEEP to return NULL if memory is not available even after some reclamation attempts, and KM_NOSLEEP_LAZY to return NULL without reclamation attempts. KM_NOSLEEP_LAZY is actually two flags combined: (KM_NOSLEEP | KM_NORMALPRI), the latter flag indicating not to attempt reclamation before giving up and returning NULL. If any mention of KM_NOSLEEP appears in this man page by itself, it applies equally to KM_NOSLEEP_LAZY as well.
Pointer to allocated memory.
The kmem_zalloc() function is like kmem_alloc() but returns zero-filled memory.
The kmem_free() function frees previously allocated kernel memory. The buffer address and size must exactly match the original allocation. Memory cannot be returned piecemeal.
Writing Device Drivers
Excessive use of kernel memory is likely to affect overall system performance. Overcommitment of kernel memory will cause the system to hang or panic.
Misuse of the kernel memory allocator, such as writing past the end of a buffer, using a buffer after freeing it, freeing a buffer twice, or freeing a null or invalid pointer, will corrupt the kernel heap and may cause the system to corrupt data or panic.
The initial contents of memory allocated using kmem_alloc() are random garbage. This random garbage may include secure kernel data. Therefore, uninitialized kernel memory should be handled carefully. For example, never copyout(9F) a potentially uninitialized buffer.