~ubuntu-branches/ubuntu/trusty/sblim-sfcb/trusty-proposed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * $Id: array.c,v 1.21 2008/04/14 18:42:34 gdread Exp $
 *
 * © Copyright IBM Corp. 2005, 2007
 *
 * THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
 * ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
 * CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
 *
 * You can obtain a current copy of the Eclipse Public License from
 * http://www.opensource.org/licenses/eclipse-1.0.php
 *
 * Author:       Frank Scheffler 
 * Contributors: Adrian Schuur <schuur@de.ibm.com>
 *
 * Description:
 * CMPIArray implementation.

  * In contrast to a regular array, there exists an additional increase()
  * method, which is only used by the native CMPIResult implementation to
  * grow an array stepwise.
 *
*/


#include <stdlib.h>
#include <string.h>

#include "native.h"
#include "array.h"
#include "objectImpl.h"

extern CMPIObjectPath *getObjectPath(char *path, char **msg);
extern const char *ClObjectGetClString(ClObjectHdr * hdr, ClString * id);
extern int localClientMode;

static struct native_array *__new_empty_array(int, CMPICount,
                                              CMPIType, CMPIStatus *);


/*****************************************************************************/

static void __make_NULL(struct native_array *a, int from, int to, int release)
{
   for (; from <= to; from++) {
      a->data[from].state = CMPI_nullValue;

      if (release) {
         sfcb_native_release_CMPIValue(a->type, &a->data[from].value);
      }
   }
}


static CMPIStatus __aft_release(CMPIArray * array)
{
   struct native_array *a = (struct native_array *) array;

   if (a->mem_state && a->mem_state != MEM_RELEASED) {
      int i = a->size;
      if (a->mem_state==MEM_NOT_TRACKED) while (i--) {
	      if (!(a->data[i].state & CMPI_nullValue) && a->refCount==0) {
		      sfcb_native_release_CMPIValue(a->type, &a->data[i].value);
	      }
      }
      memUnlinkEncObj(a->mem_state);
      a->mem_state = MEM_RELEASED;
      if (a->data) free(a->data);
      free(array);
      CMReturn(CMPI_RC_OK);
   }
   
   CMReturn(CMPI_RC_ERR_FAILED);
}


static CMPIArray *__aft_clone(const CMPIArray * array, CMPIStatus * rc)
{
   CMPIStatus tmp;
   struct native_array *a = (struct native_array *) array;
   struct native_array *new = __new_empty_array(MEM_NOT_TRACKED,
                                                a->size,
                                                a->type,
                                                &tmp);

   int i = a->size;
   while (i-- && tmp.rc == CMPI_RC_OK) {
      new->data[i].state = a->data[i].state;
      if (!(new->data[i].state & CMPI_nullValue)) {

         new->data[i].value =
             sfcb_native_clone_CMPIValue(a->type, &a->data[i].value, &tmp);
      }
   }

   if (rc)
      CMSetStatus(rc, tmp.rc);

   return (CMPIArray *) new;
}


static CMPICount __aft_getSize(const CMPIArray * array, CMPIStatus * rc)
{
   struct native_array *a = (struct native_array *) array;

   if (rc)
      CMSetStatus(rc, CMPI_RC_OK);
   return a->size;
}


static CMPIType __aft_getSimpleType(const CMPIArray * array, CMPIStatus * rc)
{
   struct native_array *a = (struct native_array *) array;

   if (rc)
      CMSetStatus(rc, CMPI_RC_OK);
   return a->type;
}


static CMPIData __aft_getElementAt(const CMPIArray * array,
                                   CMPICount index, CMPIStatus * rc)
{
   struct native_array *a = (struct native_array *) array;

   CMPIData result = { a->type, CMPI_badValue, {0} };

   if (index < a->size) {

      result.state = a->data[index].state;
      result.value = a->data[index].value;
   }

   if (rc)
      CMSetStatus(rc, CMPI_RC_OK);
   return result;
}

