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

Linux Cross Reference
Tina4/src/sys/dynamic/ndynamic.c

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

  1 /**@(#)Vector (1 dimension array) handling.
  2  * @(#)Allocate and free displaced vectors &arrays.
  3  * @(#)Elements (of general size) are initialised to zero.
  4  */
  5 
  6 #include <math.h>
  7 #include <stdio.h>
  8 #include <tina/sys.h>
  9 #include <tina/sysfuncs.h>
 10 #include  <tina/dynamic.h>
 11 
 12 #ifdef __STRICT_ANSI__
 13 #include  <string.h>
 14 #elif __GNUC__
 15 #ifndef ANSIFYING
 16 void   *memcpy();
 17 void   *memset();
 18 
 19 #endif                          /* ANSIFYING */
 20 #else
 21 char   *memcpy();
 22 char   *memset();
 23 
 24 #endif
 25 
 26 Bool nvector_test(void *v, int n1, size_t size, Bool set)
 27 /* set == true; update the starting point of an offset vector in the bytes
 28    immediately prior to the start of allocated memory leaving the lowest bit
 29    for use by ralloc().
 30    set == false;  test the stored value with that expected. NAT 1999 */
 31 {
 32      Align  *iptr = (Align *)v;
 33      iptr--;
 34      if (set)
 35         *(int *)iptr = *(int *)iptr + 2*n1*(int)size;
 36      else
 37         if (floor((*(int *)iptr)/2.0) != n1*(int)size)
 38            return(false);
 39      return(true);
 40 }
 41 
 42 /* Displaced vector of size s v[i]: n1 <= i < n2 */
 43 void   *nvector_alloc(int n1, int n2, size_t size)
 44 {
 45     char   *v = NULL;
 46     int     n = n2 - n1;
 47 
 48     if (n >= 1)
 49     {
 50         v = ((char *) ralloc((size_t) n * size));
 51 /* store offset vector for checking later NAT 1999 */
 52         nvector_test(v,n1,size,true);
 53         v -= (n1 * (int)size);
 54     }
 55     else
 56         error("attempt to allocate invalid vector in nvector_alloc()",non_fatal); 
 57 
 58     return (void *) v;
 59 }
 60 
 61 void   *nvector_shift(void *v, int n1, int new_n1, unsigned int s)
 62 {
 63    void *w;
 64 
 65    w = (void *) ((char *) v - (new_n1-n1)*(int)s);
 66    v += (n1 * (int)s);
 67 
 68    if (!nvector_test(v,n1,(size_t)s,false))
 69        error("attempt to move re-defined vector in nvector_shift()",non_fatal);
 70    else
 71        nvector_test(v,new_n1-n1,(size_t)s,true);
 72 /* store new offset vector for checking later NAT 1999 */
 73    
 74    return (w);
 75 }
 76 
 77 void   *nvector_copy(char *v, int n1, int n2, unsigned int s)
 78 
 79 
 80 /* element size */
 81 {
 82     char   *w;
 83     int     n = n2 - n1;
 84 
 85     if (v == NULL || n < 1 || s < 1)
 86         return (NULL);
 87     w = (char *) nvector_alloc(n1, n2, s);
 88     (void) memcpy(w + n1 * (int) s, v + n1 * (int) s, n * (int) s);
 89     return ((void *) w);
 90 }
 91 
 92 void    nvector_copy_inplace(char *w, char *v, int n1, int n2, unsigned int s)
 93 /* s: element size */
 94 {
 95     int     n = n2 - n1;
 96 
 97     if (w == NULL || v == NULL || n < 1 || s < 1)
 98         return;
 99     (void) memcpy(w + n1 * (int) s, v + n1 * (int) s, n * (int) s);
100 }
101 
102 void    nvector_zero_inplace(char *v, int n1, int n2, unsigned int s)
103 /* element size */
104 {
105     int     n = n2 - n1;
106 
107     if (v == NULL || n < 1 || s < 1)
108         return;
109     (void) memset(v + n1 * (int) s, 0, n * (int) s);
110 }
111 
112 void    nvector_free(void *v, int n1, unsigned int s)
113 {
114     if (v!=NULL)
115     {
116         v = (void *)((char *)v + n1 *(int)s);
117 /* check offset with stored value NAT 1999 */
118         if (!nvector_test(v,n1,(size_t)s,false))
119            error("attempt to free re-defined vector in nvector_free()",non_fatal);
120         else
121         {
122 /* restore offset count to zero before calling rfree() */
123            nvector_test(v,-n1,(size_t)s,true);
124            rfree(v);
125         }
126     }
127     else
128         error("attempt to free NULL pointer in nvector_free()", warning);
129         
130 }
131 
132 /* displaced array, element size s a[i][j]: m1 <=i < m2, n1 <= j < n2 */
133 void  **narray_alloc(int m1, int n1, int m2, int n2, unsigned int s)
134 {
135     char  **a;
136     int     m = m2 - m1, n = n2 - n1;
137     int     i;
138 
139     if (m < 1 || n < 1 || s < 1)
140         return (NULL);
141     a = (char **) nvector_alloc(m1, m2, (int) sizeof(char *));
142     for (i = m1; i < m2; ++i)
143         a[i] = (char *) nvector_alloc(n1, n2, s);
144     return ((void **) a);
145 }
146 
147 void  **narray_copy(char **a, int m1, int n1, int m2, int n2, unsigned int s)
148 {
149     char  **b;
150     int     m = m2 - m1, n = n2 - n1;
151     int     i;
152 
153     if (a == NULL || m < 1 || n < 1 || s < 1)
154         return (NULL);
155     b = (char **) narray_alloc(m1, n1, m2, n2, s);
156     for (i = m1; i < m2; ++i)
157         nvector_copy_inplace(b[i], a[i], n1, n2, s);
158     return ((void **) b);
159 }
160 
161 void    narray_copy_inplace(char **b, char **a, int m1, int n1, int m2, int n2, unsigned int s)
162 {
163     int     m = m2 - m1, n = n2 - n1;
164     int     i;
165 
166     if (b == NULL || a == NULL || m < 1 || n < 1 || s < 1)
167         return;
168     for (i = m1; i < m2; ++i)
169         nvector_copy_inplace(b[i], a[i], n1, n2, s);
170 }
171 
172 void    narray_zero_inplace(char **a, int m1, int n1, int m2, int n2, unsigned int s)
173 {
174     int     m = m2 - m1, n = n2 - n1;
175     int     i;
176 
177     if (a == NULL || m < 1 || n < 1 || s < 1)
178         return;
179     for (i = m1; i < m2; ++i)
180         nvector_zero_inplace(a[i], n1, n2, s);
181 }
182 
183 /* ARGSUSED Quieten Lint */
184 void    narray_free(char **a, int m1, int n1, int m2, int n2, unsigned int s)
185 {
186     int     i;
187 
188     if (a == NULL)
189     {
190         error("attempt to free NULL pointer in narray_free()",warning);
191         return;
192     }
193     for (i = m1; i < m2; ++i)
194         nvector_free((void *) a[i], n1, s);
195     nvector_free((void *) a, m1, sizeof(char *));
196 }
197 
198 /* displaced lower triangle, element size s a[i][j]: n1 <=i < n2, i <=
199  * j < n2 */
200 void  **nlower_alloc(int n1, int n2, unsigned int s)
201 {
202     char  **a;
203     int     n = n2 - n1;
204     int     i;
205 
206     if (n < 1 || s < 1)
207         return (NULL);
208     a = (char **) nvector_alloc(n1, n2, sizeof(char *));
209     for (i = n1; i < n2; ++i)
210         a[i] = (char *) nvector_alloc(n1, i + 1, s);
211     return ((void **) a);
212 }
213 
214 void  **nlower_copy(char **a, int n1, int n2, unsigned int s)
215 {
216     char  **b;
217     int     n = n2 - n1;
218     int     i;
219 
220     if (a == NULL || n < 1 || s < 1)
221         return (NULL);
222     b = (char **) nlower_alloc(n1, n2, s);
223     for (i = n1; i < n2; ++i)
224         nvector_copy_inplace(b[i], a[i], n1, i + 1, s);
225     return ((void **) b);
226 }
227 
228 void    nlower_copy_inplace(char **b, char **a, int n1, int n2, unsigned int s)
229 {
230     int     n = n2 - n1;
231     int     i;
232 
233     if (b == NULL || a == NULL || n < 1 || s < 1)
234         return;
235     b = (char **) nlower_alloc(n1, n2, s);
236     for (i = n1; i < n2; ++i)
237         nvector_copy_inplace(b[i], a[i], n1, i + 1, s);
238 }
239 
240 void    nlower_zero_inplace(char **a, int n1, int n2, unsigned int s)
241 {
242     int     n = n2 - n1;
243     int     i;
244 
245     if (a == NULL || n < 1 || s < 1)
246         return;
247     for (i = n1; i < n2; ++i)
248         nvector_zero_inplace(a[i], n1, i + 1, s);
249 }
250 
251 void    nlower_free(char **a, int n1, int n2, unsigned int s)
252 {
253     int     i;
254 
255     if (a == NULL)
256     {
257         error("attempt to free NULL pointer in nlower_free()",warning);
258         return;
259     }
260     for (i = n1; i < n2; ++i)
261         nvector_free((void *) a[i], n1, s);
262     nvector_free((void *) a, n1, sizeof(char *));
263 }
264 
265 /* displaced upper triangle, element size s a[i][j]: n1 <=i < n2, 0 <=
266  * j <= i */
267 void  **nupper_alloc(int n1, int n2, unsigned int s)
268 {
269     char  **a;
270     int     n = n2 - n1;
271     int     i;
272 
273     if (n < 1 || s < 1)
274         return (NULL);
275     a = (char **) nvector_alloc(n1, n2, sizeof(char *));
276     for (i = n1; i < n2; ++i)
277         a[i] = (char *) nvector_alloc(i, n2, s);
278     return ((void **) a);
279 }
280 
281 void  **nupper_copy(char **a, int n1, int n2, unsigned int s)
282 {
283     char  **b;
284     int     n = n2 - n1;
285     int     i;
286 
287     if (a == NULL || n < 1 || s < 1)
288         return (NULL);
289     b = (char **) nupper_alloc(n1, n2, s);
290     for (i = n1; i < n2; ++i)
291         nvector_copy_inplace(b[i], a[i], i, n2, s);
292     return ((void **) b);
293 }
294 
295 void    nupper_copy_inplace(char **b, char **a, int n1, int n2, unsigned int s)
296 {
297     int     n = n2 - n1;
298     int     i;
299 
300     if (b == NULL || a == NULL || n < 1 || s < 1)
301         return;
302     b = (char **) nupper_alloc(n1, n2, s);
303     for (i = n1; i < n2; ++i)
304         nvector_copy_inplace(b[i], a[i], i, n2, s);
305 }
306 
307 void    nupper_zero_inplace(char **a, int n1, int n2, unsigned int s)
308 {
309     int     n = n2 - n1;
310     int     i;
311 
312     if (a == NULL || n < 1 || s < 1)
313         return;
314     for (i = n1; i < n2; ++i)
315         nvector_zero_inplace(a[i], i, n2, s);
316 }
317 
318 void    nupper_free(char **a, int n1, int n2, unsigned int s)
319 {
320     int     i;
321 
322     if (a == NULL)
323     {
324         error("attempt to free NULL structure in nupper_free()",warning);
325         return;
326     }
327     for (i = n1; i < n2; ++i)
328         nvector_free((void *) a[i], i, s);
329     nvector_free((void *) a, n1, sizeof(char *));
330 }
331 

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