---
name: "rchunk_alloc0"
section: "3"
platform: "linux"
package: "libroy-dev"
version: "1.0.8-1"
release: "Debian 3.1 (Sarge)"
platforms: ["linux"]
url: "https://new.carta.tech/rchunk"
---

# rchunk\_alloc0(3)

Roy memory chunks

Part of [rchunk(3)](/rchunk/3.md).

## NAME

rchunk - Roy memory chunks

## SYNOPSIS

\#include \<roy.h\>

void rchunk\_free (void \**chunk*, unsigned int *size*);

void \* rchunk\_alloc (unsigned int *size*);

void \* rchunk\_alloc0 (unsigned int *size*);

## DESCRIPTION

An extremely fast memory chunk allocator.  This algorithm keeps a list of chunks of suitable sizes, and simply returns the first one off the top of the list that matches the size requested. No overhead is performed in looking up the memory, so it is very  close to a single memory dereference to allocate or free a peice of memory.

If more memory is needed by the chunk allocator, it allocates a number of chunks at a time, adding them to its internal chunk list all at once.

There is also no memory overhead for each allocated chunk.  Most malloc implementations store several bytes of meta-info for each chunk, whereas the chunk allocator does not store any.

The benifits come with a drawback however, and that is that the memory allocated via the chunk allocator is never returned to the system pool. However, the chunks are recycled of course, so after freeing a chunk, that chunk will again be available for allocation by the chunk allocator.

This allocator is recommened for areas where you must allocate and free many objects of the same size, especially small objects, and need  the speed.  It is excellent for use with hash or linked list entries  for example.

There is a maximum allocation size for RChunk, this is currently set to 8192 times the system pointer size (4 on x86).  However, I would not recommend allocating anything near that size with RChunk.

[*rchunk\_alloc*(3)](/rchunk_alloc/3.md) allocates and returns a memory chunk of *size* bytes.

[*rchunk\_alloc0*(3)](/rchunk_alloc0/3.md) allocates and returns a memory chunk of *size* bytes of zero'd memory.

[*rchunk\_free*(3)](/rchunk_free/3.md) puts the chunk *chunk* back onto the list of available chunks for allocation based on the *size* parameter.  The *size* parameter should be the same as the one used to allocate this chunk.

## MEMORY DEBUGGING

In the event that you want to do memory debugging when using rchunks, it is recommended that you comment out the definition of the define *RCHUNK\_ENABLED*  in the roy.h, and recompile roy.  This will cause the rchunk routines to use malloc/free directly allowing normal memory debugging.

## SEE ALSO

[*rchunk\_alloc*(3)](/rchunk_alloc/3.md), [*rchunk\_alloc0*(3)](/rchunk_alloc0/3.md),  [*rchunk\_free*(3)](/rchunk_free/3.md), [*roy*(3)](/roy/3.md)
