FreeWRL/FreeX3D  3.0.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 
41 static int _noisy = 0;
42 /* ************************************************************************** */
43 /* ******************************** Vector ********************************** */
44 /* ************************************************************************** */
45 
46 /* Constructor/destructor */
47 
48 struct 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 
73 void 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 }
88 #if defined(WRAP_MALLOC) || defined(DEBUG_MALLOC)
89 void deleteVectorDebug_(char *file, int line, int elSize, struct Vector** myp) {
90  struct Vector *me = *myp;
91 
92  if (!me) {
93  //ConsoleMessage ("Vector - already empty");
94  return;
95  }
96 
97  ASSERT(me);
98  if(_noisy) printf("vector, deleting me %p data %p at %s:%d\n",me,me->data,file,line);
99  if(me->data) {freewrlFree(line,file,me->data);}
100  freewrlFree(line + 1,file,me);
101  *myp = NULL;
102 }
103 #endif
104 
105 /* Ensures there's at least one space free. */
106 void vector_ensureSpace_(int elSize, struct Vector* me, char *fi, int line) {
107  ASSERT(me);
108  if (me->n>me->allocn)
109  {
110  ASSERT(FALSE);
111  }
112  if(me->n==me->allocn) {
113  int istart, iend;
114  istart = me->allocn;
115  if(me->allocn)
116  {
117  me->allocn*=2;
118  }
119  else
120  {
121  me->allocn=1;
122  me->n = 0;
123  }
124  iend = me->allocn;
125 #ifdef DEBUG_MALLOC
126  me->data=freewrlRealloc(line, fi,me->data, elSize*me->allocn);
127 #else
128  me->data=REALLOC(me->data, elSize*me->allocn);
129 #endif
130  //if(iend > istart){
131  // char *cdata = (char*)me->data;
132  // memset(&cdata[istart*elSize],0,(iend-istart)*elSize);
133  //}
134  #ifdef DEBUG_MALLOC
135  if(_noisy) printf ("vector, ensureSpace, me %p, data %p\n",me, me->data);
136  #endif
137  ASSERT(me->data);
138  }
139  ASSERT(me->n<me->allocn);
140 }
141 
142 void vector_popBack_(struct Vector* me, size_t count)
143 {
144  ASSERT(!vector_empty(me));
145  me->n -= count;
146 
147  #ifdef DEBUG_MALLOC
148  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);
149  #endif
150 }
151 
152 /* Shrinks the vector to allocn==n. */
153 void vector_shrink_(int elSize, struct Vector* me) {
154  void *oldData;
155  ASSERT(me);
156  ASSERT(me->allocn>=me->n);
157  if(me->n==me->allocn) return;
158 
159  me->allocn=me->n;
160  oldData = me->data;
161  me->data=REALLOC(oldData, elSize*me->allocn);
162 
163  #ifdef DEBUG_MALLOC
164  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);
165  #endif
166 
167  //if (!me->data)
168  //{
169  // 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
170  //}
171  ASSERT(!me->allocn || me->data);
172 }
173 
174 void* vector_releaseData_(int elSize, struct Vector* me) {
175  void* ret;
176 
177  vector_shrink_(elSize, me);
178  ret=me->data;
179  #ifdef DEBUG_MALLOC
180  if(_noisy) printf ("vector, me %p data %p\n",me, me->data);
181  #endif
182 
183  me->data=NULL;
184  me->n = 0;
185  me->allocn = 0;
186  return ret;
187 }
188 
189 void vector_removeElement(int elSize,struct Vector* myp, int element)
190 {
191  struct Vector *me = myp;
192  if(me){
193  if(me->data && me->n > 0 && element < me->n && element > -1) {
194  char *el0,*el1;
195  int i;
196  for(i=element;i<me->n;i++){
197  el0 = (char *)(me->data) + i*elSize;
198  el1 = el0 + elSize;
199  memcpy(el0,el1,elSize);
200  }
201  me->n--;
202 
203  #ifdef DEBUG_MALLOC
204  if(_noisy) printf ("vector, removing element me 0x%016llx data 0x%016llx\n", (unsigned long long)me, (unsigned long long)me->data);
205  #endif
206 
207  //me->n--; two of these
208  }
209  }
210 }
Definition: Vector.h:36