dbuffer.c File Reference

A dbuffer is a dynamically sized buffer that stores arbitrary bytes in a queue. It does not provide random access; characters may be inserted only at the end and removed only from the beginning. More...

#include "common.h"
#include <SDL_thread.h>
Include dependency graph for dbuffer.c:

Go to the source code of this file.

Data Structures

struct  dbuffer_element

Defines

#define DBUFFER_ELEMENT_SIZE   4000
#define DBUFFER_ELEMENTS_FREE_THRESHOLD   1000
#define DBUFFER_FREE_THRESHOLD   1000

Functions

static struct dbuffer_elementallocate_element (void)
static void free_element (struct dbuffer_element *e)
struct dbuffernew_dbuffer (void)
 Allocate a dbuffer.
void free_dbuffer (struct dbuffer *buf)
 Deallocate a dbuffer.
static void dbuffer_grow (struct dbuffer *buf, size_t len)
 This grows the buffer to provide at least enough space for the.
struct dbufferdbuffer_merge (struct dbuffer *old, struct dbuffer *old2)
 Merges two dbuffers.
struct dbufferdbuffer_prepend (struct dbuffer *old, const char *data, size_t len)
 Allocate a dbuffer and prepend the given data to it.
void dbuffer_add (struct dbuffer *buf, const char *data, size_t len)
 Append data to a dbuffer.
size_t dbuffer_get (const struct dbuffer *buf, char *data, size_t len)
 Read data from a dbuffer.
size_t dbuffer_get_at (const struct dbuffer *buf, size_t offset, char *data, size_t len)
 Read data from a dbuffer.
struct dbufferdbuffer_dup (struct dbuffer *old)
 Allocate a dbuffer.
size_t dbuffer_remove (struct dbuffer *buf, size_t len)
 Deletes data from a dbuffer.
size_t dbuffer_extract (struct dbuffer *buf, char *data, size_t len)
 Read and delete data from a dbuffer.
void dbuffer_init (void)
void dbuffer_shutdown (void)

Variables

static int free_elements = 0
static int allocated_elements = 0
static struct dbuffer_elementfree_element_list = NULL
static int free_dbuffers = 0
static int allocated_dbuffers = 0
static struct dbufferfree_dbuffer_list = NULL
static SDL_mutex * dbuf_lock

Detailed Description

A dbuffer is a dynamically sized buffer that stores arbitrary bytes in a queue. It does not provide random access; characters may be inserted only at the end and removed only from the beginning.

Definition in file dbuffer.c.


Define Documentation

#define DBUFFER_ELEMENT_SIZE   4000
#define DBUFFER_ELEMENTS_FREE_THRESHOLD   1000

Definition at line 36 of file dbuffer.c.

Referenced by free_element().

#define DBUFFER_FREE_THRESHOLD   1000

Definition at line 37 of file dbuffer.c.

Referenced by free_dbuffer().


Function Documentation

static struct dbuffer_element* allocate_element ( void   )  [static, read]
void dbuffer_add ( struct dbuffer buf,
const char *  data,
size_t  len 
)

Append data to a dbuffer.

Parameters:
[in,out] buf the target buffer
[in] data pointer to the start of the bytes to add
[in] len number of bytes to add Adds the given sequence of bytes to the end of the buffer.

Definition at line 263 of file dbuffer.c.

References dbuffer_element::data, dbuffer_grow(), dbuffer::end, dbuffer::len, dbuffer_element::len, dbuffer_element::next, dbuffer_element::space, dbuffer::space, and dbuffer::tail.

Referenced by CL_ForwardToServer_f(), Cmd_ForwardToServer(), dbuffer_dup(), dbuffer_merge(), dbuffer_prepend(), NET_ReadMsg(), NET_StreamEnqueue(), NET_VPrintf(), NET_Wait(), NET_WriteByte(), NET_WriteChar(), NET_WriteLong(), NET_WriteRawString(), NET_WriteShort(), and NET_WriteString().

struct dbuffer* dbuffer_dup ( struct dbuffer old  )  [read]

Allocate a dbuffer.

Parameters:
[in] old the source buffer
Returns:
the newly allocated buffer Allocates a new dbuffer and initialises it to contain a copy of the data in old

Definition at line 397 of file dbuffer.c.

References dbuffer_element::data, dbuffer_add(), dbuffer::head, dbuffer_element::len, new_dbuffer(), dbuffer_element::next, and dbuffer::start.

Referenced by CL_ParseEvent().

size_t dbuffer_extract ( struct dbuffer buf,
char *  data,
size_t  len 
)

Read and delete data from a dbuffer.

Parameters:
[in,out] buf the source buffer
[out] data pointer to where the data should be copied
[in] len maximum number of bytes to copy
Returns:
number of bytes copied
Copies up to len bytes into data, and removes them from the dbuffer
If the buffer does not contain at least len bytes, then as many bytes as are present will be copied.
However many bytes are copied, exactly that many will be removed from the start of the dbuffer.

Definition at line 471 of file dbuffer.c.

References dbuffer_element::data, DBUFFER_ELEMENT_SIZE, dbuffer::end, free_element(), dbuffer::head, dbuffer_element::len, dbuffer::len, dbuffer_element::next, dbuffer::space, and dbuffer::start.

