Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages


home | help
MALLOC(9)              FreeBSD Kernel Developer's Manual             MALLOC(9)

     malloc, MALLOC, free, FREE -- kernel memory management routines

     #include <sys/types.h>
     #include <sys/malloc.h>

     void *
     malloc(unsigned long size, struct malloc_type *type, int flags);

     MALLOC(space, cast, unsigned long size, struct malloc_type *type,
         int flags);

     free(void *addr, struct malloc_type *type);

     FREE(void *addr, struct malloc_type *type);

     void *
     realloc(void *addr, unsigned long size, struct malloc_type *type,
         int flags);

     void *
     reallocf(void *addr, unsigned long size, struct malloc_type *type,
         int flags);

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

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

     The realloc() function changes the size of the previously allocated mem-
     ory referenced by addr to 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 addr.  If the requested memory cannot be
     allocated, NULL is returned and the memory referenced by addr is valid
     and unchanged.  If addr is NULL, the realloc() function behaves identi-
     cally to malloc() for the specified size.

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

     The MALLOC() macro variant is functionally equivalent to

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

     and the 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 flags argument further qualifies
     malloc()'s operational characteristics as follows:

             Causes malloc(), realloc(), or reallocf() to return NULL if the
             request cannot be immediately fulfilled due to resource shortage.
             Otherwise, the current process may be put to sleep to wait for
             resources to be released by other processes.  If this flag is
             set, malloc() will return NULL rather then block.  Note that
             M_WAITOK is defined to be 0, meaning that blocking operation is
             the default.  Also note that M_NOWAIT is required when running in
             an interrupt context.

             Causes malloc(), realloc(), or reallocf() to call asleep() if the
             request cannot be immediately fulfilled due to a resource short-
             age.  M_ASLEEP is not useful alone and should always be or'd with
             M_NOWAIT to allow the function to call asleep() and return NULL
             immediately.  It is expected that the caller will at some point
             call await() and then retry the allocation.  Depending on the
             routine in question, the caller may decide to propagate the tem-
             porary failure up the call chain and actually have some other
             higher level routine block on the async wait that the function

             Indicates that it is Ok to wait for resources.  It is unconve-
             niently defined as 0 so care should be taken never to compare
             against this value directly or try to AND it as a flag.  The
             default operation is to block until the memory allocation suc-
             ceeds.  malloc(), realloc(), and reallocf() can only return NULL
             if M_NOWAIT is specified.

             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.

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

     A type is defined using the malloc_type_t typedef via the

           /* sys/something/foo_extern.h */


           /* 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);

     malloc(), realloc(), and reallocf() 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).  If
     M_ASLEEP was set and the function returns NULL, it will call asleep() as
     a side effect.

     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.


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

           +o   panic: ``malloc: bogus type''
           +o   panic: ``malloc: allocation too large''
           +o   panic: ``malloc: wrong bucket''
           +o   panic: ``malloc: lost data''
           +o   panic: ``free: address 0x%x out of range''
           +o   panic: ``free: type %d out of range''
           +o   panic: ``free: unaligned addr <description of object>''
           +o   panic: ``free: item modified''
           +o   panic: ``free: multiple free[s]''
           +o   ``Data modified on freelist: <description of object>''

FreeBSD 4.10                     June 16, 1996                    FreeBSD 4.10


Want to link to this manual page? Use this URL:

home | help