Path: blob/21.2-virgl/src/gallium/auxiliary/util/u_fifo.h
4561 views
/**************************************************************************1*2* Copyright © 2009 Jakob Bornecrantz3*4* Permission is hereby granted, free of charge, to any person obtaining a5* copy of this software and associated documentation files (the "Software"),6* to deal in the Software without restriction, including without limitation7* the rights to use, copy, modify, merge, publish, distribute, sublicense,8* and/or sell copies of the Software, and to permit persons to whom the9* Software is furnished to do so, subject to the following conditions:10*11* The above copyright notice and this permission notice (including the next12* paragraph) shall be included in all copies or substantial portions of the13* Software.14*15* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR16* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,17* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL18* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER19* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING20* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER21* DEALINGS IN THE SOFTWARE.22*23**************************************************************************/2425#ifndef U_FIFO_H26#define U_FIFO_H2728#include "util/u_memory.h"2930struct util_fifo31{32size_t head;33size_t tail;34size_t num;35size_t size;36};3738static inline struct util_fifo *39u_fifo_create(size_t size)40{41struct util_fifo *fifo;42fifo = MALLOC(sizeof(*fifo) + size * sizeof(void*));4344fifo->head = 0;45fifo->tail = 0;46fifo->num = 0;47fifo->size = size;4849return fifo;50}5152static inline boolean53u_fifo_add(struct util_fifo *fifo, void *ptr)54{55void **array = (void**)&fifo[1];56if (fifo->num >= fifo->size)57return FALSE;5859if (++fifo->head >= fifo->size)60fifo->head = 0;6162array[fifo->head] = ptr;6364++fifo->num;6566return TRUE;67}6869static inline boolean70u_fifo_pop(struct util_fifo *fifo, void **ptr)71{72void **array = (void**)&fifo[1];7374if (!fifo->num)75return FALSE;7677if (++fifo->tail >= fifo->size)78fifo->tail = 0;7980*ptr = array[fifo->tail];8182--fifo->num;8384return TRUE;85}8687static inline void88u_fifo_destroy(struct util_fifo *fifo)89{90FREE(fifo);91}9293#endif949596