FreeWRL / FreeX3D 4.3.0
Vector.c
1/*
2
3
4???
5
6*/
7
8/****************************************************************************
9 This file is part of the FreeWRL/FreeX3D Distribution.
10
11 Copyright 2009 CRC Canada. (http://www.crc.gc.ca)
12
13 FreeWRL/FreeX3D is free software: you can redistribute it and/or modify
14 it under the terms of the GNU Lesser Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 FreeWRL/FreeX3D is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with FreeWRL/FreeX3D. If not, see <http://www.gnu.org/licenses/>.
25****************************************************************************/
26
27
28
29#include <config.h>
30#include <system.h>
31#include <display.h>
32#include <internal.h>
33
34#include <libFreeWRL.h>
35
36#include "../vrml_parser/Structs.h"
37#include "../main/headers.h"
38
39#include "Vector.h"
40
41static int _noisy = 0;
42/* ************************************************************************** */
43/* ******************************** Vector ********************************** */
44/* ************************************************************************** */
45
46/* Constructor/destructor */
47
48struct Vector* newVector_(int elSize, int initSize,char *fi, int line) {
49 struct Vector* ret;
50#ifdef DEBUG_MALLOC
51 //inherit __line__ and __file__ from particular spot in code that does newVector
52 ret=(struct Vector *)freewrlMalloc(line,fi,sizeof(struct Vector), FALSE);
53#else
54 ret=MALLOC(struct Vector *, sizeof(struct Vector));
55#endif
56 ASSERT(ret);
57 ret->n=0;
58 ret->allocn=initSize;
59#ifdef DEBUG_MALLOC
60 //inherit __line__ and __file__ from particular spot in code that does newVector
61 ret->data=(void *)freewrlMalloc(line+1, fi,elSize*ret->allocn, FALSE);
62#else
63 ret->data=MALLOC(void *, elSize*ret->allocn);
64#endif
65 ASSERT(ret->data);
66 #ifdef DEBUG_MALLOC2
67 ConsoleMessage ("vector, new %x, data %x, size %d at %s:%d",ret, ret->data, initSize,fi,line);
68 #endif
69
70 return ret;
71}
72
73void deleteVector_(int elSize, struct Vector** myp) {
74
75
76 struct Vector *me = *myp;
77
78 if (!me) {
79 //ConsoleMessage ("Vector - already empty");
80 return;
81 }
82
83 ASSERT(me);
84 if(me->data) {FREE_IF_NZ(me->data);}
85 FREE_IF_NZ(me);
86 *myp = NULL;
87}
88void vector_clear(struct Vector* me) {
89 //clear out any allocated data, but preserve the vector for vector_pushBack
90 if (!me) {
91 //ConsoleMessage ("Vector - already empty");
92 return;
93 }
94
95 ASSERT(me);
96 if(me->data) {FREE_IF_NZ(me->data);}
97 me->data = NULL;
98 me->allocn = 0;
99 me->n = 0;
100}
101#if defined(WRAP_MALLOC) || defined(DEBUG_MALLOC)
102void deleteVectorDebug_(char *file, int line, int elSize, struct Vector** myp) {
103 struct Vector *me = *myp;
104
105 if (!me) {
106 //ConsoleMessage ("Vector - already empty");
107 return;
108 }
109
110 ASSERT(me);
111 if(_noisy) printf("vector, deleting me %p data %p at %s:%d\n",me,me->data,file,line);
112 if(me->data) {freewrlFree(line,file,me->data);}
113 freewrlFree(line + 1,file,me);
114 *myp = NULL;
115}
116#endif
117
118/* Ensures there's at least one space free. */
119void vector_ensureSpace_(int elSize, struct Vector* me, char *fi, int line) {
120 ASSERT(me);
121 if (me->n > me->allocn)
122 {
123 ASSERT(FALSE);
124 }
125 if(me->n == me->allocn) {
126 int istart, iend;
127 istart = me->allocn;
128 if(me->allocn)
129 {
130 me->allocn*=2;
131 }
132 else
133 {
134 me->allocn=1;
135 me->n = 0;
136 }
137 iend = me->allocn;
138#ifdef DEBUG_MALLOC
139 me->data=freewrlRealloc(line, fi,me->data, elSize*me->allocn);
140#else
141 me->data=REALLOC(me->data, elSize*me->allocn);
142#endif
143 //if(iend > istart){
144 // char *cdata = (char*)me->data;
145 // memset(&cdata[istart*elSize],0,(iend-istart)*elSize);
146 //}
147 #ifdef DEBUG_MALLOC
148 if(_noisy) printf ("vector, ensureSpace, me %p, data %p\n",me, me->data);
149 #endif
150 ASSERT(me->data);
151 }
152 ASSERT(me->n<me->allocn);
153}
154
155void vector_popBack_(struct Vector* me, size_t count)
156{
157 ASSERT(!vector_empty(me));
158 me->n -= count;
159
160 #ifdef DEBUG_MALLOC
161 if(_noisy) printf ("vector, popping back, me 0x%016llx, data 0x%016llx n %zu\n", (unsigned long long)me, (unsigned long long)me->data, me->n);
162 #endif
163}
164
165/* Shrinks the vector to allocn==n. */
166void vector_shrink_(int elSize, struct Vector* me) {
167 void *oldData;
168 ASSERT(me);
169 ASSERT(me->allocn>=me->n);
170 if(me->n==me->allocn) return;
171
172 me->allocn=me->n;
173 oldData = me->data;
174 me->data=REALLOC(oldData, elSize*me->allocn);
175
176 #ifdef DEBUG_MALLOC
177 if(_noisy) printf ("vector, shrink, me 0x%016llx, data 0x%016llx\n size %zu allocatedSize %zu", (unsigned long long)me, (unsigned long long)me->data, me->n, me->allocn);
178 #endif
179
180 //if (!me->data)
181 //{
182 // FREE_IF_NZ(oldData); //bombs in win32 due to REALLOC above doing the equivalent of freeing the memory, so oldData is pointing to invalid memory
183 //}
184 ASSERT(!me->allocn || me->data);
185}
186
187void* vector_releaseData_(int elSize, struct Vector* me) {
188 void* ret;
189
190 vector_shrink_(elSize, me);
191 ret=me->data;
192 #ifdef DEBUG_MALLOC
193 if(_noisy) printf ("vector, me %p data %p\n",me, me->data);
194 #endif
195
196 me->data=NULL;
197 me->n = 0;
198 me->allocn = 0;
199 return ret;
200}
201
202void vector_removeElement(int elSize,struct Vector* myp, int element)
203{
204 struct Vector *me = myp;
205 if(me){
206 if(me->data && me->n > 0 && element < me->n && element > -1) {
207 char *el0,*el1;
208 int i;
209 for(i=element;i<me->n;i++){
210 el0 = (char *)(me->data) + i*elSize;
211 el1 = el0 + elSize;
212 memcpy(el0,el1,elSize);
213 }
214 me->n--;
215
216 #ifdef DEBUG_MALLOC
217 if(_noisy) printf ("vector, removing element me 0x%016llx data 0x%016llx\n", (unsigned long long)me, (unsigned long long)me->data);
218 #endif
219
220 //me->n--; two of these
221 }
222 }
223}