File: gvar.c

package info (click to toggle)
scilab 2.6-4
  • links: PTS
  • area: non-free
  • in suites: woody
  • size: 54,632 kB
  • ctags: 40,267
  • sloc: ansic: 267,851; fortran: 166,549; sh: 10,005; makefile: 4,119; tcl: 1,070; cpp: 233; csh: 143; asm: 135; perl: 130; java: 39
file content (354 lines) | stat: -rw-r--r-- 8,587 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/* Copyright INRIA */
/* basic management of a global variable list */

#include "gvar.h"
#include <stdlib.h>
#include <string.h>

/* CONSTANTS DEF */
#ifndef NULL
#define NULL 0
#endif

/* I want trace */
/* #define GVAR_TRACE */

/* size of a reallocation */
#define PAGESZ 20

#define gvar_nsiz (nsiz*4)
/* TYPES DEF */ /*******************************************/

/* the elements of the global var list */
typedef struct {
  Matrix *name;
  double *ptr;
} t_gvar;



/* GLOBAL STATIC VAR DEF */ /*******************************************/

/* the list itself */
static t_gvar *gvarLst=NULL;

/* the current size of the list */
static int gvarLstSz=0;
/* the smallest free element in the list */
static int curFreeElt=0;


/* SUBS DEF */ /*******************************************/



/********** reallocLst ***********/
int reallocLst()
     /* resize the gvar list */
     /* OUTPUT : result, if non zero, the operation has failed */
{
  t_gvar *newLst;
  
  newLst = (t_gvar *)realloc(gvarLst, (gvarLstSz +  PAGESZ) * sizeof(t_gvar));
  if (newLst == NULL ) return(-1);
  
  
  gvarLst = newLst;
  memset(gvarLst + gvarLstSz, (int)0, PAGESZ * sizeof(t_gvar) );
  gvarLstSz += PAGESZ;
  return(0);
}


/********** newRank ***********/
int newRank()
     /* get a free element in the gvar list*/
     /* OUTPUT : rank of the new element, negative if  */
     /*           the operation is impossible */
{
  int rank;
  int i;
  int res;

  rank = curFreeElt;
      
  if ( curFreeElt<gvarLstSz )
    {
      do  /* find the next free element */
	curFreeElt++;
      while ( (curFreeElt < gvarLstSz) && ((gvarLst[curFreeElt]).ptr != NULL ) );
      return( rank );
    }
  
  res = reallocLst();
  if (res==0) 
    {
      curFreeElt++;
      return(rank);
    }
  else return(-1);

}

/********** updateElt **********/
#ifndef __STDC__
void updateElt(rank, Mname, objPtr)
     int rank;
     Matrix *Mname;
     double *objPtr;
#else /* __STDC__ */
void updateElt( int rank, Matrix *Mname, double *objPtr )
#endif /* __STDC__ */
     /* update the fields of a gvar list element */
     /* INPUT  : -rank, element to update in the list */
     /*          -name, new name of the scilab variable */
     /*          -objPtr, new 'global' instance of the variable */
{
  
  /* memcpy( (gvarLst[rank]).name, name, gvar_nsiz * sizeof(int)); */
  (gvarLst[rank]).name = Mname;
  (gvarLst[rank]).ptr = objPtr;

}




/*********** newGvar **********/
#ifndef __STDC__
int newGvar(Mname, sciObj)
     Matrix *Mname;
     double *sciObj;
#else /* __STDC__ */
int newGvar( Matrix *Mname, double *sciObj)
#endif /* __STDC__ */
     /* add a scilab object to the list */
     /* INPUT  : -name, name of the sci var */
     /*          -sciObj, pointer on the scilab var in the sci stack */
     /* OUTPUT : 0-> OK, 1-> no more stack, 2-> can not add new elt */
     /*           to the list */
{
  int objSz; /* size in bytes of the scilab object */
  int rank; /* rank of the object in the gvar list */
  double *objPtr; /* ptr on the actual global object */
  Matrix *objName;
  int objNameSz;

  /* get the memory for the object */
  objSz = MatrixMemSize( sciObj );
  objPtr = malloc( objSz * sizeof(char) );
  if ( objPtr == NULL ) return(1); /* no more heap memory free */
  objNameSz = MatrixMemSize( Mname );
  objName = (Matrix *) malloc( objNameSz * sizeof(char) );

  /* get a place in the global var list */
  rank = newRank();
  if (rank <0 ) return(2); /* unable to get a free element in the global var list */
  
  /* copy the object */
  memcpy(objPtr, sciObj, objSz);
  memcpy(objName, Mname, objNameSz);
  /* update the info in the list */
  updateElt( rank, objName, objPtr );
  return 0;
}


#ifndef __STDC__
int Mstrcmp(Ms1, Ms2)
     Matrix *Ms1;
     Matrix *Ms2;
#else /* __STDC__ */
int Mstrcmp( Matrix *Ms1, Matrix *Ms2)
#endif /* __STDC__ */
{
 int sz1;
 int sz2;
 int *m1;
 int *m2;
 
 if ((Ms1==NULL) || (Ms2==NULL) ) return(-1);
 m1 = (int *)Ms1;
 m2 = (int *)Ms2;
 
 sz1 = m1[5] -1;
 sz2 = m2[5] -1;

 if (sz1 != sz2) return(-1);
 return(memcmp(m1+6, m2+6, sz1 * sizeof(int)));

}

