SDL_dataqueue.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

## Functions

SDL_DataQueue * | SDL_NewDataQueue (const size_t packetlen, const size_t initialslack)
void | SDL_FreeDataQueue (SDL_DataQueue *queue)
void | SDL_ClearDataQueue (SDL_DataQueue *queue, const size_t slack)
int | SDL_WriteToDataQueue (SDL_DataQueue *queue, const void *data, const size_t len)
size_t | SDL_ReadFromDataQueue (SDL_DataQueue *queue, void *buf, const size_t len)
size_t | SDL_PeekIntoDataQueue (SDL_DataQueue *queue, void *buf, const size_t len)
size_t | SDL_CountDataQueue (SDL_DataQueue *queue)
void * | SDL_ReserveSpaceInDataQueue (SDL_DataQueue *queue, const size_t len)

## Function Documentation

## SDL_ClearDataQueue()

void SDL_ClearDataQueue | ( | SDL_DataQueue * | queue,
|  | const size_t | slack
| ) |  |

Definition at line 98 of file SDL_dataqueue.c.

References SDL_DataQueue::head, i, SDL_DataQueuePacket::next, NULL, SDL_DataQueue::packet_size, SDL_DataQueue::pool, SDL_DataQueue::queued_bytes, SDL_FreeDataQueueList(), and SDL_DataQueue::tail.

Referenced by SDL_AudioStreamClear(), and SDL_ClearQueuedAudio().

{
const size_t packet_size = queue ? queue->packet_size : 1;
const size_t slackpackets = (slack + (packet_size-1)) / packet_size;
SDL_DataQueuePacket *packet;
SDL_DataQueuePacket *prev = NULL;
size_t i;
if (!queue) {
return;
}
packet = queue->head;
/* merge the available pool and the current queue into one list. */
if (packet) {
queue->tail->next = queue->pool;
} else {
packet = queue->pool;
}
/* Remove the queued packets from the device. */
queue->tail = NULL;
queue->head = NULL;
queue->queued_bytes = 0;
queue->pool = packet;
/* Optionally keep some slack in the pool to reduce malloc pressure. */
for (i = 0; packet && (i < slackpackets); i++) {
prev = packet;
packet = packet->next;
}
if (prev) {
prev->next = NULL;
} else {
queue->pool = NULL;
}
SDL_FreeDataQueueList(packet); /* free extra packets */
}
SDL_DataQueue::head
SDL_DataQueuePacket * head
Definition: SDL_dataqueue.c:37
SDL_DataQueue::queued_bytes
size_t queued_bytes
Definition: SDL_dataqueue.c:41
SDL_DataQueue::pool
SDL_DataQueuePacket * pool
Definition: SDL_dataqueue.c:39
SDL_FreeDataQueueList
static void SDL_FreeDataQueueList(SDL_DataQueuePacket *packet)
Definition: SDL_dataqueue.c:45
SDL_DataQueuePacket::next
struct SDL_DataQueuePacket * next
Definition: SDL_dataqueue.c:31
SDL_DataQueuePacket
Definition: SDL_dataqueue.c:27
SDL_DataQueue::tail
SDL_DataQueuePacket * tail
Definition: SDL_dataqueue.c:38
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
NULL
#define NULL
Definition: begin_code.h:164
SDL_DataQueue::packet_size
size_t packet_size
Definition: SDL_dataqueue.c:40

## SDL_CountDataQueue()

size_t SDL_CountDataQueue | ( | SDL_DataQueue * | queue | ) |

Definition at line 294 of file SDL_dataqueue.c.

References SDL_DataQueue::queued_bytes.

Referenced by SDL_AudioStreamAvailable(), SDL_BufferQueueDrainCallback(), and SDL_GetQueuedAudioSize().

{
return queue ? queue->queued_bytes : 0;
}
SDL_DataQueue::queued_bytes
size_t queued_bytes
Definition: SDL_dataqueue.c:41

## SDL_FreeDataQueue()

void SDL_FreeDataQueue | ( | SDL_DataQueue * | queue | ) |

