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 |
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.
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.
These functions are exclusive for system modules.
void *MemManager_Alloc(unsigned int size, int prop);
This function allocates size bytes of memory with properties prop.
prop must be 0 or the sum of:
Use the macros IS_PTR_ERROR and PTR_TO_ERROR to know whether an error occurred and to get the error.
void *MemManager_AllocObj(MemManager_Cache *cache);
This function allocates an object from cache.
Use the macros IS_PTR_ERROR and PTR_TO_ERROR to know whether an error occurred and to get the error.
void *MemManager_AllocUserPages(void *start, unsigned int size, unsigned int align, int prop, TaskManager_Task *task);
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:
Use the macros IS_PTR_ERROR and PTR_TO_ERROR to know whether an error occurred and to get the error.
void *MemManager_AttachShMemory(MemManager_ShMem *shmem, void *start, unsigned int offset, unsigned int size, int prop, TaskManager_Task *task);
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:
Use the macros IS_PTR_ERROR and PTR_TO_ERROR to know whether an error occurred and to get the error.
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));
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:
Use the macros IS_PTR_ERROR and PTR_TO_ERROR to know whether an error occurred and to get the error.
MemManager_ShMem *MemManager_CreateShMemory(unsigned int size, int prop, const MemManager_ShMemFunctions *functions, void *arg);
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:
Use the macros IS_PTR_ERROR and PTR_TO_ERROR to know whether an error occurred and to get the error.
int MemManager_DestroyObjCache(MemManager_Cache *cache);
This function destroys the object cache cache.
All objects of the cache must have been freed by FreeObj.
int MemManager_DestroyShMemory(MemManager_ShMem *shmem);
This function destroys the shared memory shmem.
All allocated memory pages will be freed.
No task can have shmem attached.
int MemManager_DetachShMemory(MemManager_ShMem *shmem, void *start, unsigned int size, int prop, TaskManager_Task *task);
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:
int MemManager_Free(void *ptr);
This function frees system memory ptr.
int MemManager_FreeObj(MemManager_Cache *cache, void *obj);
This function frees obj from cache.
Object must be in its initial state, that is, the same state after been constructed.
int MemManager_FreeUserPages(void *start, unsigned int size, int prop, TaskManager_Task *task);
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:
int MemManager_MapPageAtUserPage(void *logPage, unsigned int physPage, TaskManager_Task *task);
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.
int MemManager_ProtectShMemory(MemManager_ShMem *shmem, const void *start, unsigned int size, int prop, TaskManager_Task *task);
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:
int MemManager_SetUserPagesProt(const void *start, unsigned int size, int prop, TaskManager_Task *task);
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:
These functions were designed for user modules.
void *MemManager_UAllocUserPages(void *start, unsigned int size, unsigned int align, int prop);
User modules
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:
Use the macros IS_PTR_ERROR and PTR_TO_ERROR to know whether an error occurred and to get the error.
int MemManager_UFreeUserPages(void *start, unsigned int size);
User modules
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).
int MemManager_USetUserPagesProt(const void *start, unsigned int size, int prop);
User modules
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: