General Info

Interface: MemManager
Files: memmanager.h
Last change: 08/06/2003
Author: Luiz Henrique Shigunov
Description
Structures
System Functions

0x00 - Alloc - Allocate system memory
0x01 - AllocObj - Allocate an object from system memory
0x06 - AllocUserPages - Allocate pages in user space
0x07 - AttachShMemory - Attach a shared memory
0x02 - CreateObjCache - Create an object cache
0x08 - CreateShMemory - Create a shared memory
0x03 - DestroyObjCache - Destroy an object cache
0x09 - DestroyShMemory - Destroy a shared memory

0x0a - DetachShMemory - Detach a shared memory
0x04 - Free - Free system memory
0x05 - FreeObj - Free an object
0x0b - FreeUserPages - Free pages in user space
0x0c - MapPageAtUserPage - Map a physical page in user space
0x0d - ProtectShMemory - Change access permissions of a shared memory
0x0e - SetUserPagesProt - Change access permissions of pages in user space
User Functions

0x0f - UAllocUserPages - Allocate pages in user space
0x10 - UFreeUserPages - Free pages in user space

0x11 - USetUserPagesProt - Change access permissions of pages in user space

Description

This page describes the MemManager interface which provides functions related with memory management.

The module that implements this interface must generate events defined in MemMEvent interface.

Structures

typedef struct {
    int (*readpage)(void *arg, void *page, unsigned int offset);
} MemManager_ShMemFunctions;

Function readpage, if different than NULL, is called whenever a memory page must be read. page is the address of the page to be read and offset the offset of this page in the shared memory. It must return 0 for success and other value for error. In case of error the memory manager exception will be generated.

System Functions

These functions are exclusive for system modules.

0x00 - Alloc

Syntax

void *MemManager_Alloc(unsigned int size, int prop);

Properties

Description

This function allocates size bytes of memory with properties prop.

prop must be 0 or the sum of:

Return value

Use the macros IS_PTR_ERROR and PTR_TO_ERROR to know whether an error occurred and to get the error.

0x01 - AllocObj

Syntax

void *MemManager_AllocObj(MemManager_Cache *cache);

Properties

Description

This function allocates an object from cache.

Return value

Use the macros IS_PTR_ERROR and PTR_TO_ERROR to know whether an error occurred and to get the error.

0x06 - AllocUserPages

Syntax

void *MemManager_AllocUserPages(void *start, unsigned int size, unsigned int align, int prop, TaskManager_Task *task);

Properties

Description

This function allocates size bytes, which must be different than zero, of memory from user memory space to task. Address start is only a hint and, usually, is 0. If start is used as hint, the search for a place to allocate starts at start and goes towards downwards addresses.

Allocated memory pages are freed when the task is destroyed.

If prop doesn't has MemManager_FIXED_ADDR, pages will be allocated at an address aligned in align. If 0, will be aligned in a memory page.

start, size and align must be a multiple of a memory page size (4096 bytes).

If task is NULL, will be allocated in caller task.

prop must be the sum of:

Return value

Use the macros IS_PTR_ERROR and PTR_TO_ERROR to know whether an error occurred and to get the error.

0x07 - AttachShMemory

Syntax

void *MemManager_AttachShMemory(MemManager_ShMem *shmem, void *start, unsigned int offset, unsigned int size, int prop, TaskManager_Task *task);

Properties

Description

This function attachs the shared memory shmem at address start from task. Address start is only a hint and, usually, is 0. If start is used as hint, the search for a place to attach starts at start and goes towards downwards addresses.

If task is NULL, will be attached in caller task.

size bytes, which must be different than zero, starting at offset bytes inside the shared memory will be attached.

start, size and offset must be a multiple of a memory page size (4096 bytes).

prop must be the sum of:

Return value

Use the macros IS_PTR_ERROR and PTR_TO_ERROR to know whether an error occurred and to get the error.

0x02 - CreateObjCache

Syntax

MemManager_Cache *MemManager_CreateObjCache(unsigned int size, unsigned int align, int prop, void (*ctor)(void *obj, unsigned int size), void (*dtor)(void *obj, unsigned int size));

Properties

Description

This function creates an object cache. An object is a chunk of memory that holds its initial state between calls to FreeObj and AllocObj. So, AllocObj gets an object in its initial state and FreeObj returns to cache an object in its initial state.

Doing so, we don't need to initialize an object when we call AllocObj.

size is the size of each object in the cache, align is the alignment, in power of 2 (1=2 bytes, 2=4 bytes, 3=8 bytes, ...), necessary for each object (0 for the best alignment).

ctor and dtor are functions that create and destroy objects. They receive an object obj and the size of the object in size. These functions can't allocate/destroy objects of the same type been created or destroyed. These functions can be NULL.

prop must be 0 or the sum of:

Return value

Use the macros IS_PTR_ERROR and PTR_TO_ERROR to know whether an error occurred and to get the error.

0x08 - CreateShMemory

Syntax

