Rechercher une page de manuel

Chercher une autre page de manuel:

FREE.9freebsd

Langue: en

Version: 308903 (debian - 07/07/09)

Section: 9 (Appels noyau Linux)


BSD mandoc

NAME

malloc MALLOC free FREE realloc reallocf MALLOC_DEFINE MALLOC_DECLARE - kernel memory management routines

SYNOPSIS

In sys/types.h In sys/malloc.h Ft void * Fn malloc unsigned long size struct malloc_type *type int flags Fn MALLOC space cast unsigned long size struct malloc_type *type int flags Ft void Fn free void *addr struct malloc_type *type Fn FREE void *addr struct malloc_type *type Ft void * Fn realloc void *addr unsigned long size struct malloc_type *type int flags Ft void * Fn reallocf void *addr unsigned long size struct malloc_type *type int flags Fn MALLOC_DECLARE type In sys/param.h In sys/malloc.h In sys/kernel.h Fn MALLOC_DEFINE type shortdesc longdesc

DESCRIPTION

The Fn malloc function allocates uninitialized memory in kernel address space for an object whose size is specified by Fa size .

The Fn free function releases memory at address Fa addr that was previously allocated by Fn malloc for re-use. The memory is not zeroed. If Fa addr is NULL then Fn free does nothing.

The Fn realloc function changes the size of the previously allocated memory referenced by Fa addr to Fa size bytes. The contents of the memory are unchanged up to the lesser of the new and old sizes. Note that the returned value may differ from Fa addr . If the requested memory cannot be allocated, NULL is returned and the memory referenced by Fa addr is valid and unchanged. If Fa addr is NULL the Fn realloc function behaves identically to Fn malloc for the specified size.

The Fn reallocf function is identical to Fn realloc except that it will free the passed pointer when the requested memory cannot be allocated.

The Fn MALLOC macro variant is functionally equivalent to

 (space) = (cast)malloc((u_long)(size), type, flags)
 

and the Fn FREE macro variant is equivalent to

 free((addr), type)
 

Unlike its standard C library counterpart (malloc(3) ) the kernel version takes two more arguments. The Fa flags argument further qualifies Fn malloc Ns 's operational characteristics as follows:

M_ZERO
Causes the allocated memory to be set to all zeros.
M_NOWAIT
Causes Fn malloc , Fn realloc , and Fn reallocf to return NULL if the request cannot be immediately fulfilled due to resource shortage. Note that M_NOWAIT is required when running in an interrupt context.
M_WAITOK
Indicates that it is OK to wait for resources. If the request cannot be immediately fulfilled, the current process is put to sleep to wait for resources to be released by other processes. The Fn malloc , Fn realloc , and Fn reallocf functions cannot return NULL if M_WAITOK is specified.
M_USE_RESERVE
Indicates that the system can dig into its reserve in order to obtain the requested memory. This option used to be called M_KERNEL but has been renamed to something more obvious. This option has been deprecated and is slowly being removed from the kernel, and so should not be used with any new programming.

Exactly one of either M_WAITOK or M_NOWAIT must be specified.

The Fa type argument is used to perform statistics on memory usage, and for basic sanity checks. It can be used to identify multiple allocations. The statistics can be examined by `vmstat -m'

A Fa type is defined using Vt struct malloc_type via the Fn MALLOC_DECLARE and Fn MALLOC_DEFINE macros.

 /* sys/something/foo_extern.h */
 
 MALLOC_DECLARE(M_FOOBUF);
 
 /* sys/something/foo_main.c */
 
 MALLOC_DEFINE(M_FOOBUF, "foobuffers", "Buffers to foo data into the ether");
 
 /* sys/something/foo_subr.c */
 
 ...
 MALLOC(buf, struct foo_buf *, sizeof *buf, M_FOOBUF, M_NOWAIT);
 
 

In order to use Fn MALLOC_DEFINE , one must include In sys/param.h (instead of In sys/types.h ) and In sys/kernel.h .

IMPLEMENTATION NOTES

The memory allocator allocates memory in chunks that have size a power of two for requests up to the size of a page of memory. For larger requests, one or more pages is allocated. While it should not be relied upon, this information may be useful for optimizing the efficiency of memory use.

Programmers should be careful not to confuse the malloc flags M_NOWAIT and M_WAITOK with the mbuf(9) flags M_DONTWAIT and M_TRYWAIT

CONTEXT

Fn malloc , Fn realloc and Fn reallocf may not be called from fast interrupts handlers. When called from threaded interrupts, Fa flags must contain M_NOWAIT

Fn malloc , Fn realloc and Fn reallocf may sleep when called with M_WAITOK Fn free never sleeps.

Any calls to Fn malloc (even with M_NOWAIT or Fn free when holding a vnode(9) interlock, will cause a LOR (Lock Order Reversal) due to the intertwining of VM Objects and Vnodes.

RETURN VALUES

The Fn malloc , Fn realloc , and Fn reallocf functions return a kernel virtual address that is suitably aligned for storage of any type of object, or NULL if the request could not be satisfied (implying that M_NOWAIT was set).

DIAGNOSTICS

A kernel compiled with the INVARIANTS configuration option attempts to detect memory corruption caused by such things as writing outside the allocated area and imbalanced calls to the Fn malloc and Fn free functions. Failing consistency checks will cause a panic or a system console message.

SEE ALSO

vmstat(8), contigmalloc(9), memguard(9), vnode(9)
L'homme est-il intelligent ?
Non, sinon il n'aurait pas créé Dieu.
Dieu est-il galant ?
Non, sinon il aurait créé la femme en premier.
Dieu est-il intelligent ?
Non, sinon il n'aurait pas créé l'homme