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}
154void testVector_(int elSize, struct Vector* me) {
155 //goal trigger memory checking for over-runs without changing data
156 me->data = realloc(me->data, me->allocn * elSize);
157}
158
159void vector_popBack_(struct Vector* me, size_t count)
160{
161 ASSERT(!vector_empty(me));
162 me->n -= count;
163
164 #ifdef DEBUG_MALLOC
165 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);
166 #endif
167}
168
169/* Shrinks the vector to allocn==n. */
170void vector_shrink_(int elSize, struct Vector* me) {
171 void *oldData;
172 ASSERT(me);
173 ASSERT(me->allocn>=me->n);
174 if(me->n==me->allocn) return;
175
176 me->allocn=me->n;
177 oldData = me->data;
178 me->data=REALLOC(oldData, elSize*me->allocn);
179
180 #ifdef DEBUG_MALLOC
181 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);
182 #endif
183
184 //if (!me->data)
185 //{
186 // 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
187 //}
188 ASSERT(!me->allocn || me->data);
189}
190
191void* vector_releaseData_(int elSize, struct Vector* me) {
192 void* ret;
193
194 vector_shrink_(elSize, me);
195 ret=me->data;
196 #ifdef DEBUG_MALLOC
197 if(_noisy) printf ("vector, me %p data %p\n",me, me->data);
198 #endif
199
200 me->data=NULL;
201 me->n = 0;
202 me->allocn = 0;
203 return ret;
204}
205
206void vector_removeElement(int elSize,struct Vector* myp, int element)
207{
208 struct Vector *me = myp;
209 if(me){
210 if(me->data && me->n > 0 && element < me->n && element > -1) {
211 char *el0,*el1;
212 int i;
213 for(i=element;i<me->n;i++){
214 el0 = (char *)(me->data) + i*elSize;
215 el1 = el0 + elSize;
216 memcpy(el0,el1,elSize);
217 }
218 me->n--;
219
220 #ifdef DEBUG_MALLOC
221 if(_noisy) printf ("vector, removing element me 0x%016llx data 0x%016llx\n", (unsigned long long)me, (unsigned long long)me->data);
222 #endif
223
224 //me->n--; two of these
225 }
226 }
227}