static CMPIStatus setElementAt ( CMPIArray * array, CMPICount index, const CMPIValue * val,
       CMPIType type, int opt )
{
   struct native_array * a = (struct native_array *) array;

   if ( a->dynamic && index==a->size ) {
      sfcb_native_array_increase_size(array, 1); 
   }

   if ( index < a->size ) {
      CMPIValue v;

      if ( type == CMPI_chars && a->type == CMPI_string ) {
	 if (val) {
	     v.string = sfcb_native_new_CMPIString ( (char *) val, NULL, 0 );
	     type = CMPI_string;
	     val  = &v;
	 } else {
	     type = CMPI_null;
	 }
      }

      if ((type & (CMPI_ENC|CMPI_ARRAY)) && (val==NULL || val->chars==NULL)) {
	 type = CMPI_null;
      }

      if ( opt || type == a->type ) {
         CMPIStatus rc = {CMPI_RC_OK, NULL};
         if ( ! ( a->data[index].state & CMPI_nullValue ) ) {
            __make_NULL ( a, index, index, a->mem_state == MEM_NOT_TRACKED );
         }

	 if (opt) {
	     sfcb_setAlignedValue(&(a->data[index].value),val,type);
	 } else if (a->mem_state != MEM_NOT_TRACKED) {
	     sfcb_setAlignedValue(&(a->data[index].value),val,type);
	 } else {
	     a->data[index].value = 
		 sfcb_native_clone_CMPIValue ( type, val, &rc );
	 }
	 if (localClientMode) switch (a->type) {
	 case CMPI_instance:
	 case CMPI_ref:
	 case CMPI_class:
	 case CMPI_string:
		 ((nativeEncObject*)a->data[index].value.inst)->refCount++;
	 default: ;
	 }
	 if (rc.rc == CMPI_RC_OK) {
		 a->data[index].state = 0;
	 }		 
	 return rc;
      }

      if ( type == CMPI_null ) {
         if ( ! ( a->data[index].state & CMPI_nullValue ) ) {
            __make_NULL ( a, index, index, a->mem_state == MEM_NOT_TRACKED );
         }
         CMReturn ( CMPI_RC_OK );
      }
   }
   CMReturn ( CMPI_RC_ERR_FAILED );
}

static CMPIStatus __aft_setElementAt (  CMPIArray * array, CMPICount index, const CMPIValue * val,
          CMPIType type )
{ 
   return setElementAt(array,index,val,type,0);
}

CMPIStatus arraySetElementNotTrackedAt(CMPIArray * array,
                                     CMPICount index,
                                     CMPIValue * val, CMPIType type)
{
   struct native_array *a = (struct native_array *) array;

   if (index < a->size) {
      CMPIValue v;

      if (type == CMPI_chars && a->type == CMPI_string) {
	 v.string = sfcb_native_new_CMPIString((char *) val, NULL, 0);
         type = CMPI_string;
         val = &v;
      }

      if (type == a->type) {
         CMPIStatus rc = { CMPI_RC_OK, NULL };
         a->data[index].state = 0;
	 sfcb_setAlignedValue(&(a->data[index].value),val,type);
         if (localClientMode) {
            switch (a->type) {
            case CMPI_instance:
            case CMPI_ref:
            case CMPI_class:
            case CMPI_string:
               ((nativeEncObject*)a->data[index].value.inst)->refCount++;
            default: ;
            }
         }
         return rc;
      }

      if (type == CMPI_null) {
         if (!(a->data[index].state & CMPI_nullValue)) {
            __make_NULL(a, index, index, a->mem_state == MEM_NOT_TRACKED);
         }
         CMReturn(CMPI_RC_OK);
      }
   }

   CMReturn(CMPI_RC_ERR_FAILED);
}



void sfcb_native_array_increase_size(const CMPIArray * array, CMPICount increment)
{
   struct native_array *a = (struct native_array *) array;

   if ((a->size+increment)>a->max) {
      if (a->size==0) a->max=8;
      else while ((a->size+increment)>a->max) a->max*=2;
      a->data = (struct native_array_item *)
         realloc(a->data, a->max * sizeof(struct native_array_item));
      memset(&a->data[a->size], 0, sizeof(struct native_array_item) * increment);
      __make_NULL(a , a->size , a->max - 1, 0);
   }
   a->size += increment;
}


void native_array_reset_size(const CMPIArray * array, CMPICount increment)
{
   struct native_array *a = (struct native_array *) array;
   a->size = increment;
}

static CMPIArrayFT aft = {
   NATIVE_FT_VERSION,
   __aft_release,
   __aft_clone,
   __aft_getSize,
   __aft_getSimpleType,
   __aft_getElementAt,
   __aft_setElementAt
};

