libre  0.5.0
Data Structures | Typedefs | Functions
re_mem.h File Reference

Interface to Memory management with reference counting. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  memstat
 Memory Statistics. More...

Typedefs

typedef void( mem_destroy_h )(void *data)
 Defines the memory destructor handler, which is called when the reference of a memory object goes down to zero.

Functions

void * mem_alloc (size_t size, mem_destroy_h *dh)
 Allocate a new reference-counted memory object.
void * mem_zalloc (size_t size, mem_destroy_h *dh)
 Allocate a new reference-counted memory object.
void * mem_realloc (void *data, size_t size)
 Re-allocate a reference-counted memory object.
void * mem_reallocarray (void *ptr, size_t nmemb, size_t membsize, mem_destroy_h *dh)
 Re-allocate a reference-counted array.
void * mem_ref (void *data)
 Reference a reference-counted memory object.
void * mem_deref (void *data)
 Dereference a reference-counted memory object.
uint32_t mem_nrefs (const void *data)
 Get number of references to a reference-counted memory object.
void mem_debug (void)
 Debug all allocated memory objects.
void mem_threshold_set (ssize_t n)
 Set the memory allocation threshold.
int mem_status (struct re_printf *pf, void *unused)
 Print memory status.
int mem_get_stat (struct memstat *mstat)
 Get memory statistics.

Detailed Description

Interface to Memory management with reference counting.

Copyright (C) 2010 Creytiv.com

Definition in file re_mem.h.

Typedef Documentation

typedef void( mem_destroy_h)(void *data)

Defines the memory destructor handler, which is called when the reference of a memory object goes down to zero.

Parameters
dataPointer to memory object

Definition at line 14 of file re_mem.h.

Function Documentation

void* mem_alloc ( size_t  size,
mem_destroy_h dh 
)

Allocate a new reference-counted memory object.

Parameters
sizeSize of memory object
dhOptional destructor, called when destroyed
Returns
Pointer to allocated object

Definition at line 126 of file mem.c.

References memstat::blocks_cur, mem::dh, mem::le, list_append(), mem_lock, mem_unlock, mem::nrefs, NULL, and STAT_ALLOC.

Referenced by dns_dname_decode(), lock_alloc(), mbuf_resize(), mbuf_strdup(), mem_reallocarray(), mem_zalloc(), net_rt_list(), pl_dup(), pl_strdup(), re_vsdprintf(), rtcp_decode(), rtcp_psfb_decode(), rtcp_rtpfb_decode(), rtcp_sdes_decode(), and str_dup().

void* mem_deref ( void *  data)

Dereference a reference-counted memory object.

When the reference count is zero, the destroy handler will be called (if present) and the memory will be freed

Parameters
dataMemory object
Returns
Always NULL

Definition at line 303 of file mem.c.

References mem::dh, mem::le, list_unlink(), MAGIC_CHECK, mem_lock, mem_unlock, mem::nrefs, NULL, and STAT_DEREF.

Referenced by bfcp_listen(), bfcp_msg_decode(), bfcp_reply(), conf_alloc(), conf_alloc_buf(), dns_rr_decode(), dnsc_alloc(), dtls_accept(), dtls_connect(), dtls_listen(), hash_alloc(), http_client_alloc(), http_conn_close(), http_creply(), http_listen(), http_msg_decode(), http_request(), https_listen(), ice_alloc(), ice_list_unique(), icem_alloc(), icem_candpairs_flush(), jbuf_alloc(), list_flush(), lock_alloc(), mbuf_alloc(), mbuf_reset(), mbuf_strdup(), mod_add(), mod_load(), mqueue_alloc(), nat_filtering_alloc(), nat_genalg_alloc(), nat_hairpinning_alloc(), nat_lifetime_alloc(), nat_mapping_alloc(), net_rt_list(), re_vsdprintf(), rtcp_decode(), rtp_listen(), sdp_encode(), sdp_format_add(), sdp_format_set_params(), sdp_media_add(), sdp_media_align_formats(), sdp_media_proto_cmp(), sdp_media_radd(), sdp_media_set_alt_protos(), sdp_session_alloc(), sip_alloc(), sip_close(), sip_dialog_accept(), sip_dialog_alloc(), sip_dialog_create(), sip_dialog_fork(), sip_dialog_update(), sip_drequestf(), sip_keepalive_start(), sip_msg_decode(), sip_request(), sip_requestf(), sip_strans_reply(), sip_transp_add(), sipreg_register(), sipsess_accept(), sipsess_connect(), sipsess_info(), sipsess_listen(), sipsess_modify(), sipsess_set_close_headers(), stun_ereply(), stun_indication(), stun_keepalive_alloc(), stun_msg_decode(), stun_recv(), stun_reply(), stun_request(), stun_server_discover(), tcp_accept(), tcp_conn_alloc(), tcp_connect(), tcp_listen(), tcp_sock_alloc(), telev_alloc(), tls_alloc(), tls_set_ciphers(), tls_start_tcp(), turnc_add_chan(), turnc_add_perm(), turnc_alloc(), udp_listen(), and udp_send_anon().

int mem_get_stat ( struct memstat mstat)

Get memory statistics.

Parameters
mstatReturned memory statistics
Returns
0 if success, otherwise errorcode

Definition at line 497 of file mem.c.

References mem_lock, and mem_unlock.

uint32_t mem_nrefs ( const void *  data)

Get number of references to a reference-counted memory object.

Parameters
dataMemory object
Returns
Number of references

Definition at line 345 of file mem.c.

References MAGIC_CHECK, and mem::nrefs.

Referenced by mod_debug().

void* mem_realloc ( void *  data,
size_t  size 
)

Re-allocate a reference-counted memory object.

Parameters
dataMemory object
sizeNew size of memory object
Returns
New pointer to allocated object
Note
Realloc NULL pointer is not supported

Definition at line 191 of file mem.c.

References memstat::blocks_cur, mem::le, list_append(), list_unlink(), MAGIC_CHECK, mem_lock, mem_unlock, NULL, mem::size, and STAT_REALLOC.

Referenced by mbuf_resize(), mem_reallocarray(), and rtcp_sdes_decode().

void* mem_reallocarray ( void *  ptr,
size_t  nmemb,
size_t  membsize,
mem_destroy_h dh 
)

Re-allocate a reference-counted array.

Parameters
ptrPointer to existing array, NULL to allocate a new array
nmembNumber of members in array
membsizeNumber of bytes in each member
dhOptional destructor, only used when ptr is NULL
Returns
New pointer to allocated array

Definition at line 250 of file mem.c.

References mem_alloc(), mem_realloc(), and NULL.

void* mem_ref ( void *  data)
int mem_status ( struct re_printf pf,
void *  unused 
)

Print memory status.

Parameters
pfPrint handler for debug output
unusedUnused parameter
Returns
0 if success, otherwise errorcode

Definition at line 453 of file mem.c.

References memstat::blocks_cur, memstat::blocks_peak, memstat::bytes_cur, memstat::bytes_peak, list_count(), mem_lock, mem_unlock, re_hprintf(), memstat::size_max, and memstat::size_min.

void mem_threshold_set ( ssize_t  n)

Set the memory allocation threshold.

This is only used for debugging and out-of-memory simulation

Parameters
nThreshold value

Definition at line 433 of file mem.c.

References mem_lock, and mem_unlock.

void* mem_zalloc ( size_t  size,
mem_destroy_h dh 
)