---
name: "wnmem"
section: "3"
platform: "linux"
package: "libwn-dev"
version: "6.0-17"
release: "Debian 4.0 (Etch)"
platforms: ["linux"]
url: "https://new.carta.tech/wnmem"
---

# wnmem(3)

grouping memory allocator

## NAME

wn\_free, wn\_gperrfpush, wn\_gperrfpop, wn\_gpmake, wn\_gpfree, wn\_gppush, wn\_gppop, wn\_curgp, wn\_defaultgp, wn\_alloc, wn\_zalloc, wn\_realloc, wn\_zrealloc - grouping memory allocator

## SYNOPSIS

```
#include <wn/wnmem.h>

void wn_gpmake(char parms[])

void wn_gpfree(void)

void wn_gppush(wn_memgp group);

void wn_gppop(void)

wn_memgp wn_curgp(void)

wn_memgp wn_defaultgp(void)

ptr wn_alloc(int size)

ptr wn_zalloc(int size)

void wn_realloc(ptr &p,int old_size,int new_size)

void wn_zrealloc(ptr &p,int old_size,int new_size)

void wn_free(ptr p)

void wn_gperrfpush(void (*perror_func)(void));

void wn_gperrfpop(void)
```

## DESCRIPTION

This package is a memory allocator designed to replace the [malloc(3)](/malloc/3.md) package provided by UNIX.  It is designed to be faster and easier to program.

All memory comes from a "memory group", which can be declared using the type "wn\_memgp".  wnmem keeps a stack of memory groups.  The top of this stack is called the "current memory group" and can be accessed using **wn\_curgp**.  All memory is allocated from and freed to the current memory group.  The current memory group is **NULL** until a **wn\_gppush** or **wn\_gpmake** is done.

**wn\_gppush** pushes *group* onto the memory group stack.

**wn\_gppop** pops the memory group stack.

**wn\_gpmake** makes a memory group of type specified by *parms* and pushes it onto the memory group stack.  *parms* is a string  with the following syntax:

parms --\> free\_method chunk\_size

free\_method --\> "no\_free"\|"general\_free"

chunk\_size --\> \<non-negative integer\>\|\<empty\>

"free method" controls how **wn\_free** works. "no\_free" means that **wn\_free** does nothing.  "no\_free" groups run much faster and waste less space.  "general\_free" groups allow piecemeal freeing with **wn\_free**.  Note that **wn\_gpfree** works for both types of groups.  "chunk\_size" is the size of the memory chunk that wnmem requests from the system.  The default is 4000, which is a good choice unless you have many small groups. Giving 0 causes the smallest chunk possible to be requested.

Some example **wn\_gpmake** calls follow:

**wn\_gpmake**("no\_free");\
 **wn\_gpmake**("general\_free");\
 **wn\_gpmake**("no\_free 100");\
 **wn\_gpmake**("no\_free 0");

The memory group created by **wn\_gpmake** is a subgroup of the current group.  If a group is freed, all of its subgroups are freed.  If the current group is **NULL**, the memory group is a subgroup of no other group.

**wn\_gpfree** frees all of the memory in the current memory group, recursively frees its subgroups, invalidates the group, and then pops it from memory group stack.

**wn\_alloc** allocates *size* bytes from the current memory group and returns a pointer to it.

**wn\_zalloc** allocates *size* bytes from the current memory group and returns a pointer to it.  The memory is guaranteed to be null initialized.

**wn\_free** frees memory pointed to by *p* into the current memory group.  This group must be the same group that *p* was allocated from or trouble will result.  **wn\_free** only works for "general\_free" memory groups; it will do nothing for a "no\_free" group.

**wn\_realloc** changes the *size* of a block of memory referenced by *p*. *p* may be moved.

**wn\_zrealloc** does the same as **wn\_realloc** except that all extra memory allocated will be null initialized.

**wn\_curgp** returns the current memory group.

**wn\_defaultgp** returns the "default" memory group, which is a no\_free group used for various system purposes.

## DIAGNOSTICS

If memory is exhausted, the function provided to **wn\_gperrfpush** is called.  By default, this function prints a message and then crashes.

**wn\_alloc**, **wn\_zalloc**, and **wn\_free** crash if the current memory group is **NULL**.

## SEE ALSO

wnmemd *doc/papers/memory\_paper.txt*\
 *cc/low/examples.c*

## AUTHOR

Will Naylor