static struct native_array *__new_empty_array(int mm_add,
                                              CMPICount size,
                                              CMPIType type, CMPIStatus * rc)
{
   static CMPIArray a = {
      "CMPIArray",
      &aft
   };
   struct native_array array,*tArray;
   int state;

   array.array = a;
   tArray=memAddEncObj(mm_add, &array, sizeof(array), &state);
   tArray->mem_state = state;   
   tArray->refCount=0;   
   
   type &= ~CMPI_ARRAY;
   tArray->type  = ( type == CMPI_chars )? CMPI_string: type;
   tArray->size  = size;
 
   if (tArray->size==0) {
      tArray->max=8;
      tArray->dynamic=1;
   }
   else {
      tArray->max=tArray->size;
      tArray->dynamic=0;
   }    
   
   tArray->data = (struct native_array_item *)
      malloc ( tArray->max * sizeof ( struct native_array_item ) );

   __make_NULL(tArray, 0, tArray->max - 1, 0);

   if (rc) CMSetStatus(rc, CMPI_RC_OK);
   return (struct native_array*)tArray;
}


CMPIArray *TrackedCMPIArray(CMPICount size, CMPIType type, CMPIStatus * rc)
{
   void *array = internal_new_CMPIArray(MEM_TRACKED, size, type, rc);
   return (CMPIArray *) array;
}

CMPIArray *internal_new_CMPIArray(int mode, CMPICount size, CMPIType type,
                                  CMPIStatus * rc)
{
   void *array = __new_empty_array(mode, size, type, rc);
   return (CMPIArray *) array;
}

CMPIArray *NewCMPIArray(CMPICount size, CMPIType type, CMPIStatus * rc)
{
   void *array = internal_new_CMPIArray(MEM_NOT_TRACKED, size, type, rc);
   return (CMPIArray *) array;
}



CMPIArray *internal_native_make_CMPIArray(CMPIData * av, CMPIStatus * rc,
                                 ClObjectHdr * hdr, int mode)
{
   void *array =
       __new_empty_array(mode, av->value.sint32, av->type, rc);
   int i, m;
   CMPIValue value;

   for (i = 0, m = (int) av->value.sint32; i < m; i++)
      if (av[i + 1].state != CMPI_nullValue) {
	  if (av[i + 1].type == CMPI_string) {
	      char *chars = (char *) ClObjectGetClString(hdr, (ClString *) & av[i + 1].value.chars);
	      arraySetElementNotTrackedAt((CMPIArray *) array, i, (CMPIValue *) chars, CMPI_chars);
	  } else if (av[i + 1].type == CMPI_ref) {
	      CMPIValue value;
	      char *msg = "";
	      char *chars = (char *) ClObjectGetClString(hdr, (ClString *) & av[i + 1].value.chars);
	      value.ref = getObjectPath(chars,&msg);	      
	      arraySetElementNotTrackedAt((CMPIArray *) array, i, &value, CMPI_ref);
	  } else if (av[i + 1].type == CMPI_instance) {
              value.inst = (void *)ClObjectGetClObject(hdr, (ClString *) & av[i + 1].value.inst);
              if(value.inst) {
              	 relocateSerializedInstance(value.inst);
              }
              arraySetElementNotTrackedAt((CMPIArray *) array, i, &value, CMPI_instance);
	  } else if (av[i + 1].type == CMPI_dateTime) {
          const char *str =
          ClObjectGetClString(hdr, (ClString *) & av[i + 1].value.chars);
          value.dateTime = sfcb_native_new_CMPIDateTime_fromChars(str, NULL);
          arraySetElementNotTrackedAt((CMPIArray *) array, i, &value, CMPI_dateTime);
      } else {
	      arraySetElementNotTrackedAt((CMPIArray *) array, i, &av[i + 1].value,
					  av[i + 1].type);
	  }
      }           
   return (CMPIArray *) array;
}

CMPIArray *native_make_CMPIArray(CMPIData * av, CMPIStatus * rc,
                                 ClObjectHdr * hdr)
{
    return internal_native_make_CMPIArray(av, rc, hdr, MEM_TRACKED);
}

CMPIStatus sfcb_simpleArrayAdd(CMPIArray * array, CMPIValue * val, CMPIType type)
{
   struct native_array * a = (struct native_array *) array;
   if (a->dynamic) {
      if (a->size==0) {
         a->type=type;
         if (a->type==CMPI_chars) a->type=CMPI_string;
      }   
      return setElementAt(array,a->size,val,type,1);
   }   
   CMReturn ( CMPI_RC_ERR_FAILED );
} 

/****************************************************************************/

/*** Local Variables:  ***/
/*** mode: C           ***/
/*** c-basic-offset: 4 ***/
/*** End:              ***/