Libical API Documentation 3.0
|
Common memory management routines. More...
Go to the source code of this file.
Functions | |
void | icalmemory_add_tmp_buffer (void *buf) |
Adds an externally allocated buffer to the ring. | |
void | icalmemory_append_char (char **buf, char **pos, size_t *buf_size, char ch) |
Appends a character to a buffer. | |
void | icalmemory_append_string (char **buf, char **pos, size_t *buf_size, const char *string) |
Appends a string to a buffer. | |
void | icalmemory_free_buffer (void *buf) |
Releases a buffer. | |
void | icalmemory_free_ring (void) |
Frees all memory used in the ring. | |
void * | icalmemory_new_buffer (size_t size) |
Creates new buffer with the specified size. | |
void * | icalmemory_resize_buffer (void *buf, size_t size) |
Resizes a buffer created with icalmemory_new_buffer(). | |
char * | icalmemory_strdup (const char *s) |
Creates a duplicate of a string. | |
void * | icalmemory_tmp_buffer (size_t size) |
Creates a new temporary buffer on the ring and returns it. | |
char * | icalmemory_tmp_copy (const char *str) |
Creates a copy of the given string, stored on the ring buffer, and returns it. | |
Common memory management routines.
libical often passes strings back to the caller. To make these interfaces simple, I did not want the caller to have to pass in a memory buffer, but having libical pass out newly allocated memory makes it difficult to de-allocate the memory.
The ring buffer in this scheme makes it possible for libical to pass out references to memory which the caller does not own, and be able to de-allocate the memory later. The ring allows libical to have several buffers active simultaneously, which is handy when creating string representations of components. Methods for working with these temporary buffers are marked with icalmemory_tmp_*()
.
Other memory management routines include wrappers around the system management routines like icalmemory_new_buffer() and icalmemory_free_buffer() as well as routines to work with strings, like icalmemory_append_string().
void icalmemory_add_tmp_buffer | ( | void * | buf | ) |
Adds an externally allocated buffer to the ring.
buf | The externally allocated buffer to add to the ring |
Adds an externally allocated buffer to the ring. This ensures that libical will free()
the buffer automatically, either after BUFFER_RING_SIZE other buffers have been created or added, or after icalmemory_free_ring() has been called.
NULL
.free()
d manually anymore, it leads to a double-free()
when icalmemory reclaims the memory.void icalmemory_append_char | ( | char ** | buf, |
char ** | pos, | ||
size_t * | buf_size, | ||
char | ch | ||
) |
Appends a character to a buffer.
buf | The buffer to append the character to. |
pos | The position to append the character at. |
buf_size | The size of the buffer (will be changed if buffer is reallocated) |
ch | The character to append to the buffer. |
*buf
, pos, * pos
, or buf_size NULL
.This method will copy the character ch and a ‘’\0'character after it to the buffer @a buf starting at position @a pos, reallocing @a buf if it is too small. @a buf_size is the size of @a buf and will be changed if @a buf is reallocated. @a pos will point to the new terminating
'\0'` character buf.
void icalmemory_append_string | ( | char ** | buf, |
char ** | pos, | ||
size_t * | buf_size, | ||
const char * | string | ||
) |
Appends a string to a buffer.
buf | The buffer to append the string to. |
pos | The position to append the string at. |
buf_size | The size of the buffer (will be changed if buffer is reallocated) |
string | The string to append to the buffer. |
*buf
, pos, * pos
, buf_size or string are NULL
.This method will copy the string string to the buffer buf starting at position pos, reallocing buf if it is too small. buf_size is the size of buf and will be changed if buf is reallocated. pos will point to the last byte of the new string in buf, usually a ‘’\0'`
void icalmemory_free_buffer | ( | void * | buf | ) |
Releases a buffer.
buf | The buffer to release |
Releases the memory of the buffer.
void icalmemory_free_ring | ( | void | ) |
Frees all memory used in the ring.
Frees all memory used in the ring. Depending on if HAVE_PTHREAD is set or not, the ring buffer is allocated on a per-thread basis, meaning that if all rings are to be released, it must be called once in every thread.
void * icalmemory_new_buffer | ( | size_t | size | ) |
Creates new buffer with the specified size.
size | The size of the buffer that is to be created. |
NULL
.This creates a new (non-temporary) buffer of the specified size. All buffers returned by this method are zeroed-out.
void * icalmemory_resize_buffer | ( | void * | buf, |
size_t | size | ||
) |
Resizes a buffer created with icalmemory_new_buffer().
buf | The buffer to be resized. |
size | The new size of the buffer. |
NULL
.char * icalmemory_strdup | ( | const char * | s | ) |
Creates a duplicate of a string.
s | The string to duplicate. |
NULL
, otherwise depending on the libc
used, it might lead to undefined behaviour (read: segfaults).free()
method.A wrapper around strdup()
. Partly to trap calls to strdup()
, partly because in -ansi
, gcc
on Red Hat claims that strdup()
is undeclared.
void * icalmemory_tmp_buffer | ( | size_t | size | ) |
Creates a new temporary buffer on the ring and returns it.
size | How big (in bytes) the buffer should be |
Creates a temporary buffer on the ring. Regardless of what size you specify, the buffer will always be at least MIN_BUFFER_SIZE big, and it will be zeroed out.
NULL
.char * icalmemory_tmp_copy | ( | const char * | str | ) |
Creates a copy of the given string, stored on the ring buffer, and returns it.
str | The string to copy |
NULL
, otherwise a segfault might ensue, since the routine calls strlen()
on it.