Documentation |
Documentation -> Development Manual 3.1 -> Memory ManagementThis page has been visited 3220 times. Pages for other versions: devel 3.5 3.4 Older versions: 3.3 3.2 3.1
Table of Content (hide) OpenSIPS has its own memory allocator. This provides some important advantages over the system memory allocator:
1. 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!).
/* 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); 2. 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 : /* 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); 3. Memory AllocatorsTo find out more about the strengths and weaknesses of each memory allocator, be sure to read this comprehensive blog post |