Chapter 2. Allocating Memory

image with no caption

In the previous chapter we used malloc and free for the allocation and release of memory. The FreeBSD kernel, however, contains a richer set of memory allocation primitives. In this chapter we’ll look at the stock kernel memory management routines. This includes describing malloc and free in more detail and introducing the malloc_type structure. We’ll finish this chapter by describing the contiguous physical memory management routines.

Memory Management Routines

The FreeBSD kernel provides four functions for non-pageable memory allocation and release: malloc, free, realloc, and reallocf. These functions can handle requests of arbitrary size or alignment, and they are the preferred way to allocate kernel memory.

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

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

void
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 size bytes of memory in kernel space. If successful, a kernel virtual address is returned; otherwise, NULL is returned.

The free function releases the memory at addr—that was previously allocated by malloc—for reuse. Note that free doesn’t clear this memory, which means that you should explicitly zero any memory whose contents you need to keep private. If addr is NULL, then free does nothing.

Note

If INVARIANTS is enabled, then free will stuff any released memory with 0xdeadc0de.

Thus, if you get a page fault panic and the faulting address is around 0xdeadc0de, this can be a sign that you’re using freed memory.[1]

The realloc function changes the size of the memory at addr to size bytes. If successful, a kernel virtual address is returned; otherwise, NULL is returned, and the memory is left alone. Note that the returned address may differ from addr, because when the size changes, the memory may be relocated to acquire or provide additional room. Interestingly, this implies that you should not have any pointers into the memory at addr when calling realloc. If addr is NULL, then realloc behaves identically to malloc.

The reallocf function is identical to realloc except that on failure it releases the memory at addr.

The malloc, realloc, and reallocf functions provide a flags argument to further qualify their operational characteristics. Valid values for this argument are shown in Table 2-1.

Table 2-1. malloc, realloc, and reallocf Symbolic Constants

Constant

Description

M_ZERO

Causes the allocated memory to be set to zero

M_NOWAIT

Causes malloc, realloc, and reallocf to return NULL if the allocation cannot be immediately fulfilled due to resource shortage; M_NOWAIT is required when running in an interrupt context

M_WAITOK

Indicates that it is okay to wait for resources; if the allocation cannot be immediately fulfilled, the current process is put to sleep to wait for resources to become available; when M_WAITOK is specified, malloc, realloc, and reallocf cannot return NULL

The flags argument must include either M_NOWAIT or M_WAITOK.



[1] INVARIANTS is a kernel debugging option. For more on INVARIANTS, see /sys/conf/NOTES.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.35.122