src/SDL_dataqueue.c
changeset 10681 34b25c97b17f
child 10737 3406a0f8b041
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/SDL_dataqueue.c	Tue Dec 06 02:23:54 2016 -0500
     1.3 @@ -0,0 +1,260 @@
     1.4 +/*
     1.5 +  Simple DirectMedia Layer
     1.6 +  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +#include "./SDL_internal.h"
    1.26 +#include "SDL.h"
    1.27 +#include "./SDL_dataqueue.h"
    1.28 +#include "SDL_assert.h"
    1.29 +
    1.30 +typedef struct SDL_DataQueuePacket
    1.31 +{
    1.32 +    size_t datalen;  /* bytes currently in use in this packet. */
    1.33 +    size_t startpos;  /* bytes currently consumed in this packet. */
    1.34 +    struct SDL_DataQueuePacket *next;  /* next item in linked list. */
    1.35 +    Uint8 data[SDL_VARIABLE_LENGTH_ARRAY];  /* packet data */
    1.36 +} SDL_DataQueuePacket;
    1.37 +
    1.38 +struct SDL_DataQueue
    1.39 +{
    1.40 +    SDL_DataQueuePacket *head; /* device fed from here. */
    1.41 +    SDL_DataQueuePacket *tail; /* queue fills to here. */
    1.42 +    SDL_DataQueuePacket *pool; /* these are unused packets. */
    1.43 +    size_t packet_size;   /* size of new packets */
    1.44 +    size_t queued_bytes;  /* number of bytes of data in the queue. */
    1.45 +};
    1.46 +
    1.47 +static void
    1.48 +SDL_FreeDataQueueList(SDL_DataQueuePacket *packet)
    1.49 +{
    1.50 +    while (packet) {
    1.51 +        SDL_DataQueuePacket *next = packet->next;
    1.52 +        SDL_free(packet);
    1.53 +        packet = next;
    1.54 +    }
    1.55 +}
    1.56 +
    1.57 +
    1.58 +/* this all expects that you managed thread safety elsewhere. */
    1.59 +
    1.60 +SDL_DataQueue *
    1.61 +SDL_NewDataQueue(const size_t _packetlen, const size_t initialslack)
    1.62 +{
    1.63 +    SDL_DataQueue *queue = (SDL_DataQueue *) SDL_malloc(sizeof (SDL_DataQueue));
    1.64 +
    1.65 +    if (!queue) {
    1.66 +        SDL_OutOfMemory();
    1.67 +        return NULL;
    1.68 +    } else {
    1.69 +        const size_t packetlen = _packetlen ? _packetlen : 1024;
    1.70 +        const size_t wantpackets = (initialslack + (packetlen - 1)) / packetlen;
    1.71 +        size_t i;
    1.72 +
    1.73 +        SDL_zerop(queue);
    1.74 +        queue->packet_size = packetlen;
    1.75 +
    1.76 +        for (i = 0; i < wantpackets; i++) {
    1.77 +            SDL_DataQueuePacket *packet = (SDL_DataQueuePacket *) SDL_malloc(sizeof (SDL_DataQueuePacket) + packetlen);
    1.78 +            if (packet) { /* don't care if this fails, we'll deal later. */
    1.79 +                packet->datalen = 0;
    1.80 +                packet->startpos = 0;
    1.81 +                packet->next = queue->pool;
    1.82 +                queue->pool = packet;
    1.83 +            }
    1.84 +        }
    1.85 +    }
    1.86 +
    1.87 +    return queue;
    1.88 +}
    1.89 +
    1.90 +void
    1.91 +SDL_FreeDataQueue(SDL_DataQueue *queue)
    1.92 +{
    1.93 +    if (queue) {
    1.94 +        SDL_FreeDataQueueList(queue->head);
    1.95 +        SDL_FreeDataQueueList(queue->pool);
    1.96 +        SDL_free(queue);
    1.97 +    }
    1.98 +}
    1.99 +
   1.100 +void
   1.101 +SDL_ClearDataQueue(SDL_DataQueue *queue, const size_t slack)
   1.102 +{
   1.103 +    const size_t packet_size = queue ? queue->packet_size : 1;
   1.104 +    const size_t slackpackets = (slack + (packet_size-1)) / packet_size;
   1.105 +    SDL_DataQueuePacket *packet;
   1.106 +    SDL_DataQueuePacket *prev = NULL;
   1.107 +    size_t i;
   1.108 +
   1.109 +    if (!queue) {
   1.110 +        return;
   1.111 +    }
   1.112 +
   1.113 +    packet = queue->head;
   1.114 +
   1.115 +    /* merge the available pool and the current queue into one list. */
   1.116 +    if (packet) {
   1.117 +        queue->tail->next = queue->pool;
   1.118 +    } else {
   1.119 +        packet = queue->pool;
   1.120 +    }
   1.121 +
   1.122 +    /* Remove the queued packets from the device. */
   1.123 +    queue->tail = NULL;
   1.124 +    queue->head = NULL;
   1.125 +    queue->queued_bytes = 0;
   1.126 +    queue->pool = packet;
   1.127 +
   1.128 +    /* Optionally keep some slack in the pool to reduce malloc pressure. */
   1.129 +    for (i = 0; packet && (i < slackpackets); i++) {
   1.130 +        prev = packet;
   1.131 +        packet = packet->next;
   1.132 +    }
   1.133 +
   1.134 +    if (prev) {
   1.135 +        prev->next = NULL;
   1.136 +    } else {
   1.137 +        queue->pool = NULL;
   1.138 +    }
   1.139 +
   1.140 +    SDL_FreeDataQueueList(packet);  /* free extra packets */
   1.141 +}
   1.142 +
   1.143 +int
   1.144 +SDL_WriteToDataQueue(SDL_DataQueue *queue, const void *_data, const size_t _len)
   1.145 +{
   1.146 +    size_t len = _len;
   1.147 +    const Uint8 *data = (const Uint8 *) _data;
   1.148 +    const size_t packet_size = queue ? queue->packet_size : 0;
   1.149 +    SDL_DataQueuePacket *orighead;
   1.150 +    SDL_DataQueuePacket *origtail;
   1.151 +    size_t origlen;
   1.152 +    size_t datalen;
   1.153 +
   1.154 +    if (!queue) {
   1.155 +        return SDL_InvalidParamError("queue");
   1.156 +    }
   1.157 +
   1.158 +    orighead = queue->head;
   1.159 +    origtail = queue->tail;
   1.160 +    origlen = origtail ? origtail->datalen : 0;
   1.161 +
   1.162 +    while (len > 0) {
   1.163 +        SDL_DataQueuePacket *packet = queue->tail;
   1.164 +        SDL_assert(!packet || (packet->datalen <= packet_size));
   1.165 +        if (!packet || (packet->datalen >= packet_size)) {
   1.166 +            /* tail packet missing or completely full; we need a new packet. */
   1.167 +            packet = queue->pool;
   1.168 +            if (packet != NULL) {
   1.169 +                /* we have one available in the pool. */
   1.170 +                queue->pool = packet->next;
   1.171 +            } else {
   1.172 +                /* Have to allocate a new one! */
   1.173 +                packet = (SDL_DataQueuePacket *) SDL_malloc(sizeof (SDL_DataQueuePacket) + packet_size);
   1.174 +                if (packet == NULL) {
   1.175 +                    /* uhoh, reset so we've queued nothing new, free what we can. */
   1.176 +                    if (!origtail) {
   1.177 +                        packet = queue->head;  /* whole queue. */
   1.178 +                    } else {
   1.179 +                        packet = origtail->next;  /* what we added to existing queue. */
   1.180 +                        origtail->next = NULL;
   1.181 +                        origtail->datalen = origlen;
   1.182 +                    }
   1.183 +                    queue->head = orighead;
   1.184 +                    queue->tail = origtail;
   1.185 +                    queue->pool = NULL;
   1.186 +
   1.187 +                    SDL_FreeDataQueueList(packet);  /* give back what we can. */
   1.188 +
   1.189 +                    return SDL_OutOfMemory();
   1.190 +                }
   1.191 +            }
   1.192 +            packet->datalen = 0;
   1.193 +            packet->startpos = 0;
   1.194 +            packet->next = NULL;
   1.195 +                
   1.196 +            SDL_assert((queue->head != NULL) == (queue->queued_bytes != 0));
   1.197 +            if (queue->tail == NULL) {
   1.198 +                queue->head = packet;
   1.199 +            } else {
   1.200 +                queue->tail->next = packet;
   1.201 +            }
   1.202 +            queue->tail = packet;
   1.203 +        }
   1.204 +
   1.205 +        datalen = SDL_min(len, packet_size - packet->datalen);
   1.206 +        SDL_memcpy(packet->data + packet->datalen, data, datalen);
   1.207 +        data += datalen;
   1.208 +        len -= datalen;
   1.209 +        packet->datalen += datalen;
   1.210 +        queue->queued_bytes += datalen;
   1.211 +    }
   1.212 +
   1.213 +    return 0;
   1.214 +}
   1.215 +
   1.216 +size_t
   1.217 +SDL_ReadFromDataQueue(SDL_DataQueue *queue, void *_buf, const size_t _len)
   1.218 +{
   1.219 +    size_t len = _len;
   1.220 +    Uint8 *buf = (Uint8 *) _buf;
   1.221 +    Uint8 *ptr = buf;
   1.222 +    SDL_DataQueuePacket *packet;
   1.223 +
   1.224 +    if (!queue) {
   1.225 +        return 0;
   1.226 +    }
   1.227 +
   1.228 +    while ((len > 0) && ((packet = queue->head) != NULL)) {
   1.229 +        const size_t avail = packet->datalen - packet->startpos;
   1.230 +        const size_t cpy = SDL_min(len, avail);
   1.231 +        SDL_assert(queue->queued_bytes >= avail);
   1.232 +
   1.233 +        SDL_memcpy(ptr, packet->data + packet->startpos, cpy);
   1.234 +        packet->startpos += cpy;
   1.235 +        ptr += cpy;
   1.236 +        queue->queued_bytes -= cpy;
   1.237 +        len -= cpy;
   1.238 +
   1.239 +        if (packet->startpos == packet->datalen) {  /* packet is done, put it in the pool. */
   1.240 +            queue->head = packet->next;
   1.241 +            SDL_assert((packet->next != NULL) || (packet == queue->tail));
   1.242 +            packet->next = queue->pool;
   1.243 +            queue->pool = packet;
   1.244 +        }
   1.245 +    }
   1.246 +
   1.247 +    SDL_assert((queue->head != NULL) == (queue->queued_bytes != 0));
   1.248 +
   1.249 +    if (queue->head == NULL) {
   1.250 +        queue->tail = NULL;  /* in case we drained the queue entirely. */
   1.251 +    }
   1.252 +
   1.253 +    return (size_t) (ptr - buf);
   1.254 +}
   1.255 +
   1.256 +size_t
   1.257 +SDL_CountDataQueue(SDL_DataQueue *queue)
   1.258 +{
   1.259 +    return queue ? queue->queued_bytes : 0;
   1.260 +}
   1.261 +
   1.262 +/* vi: set ts=4 sw=4 expandtab: */
   1.263 +