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_n.c,v $
37 * Date : $Date: 2003/09/22 16:09:02 $
38 * Version : $Revision: 1.6 $
39 * CVS Id : $Id: sysMem_dynamic_n.c,v 1.6 2003/09/22 16:09:02 tony Exp $
40 *
41 * Notes :
42 *
43 * Vector (1 dimension array) handling.
44 * Allocate and free displaced vectors &arrays.
45 * Elements (of general size) are initialised to zero.
46 *
47 *********
48 */
49
50 #include "sysMem_dynamic_n.h"
51
52 #if HAVE_CONFIG_H
53 #include <config.h>
54 #endif
55
56 #include <math.h>
57 #include <stdio.h>
58
59 #if STDC_HEADERS
60 # include <string.h>
61 #elif __GNUC__
62 void *memcpy();
63 void *memset();
64 #else
65 char *memcpy();
66 char *memset();
67 #endif
68
69 #include <tina/sys/sysGen_error.h>
70 #include <tina/sys/sysMem_ralloc.h>
71
72 Bool nvector_test(void *v, int n1, size_t size, Bool set)
73 /* set == true; update the starting point of an offset vector in the bytes
74 immediately prior to the start of allocated memory leaving the lowest bit
75 for use by ralloc().
76 set == false; test the stored value with that expected. NAT 1999 */
77 {
78 Align *iptr = (Align *) v;
79 iptr--;
80 if (set)
81 *(int *) iptr = *(int *) iptr + 2 * n1 * (int) size;
82 else if (floor((*(int *) iptr) / 2.0) != n1 * (int) size)
83 return (false);
84 return (true);
85 }
86
87 /* Displaced vector of size s v[i]: n1 <= i < n2 */
88 void *nvector_alloc(int n1, int n2, size_t size)
89 {
90 char *v = NULL;
91 int n = n2 - n1;
92
93 if (n >= 1)
94 {
95 v = ((char *) ralloc((size_t) n * size));
96 /* store offset vector for checking later NAT 1999 */
97 nvector_test(v, n1, size, true);
98 v -= (n1 * (int) size);
99 } else
100 error("attempt to allocate invalid vector in nvector_alloc()",
101 non_fatal);
102
103 return (void *) v;
104 }
105
106 void *nvector_shift(void *v, int n1, int new_n1, unsigned int s)
107 {
108 void *w;
109
110 w = (void *) ((char *) v - (new_n1 - n1) * (int) s);
111 v = (char*)v + (n1 * (int) s); /* ipoole */
112
113 if (!nvector_test(v, n1, (size_t) s, false))
114 error("attempt to move re-defined vector in nvector_shift()",
115 non_fatal);
116 else
117 nvector_test(v, new_n1 - n1, (size_t) s, true);
118 /* store new offset vector for checking later NAT 1999 */
119
120 return (w);
121 }
122
123 void *nvector_copy(char *v, int n1, int n2, unsigned int s)
124 /* element size */
125 {
126 char *w;
127 int n = n2 - n1;
128
129 if (v == NULL || n < 1 || s < 1)
130 return (NULL);
131 w = (char *) nvector_alloc(n1, n2, s);
132 (void) memcpy(w + n1 * (int) s, v + n1 * (int) s, n * (int) s);
133 return ((void *) w);
134 }
135
136 void nvector_copy_inplace(char *w, char *v, int n1, int n2, unsigned int s)
137 /* s: element size */
138 {
139 int n = n2 - n1;
140
141 if (w == NULL || v == NULL || n < 1 || s < 1)
142 return;
143 (void) memcpy(w + n1 * (int) s, v + n1 * (int) s, n * (int) s);
144 }
145
146 void nvector_zero_inplace(char *v, int n1, int n2, unsigned int s)
147 /* element size */
148 {
149 int n = n2 - n1;
150
151 if (v == NULL || n < 1 || s < 1)
152 return;
153 (void) memset(v + n1 * (int) s, 0, n * (int) s);
154 }
155
156 void nvector_free(void *v, int n1, unsigned int s)
157 {
158 if (v != NULL)
159 {
160 v = (void *) ((char *) v + n1 * (int) s);
161 /* check offset with stored value NAT 1999 */
162 if (!nvector_test(v, n1, (size_t) s, false))
163 error("attempt to free re-defined vector in nvector_free()",
164 non_fatal);
165 else
166 {
167 /* restore offset count to zero before calling rfree() */
168 nvector_test(v, -n1, (size_t) s, true);
169 rfree(v);
170 }
171 } else
172 error("attempt to free NULL pointer in nvector_free()", warning);
173
174 }
175
176 /* displaced array, element size s a[i][j]: m1 <=i < m2, n1 <= j < n2 */
177 void **narray_alloc(int m1, int n1, int m2, int n2, unsigned int s)
178 {
179 char **a;
180 int m = m2 - m1, n = n2 - n1;
181 int i;
182
183 if (m < 1 || n < 1 || s < 1)
184 return (NULL);
185 a = (char **) nvector_alloc(m1, m2, (int) sizeof(char *));
186 for (i = m1; i < m2; ++i)
187 a[i] = (char *) nvector_alloc(n1, n2, s);
188 return ((void **) a);
189 }
190
191 void **narray_copy(char **a, int m1, int n1, int m2, int n2,
192 unsigned int s)
193 {
194 char **b;
195 int m = m2 - m1, n = n2 - n1;
196 int i;
197
198 if (a == NULL || m < 1 || n < 1 || s < 1)
199 return (NULL);
200 b = (char **) narray_alloc(m1, n1, m2, n2, s);
201 for (i = m1; i < m2; ++i)
202 nvector_copy_inplace(b[i], a[i], n1, n2, s);
203 return ((void **) b);
204 }
205
206 void narray_copy_inplace(char **b, char **a, int m1, int n1, int m2,
207 int n2, unsigned int s)
208 {
209 int m = m2 - m1, n = n2 - n1;
210 int i;
211
212 if (b == NULL || a == NULL || m < 1 || n < 1 || s < 1)
213 return;
214 for (i = m1; i < m2; ++i)
215 nvector_copy_inplace(b[i], a[i], n1, n2, s);
216 }
217
218 void narray_zero_inplace(char **a, int m1, int n1, int m2, int n2,
219 unsigned int s)
220 {
221 int m = m2 - m1, n = n2 - n1;
222 int i;
223
224 if (a == NULL || m < 1 || n < 1 || s < 1)
225 return;
226 for (i = m1; i < m2; ++i)
227 nvector_zero_inplace(a[i], n1, n2, s);
228 }
229
230 /* ARGSUSED Quieten Lint */
231 void narray_free(char **a, int m1, int n1, int m2, int n2, unsigned int s)
232 {
233 int i;
234
235 if (a == NULL)
236 {
237 error("attempt to free NULL pointer in narray_free()", warning);
238 return;
239 }
240 for (i = m1; i < m2; ++i)
241 nvector_free((void *) a[i], n1, s);
242 nvector_free((void *) a, m1, sizeof(char *));
243 }
244
245 /* displaced lower triangle, element size s a[i][j]: n1 <=i < n2, i <=
246 * j < n2 */
247 void **nlower_alloc(int n1, int n2, unsigned int s)
248 {
249 char **a;
250 int n = n2 - n1;
251 int i;
252
253 if (n < 1 || s < 1)
254 return (NULL);
255 a = (char **) nvector_alloc(n1, n2, sizeof(char *));
256 for (i = n1; i < n2; ++i)
257 a[i] = (char *) nvector_alloc(n1, i + 1, s);
258 return ((void **) a);
259 }
260
261 void **nlower_copy(char **a, int n1, int n2, unsigned int s)
262 {
263 char **b;
264 int n = n2 - n1;
265 int i;
266
267 if (a == NULL || n < 1 || s < 1)
268 return (NULL);
269 b = (char **) nlower_alloc(n1, n2, s);
270 for (i = n1; i < n2; ++i)
271 nvector_copy_inplace(b[i], a[i], n1, i + 1, s);
272 return ((void **) b);
273 }
274
275 void nlower_copy_inplace(char **b, char **a, int n1, int n2,
276 unsigned int s)
277 {
278 int n = n2 - n1;
279 int i;
280
281 if (b == NULL || a == NULL || n < 1 || s < 1)
282 return;
283 b = (char **) nlower_alloc(n1, n2, s);
284 for (i = n1; i < n2; ++i)
285 nvector_copy_inplace(b[i], a[i], n1, i + 1, s);
286 }
287
288 void nlower_zero_inplace(char **a, int n1, int n2, unsigned int s)
289 {
290 int n = n2 - n1;
291 int i;
292
293 if (a == NULL || n < 1 || s < 1)
294 return;
295 for (i = n1; i < n2; ++i)
296 nvector_zero_inplace(a[i], n1, i + 1, s);
297 }
298
299 void nlower_free(char **a, int n1, int n2, unsigned int s)
300 {
301 int i;
302
303 if (a == NULL)
304 {
305 error("attempt to free NULL pointer in nlower_free()", warning);
306 return;
307 }
308 for (i = n1; i < n2; ++i)
309 nvector_free((void *) a[i], n1, s);
310 nvector_free((void *) a, n1, sizeof(char *));
311 }
312
313 /* displaced upper triangle, element size s a[i][j]: n1 <=i < n2, 0 <=
314 * j <= i */
315 void **nupper_alloc(int n1, int n2, unsigned int s)
316 {
317 char **a;
318 int n = n2 - n1;
319 int i;
320
321 if (n < 1 || s < 1)
322 return (NULL);
323 a = (char **) nvector_alloc(n1, n2, sizeof(char *));
324 for (i = n1; i < n2; ++i)
325 a[i] = (char *) nvector_alloc(i, n2, s);
326 return ((void **) a);
327 }
328
329 void **nupper_copy(char **a, int n1, int n2, unsigned int s)
330 {
331 char **b;
332 int n = n2 - n1;
333 int i;
334
335 if (a == NULL || n < 1 || s < 1)
336 return (NULL);
337 b = (char **) nupper_alloc(n1, n2, s);
338 for (i = n1; i < n2; ++i)
339 nvector_copy_inplace(b[i], a[i], i, n2, s);
340 return ((void **) b);
341 }
342
343 void nupper_copy_inplace(char **b, char **a, int n1, int n2,
344 unsigned int s)
345 {
346 int n = n2 - n1;
347 int i;
348
349 if (b == NULL || a == NULL || n < 1 || s < 1)
350 return;
351 b = (char **) nupper_alloc(n1, n2, s);
352 for (i = n1; i < n2; ++i)
353 nvector_copy_inplace(b[i], a[i], i, n2, s);
354 }
355
356 void nupper_zero_inplace(char **a, int n1, int n2, unsigned int s)
357 {
358 int n = n2 - n1;
359 int i;
360
361 if (a == NULL || n < 1 || s < 1)
362 return;
363 for (i = n1; i < n2; ++i)
364 nvector_zero_inplace(a[i], i, n2, s);
365 }
366
367 void nupper_free(char **a, int n1, int n2, unsigned int s)
368 {
369 int i;
370
371 if (a == NULL)
372 {
373 error("attempt to free NULL structure in nupper_free()", warning);
374 return;
375 }
376 for (i = n1; i < n2; ++i)
377 nvector_free((void *) a[i], i, s);
378 nvector_free((void *) a, n1, sizeof(char *));
379 }
380
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.