GNUnet  0.11.x
Data Structures | Functions
gnunet_buffer_lib.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  GNUNET_Buffer
 Common buffer management functions. More...
 

Functions

void GNUNET_buffer_prealloc (struct GNUNET_Buffer *buf, size_t capacity)
 Initialize a buffer with the given capacity. More...
 
void GNUNET_buffer_ensure_remaining (struct GNUNET_Buffer *buf, size_t n)
 Make sure that at least n bytes remaining in the buffer. More...
 
void GNUNET_buffer_write (struct GNUNET_Buffer *buf, const char *data, size_t len)
 Write bytes to the buffer. More...
 
void GNUNET_buffer_write_str (struct GNUNET_Buffer *buf, const char *str)
 Write a 0-terminated string to a buffer, excluding the 0-terminator. More...
 
void GNUNET_buffer_write_path (struct GNUNET_Buffer *buf, const char *str)
 Write a path component to a buffer, ensuring that there is exactly one slash between the previous contents of the buffer and the new string. More...
 
void GNUNET_buffer_write_fstr (struct GNUNET_Buffer *buf, const char *fmt,...)
 Write a 0-terminated formatted string to a buffer, excluding the 0-terminator. More...
 
void GNUNET_buffer_write_vfstr (struct GNUNET_Buffer *buf, const char *fmt, va_list args)
 Write a 0-terminated formatted string to a buffer, excluding the 0-terminator. More...
 
char * GNUNET_buffer_reap_str (struct GNUNET_Buffer *buf)
 Clear the buffer and return the string it contained. More...
 
void * GNUNET_buffer_reap (struct GNUNET_Buffer *buf, size_t *size)
 Clear the buffer and return its contents. More...
 
void GNUNET_buffer_clear (struct GNUNET_Buffer *buf)
 Free the backing memory of the given buffer. More...
 

Function Documentation

◆ GNUNET_buffer_prealloc()

void GNUNET_buffer_prealloc ( struct GNUNET_Buffer buf,
size_t  capacity 
)

Initialize a buffer with the given capacity.

When a buffer is allocated with this function, a warning is logged when the buffer exceeds the initial capacity.

Parameters
bufthe buffer to initialize
capacitythe capacity (in bytes) to allocate for buf

Definition at line 35 of file buffer.c.

References GNUNET_Buffer::capacity, GNUNET_assert, GNUNET_malloc, GNUNET_YES, GNUNET_Buffer::mem, GNUNET_Buffer::position, and GNUNET_Buffer::warn_grow.

36 {
37  /* Buffer should be zero-initialized */
38  GNUNET_assert (0 == buf->mem);
39  GNUNET_assert (0 == buf->capacity);
40  GNUNET_assert (0 == buf->position);
41  buf->mem = GNUNET_malloc (capacity);
42  buf->capacity = capacity;
43  buf->warn_grow = GNUNET_YES;
44 }
size_t capacity
Capacity of the buffer.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
char * mem
Backing memory.
size_t position
Current write position.
int warn_grow
Log a warning if the buffer is grown over its initially allocated capacity.
#define GNUNET_malloc(size)
Wrapper around malloc.

◆ GNUNET_buffer_ensure_remaining()

void GNUNET_buffer_ensure_remaining ( struct GNUNET_Buffer buf,
size_t  n 
)

Make sure that at least n bytes remaining in the buffer.

Parameters
bufbuffer to potentially grow
nnumber of bytes that should be available to write

Definition at line 54 of file buffer.c.

References GNUNET_Buffer::capacity, GNUNET_assert, GNUNET_break, GNUNET_malloc, GNUNET_realloc, GNUNET_YES, GNUNET_Buffer::mem, GNUNET_Buffer::position, and GNUNET_Buffer::warn_grow.

Referenced by GNUNET_buffer_reap_str(), GNUNET_buffer_write(), GNUNET_buffer_write_path(), and GNUNET_buffer_write_vfstr().

