buffer.h File Reference

This file contains the definition of ldns_buffer, and functions to manipulate those. More...

Go to the source code of this file.

Data Structures

struct  ldns_struct_buffer
 implementation of buffers to ease operations More...
 

Macros

#define LDNS_MIN_BUFLEN   512
 number of initial bytes in buffer of which we cannot tell the size before hand More...
 

Typedefs

typedef struct ldns_struct_buffer ldns_buffer
 

Functions

ldns_bufferldns_buffer_new (size_t capacity)
 creates a new buffer with the specified capacity. More...
 
void ldns_buffer_new_frm_data (ldns_buffer *buffer, const void *data, size_t size)
 creates a buffer with the specified data. More...
 
bool ldns_buffer_set_capacity (ldns_buffer *buffer, size_t capacity)
 changes the buffer's capacity. More...
 
bool ldns_buffer_reserve (ldns_buffer *buffer, size_t amount)
 ensures BUFFER can contain at least AMOUNT more bytes. More...
 
int ldns_buffer_printf (ldns_buffer *buffer, const char *format,...)
 prints to the buffer, increasing the capacity if required using buffer_reserve(). More...
 
void ldns_buffer_free (ldns_buffer *buffer)
 frees the buffer. More...
 
void * ldns_buffer_export (ldns_buffer *buffer)
 Makes the buffer fixed and returns a pointer to the data. More...
 
void ldns_buffer_copy (ldns_buffer *result, const ldns_buffer *from)
 Copy contents of the from buffer to the result buffer and then flips the result buffer. More...
 

Detailed Description

This file contains the definition of ldns_buffer, and functions to manipulate those.

Definition in file buffer.h.

Macro Definition Documentation

◆ LDNS_MIN_BUFLEN

#define LDNS_MIN_BUFLEN   512

number of initial bytes in buffer of which we cannot tell the size before hand

Definition at line 33 of file buffer.h.

Typedef Documentation

◆ ldns_buffer

Definition at line 1 of file buffer.h.

Function Documentation

◆ ldns_buffer_new()

ldns_buffer* ldns_buffer_new ( size_t  capacity)

creates a new buffer with the specified capacity.

Parameters
[in]capacitythe size (in bytes) to allocate for the buffer
Returns
the created buffer

Definition at line 16 of file buffer.c.

References ldns_struct_buffer::_capacity, ldns_struct_buffer::_data, ldns_struct_buffer::_fixed, ldns_struct_buffer::_limit, ldns_struct_buffer::_position, ldns_struct_buffer::_status, LDNS_FREE, LDNS_MALLOC, LDNS_STATUS_OK, and LDNS_XMALLOC.

◆ ldns_buffer_new_frm_data()

void ldns_buffer_new_frm_data ( ldns_buffer buffer,
const void *  data,
size_t  size 
)

creates a buffer with the specified data.

The data IS copied and MEMORY allocations are done. The buffer is not fixed and can be resized using buffer_reserve().

Parameters
[in]bufferpointer to the buffer to put the data in
[in]datathe data to encapsulate in the buffer
[in]sizethe size of the data

Definition at line 41 of file buffer.c.

References ldns_struct_buffer::_capacity, ldns_struct_buffer::_data, ldns_struct_buffer::_fixed, ldns_struct_buffer::_limit, ldns_struct_buffer::_position, ldns_struct_buffer::_status, LDNS_STATUS_MEM_ERR, LDNS_STATUS_OK, and LDNS_XMALLOC.

◆ ldns_buffer_set_capacity()

bool ldns_buffer_set_capacity ( ldns_buffer buffer,
size_t  capacity 
)

changes the buffer's capacity.

The data is reallocated so any pointers to the data may become invalid. The buffer's limit is set to the buffer's new capacity.

Parameters
[in]bufferthe buffer
[in]capacitythe capacity to use
Returns
whether this failed or succeeded

Definition at line 60 of file buffer.c.

◆ ldns_buffer_reserve()

bool ldns_buffer_reserve ( ldns_buffer buffer,
size_t  amount 
)

ensures BUFFER can contain at least AMOUNT more bytes.

The buffer's capacity is increased if necessary using buffer_set_capacity().

The buffer's limit is always set to the (possibly increased) capacity.

Parameters
[in]bufferthe buffer
[in]amountamount to use
Returns
whether this failed or succeeded

Definition at line 80 of file buffer.c.

◆ ldns_buffer_printf()

int ldns_buffer_printf ( ldns_buffer buffer,
const char *  format,
  ... 
)

prints to the buffer, increasing the capacity if required using buffer_reserve().

The buffer's position is set to the terminating '\0' Returns the number of characters written (not including the terminating '\0') or -1 on failure.

Definition at line 99 of file buffer.c.

◆ ldns_buffer_free()

void ldns_buffer_free ( ldns_buffer buffer)

frees the buffer.

Parameters
[in]*bufferthe buffer to be freed
Returns
void

Definition at line 137 of file buffer.c.

References ldns_struct_buffer::_data, ldns_struct_buffer::_fixed, and LDNS_FREE.

◆ ldns_buffer_export()

void* ldns_buffer_export ( ldns_buffer buffer)

Makes the buffer fixed and returns a pointer to the data.

The caller is responsible for free'ing the result.

Parameters
[in]*bufferthe buffer to be exported
Returns
void

Definition at line 150 of file buffer.c.

References ldns_struct_buffer::_data, and ldns_struct_buffer::_fixed.

◆ ldns_buffer_copy()

void ldns_buffer_copy ( ldns_buffer result,
const ldns_buffer from 
)

Copy contents of the from buffer to the result buffer and then flips the result buffer.

Data will be silently truncated if the result buffer is too small.

Parameters
[out]*resultresulting buffer which is copied to.
[in]*fromwhat to copy to result.

Definition at line 168 of file buffer.c.