Referenced by NET_ReadByte(), NET_ReadChar(), NET_ReadLong(), NET_ReadShort(), NET_StreamDequeue(), and NET_WriteMsg().

size_t dbuffer_get ( const struct dbuffer buf,
char *  data,
size_t  len 
)

Read data from a dbuffer.

Parameters:
[in] buf the source buffer
[out] data pointer to where the data should be copied
[in] len maximum number of bytes to copy
Returns:
number of bytes copied
Copies up to len bytes into data
If the buffer does not contain at least len bytes, then as many bytes as are present will be copied.

Definition at line 310 of file dbuffer.c.

References dbuffer_element::data, dbuffer::head, dbuffer_element::len, dbuffer_element::next, and dbuffer::start.

Referenced by NET_PeekShort(), NET_StreamPeek(), and NET_Wait().

size_t dbuffer_get_at ( const struct dbuffer buf,
size_t  offset,
char *  data,
size_t  len 
)

Read data from a dbuffer.

Parameters:
[in] buf the source buffer
[in] offset the offset in the source buffer where data should be copied from
[out] data pointer to where the data should be copied
[in] len maximum number of bytes to copy
Returns:
number of bytes copied
Copies up to len bytes into data
If the buffer does not contain at least len bytes after offset, then as many bytes as are present will be copied.

Definition at line 350 of file dbuffer.c.

References dbuffer_element::data, dbuffer::head, dbuffer_element::len, dbuffer_element::next, and dbuffer::start.

Referenced by NET_WriteConstMsg().

static void dbuffer_grow ( struct dbuffer buf,
size_t  len 
) [inline, static]

This grows the buffer to provide at least enough space for the.

Parameters:
[in,out] buf the target buffer
[in] len number of bytes to add given length, plus one character (see the comments in dbuffer_add() for the explanation of that extra character).

Definition at line 181 of file dbuffer.c.

References allocate_element(), DBUFFER_ELEMENT_SIZE, dbuffer_element::next, dbuffer::space, and dbuffer::tail.

Referenced by dbuffer_add().

void dbuffer_init ( void   ) 

Definition at line 503 of file dbuffer.c.

References dbuf_lock.

Referenced by NET_Init().

struct dbuffer* dbuffer_merge ( struct dbuffer old,
struct dbuffer old2 
) [read]

Merges two dbuffers.

Parameters:
[in] old the source buffer
[in] old2 the second source buffer
Returns:
the newly allocated buffer Allocates a new dbuffer and initialises it to contain a copy of the data in old ones

Definition at line 201 of file dbuffer.c.

References dbuffer_element::data, dbuffer_add(), dbuffer::head, dbuffer_element::len, new_dbuffer(), dbuffer_element::next, and dbuffer::start.

struct dbuffer* dbuffer_prepend ( struct dbuffer old,
const char *  data,
size_t  len 
) [read]

Allocate a dbuffer and prepend the given data to it.

Parameters:
[in] old The source buffer
[in] data The data to insert at the beginning
[in] len The length of that data
Returns:
the newly allocated buffer Allocates a new dbuffer and initialises it to contain a copy of the data in old

Definition at line 236 of file dbuffer.c.

References dbuffer_element::data, dbuffer_add(), dbuffer::head, dbuffer_element::len, new_dbuffer(), dbuffer_element::next, and dbuffer::start.

size_t dbuffer_remove ( struct dbuffer buf,
size_t  len 
)

Deletes data from a dbuffer.

Parameters:
[in,out] buf the target buffer
[in] len number of bytes to delete Deletes the given number of bytes from the start of the dbuffer

Definition at line 421 of file dbuffer.c.

References dbuffer_element::data, DBUFFER_ELEMENT_SIZE, dbuffer::end, free_element(), dbuffer::head, dbuffer_element::len, dbuffer::len, dbuffer_element::next, dbuffer::space, and dbuffer::start.

Referenced by NET_Wait().

void dbuffer_shutdown ( void   ) 

Definition at line 508 of file dbuffer.c.

References dbuf_lock.

Referenced by NET_Shutdown().

void free_dbuffer ( struct dbuffer buf  ) 
static void free_element ( struct dbuffer_element e  )  [static]
struct dbuffer* new_dbuffer ( void   )  [read]

Variable Documentation

int allocated_dbuffers = 0 [static]

Definition at line 49 of file dbuffer.c.

Referenced by free_dbuffer(), and new_dbuffer().

int allocated_elements = 0 [static]

Definition at line 46 of file dbuffer.c.

Referenced by allocate_element(), and free_element().

SDL_mutex* dbuf_lock [static]
struct dbuffer* free_dbuffer_list = NULL [static]

Definition at line 50 of file dbuffer.c.

int free_dbuffers = 0 [static]

Definition at line 49 of file dbuffer.c.

Referenced by free_dbuffer(), and new_dbuffer().

struct dbuffer_element* free_element_list = NULL [static]

Definition at line 47 of file dbuffer.c.

int free_elements = 0 [static]

Definition at line 46 of file dbuffer.c.

Referenced by allocate_element(), and free_element().


Generated by  doxygen 1.6.2