| 
									
										
										
										
											2011-02-23 10:52:22 +00:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2008-04-16 22:40:48 +00:00
										 |  |  |  * ***** BEGIN GPL LICENSE BLOCK ***** | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is free software; you can redistribute it and/or | 
					
						
							|  |  |  |  * modify it under the terms of the GNU General Public License | 
					
						
							|  |  |  |  * as published by the Free Software Foundation; either version 2 | 
					
						
							| 
									
										
										
										
											2008-04-16 22:40:48 +00:00
										 |  |  |  * of the License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  * along with this program; if not, write to the Free Software Foundation, | 
					
						
							| 
									
										
										
										
											2010-02-12 13:34:04 +00:00
										 |  |  |  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  |  * | 
					
						
							|  |  |  |  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The Original Code is: all of this file. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Contributor(s): none yet. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2008-04-16 22:40:48 +00:00
										 |  |  |  * ***** END GPL LICENSE BLOCK ***** | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2011-02-27 20:37:56 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** \file blender/blenlib/intern/gsqueue.c
 | 
					
						
							|  |  |  |  *  \ingroup bli | 
					
						
							| 
									
										
										
										
											2014-08-07 14:42:47 +10:00
										 |  |  |  * | 
					
						
							|  |  |  |  * \brief A generic structure queue | 
					
						
							|  |  |  |  * (a queue for fixed length generally small) structures. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \note Only use this if you need (first-in-first-out), | 
					
						
							| 
									
										
										
										
											2015-07-31 22:59:02 +10:00
										 |  |  |  * otherwise #BLI_Stack is more efficient (first-in-last-out). | 
					
						
							| 
									
										
										
										
											2011-02-27 20:37:56 +00:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | #include <string.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include "MEM_guardedalloc.h"
 | 
					
						
							| 
									
										
										
										
											2013-03-09 05:35:49 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "BLI_utildefines.h"
 | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | #include "BLI_gsqueue.h"
 | 
					
						
							| 
									
										
										
										
											2013-09-01 03:43:10 +00:00
										 |  |  | #include "BLI_strict_flags.h"
 | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | typedef struct _GSQueueElem GSQueueElem; | 
					
						
							|  |  |  | struct _GSQueueElem { | 
					
						
							|  |  |  | 	GSQueueElem *next; | 
					
						
							| 
									
										
										
										
											2014-06-15 04:30:32 +10:00
										 |  |  | 	char data[0]; | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct _GSQueue { | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 	GSQueueElem *head; | 
					
						
							|  |  |  | 	GSQueueElem *tail; | 
					
						
							| 
									
										
										
										
											2014-06-15 03:48:22 +10:00
										 |  |  | 	size_t elem_size; | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-24 01:51:54 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Create a new GSQueue. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \param elem_size The size of the structures in the queue. | 
					
						
							|  |  |  |  * \retval The new queue | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-06-15 03:48:22 +10:00
										 |  |  | GSQueue *BLI_gsqueue_new(size_t elem_size) | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 	GSQueue *gq = MEM_mallocN(sizeof(*gq), "gqueue_new"); | 
					
						
							|  |  |  | 	gq->head = gq->tail = NULL; | 
					
						
							|  |  |  | 	gq->elem_size = elem_size; | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	return gq; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-24 01:51:54 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Query if the queue is empty | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2013-03-09 05:35:49 +00:00
										 |  |  | bool BLI_gsqueue_is_empty(GSQueue *gq) | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 	return (gq->head == NULL); | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-24 01:51:54 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Query number elements in the queue | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2010-01-22 11:10:24 +00:00
										 |  |  | int BLI_gsqueue_size(GSQueue *gq) | 
					
						
							|  |  |  | {  | 
					
						
							|  |  |  | 	GSQueueElem *elem; | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 	int size = 0; | 
					
						
							| 
									
										
										
										
											2010-01-22 11:10:24 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 	for (elem = gq->head; elem; elem = elem->next) | 
					
						
							| 
									
										
										
										
											2010-01-22 11:10:24 +00:00
										 |  |  | 		size++; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	return size; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-24 01:51:54 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Access the item at the head of the queue | 
					
						
							|  |  |  |  * without removing it. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-05-20 12:54:45 +10:00
										 |  |  |  * \param r_item: A pointer to an appropriately | 
					
						
							|  |  |  |  * sized structure (the size passed to #BLI_gsqueue_new) | 
					
						
							| 
									
										
										
										
											2013-03-24 01:51:54 +00:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-06-15 04:30:32 +10:00
										 |  |  | void BLI_gsqueue_peek(GSQueue *gq, void *r_item) | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-06-15 04:30:32 +10:00
										 |  |  | 	memcpy(r_item, &gq->head->data, gq->elem_size); | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2013-03-24 01:51:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Access the item at the head of the queue | 
					
						
							|  |  |  |  * and remove it. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-05-20 12:54:45 +10:00
										 |  |  |  * \param r_item: A pointer to an appropriately | 
					
						
							|  |  |  |  * sized structure (the size passed to #BLI_gsqueue_new). | 
					
						
							| 
									
										
										
										
											2013-03-24 01:51:54 +00:00
										 |  |  |  * Can be NULL if desired. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-06-15 04:30:32 +10:00
										 |  |  | void BLI_gsqueue_pop(GSQueue *gq, void *r_item) | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 	GSQueueElem *elem = gq->head; | 
					
						
							|  |  |  | 	if (elem == gq->tail) { | 
					
						
							|  |  |  | 		gq->head = gq->tail = NULL; | 
					
						
							| 
									
										
										
										
											2012-03-24 06:18:31 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 		gq->head = gq->head->next; | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2014-06-15 04:30:32 +10:00
										 |  |  | 	if (r_item) { | 
					
						
							|  |  |  | 		memcpy(r_item, elem->data, gq->elem_size); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 	MEM_freeN(elem); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-03-24 01:51:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Push an element onto the tail of the queue. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \param item A pointer to an appropriately | 
					
						
							|  |  |  |  * sized structure (the size passed to BLI_gsqueue_new). | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-06-15 04:30:32 +10:00
										 |  |  | void BLI_gsqueue_push(GSQueue *gq, const void *item) | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2006-12-20 17:57:56 +00:00
										 |  |  | 	GSQueueElem *elem; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/* compare: prevent events added double in row */ | 
					
						
							|  |  |  | 	if (!BLI_gsqueue_is_empty(gq)) { | 
					
						
							| 
									
										
										
										
											2014-06-15 04:30:32 +10:00
										 |  |  | 		if (0 == memcmp(item, gq->head->data, gq->elem_size)) | 
					
						
							| 
									
										
										
										
											2006-12-20 17:57:56 +00:00
										 |  |  | 			return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-06-15 03:48:22 +10:00
										 |  |  | 	elem = MEM_mallocN(sizeof(*elem) + gq->elem_size, "gqueue_push"); | 
					
						
							| 
									
										
										
										
											2014-06-15 04:30:32 +10:00
										 |  |  | 	memcpy(elem->data, item, gq->elem_size); | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 	elem->next = NULL; | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	if (BLI_gsqueue_is_empty(gq)) { | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 		gq->tail = gq->head = elem; | 
					
						
							| 
									
										
										
										
											2012-03-24 06:18:31 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 		gq->tail = gq->tail->next = elem; | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2013-03-24 01:51:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /**
 | 
					
						
							|  |  |  |  * Push an element back onto the head of the queue (so | 
					
						
							|  |  |  |  * it would be returned from the next call to BLI_gsqueue_pop). | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * \param item A pointer to an appropriately | 
					
						
							|  |  |  |  * sized structure (the size passed to BLI_gsqueue_new). | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2014-06-15 04:30:32 +10:00
										 |  |  | void BLI_gsqueue_pushback(GSQueue *gq, const void *item) | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2014-06-15 03:48:22 +10:00
										 |  |  | 	GSQueueElem *elem = MEM_mallocN(sizeof(*elem) + gq->elem_size, "gqueue_push"); | 
					
						
							| 
									
										
										
										
											2014-06-15 04:30:32 +10:00
										 |  |  | 	memcpy(elem->data, item, gq->elem_size); | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 	elem->next = gq->head; | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (BLI_gsqueue_is_empty(gq)) { | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 		gq->head = gq->tail = elem; | 
					
						
							| 
									
										
										
										
											2012-03-24 06:18:31 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	else { | 
					
						
							| 
									
										
										
										
											2012-05-12 15:13:06 +00:00
										 |  |  | 		gq->head = elem; | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-24 01:51:54 +00:00
										 |  |  | /**
 | 
					
						
							|  |  |  |  * Free the queue | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2002-10-12 11:37:38 +00:00
										 |  |  | void BLI_gsqueue_free(GSQueue *gq) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	while (gq->head) { | 
					
						
							|  |  |  | 		BLI_gsqueue_pop(gq, NULL); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	MEM_freeN(gq); | 
					
						
							|  |  |  | } |