Definition at line 88 of file SDL_dataqueue.c.

References SDL_DataQueue::head, SDL_DataQueue::pool, SDL_free, and SDL_FreeDataQueueList().

Referenced by close_audio_device(), and SDL_FreeAudioStream().

{
if (queue) {
SDL_FreeDataQueueList(queue->head);
SDL_FreeDataQueueList(queue->pool);
SDL_free(queue);
}
}
SDL_DataQueue::head
SDL_DataQueuePacket * head
Definition: SDL_dataqueue.c:37
SDL_DataQueue::pool
SDL_DataQueuePacket * pool
Definition: SDL_dataqueue.c:39
SDL_FreeDataQueueList
static void SDL_FreeDataQueueList(SDL_DataQueuePacket *packet)
Definition: SDL_dataqueue.c:45
SDL_free
#define SDL_free
Definition: SDL_dynapi_overrides.h:377

## SDL_NewDataQueue()

SDL_DataQueue* SDL_NewDataQueue | ( | const size_t | packetlen,
|  | const size_t | initialslack
| ) |  |

Definition at line 58 of file SDL_dataqueue.c.

References SDL_DataQueuePacket::datalen, i, SDL_DataQueuePacket::next, NULL, SDL_DataQueue::packet_size, SDL_DataQueue::pool, SDL_malloc, SDL_OutOfMemory, SDL_zerop, and SDL_DataQueuePacket::startpos.

Referenced by open_audio_device(), and SDL_NewAudioStream().

{
SDL_DataQueue *queue = (SDL_DataQueue *) SDL_malloc(sizeof (SDL_DataQueue));
if (!queue) {
SDL_OutOfMemory();
return NULL;
} else {
const size_t packetlen = _packetlen ? _packetlen : 1024;
const size_t wantpackets = (initialslack + (packetlen - 1)) / packetlen;
size_t i;
SDL_zerop(queue);
queue->packet_size = packetlen;
for (i = 0; i < wantpackets; i++) {
SDL_DataQueuePacket *packet = (SDL_DataQueuePacket *) SDL_malloc(sizeof (SDL_DataQueuePacket) + packetlen);
if (packet) { /* don't care if this fails, we'll deal later. */
packet->datalen = 0;
packet->startpos = 0;
packet->next = queue->pool;
queue->pool = packet;
}
}
}
return queue;
}
SDL_DataQueue
Definition: SDL_dataqueue.c:35
SDL_DataQueue::pool
SDL_DataQueuePacket * pool
Definition: SDL_dataqueue.c:39
SDL_zerop
#define SDL_zerop(x)
Definition: SDL_stdinc.h:417
SDL_DataQueuePacket::startpos
size_t startpos
Definition: SDL_dataqueue.c:30
SDL_DataQueuePacket::next
struct SDL_DataQueuePacket * next
Definition: SDL_dataqueue.c:31
SDL_DataQueuePacket
Definition: SDL_dataqueue.c:27
SDL_DataQueuePacket::datalen
size_t datalen
Definition: SDL_dataqueue.c:29
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
NULL
#define NULL
Definition: begin_code.h:164
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_malloc
#define SDL_malloc
Definition: SDL_dynapi_overrides.h:374
SDL_DataQueue::packet_size
size_t packet_size
Definition: SDL_dataqueue.c:40

## SDL_PeekIntoDataQueue()

size_t SDL_PeekIntoDataQueue | ( | SDL_DataQueue * | queue,
|  | void * | buf,
|  | const size_t | len
| ) |  |

Definition at line 229 of file SDL_dataqueue.c.

References SDL_DataQueuePacket::data, SDL_DataQueuePacket::datalen, SDL_DataQueue::head, SDL_DataQueuePacket::next, SDL_DataQueue::queued_bytes, SDL_assert, SDL_memcpy, SDL_min, and SDL_DataQueuePacket::startpos.

