---
name: "contigmalloc"
section: "9"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/contigmalloc"
---

# contigmalloc(9)

manage contiguous kernel physical memory

## NAME

**contigmalloc** – manage contiguous kernel physical memory

## SYNOPSIS

**#include \<sys/types.h\>**

**#include \<sys/malloc.h\>**

*void \**\

**contigmalloc**(*unsigned long size*, *struct malloc\_type \*type*, *int flags*, *vm\_paddr\_t low*, *vm\_paddr\_t high*, *unsigned long alignment*, *vm\_paddr\_t boundary*);

**#include \<sys/param.h\>**

**#include \<sys/domainset.h\>**

*void \**\

**contigmalloc\_domainset**(*unsigned long size*, *struct malloc\_type \*type*, *struct domainset \*ds*, *int flags*, *vm\_paddr\_t low*, *vm\_paddr\_t high*, *unsigned long alignment*, *vm\_paddr\_t boundary*);

## DESCRIPTION

The **contigmalloc**() function allocates *size* bytes of contiguous physical memory that is aligned to *alignment* bytes, and which does not cross a boundary of *boundary* bytes. If successful, the allocation will reside between physical addresses *low* and *high*. The returned pointer points to a wired kernel virtual address range of *size* bytes allocated from the kernel virtual address (KVA) map.

The **contigmalloc\_domainset**() variant allows the caller to additionally specify a [numa(4)](/numa/4.md) domain selection policy. See [domainset(9)](/domainset/9.md) for some example policies.

The *flags* parameter modifies **contigmalloc**(*Ns*, *'s*) behaviour as follows:

- M\_ZERO

  Causes the allocated physical memory to be zero filled.

- M\_NOWAIT

  Causes **contigmalloc**() to return NULL if the request cannot be immediately fulfilled due to resource shortage.

Other flags (if present) are ignored.

The **contigfree**() function is deprecated. Use [free(9)](/free/9.md) instead.

## IMPLEMENTATION NOTES

The **contigmalloc**() function does not sleep waiting for memory resources to be freed up, but instead actively reclaims pages before giving up. However, unless M\_NOWAIT is specified, it may select a page for reclamation that must first be written to backing storage, causing it to sleep.

## RETURN VALUES

The **contigmalloc**() function returns a kernel virtual address if allocation succeeds, or NULL otherwise. Note that in contrast with [malloc(9)](/malloc/9.md), **contigmalloc**() may return NULL even if M\_WAITOK is specified, if no physically contiguous range is available that meets the specified constraints.

## EXAMPLES

```
void *p;
p = contigmalloc(8192, M_DEVBUF, M_ZERO, 0, (1L << 22),
    32 * 1024, 1024 * 1024);
```

Ask for 8192 bytes of zero-filled memory residing between physical address 0 and 4194303 inclusive, aligned to a 32K boundary and not crossing a 1M address boundary.

## DIAGNOSTICS

The **contigmalloc**() function will panic if *size* is zero, or if *alignment* or *boundary* is not a power of two.

## SEE ALSO

[malloc(9)](/malloc/9.md), [memguard(9)](/memguard/9.md)
