plibsys 0.0.5
|
Memory management. More...
Go to the source code of this file.
Data Structures | |
struct | PMemVTable_ |
Memory management table. More... | |
Typedefs | |
typedef struct PMemVTable_ | PMemVTable |
Memory management table. | |
Functions | |
P_LIB_API ppointer | p_malloc (psize n_bytes) |
Allocates a memory block for the specified number of bytes. | |
P_LIB_API ppointer | p_malloc0 (psize n_bytes) |
Allocates a memory block for the specified number of bytes and fills it with zeros. | |
P_LIB_API ppointer | p_realloc (ppointer mem, psize n_bytes) |
Changes the memory block size. | |
P_LIB_API void | p_free (ppointer mem) |
Frees a memory block by its pointer. | |
P_LIB_API pboolean | p_mem_set_vtable (const PMemVTable *table) |
Sets custom memory management routines. | |
P_LIB_API void | p_mem_restore_vtable (void) |
Restores system memory management routines. | |
P_LIB_API ppointer | p_mem_mmap (psize n_bytes, PError **error) |
Gets a memory mapped block from the system. | |
P_LIB_API pboolean | p_mem_munmap (ppointer mem, psize n_bytes, PError **error) |
Unmaps memory back to the system. | |
Memory management.
Usually the system routines for memory management are used: malloc(), realloc(), free() and so on. But it is highly encouraged to use a more general approach: p_malloc(), p_malloc0(), p_realloc() and p_free() family of memory management routines. It gives you several advantages:
By default p_* routines are mapped to system calls, thus only NULL-checking is additionally performed. If you want to use the custom memory allocator, then fill in PMemVTable structure and pass it to the p_mem_set_vtable(). To restore system calls back use p_mem_restore_vtable().
Be careful when using the custom memory allocator: all memory chunks allocated with the custom allocator must be freed with the same allocator. If the custom allocator was installed after the library initialization call p_libsys_init() then you must to restore the original allocator before calling p_libsys_shutdown().
Use p_mem_mmap() to allocate system memory using memory mapping and p_mem_munmap() to release the mapped memory. This type of allocated memory is not backed physically (does not consume any physical storage) by operating system. It means that every memory page within the allocated region will be committed to physical backend only when you first touch it. Until that untouched pages will be reserved for future usage. It can be useful when dealing with large memory blocks which should be filled with data on demand, i.e. custom memory allocator can request a large block first, and then it allocates chunks of memory within the block upon request.
Definition in file pmem.h.
Frees a memory block by its pointer.
mem | Pointer to the memory block to free. |
You should only call this function for the pointers which were obtained using the p_malloc(), p_malloc0() and p_realloc() routines, otherwise behavior is unpredictable.
Checks the pointer for the NULL value.
Allocates a memory block for the specified number of bytes.
n_bytes | Size of the memory block in bytes. |
Allocates a memory block for the specified number of bytes and fills it with zeros.
n_bytes | Size of the memory block in bytes. |
Gets a memory mapped block from the system.
n_bytes | Size of the memory block in bytes. | |
[out] | error | Error report object, NULL to ignore. |
Note that some systems can allocate memory only in chunks of the page size, so if n_bytes is less than the page size it will try to allocate a chunk of memory equal to the page size instead.
On most systems returned memory is mapped to the null or swap device.
Unmaps memory back to the system.
mem | Pointer to a memory block previously allocated using the p_mem_mmap() call. | |
n_bytes | Size of the memory block in bytes. | |
[out] | error | Error report object, NULL to ignore. |
P_LIB_API void p_mem_restore_vtable | ( | void | ) |
Restores system memory management routines.
The following system routines are restored: malloc(), free(), realloc().
P_LIB_API pboolean p_mem_set_vtable | ( | const PMemVTable * | table | ) |
Sets custom memory management routines.
table | Table of the memory routines to use. |
In most cases you do not need to use this function. Use it only when you know what are you doing!
Changes the memory block size.
mem | Pointer to the memory block. |
n_bytes | New size for mem block. |