{
size_t len = _len;
Uint8 *buf = (Uint8 *) _buf;
Uint8 *ptr = buf;
SDL_DataQueuePacket *packet;
if (!queue) {
return 0;
}
for (packet = queue->head; len && packet; packet = packet->next) {
const size_t avail = packet->datalen - packet->startpos;
const size_t cpy = SDL_min(len, avail);
SDL_assert(queue->queued_bytes >= avail);
SDL_memcpy(ptr, packet->data + packet->startpos, cpy);
ptr += cpy;
len -= cpy;
}
return (size_t) (ptr - buf);
}
SDL_DataQueue::head
SDL_DataQueuePacket * head
Definition: SDL_dataqueue.c:37
SDL_min
#define SDL_min(x, y)
Definition: SDL_stdinc.h:406
SDL_DataQueue::queued_bytes
size_t queued_bytes
Definition: SDL_dataqueue.c:41
SDL_DataQueuePacket::data
Uint8 data[SDL_VARIABLE_LENGTH_ARRAY]
Definition: SDL_dataqueue.c:32
len
GLenum GLsizei len
Definition: SDL_opengl_glext.h:2926
SDL_DataQueuePacket::startpos
size_t startpos
Definition: SDL_dataqueue.c:30
SDL_DataQueuePacket::next
struct SDL_DataQueuePacket * next
Definition: SDL_dataqueue.c:31
SDL_memcpy
#define SDL_memcpy
Definition: SDL_dynapi_overrides.h:387
SDL_DataQueuePacket
Definition: SDL_dataqueue.c:27
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:157
SDL_DataQueuePacket::datalen
size_t datalen
Definition: SDL_dataqueue.c:29
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: SDL_opengl_glext.h:2480
SDL_assert
#define SDL_assert(condition)
Definition: SDL_assert.h:169

## SDL_ReadFromDataQueue()

size_t SDL_ReadFromDataQueue | ( | SDL_DataQueue * | queue,
|  | void * | buf,
|  | const size_t | len
| ) |  |

Definition at line 254 of file SDL_dataqueue.c.

References SDL_DataQueuePacket::data, SDL_DataQueuePacket::datalen, SDL_DataQueue::head, SDL_DataQueuePacket::next, NULL, SDL_DataQueue::pool, SDL_DataQueue::queued_bytes, SDL_assert, SDL_memcpy, SDL_min, SDL_DataQueuePacket::startpos, and SDL_DataQueue::tail.

Referenced by SDL_AudioStreamGet(), SDL_BufferQueueDrainCallback(), and SDL_DequeueAudio().

{
size_t len = _len;
Uint8 *buf = (Uint8 *) _buf;
Uint8 *ptr = buf;
SDL_DataQueuePacket *packet;
if (!queue) {
return 0;
}
while ((len > 0) && ((packet = queue->head) != NULL)) {
const size_t avail = packet->datalen - packet->startpos;
const size_t cpy = SDL_min(len, avail);
SDL_assert(queue->queued_bytes >= avail);
SDL_memcpy(ptr, packet->data + packet->startpos, cpy);
packet->startpos += cpy;
ptr += cpy;
queue->queued_bytes -= cpy;
len -= cpy;
if (packet->startpos == packet->datalen) { /* packet is done, put it in the pool. */
queue->head = packet->next;
SDL_assert((packet->next != NULL) || (packet == queue->tail));
packet->next = queue->pool;
queue->pool = packet;
}
}
SDL_assert((queue->head != NULL) == (queue->queued_bytes != 0));
if (queue->head == NULL) {
queue->tail = NULL; /* in case we drained the queue entirely. */
}
return (size_t) (ptr - buf);
}
SDL_DataQueue::head
SDL_DataQueuePacket * head
Definition: SDL_dataqueue.c:37
SDL_min
#define SDL_min(x, y)
Definition: SDL_stdinc.h:406
SDL_DataQueue::queued_bytes
size_t queued_bytes
Definition: SDL_dataqueue.c:41
SDL_DataQueue::pool
SDL_DataQueuePacket * pool
Definition: SDL_dataqueue.c:39
SDL_DataQueuePacket::data
Uint8 data[SDL_VARIABLE_LENGTH_ARRAY]
Definition: SDL_dataqueue.c:32
len
GLenum GLsizei len
Definition: SDL_opengl_glext.h:2926
SDL_DataQueuePacket::startpos
size_t startpos
Definition: SDL_dataqueue.c:30
SDL_DataQueuePacket::next
struct SDL_DataQueuePacket * next
Definition: SDL_dataqueue.c:31
SDL_memcpy
#define SDL_memcpy
Definition: SDL_dynapi_overrides.h:387
SDL_DataQueuePacket
Definition: SDL_dataqueue.c:27
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:157
SDL_DataQueuePacket::datalen
size_t datalen
Definition: SDL_dataqueue.c:29
SDL_DataQueue::tail
SDL_DataQueuePacket * tail
Definition: SDL_dataqueue.c:38
buf
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: SDL_opengl_glext.h:2480
SDL_assert
#define SDL_assert(condition)
Definition: SDL_assert.h:169
NULL
#define NULL
Definition: begin_code.h:164

