Documentation |
Documentation.Development-Manual-MM HistoryHide minor edits - Show changes to markup May 31, 2024, at 09:52 AM
by
- Added lines 1-100:
Documentation -> Development Manual -> Memory ManagementThis page has been visited 361 times. (:title OpenSIPS Development - Memory Management:)
(:toc-float Table of Content:) OpenSIPS has its own memory allocator. This provides some important advantages over the system memory allocator:
Private (PKG) MemoryPrivate memory is only specific to a single OpenSIPS process. Since it has no visibility outside the current process, no locking mechanisms are required while managing such memory, hence allocating private memory will be faster than allocating shared memory.
Common use case: before forking OpenSIPS processes, the developer stores some static variables in the private memory of the main process. After forking, each child process will have its own clone of the private memory chunk (same memory address pointer!).
(:source lang=C -link -getcode :) /* Parameters : size - size in bytes of the request private memory Returns : the actual allocated buffer, or NULL is case of error
void *pkg_malloc(unsigned int size); /* Parameters : buf - the buffer to be freed
void pkg_free(void *buf) /* Parameters : buf - buffer that we want to reallocate size - the new desired buffer size Returns : the new buffer address if reallocation is successful, or NULL in case of error. Note that pkg_realloc(NULL,size) is equivalent to pkg_malloc(size)
void *pkg_realloc(void *buf, unsigned int size); (:sourceend:) Shared (SHM) MemoryShared memory can be accessible from all OpenSIPS processes. Thus, generally speaking, all write access to a shared memory buffer should be guarded by some form of synchronization mechanism in order to ensure consistency. mem/shm_mem.h exposes all the shared memory related functions : (:source lang=C -link -getcode :) /* Parameters : size - size in bytes of the request shared memory Returns : the actual allocated buffer, or NULL is case of error
void *shm_malloc(unsigned int size); /* Parameters : buf - the buffer to be freed
void shm_free(void *buf) /* Parameters : buf - buffer that we want to reallocate size - the new desired buffer size Returns : the new buffer address if reallocation is successful, or NULL in case of error. Note that shm_realloc(NULL,size) is equivalent to shm_malloc(size)
void *shm_realloc(void *buf, unsigned int size); (:sourceend:) Memory AllocatorsTo find out more about the strengths and weaknesses of each memory allocator, be sure to read this comprehensive blog post |