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