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

Linux Cross Reference
Tina6/tina-libs/tina/sys/sysMem_dynamic_v.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/sys/sysMem_dynamic_v.c,v $
 37  * Date    :  $Date: 2003/09/22 16:09:02 $
 38  * Version :  $Revision: 1.4 $
 39  * CVS Id  :  $Id: sysMem_dynamic_v.c,v 1.4 2003/09/22 16:09:02 tony Exp $
 40  *
 41  * Notes :
 42  *
 43  * Array handling. 2+ dimensions (1 dimension arrays are called vectors).
 44  * Allocation and freeing.
 45  *
 46  *********
 47 */
 48 
 49 #include "sysMem_dynamic_v.h"
 50 
 51 #if HAVE_CONFIG_H
 52   #include <config.h>
 53 #endif
 54 
 55 #include <stdio.h>
 56 #include <math.h>
 57 
 58 #if HAVE_STDARG_H
 59 #  include <stdarg.h>
 60 #  define VA_START(a, f)                va_start(a, f)
 61 #else
 62 #  if HAVE_VARARGS_H
 63 #    include <varargs.h>
 64 #    define VA_START(a, f)  va_start(a)
 65 #  endif
 66 #endif /* HAVE_STDARG_H */
 67 #ifndef VA_START
 68   error no variadic api available
 69 #endif
 70 
 71 #include <tina/sys/sysGen_error.h>
 72 #include <tina/sys/sysMem_ralloc.h>
 73 #include <tina/sys/sysMem_dynamic_n.h>
 74 
 75 static void *rec_array_alloc(int i, int n, unsigned int s, int *m1,
 76                                                                                                                  int *m2)
 77 {
 78         void **vector;
 79         int j;
 80 
 81         if (i == n - 1)                                                         /* use s to determine block size */
 82                 return (nvector_alloc(m1[i], m2[i], s));
 83 
 84         vector = (void **) nvector_alloc(m1[i], m2[i], sizeof(void *));
 85 
 86         for (j = m1[i]; j < m2[i]; j++)
 87                 vector[j] = rec_array_alloc(i + 1, n, s, m1, m2);
 88 
 89         return ((void *) vector);
 90 }
 91 
 92 void *ndim_array_alloc(int n, unsigned int s, int *m1, int *m2)
 93 /* No of dimensions */
 94 /* element size */
 95 /* m1 and m2 for each of the dimensions   */
 96 {
 97         if (n == 0)
 98                 return ((void *) 0);
 99         return (rec_array_alloc(0, n, s, m1, m2));
100 }
101 
102 
103 static void rec_array_free(void **vector, int i, int n, unsigned int s,
104                                                                                                          int *m1, int *m2)
105 {
106         int j;
107 
108         if (i == n - 1)
109         {
110                 nvector_free((void *) vector, m1[i], s);
111                 return;
112         }
113         for (j = m1[i]; j < m2[i]; j++)
114                 rec_array_free((void **) vector[i], i + 1, n, s, m1, m2);
115 
116         nvector_free((void *) vector, m1[i], sizeof(void *));
117 }
118 
119 void ndim_array_free(void *array, int n, unsigned int s, int *m1, int *m2)
120 /* previously created array */
121 /* number of dimensions */
122 /* element size */
123 /* m1 and m2 for each of the dimensions */
124 {
125         if (n)
126         {
127                 rec_array_free((void **) array, 0, n, s, m1, m2);
128         }
129 }
130 
131 
132 /* Variable argument variable dimension array allocation. array =
133  * vdim_array_alloc(n, s, a1, b1, c1, d1, ..., a2, b2, c2, d2, ...); */
134 /* No of dimensions */
135 /* element size */
136 void 
137 #if HAVE_STDARG_H
138 *vdim_array_alloc(int n, unsigned int s, ...)
139 #else
140 *vdim_array_alloc(n, s, va_alist)
141   int n;                                                                                                        /* No of dimensions */
142   unsigned int s;                                                                       /* element size */
143   va_dcl                                                                                                        /* m1 and m2 for each of the dimensions */
144 #endif /* HAVE_STDARG_H */
145 {
146         va_list ap;
147         int *m1, *m2;                                                                   /* m1 and m2 for each of the dimensions */
148 
149         void *array;
150         int i;
151 
152         if (n == 0)
153                 return ((void *) 0);
154 
155         m1 = (int *) ralloc((unsigned) n * sizeof(int));
156         m2 = (int *) ralloc((unsigned) n * sizeof(int));
157         VA_START(ap, s);
158         for (i = 0; i != n; ++i)
159                 m1[i] = va_arg(ap, int);
160         for (i = 0; i != n; ++i)
161                 m2[i] = va_arg(ap, int);
162         va_end(ap);
163 
164         array = rec_array_alloc(0, n, s, m1, m2);
165         rfree((void *) m1);
166         rfree((void *) m2);
167         return (array);
168 }
169 
170 
171 /* free array previously created array */
172 /* n No of dimensions */
173 /* s element size */
174 void 
175 #if HAVE_STDARG_H
176 vdim_array_free(void *array, int n, unsigned int s, ...)
177 #else
178 vdim_array_free(array, n, s, va_alist)
179   void *array;                                                                          /* previously created array */
180   int n;                                                                                                        /* No of dimensions */
181   unsigned int s;                                                                       /* element size */
182   va_dcl                                                                                                        /* m1 and m2 for each of the dimensions */
183 #endif /* HAVE_STDARG_H */
184 {
185         va_list ap;
186         int *m1, *m2;                                                                   /* m1 and m2 for each of the dimensions */
187         int i;
188 
189         if (n == 0)
190                 return;
191 
192         m1 = (int *) ralloc((unsigned) n * sizeof(int));
193         m2 = (int *) ralloc((unsigned) n * sizeof(int));
194         VA_START(ap, s);
195 
196         for (i = 0; i != n; ++i)
197                 m1[i] = va_arg(ap, int);
198         for (i = 0; i != n; ++i)
199                 m2[i] = va_arg(ap, int);
200         va_end(ap);
201 
202         rec_array_free((void **) array, 0, n, s, m1, m2);
203         rfree((void *) m1);
204         rfree((void *) m2);
205 }
206 

~ [ 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.