~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina5/tina-libs/tina/file/fileTs_free.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  * 
  3  * Copyright (c) 2003, Division of Imaging Science and Biomedical Engineering,
  4  * University of Manchester, UK.  All rights reserved.
  5  * 
  6  * Redistribution and use in source and binary forms, with or without modification, 
  7  * are permitted provided that the following conditions are met:
  8  * 
  9  *   . Redistributions of source code must retain the above copyright notice, 
 10  *     this list of conditions and the following disclaimer.
 11  *    
 12  *   . Redistributions in binary form must reproduce the above copyright notice,
 13  *     this list of conditions and the following disclaimer in the documentation 
 14  *     and/or other materials provided with the distribution.
 15  * 
 16  *   . Neither the name of the University of Manchester nor the names of its
 17  *     contributors may be used to endorse or promote products derived from this 
 18  *     software without specific prior written permission.
 19  * 
 20  * 
 21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
 25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 31  * POSSIBILITY OF SUCH DAMAGE.
 32  *
 33  **********
 34  * 
 35  * Program :    TINA
 36  * File    :  $Source: /home/tina/cvs/tina-libs/tina/file/fileTs_free.c,v $
 37  * Date    :  $Date: 2005/01/09 17:49:25 $
 38  * Version :  $Revision: 1.4 $
 39  * CVS Id  :  $Id: fileTs_free.c,v 1.4 2005/01/09 17:49:25 paul Exp $
 40  *
 41  * Author  :  Legacy TINA
 42  *
 43  * Notes :
 44  *
 45  *********
 46 */
 47 
 48 #include "fileTs_free.h"
 49 
 50 #if HAVE_CONFIG_H
 51   #include <config.h>
 52 #endif
 53 
 54 #include <stdio.h>
 55 #include <stdlib.h>
 56 #include <tina/sys/sysDef.h>
 57 #include <tina/sys/sysPro.h>
 58 #include <tina/file/file_TsDef.h>
 59 
 60 
 61 #define REP_ARRAY 100
 62 #define SUBSTRUCT_ALIGNMENT 4
 63 #define ALIGN_VARPTR(start,here,size) \
 64 { \
 65    int tab=(int)((here).char_v-(char*)(start))%(size);\
 66      (here).char_v+=tab?(size)-tab:0;\
 67      }
 68 
 69 void    ts_free_gen(void *data);
 70 void    ts_free_sub_item(void *data);
 71 
 72 
 73 static int repeat_pointer_checking=1;           /* static data! */
 74 
 75 
 76 void repeatp_checker_off(void)
 77 {
 78   repeat_pointer_checking=0;
 79 }
 80 void repeatp_checker_on(void)
 81 {
 82   repeat_pointer_checking=1;
 83 }
 84 int repeatp_checking_get(void)
 85 {
 86   return repeat_pointer_checking;
 87 }
 88 void repeatp_checking_set(int f)
 89 {
 90   repeat_pointer_checking=f;
 91 }
 92 
 93 
 94 /* Is this a new pointer? If so don't tell anybody else */
 95 int     repeatp_checker(void *d)/* return 1 if new pointer */
 96 {
 97   static void **pstack = NULL, **tp;                    /* static data! */
 98   static int c = 0, n = REP_ARRAY;                              /* static data! */
 99   int     i;
100   
101   if(!repeat_pointer_checking) return 1; /* switched off so say is new */
102   if (pstack == NULL)
103     pstack = (void **) malloc(sizeof(void *) * n);
104   
105   if (d == NULL)                /* reset stored array */
106   {
107     /* mprintf("Reseting ptr list\n"); */
108     pstack = (void **) realloc(pstack, sizeof(void *) * n);
109     c = 0;
110     n = REP_ARRAY;
111     return 0;
112   }
113   for (tp = pstack, i = 0; i < c; i++)
114     if (*tp++ == d)
115       break;
116   if (i == c)                   /* not already done */
117   {
118     if (c == n)
119     {
120       n+=REP_ARRAY;
121       pstack = (void **) realloc(pstack, sizeof(void *) * n);
122       if(!pstack)
123       {
124         printf("REALLOC Failed in repeatp_checker\n");
125         exit(1);
126       }
127     }
128     pstack[c++] = d;
129     return 1;                   /* indicate new pointer */
130   } else
131     return 0;                   /* indicate repeat pointer */
132 }
133 
134 
135 static void free_parse(void *data, Varptr dp, char **ss)
136 {
137     char   *s = *ss;
138     int     repeats = 0, exiting = 0;
139     static Varptr d, nulld;                             /* static data! */
140     static int arrayp = 0;                              /* static data! */
141 
142     nulld.char_v = NULL;
143     if (dp.char_v)
144         d = dp;                 /* if dp is null use static version d */
145     /* else use new value */
146     while (*s && !exiting)
147     {
148         switch (*s++)
149         {
150         case '':
151             repeats *= 10;
152             break;
153         case '1':
154         case '2':
155         case '3':
156         case '4':
157         case '5':
158         case '6':
159         case '7':
160         case '8':
161         case '9':
162             repeats *= 10;
163             repeats += *(s - 1) - 48;
164             break;
165         case '[':
166             arrayp = 1;
167             {
168                 char   *temp_s = s;
169 
170                 while (repeats-- > 0)
171                 {
172                     temp_s = s;
173                     free_parse(data, nulld, &temp_s);
174                 }
175                 arrayp = 0;
176                 s = temp_s;     /* the rest of the swap string */
177                 repeats = 0;
178             }
179             break;
180         case '(':               /* start of a repetition */
181             {
182                 char   *temp_s = s;
183 
184                 while (repeats-- > 0)
185                 {
186                     temp_s = s;
187                     free_parse(data, nulld, &temp_s);
188                 }
189                 s = temp_s;     /* the rest of the swap string */
190                 repeats = 0;
191             }
192             break;
193         case ']':
194         case ')':
195             exiting = 1;
196             break;
197         case '/':
198             {
199                 char   *strchr(const char *s, int c);
200 
201                 char   *end = strchr(s, '/');
202 
203                 s = end + 1;
204             }
205             break;
206         default:
207             {
208                 do
209                 {
210                     switch (*(s - 1))
211                     {
212                     case 'b':
213                     case 'c':
214                         d.char_v++;
215                         break;
216                     case 'f':
217                     case 'l':
218                     case 'i':
219                     case 'o':
220                     case 'u':
221                     case 'w':
222                     case '.':
223                     case ',':
224                         ALIGN_VARPTR(data, d, 4);
225                         d.int_v++;
226                         break;
227                     case 'p':
228                     case 's':
229                         ALIGN_VARPTR(data, d, 4);
230                         break;
231                     case 'h':
232                     case 'k':
233                         ALIGN_VARPTR(data, d, 2);
234                         d.short_v++;
235                         break;
236                     case 'g':
237                     case 'e':
238                         ALIGN_VARPTR(data, d, 8);
239                         d.double_v++;
240                         break;
241                     case 't':
242                         ALIGN_VARPTR(data, d, SUBSTRUCT_ALIGNMENT);
243                         break;
244                     }
245                     switch (*(s - 1))
246                     {
247                     case 'p':   /* pointer types */
248                         if (*d.ptr_v)
249                         {
250                             Varptr  td = d;     /* keep d pointer */
251 
252                             ts_free_gen((int *) (*d.ptr_v));
253                             d = td;     /* recover d pointer */
254                         }
255                         d.ptr_v++;      /* SMC BUG FIX */
256                         break;
257                     case 's':   /* string */
258                         if (*d.ptr_v && repeatp_checker(*d.ptr_v))
259                             rfree(*d.ptr_v);
260                         d.ptr_v++;
261                         break;
262                     case 't':   /* tagged struct */
263                         ts_free_sub_item(d.int_v);
264                         break;
265                     }
266                 } while (--repeats > 0);
267                 repeats = 0;
268             }
269             break;
270         }
271     }
272     *ss = s;
273 }
274 
275 
276 void    ts_free_sub_item(void *data)
277 {
278     char   *s = tina_data_info_array[*(int *) data - ts_base_id].swap_string;
279     Varptr  d;
280 
281     d.ptr_v = data;
282     /* mprintf("Freeing a %s sub Struct with %s\n",
283      * tina_data_info_array[*d.int_v - ts_base_id].name, s); */
284     d.int_v++;                  /* skip over tag */
285     free_parse(data, d, &s);
286 }
287 void    ts_free(void *data)
288 {
289     char   *s;
290     Varptr  d;
291 
292     if (data)
293     {
294         s = tina_data_info_array[*(int *) data - ts_base_id].swap_string;
295         d.ptr_v = data;
296         /* mprintf("Freeing a %s Struct with %s\n",
297          * tina_data_info_array[*d.int_v - ts_base_id].name, s); */
298         d.int_v++;              /* skip over tag */
299         free_parse(data, d, &s);
300         rfree(data);
301     }
302 }
303 
304 void    ts_free_gen(void *data)
305 {
306     static int calls = 0;                                       /* static data! */
307     int rcheck=repeat_pointer_checking;
308     repeat_pointer_checking=1;
309 
310     if (data)
311     {
312         if (repeatp_checker(data))
313         {
314             calls++;
315             tina_data_info_array[*(int *) data - ts_base_id].free(data);
316             calls--;
317         }
318         if (calls == 0)
319             repeatp_checker(NULL);      /* reset stored p's */
320     }
321     repeat_pointer_checking=rcheck;
322 }
323 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.