FreeBASIC  0.91.0
array_tmpdesc.c
Go to the documentation of this file.
1 
2 /* temp descriptor core, for array fields passed by descriptor */
3 
4 #include "fb.h"
5 #include <stddef.h>
6 
7 /**********
8  * temp array descriptors
9  **********/
10 
11 static FB_LIST tmpdsList = { 0 };
12 
14 
15 /*:::::*/
17  (
18  void
19  )
20 {
21  FB_ARRAY_TMPDESC *dsc;
22 
23  if( (tmpdsList.fhead == NULL) && (tmpdsList.head == NULL) )
24  fb_hListInit( &tmpdsList, (void *)fb_tmpdsTB, sizeof(FB_ARRAY_TMPDESC), FB_ARRAY_TMPDESCRIPTORS );
25 
26  dsc = (FB_ARRAY_TMPDESC *)fb_hListAllocElem( &tmpdsList );
27  if( dsc == NULL )
28  return NULL;
29 
30  return (FBARRAY *)&dsc->array;
31 }
32 
33 /*:::::*/
35  (
36  FBARRAY *src
37  )
38 {
39  FB_ARRAY_TMPDESC *dsc;
40 
41  dsc = (FB_ARRAY_TMPDESC *)((char *)src - offsetof(FB_ARRAY_TMPDESC, array));
42 
43  fb_hListFreeElem( &tmpdsList, (FB_LISTELEM *)dsc );
44 }
45 
47  (
48  FBARRAY **pdesc,
49  void *arraydata,
50  size_t element_len,
51  size_t dimensions,
52  ...
53  )
54 {
55  va_list ap;
56  size_t i, elements;
57  ssize_t diff;
58  FBARRAY *array;
59  FBARRAYDIM *dim;
60  ssize_t lbTB[FB_MAXDIMENSIONS];
61  ssize_t ubTB[FB_MAXDIMENSIONS];
62 
63  FB_LOCK();
64  array = fb_hArrayAllocTmpDesc( );
65  FB_UNLOCK();
66 
67  *pdesc = array;
68 
69  if( array == NULL )
70  return NULL;
71 
72  if( dimensions == 0)
73  {
74  /* special case for GET temp arrays */
75  array->size = 0;
76  return array;
77  }
78 
79  va_start( ap, dimensions );
80 
81  dim = &array->dimTB[0];
82 
83  for( i = 0; i < dimensions; i++ )
84  {
85  lbTB[i] = va_arg( ap, ssize_t );
86  ubTB[i] = va_arg( ap, ssize_t );
87 
88  dim->elements = (ubTB[i] - lbTB[i]) + 1;
89  dim->lbound = lbTB[i];
90  dim->ubound = ubTB[i];
91  ++dim;
92  }
93 
94  va_end( ap );
95 
96  elements = fb_hArrayCalcElements( dimensions, &lbTB[0], &ubTB[0] );
97  diff = fb_hArrayCalcDiff( dimensions, &lbTB[0], &ubTB[0] ) * element_len;
98 
99  array->ptr = arraydata;
100 
101  FB_ARRAY_SETDESC( array, element_len, dimensions, elements * element_len, diff );
102 
103  return array;
104 }
105 
107 {
108 
109  FB_LOCK();
110  fb_hArrayFreeTmpDesc( pdesc );
111  FB_UNLOCK();
112 
113 }