NAME

kalloc, kfree - kernel memory allocation functions

SYNOPSIS

#include <sys.h>
#include <kalloc.h>

void *kalloc( int type , size_t size );
void kfree( int type , void *ptr );

DESCRIPTION

The kalloc() function is called by the kernel when it needs memory. The type argument describes what the memory is to be used for. The values for type are defined in the file kalloc.h. This is a very powerful tool particularly when using processors with significant memory restrictions on where sections can go and what buffers or items are required in each section.

size describes how much memory is required.

The kfree() function is called to return memory allocated with the kalloc() function.

Default routines are provided which just call malloc() and free().

RETURN VALUES

If there is no available memory, kalloc() returns a null pointer.

EXAMPLES

The first example is for a static system with no dynamic memory requirements of its own.

#include <sys.h>
#include <kalloc.h>

unsigned heap[MEMSIZE];
unsigned heapp;

void *kalloc(int type, int size)
{
        if(heapp + size < MEMSIZE)
        {
                void *ptr = &heap[heapp];
                heapp += size;
                return ptr;
        }
        return 0;
}

/* never called */
void kfree(int type, void *ptr) {}

In the second example, the user wants dynamic memory to come from pool 0, but stacks to come from pool 1. The pools would be created with calls to pool_create().

#include <sys.h>
#include <kalloc.h>

void *kalloc(int type, int size)
{
        if(type == KA_STACK)
                return pool_alloc(1, size);
        else
                return pool_alloc(0, size);
}

/* never called */
void kfree(int type, void *ptr)
{
        if(type == KA_STACK)
                pool_free(1, ptr);
        else
                pool_free(0, ptr);
}

Home page (Kernel)


< Copyright Rowebots Research Inc. and Multiprocessor Toolsmiths Inc. 1987-2018 >