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>
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_element * | allocate_element (void) |
static void | free_element (struct dbuffer_element *e) |
struct dbuffer * | new_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 dbuffer * | dbuffer_merge (struct dbuffer *old, struct dbuffer *old2) |
Merges two dbuffers. | |
struct dbuffer * | dbuffer_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 dbuffer * | dbuffer_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_element * | free_element_list = NULL |
static int | free_dbuffers = 0 |
static int | allocated_dbuffers = 0 |
static struct dbuffer * | free_dbuffer_list = NULL |
static SDL_mutex * | dbuf_lock |
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 DBUFFER_ELEMENT_SIZE 4000 |
Definition at line 30 of file dbuffer.c.
Referenced by allocate_element(), dbuffer_extract(), dbuffer_grow(), dbuffer_remove(), and new_dbuffer().
#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().
static struct dbuffer_element* allocate_element | ( | void | ) | [static, read] |
Definition at line 57 of file dbuffer.c.
References allocated_elements, com_genericPool, dbuf_lock, DBUFFER_ELEMENT_SIZE, free_elements, dbuffer_element::len, Mem_PoolAlloc, dbuffer_element::next, and dbuffer_element::space.
Referenced by dbuffer_grow(), and new_dbuffer().
void dbuffer_add | ( | struct dbuffer * | buf, | |
const char * | data, | |||
size_t | len | |||
) |
Append data to a dbuffer.
[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().
Allocate a dbuffer.
[in] | old | the source buffer |
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.
[in,out] | buf | the source buffer |
[out] | data | pointer to where the data should be copied |
[in] | len | maximum number of bytes to copy |
len
bytes into data
, and removes them from the dbufferlen
bytes, then as many bytes as are present will be copied.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.
[in] | buf | the source buffer |
[out] | data | pointer to where the data should be copied |
[in] | len | maximum number of bytes to copy |
len
bytes into data
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.
[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 |
len
bytes into data
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.
[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 | ) |
Merges two dbuffers.
[in] | old | the source buffer |
[in] | old2 | the second source buffer |
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.
Allocate a dbuffer and prepend the given data to it.
[in] | old | The source buffer |
[in] | data | The data to insert at the beginning |
[in] | len | The length of that data |
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.
[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 | ) |
void free_dbuffer | ( | struct dbuffer * | buf | ) |
Deallocate a dbuffer.
[in,out] | buf | the dbuffer to deallocate Deallocates a dbuffer, and all memory it uses |
Definition at line 146 of file dbuffer.c.
References allocated_dbuffers, dbuf_lock, DBUFFER_FREE_THRESHOLD, free_dbuffers, free_element(), dbuffer::head, Mem_Free, dbuffer_element::next, and dbuffer::next_free.
Referenced by CL_ExecuteBattlescapeEvent(), CL_FreeBattlescapeEvent(), CL_ReadPackets(), NET_StreamClose(), NET_StreamFinished(), NET_WriteMsg(), SV_AbortEvents(), SV_BroadcastPrintf(), SV_FinalMessage(), SV_Multicast(), and SV_ReadPacket().
static void free_element | ( | struct dbuffer_element * | e | ) | [static] |
Definition at line 88 of file dbuffer.c.
References allocated_elements, dbuf_lock, DBUFFER_ELEMENTS_FREE_THRESHOLD, free_elements, Mem_Free, and dbuffer_element::next.
Referenced by dbuffer_extract(), dbuffer_remove(), and free_dbuffer().
struct dbuffer* new_dbuffer | ( | void | ) | [read] |
Allocate a dbuffer.
Definition at line 111 of file dbuffer.c.
References allocate_element(), allocated_dbuffers, com_genericPool, dbuffer_element::data, dbuf_lock, DBUFFER_ELEMENT_SIZE, dbuffer::end, free_dbuffers, dbuffer::head, dbuffer::len, Mem_PoolAlloc, dbuffer::next_free, dbuffer::space, dbuffer::start, and dbuffer::tail.
Referenced by CL_ConnectionlessPacket(), CL_Disconnect(), CL_ForwardToServer_f(), CL_NextRound_f(), CL_RequestNextDownload(), CL_SendChangedUserinfos(), Cmd_ForwardToServer(), dbuffer_dup(), dbuffer_merge(), dbuffer_prepend(), do_accept(), GAME_CP_InitializeBattlescape(), GAME_SpawnSoldiers(), MSG_Write_PA(), NET_ConnectToLoopBack(), NET_DoConnect(), NET_ReadMsg(), SV_AddEvent(), SV_BroadcastPrintf(), SV_ClientCommand(), SV_ClientPrintf(), SV_Configstring(), SV_DropClient(), SV_FinalMessage(), SV_FindIndex(), SV_New_f(), SV_StartSound(), SVC_Status(), and SVC_TeamInfo().
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] |
Definition at line 52 of file dbuffer.c.
Referenced by allocate_element(), dbuffer_init(), dbuffer_shutdown(), free_dbuffer(), free_element(), and new_dbuffer().
struct dbuffer* free_dbuffer_list = NULL [static] |
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] |
int free_elements = 0 [static] |
Definition at line 46 of file dbuffer.c.
Referenced by allocate_element(), and free_element().