|
Blender
V2.59
|
00001 /* 00002 * $Id: gsqueue.c 35246 2011-02-27 20:37:56Z jesterking $ 00003 * 00004 * ***** BEGIN GPL LICENSE BLOCK ***** 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU General Public License 00008 * as published by the Free Software Foundation; either version 2 00009 * of the License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License 00017 * along with this program; if not, write to the Free Software Foundation, 00018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 * 00020 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. 00021 * All rights reserved. 00022 * 00023 * The Original Code is: all of this file. 00024 * 00025 * Contributor(s): none yet. 00026 * 00027 * ***** END GPL LICENSE BLOCK ***** 00028 */ 00029 00034 #include <string.h> 00035 00036 #include "MEM_guardedalloc.h" 00037 #include "BLI_gsqueue.h" 00038 00039 typedef struct _GSQueueElem GSQueueElem; 00040 struct _GSQueueElem { 00041 GSQueueElem *next; 00042 }; 00043 00044 struct _GSQueue { 00045 GSQueueElem *head; 00046 GSQueueElem *tail; 00047 int elem_size; 00048 }; 00049 00050 GSQueue *BLI_gsqueue_new(int elem_size) 00051 { 00052 GSQueue *gq= MEM_mallocN(sizeof(*gq), "gqueue_new"); 00053 gq->head= gq->tail= NULL; 00054 gq->elem_size= elem_size; 00055 00056 return gq; 00057 } 00058 00059 int BLI_gsqueue_is_empty(GSQueue *gq) 00060 { 00061 return (gq->head==NULL); 00062 } 00063 00064 int BLI_gsqueue_size(GSQueue *gq) 00065 { 00066 GSQueueElem *elem; 00067 int size= 0; 00068 00069 for(elem=gq->head; elem; elem=elem->next) 00070 size++; 00071 00072 return size; 00073 } 00074 00075 void BLI_gsqueue_peek(GSQueue *gq, void *item_r) 00076 { 00077 memcpy(item_r, &gq->head[1], gq->elem_size); 00078 } 00079 void BLI_gsqueue_pop(GSQueue *gq, void *item_r) 00080 { 00081 GSQueueElem *elem= gq->head; 00082 if (elem==gq->tail) { 00083 gq->head= gq->tail= NULL; 00084 } else { 00085 gq->head= gq->head->next; 00086 } 00087 00088 if (item_r) memcpy(item_r, &elem[1], gq->elem_size); 00089 MEM_freeN(elem); 00090 } 00091 void BLI_gsqueue_push(GSQueue *gq, void *item) 00092 { 00093 GSQueueElem *elem; 00094 00095 /* compare: prevent events added double in row */ 00096 if (!BLI_gsqueue_is_empty(gq)) { 00097 if(0==memcmp(item, &gq->head[1], gq->elem_size)) 00098 return; 00099 } 00100 elem= MEM_mallocN(sizeof(*elem)+gq->elem_size, "gqueue_push"); 00101 memcpy(&elem[1], item, gq->elem_size); 00102 elem->next= NULL; 00103 00104 if (BLI_gsqueue_is_empty(gq)) { 00105 gq->tail= gq->head= elem; 00106 } else { 00107 gq->tail= gq->tail->next= elem; 00108 } 00109 } 00110 void BLI_gsqueue_pushback(GSQueue *gq, void *item) 00111 { 00112 GSQueueElem *elem= MEM_mallocN(sizeof(*elem)+gq->elem_size, "gqueue_push"); 00113 memcpy(&elem[1], item, gq->elem_size); 00114 elem->next= gq->head; 00115 00116 if (BLI_gsqueue_is_empty(gq)) { 00117 gq->head= gq->tail= elem; 00118 } else { 00119 gq->head= elem; 00120 } 00121 } 00122 00123 void BLI_gsqueue_free(GSQueue *gq) 00124 { 00125 while (gq->head) { 00126 BLI_gsqueue_pop(gq, NULL); 00127 } 00128 MEM_freeN(gq); 00129 } 00130 00131