## SDL_ReserveSpaceInDataQueue()

void* SDL_ReserveSpaceInDataQueue | ( | SDL_DataQueue * | queue,
|  | const size_t | len
| ) |  |

Definition at line 300 of file SDL_dataqueue.c.

References AllocateDataQueuePacket(), SDL_DataQueuePacket::data, SDL_DataQueuePacket::datalen, SDL_DataQueue::head, NULL, SDL_DataQueue::packet_size, SDL_DataQueue::queued_bytes, retval, SDL_InvalidParamError, SDL_OutOfMemory, and SDL_SetError.

{
SDL_DataQueuePacket *packet;
if (!queue) {
SDL_InvalidParamError("queue");
return NULL;
} else if (len == 0) {
SDL_InvalidParamError("len");
return NULL;
} else if (len > queue->packet_size) {
SDL_SetError("len is larger than packet size");
return NULL;
}
packet = queue->head;
if (packet) {
const size_t avail = queue->packet_size - packet->datalen;
if (len <= avail) { /* we can use the space at end of this packet. */
void *retval = packet->data + packet->datalen;
packet->datalen += len;
queue->queued_bytes += len;
return retval;
}
}
/* Need a fresh packet. */
packet = AllocateDataQueuePacket(queue);
if (!packet) {
SDL_OutOfMemory();
return NULL;
}
packet->datalen = len;
queue->queued_bytes += len;
return packet->data;
}
SDL_DataQueue::head
SDL_DataQueuePacket * head
Definition: SDL_dataqueue.c:37
SDL_DataQueue::queued_bytes
size_t queued_bytes
Definition: SDL_dataqueue.c:41
SDL_InvalidParamError
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
SDL_DataQueuePacket::data
Uint8 data[SDL_VARIABLE_LENGTH_ARRAY]
Definition: SDL_dataqueue.c:32
len
GLenum GLsizei len
Definition: SDL_opengl_glext.h:2926
retval
SDL_bool retval
Definition: testgamecontroller.c:65
SDL_DataQueuePacket
Definition: SDL_dataqueue.c:27
SDL_DataQueuePacket::datalen
size_t datalen
Definition: SDL_dataqueue.c:29
AllocateDataQueuePacket
static SDL_DataQueuePacket * AllocateDataQueuePacket(SDL_DataQueue *queue)
Definition: SDL_dataqueue.c:141
NULL
#define NULL
Definition: begin_code.h:164
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_SetError
#define SDL_SetError
Definition: SDL_dynapi_overrides.h:30
SDL_DataQueue::packet_size
size_t packet_size
Definition: SDL_dataqueue.c:40

## SDL_WriteToDataQueue()

int SDL_WriteToDataQueue | ( | SDL_DataQueue * | queue,
|  | const void * | data,
|  | const size_t | len
| ) |  |

Definition at line 175 of file SDL_dataqueue.c.