56 {
57  size_t new_capacity = buf->position + n;
58 
59  /* guard against overflow */
60  GNUNET_assert (new_capacity >= buf->position);
61  if (new_capacity <= buf->capacity)
62  return;
63  /* warn if calculation of expected size was wrong */
65  if (new_capacity < buf->capacity * 2)
66  new_capacity = buf->capacity * 2;
67  buf->capacity = new_capacity;
68  if (NULL != buf->mem)
69  buf->mem = GNUNET_realloc (buf->mem, new_capacity);
70  else
71  buf->mem = GNUNET_malloc (new_capacity);
72 }
size_t capacity
Capacity of the buffer.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_realloc(ptr, size)
Wrapper around realloc.
char * mem
Backing memory.
size_t position
Current write position.
int warn_grow
Log a warning if the buffer is grown over its initially allocated capacity.
#define GNUNET_malloc(size)
Wrapper around malloc.
Here is the caller graph for this function:

◆ GNUNET_buffer_write()

void GNUNET_buffer_write ( struct GNUNET_Buffer buf,
const char *  data,
size_t  len 
)

Write bytes to the buffer.

Grows the buffer if necessary.

Parameters
bufbuffer to write to
datadata to read from
lennumber of bytes to copy from data to buf

Definition at line 85 of file buffer.c.

References GNUNET_buffer_ensure_remaining(), len, GNUNET_Buffer::mem, and GNUNET_Buffer::position.

Referenced by GNUNET_buffer_write_path(), GNUNET_buffer_write_str(), and write_to_buffer().

88 {
90  memcpy (buf->mem + buf->position, data, len);
91  buf->position += len;
92 }
char * mem
Backing memory.
size_t position
Current write position.
void GNUNET_buffer_ensure_remaining(struct GNUNET_Buffer *buf, size_t n)
Make sure that at least n bytes remaining in the buffer.
Definition: buffer.c:54
uint32_t data
The data value.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_buffer_write_str()

void GNUNET_buffer_write_str ( struct GNUNET_Buffer buf,
const char *  str 
)

Write a 0-terminated string to a buffer, excluding the 0-terminator.

Grows the buffer if necessary.

Parameters
bufthe buffer to write to
strthe string to write to buf
bufthe buffer to write to
strthe string to write to buf

Definition at line 102 of file buffer.c.

References GNUNET_buffer_write(), and len.

104 {
105  size_t len = strlen (str);
106 
107  GNUNET_buffer_write (buf, str, len);
108 }
void GNUNET_buffer_write(struct GNUNET_Buffer *buf, const char *data, size_t len)
Write bytes to the buffer.
Definition: buffer.c:85
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:

◆ GNUNET_buffer_write_path()

void GNUNET_buffer_write_path ( struct GNUNET_Buffer buf,
const char *  str 
)

Write a path component to a buffer, ensuring that there is exactly one slash between the previous contents of the buffer and the new string.

Parameters
bufbuffer to write to
strstring containing the new path component

Definition at line 179 of file buffer.c.

References GNUNET_buffer_ensure_remaining(), GNUNET_buffer_write(), len, GNUNET_Buffer::mem, and GNUNET_Buffer::position.

180 {
181  size_t len = strlen (str);
182 
183  while ( (0 != len) && ('/' == str[0]) )
184  {
185  str++;
186  len--;
187  }
188  if ( (0 == buf->position) || ('/' != buf->mem[buf->position - 1]) )
189  {
191  buf->mem[buf->position++] = '/';
192  }
193  GNUNET_buffer_write (buf, str, len);
194 }
void GNUNET_buffer_write(struct GNUNET_Buffer *buf, const char *data, size_t len)
Write bytes to the buffer.
Definition: buffer.c:85
char * mem
Backing memory.
size_t position
Current write position.
void GNUNET_buffer_ensure_remaining(struct GNUNET_Buffer *buf, size_t n)
Make sure that at least n bytes remaining in the buffer.
Definition: buffer.c:54
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:

◆ GNUNET_buffer_write_fstr()

void GNUNET_buffer_write_fstr ( struct GNUNET_Buffer buf,
const char *  fmt,
  ... 
)

Write a 0-terminated formatted string to a buffer, excluding the 0-terminator.

Grows the buffer if necessary.

Parameters
bufthe buffer to write to
fmtformat string
...format arguments

Definition at line 208 of file buffer.c.

References gnunet-chk::args, and GNUNET_buffer_write_vfstr().

209 {
210  va_list args;
211 
212  va_start (args, fmt);
213  GNUNET_buffer_write_vfstr (buf, fmt, args);
214  va_end (args);
215 }
void GNUNET_buffer_write_vfstr(struct GNUNET_Buffer *buf, const char *fmt, va_list args)
Write a 0-terminated formatted string to a buffer, excluding the 0-terminator.
Definition: buffer.c:229
Here is the call graph for this function:

◆ GNUNET_buffer_write_vfstr()

void GNUNET_buffer_write_vfstr ( struct GNUNET_Buffer buf,
const char *  fmt,
va_list  args 
)

Write a 0-terminated formatted string to a buffer, excluding the 0-terminator.

Grows the buffer if necessary.

Parameters
bufthe buffer to write to
fmtformat string
argsformat argument list

Definition at line 229 of file buffer.c.

References GNUNET_Buffer::capacity, GNUNET_assert, GNUNET_buffer_ensure_remaining(), GNUNET_Buffer::mem, GNUNET_Buffer::position, and res.

Referenced by GNUNET_buffer_write_fstr().

232 {
233  int res;
234  va_list args2;
235 
236  va_copy (args2, args);
237  res = vsnprintf (NULL, 0, fmt, args2);
238  va_end (args2);
239 
240  GNUNET_assert (res >= 0);
241  GNUNET_buffer_ensure_remaining (buf, res + 1);
242 
243  va_copy (args2, args);
244  res = vsnprintf (buf->mem + buf->position, res + 1, fmt, args2);
245  va_end (args2);
246 
247  GNUNET_assert (res >= 0);
248  buf->position += res;
249  GNUNET_assert (buf->position <= buf->capacity);
250 }
size_t capacity
Capacity of the buffer.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
char * mem
Backing memory.
size_t position
Current write position.
static int res
void GNUNET_buffer_ensure_remaining(struct GNUNET_Buffer *buf, size_t n)
Make sure that at least n bytes remaining in the buffer.
Definition: buffer.c:54
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_buffer_reap_str()

char* GNUNET_buffer_reap_str ( struct GNUNET_Buffer buf)

Clear the buffer and return the string it contained.

The caller is responsible to eventually GNUNET_free the returned string.

The returned string is always 0-terminated.

Parameters
bufthe buffer to reap the string from
Returns
the buffer contained in the string

Definition at line 122 of file buffer.c.

References GNUNET_buffer_ensure_remaining(), GNUNET_Buffer::mem, GNUNET_Buffer::position, and res.

123 {
124  char *res;
125 
126  /* ensure 0-termination */
127  if ( (0 == buf->position) || ('\0' != buf->mem[buf->position - 1]))
128  {
130  buf->mem[buf->position++] = '\0';
131  }
132  res = buf->mem;
133  memset (buf, 0, sizeof (struct GNUNET_Buffer));
134  return res;
135 }
char * mem
Backing memory.
size_t position
Current write position.
static int res
void GNUNET_buffer_ensure_remaining(struct GNUNET_Buffer *buf, size_t n)
Make sure that at least n bytes remaining in the buffer.
Definition: buffer.c:54
Common buffer management functions.
Here is the call graph for this function:

◆ GNUNET_buffer_reap()

void* GNUNET_buffer_reap ( struct GNUNET_Buffer buf,
size_t *  size 
)

Clear the buffer and return its contents.

The caller is responsible to eventually GNUNET_free the returned data.

Parameters
bufthe buffer to reap the contents from
sizewhere to store the size of the returned data
Returns
the data contained in the string

Definition at line 148 of file buffer.c.

References GNUNET_Buffer::mem, GNUNET_Buffer::position, and res.

Referenced by GNUNET_BIO_get_buffer_contents().

149 {
150  *size = buf->position;
151  void *res = buf->mem;
152  memset (buf, 0, sizeof (struct GNUNET_Buffer));
153  return res;
154 }
char * mem
Backing memory.
size_t position
Current write position.
static int res
static unsigned int size
Size of the "table".
Definition: peer.c:67
Common buffer management functions.
Here is the caller graph for this function:

◆ GNUNET_buffer_clear()

void GNUNET_buffer_clear ( struct GNUNET_Buffer buf)

Free the backing memory of the given buffer.

Does not free the memory of the buffer control structure, which is typically stack-allocated.

Definition at line 163 of file buffer.c.

References GNUNET_free, and GNUNET_Buffer::mem.

Referenced by GNUNET_BIO_write_close().

164 {
165  GNUNET_free (buf->mem);
166  memset (buf, 0, sizeof (struct GNUNET_Buffer));
167 }
char * mem
Backing memory.
Common buffer management functions.
#define GNUNET_free(ptr)
Wrapper around free.
Here is the caller graph for this function: