Path: blob/master/libs/fluidsynth/src/utils/fluid_ringbuffer.c
4396 views
/* FluidSynth - A Software Synthesizer1*2* Copyright (C) 2003 Peter Hanappe and others.3*4* This library is free software; you can redistribute it and/or5* modify it under the terms of the GNU Lesser General Public License6* as published by the Free Software Foundation; either version 2.1 of7* the License, or (at your option) any later version.8*9* This library is distributed in the hope that it will be useful, but10* WITHOUT ANY WARRANTY; without even the implied warranty of11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU12* Lesser General Public License for more details.13*14* You should have received a copy of the GNU Lesser General Public15* License along with this library; if not, write to the Free16* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA17* 02110-1301, USA18*/1920/*21* Josh Green <[email protected]>22* 2009-05-2823*/2425#include "fluid_ringbuffer.h"26#include "fluid_sys.h"272829/**30* Create a lock free queue with a fixed maximum count and size of elements.31* @param count Count of elements in queue (fixed max number of queued elements)32* @return New lock free queue or NULL if out of memory (error message logged)33*34* Lockless FIFO queues don't use any locking mechanisms and can therefore be35* advantageous in certain situations, such as passing data between a lower36* priority thread and a higher "real time" thread, without potential lock37* contention which could stall the high priority thread. Note that there may38* only be one producer thread and one consumer thread.39*/40fluid_ringbuffer_t *41new_fluid_ringbuffer(int count, size_t elementsize)42{43fluid_ringbuffer_t *queue;4445fluid_return_val_if_fail(count > 0, NULL);4647queue = FLUID_NEW(fluid_ringbuffer_t);4849if(!queue)50{51FLUID_LOG(FLUID_ERR, "Out of memory");52return NULL;53}5455queue->array = FLUID_MALLOC(elementsize * count);5657if(!queue->array)58{59FLUID_LOG(FLUID_ERR, "Out of memory");60delete_fluid_ringbuffer(queue);61return NULL;62}6364/* Clear array, in case dynamic pointer reclaiming is being done */65FLUID_MEMSET(queue->array, 0, elementsize * count);6667queue->totalcount = count;68queue->elementsize = elementsize;69fluid_atomic_int_set(&queue->count, 0);70queue->in = 0;71queue->out = 0;7273return (queue);74}7576/**77* Free an event queue.78* @param queue Lockless queue instance79*80* Care must be taken when freeing a queue, to ensure that the consumer and81* producer threads will no longer access it.82*/83void84delete_fluid_ringbuffer(fluid_ringbuffer_t *queue)85{86fluid_return_if_fail(queue != NULL);87FLUID_FREE(queue->array);88FLUID_FREE(queue);89}909192