MemManager_ShMem *MemManager_CreateShMemory(unsigned int size, int prop, const MemManager_ShMemFunctions *functions, void *arg);

Properties

Description

This function creates a shared memory in user memory space with size bytes. size must be different than zero and multiple of a memory page size (4096 bytes).

With a shared memory, allocated memory pages are only freed when the shared memory is destroyed, that is, a shared memory can exist even if no task has it attached.

functions is an array of functions that are called in some cases and arg is an argument which is passed to these functions. functions can't be NULL and must exist while the shared memory exists.

prop must be 0 or the sum of:

Return value

Use the macros IS_PTR_ERROR and PTR_TO_ERROR to know whether an error occurred and to get the error.

0x03 - DestroyObjCache

Syntax

int MemManager_DestroyObjCache(MemManager_Cache *cache);

Properties

Description

This function destroys the object cache cache.

All objects of the cache must have been freed by FreeObj.

Return value

0x09 - DestroyShMemory

Syntax

int MemManager_DestroyShMemory(MemManager_ShMem *shmem);

Properties

Description

This function destroys the shared memory shmem.

All allocated memory pages will be freed.

No task can have shmem attached.

Return value

0x0a - DetachShMemory

Syntax

int MemManager_DetachShMemory(MemManager_ShMem *shmem, void *start, unsigned int size, int prop, TaskManager_Task *task);

Properties

Description

This function detaches shmem starting at address start with size bytes, which must be different than zero, from task.

start and size must be a multiple of a memory page size (4096 bytes).

If task is NULL will detach from caller task.

prop must be 0 or the sum of:

Return value

0x04 - Free

Syntax

int MemManager_Free(void *ptr);

Properties

Description

This function frees system memory ptr.

Return value

0x05 - FreeObj

Syntax

int MemManager_FreeObj(MemManager_Cache *cache, void *obj);

Properties

Description

This function frees obj from cache.

Object must be in its initial state, that is, the same state after been constructed.

Return value

0x0b - FreeUserPages

Syntax

int MemManager_FreeUserPages(void *start, unsigned int size, int prop, TaskManager_Task *task);

Properties

Description

This function frees size bytes, which must be different than zero, of memory pages starting at start from task.

start and size must be a multiple of a memory page size (4096 bytes).

If task is NULL, caller task will be used.

prop must be 0 or the sum of:

Return value

0x0c - MapPageAtUserPage

Syntax

int MemManager_MapPageAtUserPage(void *logPage, unsigned int physPage, TaskManager_Task *task);

Properties

Description

This function maps the physical page physPage to address logPage from task.

physPage must be the address of the physical page (0x0, 0x1000, 0x2000, 0x24000, etc).

logPage must be inside of a memory area allocated with AllocUserPages and can't have a physical page already maped.

If task is NULL caller task will be used.

Return value

0x0d - ProtectShMemory

Syntax

int MemManager_ProtectShMemory(MemManager_ShMem *shmem, const void *start, unsigned int size, int prop, TaskManager_Task *task);

Properties

Description

This function changes access permissions of the memory pages attached from shared memory shmem starting at address start with size bytes, which must be different than zero, from task.

If task is NULL caller task will be used.

prop must be the sum of:

Return value

0x0e - SetUserPagesProt

Syntax

int MemManager_SetUserPagesProt(const void *start, unsigned int size, int prop, TaskManager_Task *task);

Properties

Description

This function changes access permissions of the memory pages starting at address start with size bytes, which must be different than zero, from task.

start and size must be a multiple of a memory page size (4096 bytes).

If task is NULL caller task will be used.

prop must be the sum of:

Return value

User Functions

These functions were designed for user modules.

0x0f - UAllocUserPages

Syntax

void *MemManager_UAllocUserPages(void *start, unsigned int size, unsigned int align, int prop);

Properties

User modules

Description

This function allocates size bytes, which must be different than zero, of memory starting at address start.

If prop doesn't has MemManager_FIXED_ADDR, an address aligned at align will be used. If 0, will be aligned in a memory page.

start, size and align must be a multiple of a memory page size (4096 bytes).

If task is NULL, will be allocated in caller task.

prop must be the sum of:

Return value

Use the macros IS_PTR_ERROR and PTR_TO_ERROR to know whether an error occurred and to get the error.

0x10 - UFreeUserPages

Syntax

int MemManager_UFreeUserPages(void *start, unsigned int size);

Properties

User modules

Description

This function frees size bytes, which must be different than zero, of memory pages starting at start.

This area must have been allocated with UAllocUserPages.

start and size must be a multiple of a memory page size (4096 bytes).

Return value

0x11 - USetUserPagesProt

Syntax

int MemManager_USetUserPagesProt(const void *start, unsigned int size, int prop);

Properties

User modules

Description

This function changes access permissions of the memory pages starting at address start with size bytes, which must be different than zero.

start and size must be a multiple of a memory page size (4096 bytes).

prop must be the sum of:

Return value