References AllocateDataQueuePacket(), SDL_DataQueuePacket::data, SDL_DataQueuePacket::datalen, SDL_DataQueue::head, SDL_DataQueuePacket::next, NULL, SDL_DataQueue::packet_size, SDL_DataQueue::pool, SDL_DataQueue::queued_bytes, SDL_assert, SDL_FreeDataQueueList(), SDL_InvalidParamError, SDL_memcpy, SDL_min, SDL_OutOfMemory, and SDL_DataQueue::tail.

Referenced by SDL_AudioStreamPut(), SDL_AudioStreamPutInternal(), SDL_BufferQueueFillCallback(), and SDL_QueueAudio().

{
size_t len = _len;
const Uint8 *data = (const Uint8 *) _data;
const size_t packet_size = queue ? queue->packet_size : 0;
SDL_DataQueuePacket *orighead;
SDL_DataQueuePacket *origtail;
size_t origlen;
size_t datalen;
if (!queue) {
return SDL_InvalidParamError("queue");
}
orighead = queue->head;
origtail = queue->tail;
origlen = origtail ? origtail->datalen : 0;
while (len > 0) {
SDL_DataQueuePacket *packet = queue->tail;
SDL_assert(!packet || (packet->datalen <= packet_size));
if (!packet || (packet->datalen >= packet_size)) {
/* tail packet missing or completely full; we need a new packet. */
packet = AllocateDataQueuePacket(queue);
if (!packet) {
/* uhoh, reset so we've queued nothing new, free what we can. */
if (!origtail) {
packet = queue->head; /* whole queue. */
} else {
packet = origtail->next; /* what we added to existing queue. */
origtail->next = NULL;
origtail->datalen = origlen;
}
queue->head = orighead;
queue->tail = origtail;
queue->pool = NULL;
SDL_FreeDataQueueList(packet); /* give back what we can. */
return SDL_OutOfMemory();
}
}
datalen = SDL_min(len, packet_size - packet->datalen);
SDL_memcpy(packet->data + packet->datalen, data, datalen);
data += datalen;
len -= datalen;
packet->datalen += datalen;
queue->queued_bytes += datalen;
}
return 0;
}
SDL_DataQueue::head
SDL_DataQueuePacket * head
Definition: SDL_dataqueue.c:37
SDL_min
#define SDL_min(x, y)
Definition: SDL_stdinc.h:406
SDL_DataQueue::queued_bytes
size_t queued_bytes
Definition: SDL_dataqueue.c:41
SDL_DataQueue::pool
SDL_DataQueuePacket * pool
Definition: SDL_dataqueue.c:39
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: SDL_opengl.h:1974
SDL_FreeDataQueueList
static void SDL_FreeDataQueueList(SDL_DataQueuePacket *packet)
Definition: SDL_dataqueue.c:45
SDL_InvalidParamError
#define SDL_InvalidParamError(param)
Definition: SDL_error.h:54
SDL_DataQueuePacket::data
Uint8 data[SDL_VARIABLE_LENGTH_ARRAY]
Definition: SDL_dataqueue.c:32
len
GLenum GLsizei len
Definition: SDL_opengl_glext.h:2926
SDL_DataQueuePacket::next
struct SDL_DataQueuePacket * next
Definition: SDL_dataqueue.c:31
SDL_memcpy
#define SDL_memcpy
Definition: SDL_dynapi_overrides.h:387
SDL_DataQueuePacket
Definition: SDL_dataqueue.c:27
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:157
SDL_DataQueuePacket::datalen
size_t datalen
Definition: SDL_dataqueue.c:29
SDL_DataQueue::tail
SDL_DataQueuePacket * tail
Definition: SDL_dataqueue.c:38
AllocateDataQueuePacket
static SDL_DataQueuePacket * AllocateDataQueuePacket(SDL_DataQueue *queue)
Definition: SDL_dataqueue.c:141
SDL_assert
#define SDL_assert(condition)
Definition: SDL_assert.h:169
NULL
#define NULL
Definition: begin_code.h:164
SDL_OutOfMemory
#define SDL_OutOfMemory()
Definition: SDL_error.h:52
SDL_DataQueue::packet_size
size_t packet_size
Definition: SDL_dataqueue.c:40