/*********** findGvar **********/
#ifndef __STDC__
int findGvar(Mname)
     Matrix *Mname;
#else /* __STDC__ */
int findGvar( Matrix *Mname )
#endif /* __STDC__ */
     /* find the rank of a global variable in the list */
     /* INPUT  : name, name of the scilab variable to find */
     /* OUTPUT : rank of the variable. -1 if not found */

{
  int rank=0;
  
  while ( (rank<gvarLstSz) && (Mstrcmp( Mname, ((gvarLst[rank]).name) ) != 0) )
    rank++;

  if (rank==gvarLstSz) return(-1);
  else return(rank);
  
}



/*********** setGvar **********/
#ifndef __STDC__
int setGvar(Mname, sciObj)
     Matrix *Mname;
     double *sciObj;
#else /* __STDC__ */
int setGvar( Matrix *Mname, double *sciObj)
#endif /* __STDC__ */
     /* set a global variable */
     /* if it doesn't exist, its created */
     /* INPUT  : -name, scilab var name */
     /*          -sciObj, scilab object to store */
     /* OUTPUT : result of the operation. */
     /*          1  -> the variable replaced an old value */
     /*          0  -> the variable has been created and stored */
     /*          <0 -> the operation has failed */

	
{
  int objSz; /* size in bytes of the scilab object */
  int rank; /* rank of the object in the gvar list */
  double *objPtr; /* ptr on the actual global object */
  Matrix *objName;
  int objNameSz;
  
#ifdef GVAR_TRACE
  printf("In setGvar\n");
#endif
  rank = findGvar( Mname );
  
  if (rank>=0) 
    { /* object already exists */
      /* free the old object */
#ifdef GVAR_TRACE
      printf("   Object found at rank %d\n", rank);
#endif
      free( (gvarLst[rank]).ptr);
      free( (gvarLst[rank]).name);
      /* get the memory for the object */
      objSz = MatrixMemSize( sciObj );
      objPtr = (double *)malloc( objSz * sizeof(char) );
      if ( objPtr == NULL ) return(-1); /* no more heap memory free */
      objNameSz = MatrixMemSize( Mname );
      objName = (Matrix *) malloc( objNameSz * sizeof(char) );

      /* copy the object */
      memcpy(objPtr, sciObj, objSz);
      memcpy(objName, Mname, objNameSz);
  
      /* update the info in the list */
      updateElt( rank, objName, objPtr );
      return(1);
    }
  else
    { /* object must be created */
#ifdef GVAR_TRACE
      printf("   Object not found. Needs to be created\n");
#endif

      return( newGvar( Mname, sciObj) );
      
    }
}


/*********** getGvar **********/
#ifndef __STDC__
int getGvar(Mname, ptrObj)
     Matrix *Mname;
     double **ptrObj;
#else /* __STDC__ */
int getGvar( Matrix *Mname, double **ptrObj)
#endif /* __STDC__ */
     /* get a global variable */
     /* INPUT  : -name, scilab var name */
     /*          -sciObj, scilab object to store */
     /* OUTPUT : result of the operation. */
     /*         -1  -> the variable didn't exist */
     /*              in this case, NULL is returned in ptrObj */
     /*          0  -> the variable has been read */
     
     

	
{
  int objSz; /* size in bytes of the scilab object */
  int rank; /* rank of the object in the gvar list */
  double *objPtr; /* ptr on the actual global object */
#ifdef GVAR_TRACE
  printf("In getGvar\n");
#endif
  
  rank = findGvar( Mname );
  
  if (rank>=0) 
    { /* var exists as global*/
#ifdef GVAR_TRACE
      printf("   Object found at rank %d\n", rank);
#endif

      *ptrObj = (double *)(gvarLst[rank]).ptr;
      return(0);
    }
  else
    { /* var doesn't exist as a global var */
#ifdef GVAR_TRACE
      printf("   Object not found\n");
#endif

      *ptrObj = (double *)NULL;
      return(-1); 
    }
}


/*********** rmGvar **********/
#ifndef __STDC__
int delGvar(Mname)
     Matrix *Mname;
#else /* __STDC__ */
int delGvar( Matrix* Mname )
#endif /* __STDC__ */
     /* delete a global var */
     /* the variable memory is freed and its entry */
     /* in the global list is supressed */
     /* INPUT  : name, the name of the scilab var */
     /* OUTPUT : 0 is the operation was successful, -1 otherwise */

{
  int rank;
  
  rank = findGvar( Mname );
  if (rank>=0) 
    { /* var exists as global*/
      free ( (gvarLst[rank]).ptr );
      (gvarLst[rank]).ptr = (double *)NULL;
      free ( (gvarLst[rank]).name );
      (gvarLst[rank]).name = (double *)NULL;
      /* memcpy((gvarLst[rank]).name, (int)0, gvar_nsiz * sizeof(int)); */
      
      if (rank < curFreeElt) curFreeElt=rank;
      return(0);
    }
  else
    { /* var doesn't exist as a global var */
      /* *ptrObj = (double *)NULL */
      return(-1); 
    }
}