XROAD
xroad_mem.h File Reference
#include <common/xroad_common_fwd.h>
#include <common/xroad_common_types.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
Include dependency graph for xroad_mem.h:

Go to the source code of this file.

Classes

struct  xroad_alloc_res_t
 Structure of the result of memory allocation (via mmap) More...
 

Enumerations

enum  xroad_huge_page_request_t { xroad_request_4k , xroad_prefer_2m , xroad_request_2m }
 

Functions

void * xroad_mem_malloc (xroad_mem_pool_t *pool, size_t size)
 
void * xroad_mem_calloc (xroad_mem_pool_t *pool, size_t nmemb, size_t size)
 
void * xroad_mem_realloc (xroad_mem_pool_t *pool, void *ptr, size_t size)
 
void xroad_mem_free (xroad_mem_pool_t *pool, void *ptr)
 
xroad_errno_t xroad_get_file_page_size (int fd)
 
xroad_alloc_res_t xroad_alloc_ring (int fd, size_t min_offset, size_t min_size, bool extend)
 
xroad_alloc_res_t xroad_alloc_priv_ring (size_t size, xroad_huge_page_request_t page_type)
 
xroad_errno_t xroad_free_ring (void *ptr, size_t size)
 

Detailed Description

Enumeration Type Documentation

◆ xroad_huge_page_request_t

Enumerator
xroad_prefer_2m 

Require reglar (4K) pages.

xroad_request_2m 

Prefer 2M pages but silently fallback to 4K if no huge pages available.

Require huge (2M) pages

Function Documentation

◆ xroad_alloc_priv_ring()

xroad_alloc_res_t xroad_alloc_priv_ring ( size_t  size,
xroad_huge_page_request_t  page_type 
)

Allocate a "magic" memory ring in a temporary file (deleted immediately).

Parameters
size– size of the buffer (the whole ring is 2 * size)
page_type– required type of pages
Returns
– see xroad_alloc_res_t

◆ xroad_alloc_ring()

xroad_alloc_res_t xroad_alloc_ring ( int  fd,
size_t  min_offset,
size_t  min_size,
bool  extend 
)

Allocate a "magic" memory ring in the file. It also detects the page size by probing.

Parameters
fd– file descriptor
min_offset– minimal offset in bytes (users may have data in the file but don't know the page size))
min_size– minimal required size – could be extended based on
extend– enable or disable extending the file
Returns
– see xroad_alloc_res_t

◆ xroad_free_ring()

xroad_errno_t xroad_free_ring ( void *  ptr,
size_t  size 
)

Free the ring. Assume the buffer is mapped twice, so munmap() is called with 2 * size

Parameters
ptr– poiner to the first byte of the ring-buffer
size– size of the buffer (the buffer is mapped twice)
Returns
– retcode (XROAD_OK or XROAD_FAILED)

◆ xroad_get_file_page_size()

xroad_errno_t xroad_get_file_page_size ( int  fd)

Returns the file page size for mmap()

Returns
either xroad_reg_page_sz or xroad_huge_page_sz or XROAD_FAIL in case of error

◆ xroad_mem_calloc()

void* xroad_mem_calloc ( xroad_mem_pool_t *  pool,
size_t  nmemb,
size_t  size 
)

allocate (calloc like) space in memory pool or in glibc, and zero it

Parameters
[in]pool- memory pool. If NuLL, glibc calloc used
[in]nmemb- number of elements to allocate
[in]size- size of member to allocate
Returns
pointer to allocated memory, else failed

◆ xroad_mem_free()

void xroad_mem_free ( xroad_mem_pool_t *  pool,
void *  ptr 
)

free allocated memory in memory pool

Parameters
[in]ptr- memory to free. If NULL, glibc free used

◆ xroad_mem_malloc()

void* xroad_mem_malloc ( xroad_mem_pool_t *  pool,
size_t  size 
)

allocate (malloc like) space in memory pool or in glibc

Parameters
[in]pool- memory pool, If NULL, glibc malloc used
[in]size- size of member to allocate
Returns
pointer to allocated memory, else failed

◆ xroad_mem_realloc()

void* xroad_mem_realloc ( xroad_mem_pool_t *  pool,
void *  ptr,
size_t  size 
)

realloc memory chunk in memory pool or glibc

Parameters
[in]pool- memory pool. If NULL glibc realloc used
[in]ptr- memory to realloc
[in]size- new memory chunk size
Returns
reallocated memory chunk