Move write_lock from attrs to each attribute
[platform/core/multimedia/libmm-common.git] / mm_attrs_private.c
1 /*
2  * libmm-common
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jonghyuk Choi <jhchoi.choi@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <malloc.h>
28 #include <assert.h>
29 #include <stdbool.h>
30 #include "mm_debug.h"
31 #include "mm_attrs_private.h"
32
33 int mmf_value_init(mmf_value_t *value, int type)
34 {
35         return_val_if_fail(value, -1);
36         return_val_if_fail(MMF_IS_VALUE_TYPE(type), -1);
37         memset(value, 0, sizeof(*value));
38         value->type = type;
39         return 0;
40 }
41
42 int mmf_value_copy(mmf_value_t *dest, const mmf_value_t *src)
43 {
44         return_val_if_fail(dest && src && src->type == dest->type, -1);
45         switch (src->type) {
46         case MM_ATTRS_TYPE_INT:
47                 dest->value.i_val = src->value.i_val;
48                 break;
49         case MM_ATTRS_TYPE_DOUBLE:
50                 dest->value.d_val = src->value.d_val;
51                 break;
52         case MM_ATTRS_TYPE_STRING:
53                 if (dest->value.s_val) {
54                         free(dest->value.s_val);
55                         dest->value.s_val = NULL;
56                         dest->size = 0;
57                 }
58                 if (src->value.s_val) {
59                         dest->value.s_val = strdup(src->value.s_val);
60                         dest->size = src->size;
61                 }
62                 break;
63         case MM_ATTRS_TYPE_DATA:
64                 dest->value.p_val = src->value.p_val;
65                 dest->size = src->size;
66                 break;
67         default:
68                 break;
69         }
70         return 0;
71 }
72
73 int mmf_value_set_int(mmf_value_t *v, int ival)
74 {
75         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_INT, -1);
76         v->value.i_val = ival;
77         return 0;
78 }
79
80 int mmf_value_get_int(const mmf_value_t *v)
81 {
82         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_INT, -1);
83         return v->value.i_val;
84 }
85
86 int mmf_value_set_double(mmf_value_t *v, double dval)
87 {
88         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_DOUBLE, -1);
89         v->value.d_val = dval;
90         return 0;
91 }
92
93 double mmf_value_get_double(mmf_value_t *v)
94 {
95         return_val_if_fail(v->type == MMF_VALUE_TYPE_DOUBLE, -1);
96         return v->value.d_val;
97 }
98
99 int mmf_value_set_string(mmf_value_t *v, const char *sval,int size)
100 {
101         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_STRING, -1);
102         if (v->value.s_val != NULL) {
103                 free(v->value.s_val);
104                 v->value.s_val = NULL;
105                 v->size = 0;
106         }
107         if (sval){
108                 v->value.s_val = strdup(sval);
109                 v->size = size;
110                 }
111         else{
112                 v->value.s_val = NULL;
113                 v->size = 0;
114                 }
115         return 0;
116 }
117
118 char* mmf_value_get_string(const mmf_value_t *v, int *size)
119 {
120         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_STRING, NULL);
121         *size=v->size;
122         return v->value.s_val;
123 }
124
125 int mmf_value_set_data(mmf_value_t *v, void *data,int size)
126 {
127         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_DATA, -1);
128         v->value.p_val = data;
129         v->size=size;
130         return 0;
131 }
132
133 void* mmf_value_get_data(const mmf_value_t *v,int *size)
134 {
135         return_val_if_fail(v && v->type == MMF_VALUE_TYPE_DATA, NULL);
136         *size=v->size;
137         return v->value.p_val;
138 }
139
140 void mmf_value_dump(const mmf_value_t *value)
141 {
142         return_if_fail(value);
143         switch (value->type) {
144         case MMF_VALUE_TYPE_INT:
145                 //mmf_debug(MMF_DEBUG_LOG, "value[int]: %d\n", value->value.i_val);
146                 break;
147         case MMF_VALUE_TYPE_DOUBLE:
148                 //mmf_debug(MMF_DEBUG_LOG, "value[double]: %f\n", value->value.d_val);
149                 break;
150         case MMF_VALUE_TYPE_STRING:
151                 //mmf_debug(MMF_DEBUG_LOG, "value[string]: %s\n", value->value.s_val);
152                 break;
153         case MMF_VALUE_TYPE_DATA:
154                 //mmf_debug(MMF_DEBUG_LOG, "value[data]: %p\n", value->value.p_val);
155                 break;
156         default:
157                 //mmf_debug(MMF_DEBUG_LOG, "value invalid!!\n");
158                 break;
159         }
160 }
161
162 int mmf_value_clear(mmf_value_t *value)
163 {
164         return_val_if_fail(value, -1);
165         if (value->type == MMF_VALUE_TYPE_STRING) {
166                 if (value->value.s_val) {
167                         free(value->value.s_val);
168                         value->value.s_val = NULL;
169                         value->size = 0;
170                 }
171         }
172         return 0;
173 }
174
175 int mmf_value_spec_init(mmf_value_spec_t *vs, int vs_type)
176 {
177         return_val_if_fail(vs, -1);
178         memset(vs, 0, sizeof(*vs));
179         vs->type = vs_type;
180         return 0;
181 }
182
183 int mmf_value_spec_set_int_range(mmf_value_spec_t *vs, int min, int max, int dval)
184 {
185         return_val_if_fail(vs && vs->type == MMF_VALUE_SPEC_INT_RANGE, -1);
186
187         vs->spec.int_spec.range.min = min;
188         vs->spec.int_spec.range.max = max;
189         vs->spec.int_spec.range.dval = dval;
190
191         return 0;
192 }
193
194 int mmf_value_spec_get_int_range(mmf_value_spec_t *vs, int *min, int *max, int *dval)
195 {
196         return_val_if_fail(vs && min && max && dval && vs->type == MMF_VALUE_SPEC_INT_RANGE, -1);
197
198         *min = vs->spec.int_spec.range.min;
199         *max = vs->spec.int_spec.range.max;
200         *dval = vs->spec.int_spec.range.dval;
201
202         return 0;
203 }
204
205 int mmf_value_spec_set_int_array(mmf_value_spec_t *vs, const int *array, int count, int dval)
206 {
207         int *array_copy = NULL;
208
209         return_val_if_fail(vs && array && vs->type == MMF_VALUE_SPEC_INT_ARRAY && count > 0, -1);
210
211         array_copy = (int *) malloc(sizeof(int) * count);
212         if (array_copy == NULL) {
213                 debug_error("malloc failed");
214                 return -1;
215         }
216
217         memcpy(array_copy, array, sizeof(int) * count);
218         vs->spec.int_spec.array.array = array_copy;
219         vs->spec.int_spec.array.count = count;
220         vs->spec.int_spec.array.dval = dval;
221
222         return 0;
223 }
224
225 int mmf_value_spec_get_int_array(mmf_value_spec_t *vs, int **array, int *count, int *dval)
226 {
227         return_val_if_fail(vs && array && count && dval && vs->type == MMF_VALUE_SPEC_INT_ARRAY, -1);
228
229         *array = vs->spec.int_spec.array.array;
230         *count = vs->spec.int_spec.array.count;
231         *dval = vs->spec.int_spec.array.dval;
232
233         return 0;
234 }
235
236 int mmf_value_spec_set_double_range(mmf_value_spec_t *vs, double min, double max, double dval)
237 {
238         return_val_if_fail(vs && vs->type == MMF_VALUE_SPEC_DOUBLE_RANGE, -1);
239
240         vs->spec.double_spec.range.min = min;
241         vs->spec.double_spec.range.max = max;
242         vs->spec.double_spec.range.dval = dval;
243
244         return 0;
245 }
246
247 int mmf_value_spec_get_double_range(mmf_value_spec_t *vs, double *min, double *max, double *dval)
248 {
249         return_val_if_fail(vs && min && max && dval && vs->type == MMF_VALUE_SPEC_DOUBLE_RANGE, -1);
250
251         *min = vs->spec.double_spec.range.min;
252         *max = vs->spec.double_spec.range.max;
253         *dval = vs->spec.double_spec.range.dval;
254
255         return 0;
256 }
257
258 int mmf_value_spec_set_double_array(mmf_value_spec_t *vs, const double *array, int count, double dval)
259 {
260         double *array_copy = NULL;
261
262         return_val_if_fail(vs && array && vs->type == MMF_VALUE_SPEC_DOUBLE_ARRAY && count > 0, -1);
263
264         array_copy = (double *) malloc(sizeof(double) * count);
265         if (array_copy == NULL) {
266                 debug_error("malloc failed");
267                 return -1;
268         }
269
270         memcpy(array_copy, array, sizeof(double) * count);
271         vs->spec.double_spec.array.array = array_copy;
272         vs->spec.double_spec.array.count = count;
273         vs->spec.double_spec.array.dval = dval;
274
275         return 0;
276 }
277
278 int mmf_value_spec_get_double_array(mmf_value_spec_t *vs, double **array, int *count, double *dval)
279 {
280         return_val_if_fail(vs && array && count && dval && vs->type == MMF_VALUE_SPEC_DOUBLE_ARRAY, -1);
281
282         *array = vs->spec.double_spec.array.array;
283         *count = vs->spec.double_spec.array.count;
284         *dval = vs->spec.double_spec.array.dval;
285
286         return 0;
287 }
288
289 int mmf_value_spec_clear(mmf_value_spec_t *vs)
290 {
291         return_val_if_fail(vs, -1);
292         switch(vs->type) {
293         case MMF_VALUE_SPEC_INT_ARRAY:
294                 if (vs->spec.int_spec.array.array) {
295                         free(vs->spec.int_spec.array.array);
296                         vs->spec.int_spec.array.array = NULL;
297                         vs->spec.int_spec.array.count = 0;
298                         vs->spec.int_spec.array.dval = 0;
299                 }
300                 break;
301         case MMF_VALUE_SPEC_DOUBLE_ARRAY:
302                 if (vs->spec.double_spec.array.array) {
303                         free(vs->spec.double_spec.array.array);
304                         vs->spec.double_spec.array.array = NULL;
305                         vs->spec.double_spec.array.count = 0;
306                         vs->spec.double_spec.array.dval = 0;
307                 }
308                 break;
309
310         default:
311                 break;
312         }
313
314         return 0;
315 }
316
317 int mmf_attribute_init(mmf_attribute_t *item, const char *name, int value_type, int flags)
318 {
319         return_val_if_fail(item && name, -1);
320
321         item->name = strdup(name);
322         item->flags = flags;
323
324         mmf_value_spec_init(&item->value_spec, MMF_VALUE_SPEC_NONE);
325         mmf_value_init(&item->value, value_type);
326         mmf_value_init(&item->tmpval, value_type);
327
328         return 0;
329 }
330
331 bool mmf_attribute_check_flags(mmf_attribute_t *item, int flags)
332 {
333         return_val_if_fail(item, false);
334         return item->flags & flags;
335 }
336
337 bool mmf_attribute_validate_int(mmf_attribute_t *item, int val)
338 {
339         return_val_if_fail(item, false);
340         return_val_if_fail(item->value.type == MMF_VALUE_TYPE_INT, false);
341
342         bool valid = true;
343         int i = 0;
344
345         switch (item->value_spec.type) {
346         case MMF_VALUE_SPEC_INT_RANGE:
347                 if (val < item->value_spec.spec.int_spec.range.min ||
348                                 val > item->value_spec.spec.int_spec.range.max) {
349                         valid = false;
350                         //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of range\n", item->name);
351                 }
352                 break;
353         case MMF_VALUE_SPEC_INT_ARRAY:
354                 valid = false;
355                 for (i = 0; i < item->value_spec.spec.int_spec.array.count; i++) {
356                         if (val == item->value_spec.spec.int_spec.array.array[i]) {
357                                 valid = true;
358                                 break;
359                         }
360                 }
361                 if (!valid) {
362                         //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of array\n", item->name);
363                 }
364                 break;
365         default:
366                 break;
367         }
368
369         return valid;
370 }
371
372 bool mmf_attribute_validate_double(mmf_attribute_t *item, double val)
373 {
374         return_val_if_fail(item, false);
375         return_val_if_fail(item->value.type == MMF_VALUE_TYPE_DOUBLE, false);
376
377         bool valid = true;
378         int i = 0;
379
380         switch (item->value_spec.type) {
381         case MMF_VALUE_SPEC_DOUBLE_RANGE:
382                 if (val < item->value_spec.spec.double_spec.range.min ||
383                                 val > item->value_spec.spec.double_spec.range.max) {
384                         valid = false;
385                         //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of range\n", item->name);
386                 }
387                 break;
388         case MMF_VALUE_SPEC_DOUBLE_ARRAY:
389                 valid = false;
390                 for (i = 0; i < item->value_spec.spec.double_spec.array.count; i++) {
391                         if (val == item->value_spec.spec.double_spec.array.array[i]) {
392                                 valid = true;
393                                 break;
394                         }
395                 }
396                 if (!valid) {
397                         //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of array\n", item->name);
398                 }
399                 break;
400         default:
401                 break;
402         }
403
404         return valid;
405 }
406
407 void mmf_attribute_clear(mmf_attribute_t *item)
408 {
409         assert(item);
410         if (item->name) {
411                 free(item->name);
412                 item->name = NULL;
413         }
414
415         mmf_value_clear(&item->tmpval);
416         mmf_value_clear(&item->value);
417         mmf_value_spec_clear(&item->value_spec);
418         pthread_mutex_destroy(&item->write_lock);
419 }
420
421 bool mmf_attribute_is_modified(mmf_attribute_t *item)
422 {
423         return_val_if_fail(item, false);
424         return (item->flags & MM_ATTRS_FLAG_MODIFIED);
425 }
426
427 void mmf_attribute_set_modified(mmf_attribute_t *item)
428 {
429         return_if_fail(item);
430         if (!(item->flags & MM_ATTRS_FLAG_MODIFIED)) {
431                 mmf_value_copy(&item->tmpval, &item->value);
432                 item->flags |= MM_ATTRS_FLAG_MODIFIED;
433         }
434 }
435
436 void mmf_attribute_set_readonly(mmf_attribute_t *item)
437 {
438         return_if_fail(item);
439         if (item->flags & MM_ATTRS_FLAG_WRITABLE)
440                 item->flags -= MM_ATTRS_FLAG_WRITABLE;
441 }
442
443 void mmf_attribute_set_disabled(mmf_attribute_t *item)
444 {
445         return_if_fail(item);
446         if (item->flags & MM_ATTRS_FLAG_WRITABLE)
447                 item->flags -= MM_ATTRS_FLAG_WRITABLE;
448         if (item->flags & MM_ATTRS_FLAG_READABLE)
449                 item->flags -= MM_ATTRS_FLAG_READABLE;
450         if (item->flags & MM_ATTRS_FLAG_MODIFIED)
451                 item->flags -= MM_ATTRS_FLAG_MODIFIED;
452 }
453
454 void mmf_attribute_commit(mmf_attribute_t *item)
455 {
456         return_if_fail(item);
457         if (item->flags & MM_ATTRS_FLAG_MODIFIED) {
458                 mmf_value_copy(&item->value, &item->tmpval);
459                 mmf_value_clear(&item->tmpval);
460                 item->flags ^= MM_ATTRS_FLAG_MODIFIED;
461         }
462 }
463
464 int mmf_attribute_set_int(mmf_attribute_t *item, int val)
465 {
466         return_val_if_fail(item, -1);
467         if (mmf_value_set_int(&item->tmpval, val) == 0) {
468                 item->flags |= MM_ATTRS_FLAG_MODIFIED;
469                 return 0;
470         }
471         return -1;
472 }
473
474 int mmf_attribute_set_double(mmf_attribute_t *item, double val)
475 {
476         return_val_if_fail(item, -1);
477         if (mmf_value_set_double(&item->tmpval, val) == 0) {
478                 item->flags |= MM_ATTRS_FLAG_MODIFIED;
479                 return 0;
480         }
481         return -1;
482 }
483
484 int mmf_attribute_set_string(mmf_attribute_t *item, const char *string, int size)
485 {
486         return_val_if_fail(item, -1);
487
488         if (mmf_value_set_string(&item->tmpval, string,size) == 0) {
489                 if (string)
490                         item->flags |= MM_ATTRS_FLAG_MODIFIED;
491
492                 return 0;
493         }
494         return -1;
495 }
496
497 int mmf_attribute_set_data(mmf_attribute_t *item, void *data, int size)
498 {
499         return_val_if_fail(item, -1);
500
501         if (mmf_value_set_data(&item->tmpval, data,size) == 0) {
502                 item->flags |= MM_ATTRS_FLAG_MODIFIED;
503                 return 0;
504         }
505         return -1;
506 }
507
508 MMHandleType mmf_attrs_new(int count)
509 {
510         return_val_if_fail(count > 0, 0);
511         mmf_attrs_t *attrs;
512         attrs = (mmf_attrs_t *) malloc (sizeof(mmf_attrs_t));
513
514         if (attrs == NULL) {
515                 debug_error("malloc failed");
516                 return 0;
517         }
518
519         attrs->count = count;
520         attrs->items = (mmf_attribute_t *) malloc (sizeof(mmf_attribute_t) * count);
521         if(attrs->items == NULL) {
522                 debug_error("Failed to malloc for attrs->items.");
523                 free(attrs);
524                 attrs=NULL;
525                 return 0;
526         }
527
528         memset(attrs->items, 0, sizeof(mmf_attribute_t) * count);
529
530         return (MMHandleType) attrs;
531 }
532
533 MMHandleType mmf_attrs_new_from_data(const char *name,
534                                      mmf_attrs_construct_info_t *info,
535                                      int count,
536                                      mmf_attrs_commit_func_t commit_func,
537                                      void *commit_param)
538 {
539         return_val_if_fail(info && count > 0, 0);
540         MMHandleType h;
541         mmf_attrs_t *attrs;
542
543         h = mmf_attrs_new(count);
544         if(!h) {
545                 return 0;
546         }
547         mmf_attrs_init(h, info, count);
548         attrs = (mmf_attrs_t *) h;
549         attrs->name = NULL;
550         if (name)
551                 attrs->name = strdup(name);
552         attrs->commit_func = commit_func;
553         attrs->commit_param = commit_param;
554         return h;
555 }
556
557 void mmf_attrs_free(MMHandleType h)
558 {
559         return_if_fail(h);
560         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
561         if (attrs) {
562                 if (attrs->name) {
563                         free(attrs->name);
564                         attrs->name = NULL;
565                 }
566                 if (attrs->items) {
567                         int i;
568                         for (i = 0; i < attrs->count; i++) {
569                                 mmf_attribute_clear(&attrs->items[i]);
570                         }
571                         free(attrs->items);
572                         attrs->items = NULL;
573                 }
574                 free(attrs);
575         }
576 }
577
578 int mmf_attrs_init(MMHandleType h, mmf_attrs_construct_info_t *info, int count)
579 {
580         return_val_if_fail(h && info && count > 0, -1);
581         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
582         mmf_attribute_t *item = NULL;
583         int i;
584         int size = 0;
585
586         for (i = 0; i < count; i++) {
587                 item = &attrs->items[i];
588
589                 pthread_mutex_init(&item->write_lock, NULL);
590
591                 mmf_attribute_init(item,
592                                    info[i].name,
593                                    info[i].value_type,
594                                    info[i].flags);
595
596                 switch (info[i].value_type) {
597                 case MMF_VALUE_TYPE_INT:
598                         assert(item->value.value.i_val == 0);
599                         mmf_value_set_int(&item->value,
600                                           (intptr_t)info[i].default_value);
601                         break;
602                 case MMF_VALUE_TYPE_DOUBLE:
603                 {
604                         int i_val = (intptr_t)info[i].default_value;
605                         double d_val = (double) i_val;
606                         assert(item->value.value.d_val == 0);
607                         mmf_value_set_double(&item->value, d_val);
608                         break;
609                 }
610                 case MMF_VALUE_TYPE_STRING:
611                         assert(item->value.value.s_val == NULL);
612                         if (info[i].default_value) {
613                                 size = strlen(info[i].default_value)+1;
614                         }
615                         mmf_value_set_string(&item->value,
616                                              (const char *)info[i].default_value,size);
617                         break;
618                 case MMF_VALUE_TYPE_DATA:
619                         assert(item->value.value.p_val == NULL);
620                         if (info[i].default_value) {
621                                 size = sizeof(info[i].default_value)+1;
622                         }
623                         mmf_value_set_data(&item->value, info[i].default_value,size);
624                         break;
625                 default:
626                         //mmf_debug(MMF_DEBUG_LOG, "ERROR: Invalid MMF_VALUE_TYPE\n");
627                         assert(0);
628                         break;
629                 }
630         }
631
632         return 0;
633 }
634
635 int mmf_attrs_commit(MMHandleType h)
636 {
637         return_val_if_fail(h, -1);
638
639         mmf_attrs_t *attrs = (mmf_attrs_t * )h;
640         mmf_attribute_t *item = NULL;
641         int i;
642         int ret = 0;
643
644         for (i = 0; i < attrs->count; ++i) {
645                 item = &attrs->items[i];
646
647                 MM_ATTR_ITEM_WRITE_LOCK(item);
648
649                 if (mmf_attribute_is_modified(item)) {
650                         if (attrs->commit_func) {
651                                 if (attrs->commit_func(i, item->name,
652                                                                                 &item->tmpval,
653                                                                                 attrs->commit_param)) {
654                                         mmf_attribute_commit(item);
655                                 } else {
656                                         /* without this, there is no way to solve modify when commit_func failed. */
657                                         if (item->flags & MM_ATTRS_FLAG_MODIFIED)
658                                                 item->flags ^= MM_ATTRS_FLAG_MODIFIED;
659                                         ret = -1;
660                                 }
661                         } else {
662                                 mmf_attribute_commit(item);
663                         }
664                 }
665
666                 MM_ATTR_ITEM_WRITE_UNLOCK(item);
667         }
668
669         return ret;
670 }
671
672 int mmf_attrs_commit_err(MMHandleType h, char **err_attr_name)
673 {
674         mmf_attrs_t *attrs = (mmf_attrs_t * )h;
675         mmf_attribute_t *item = NULL;
676         int i;
677         int ret = 0;
678
679         return_val_if_fail(h, -1);
680
681         for (i = 0; i < attrs->count; ++i) {
682                 item = &attrs->items[i];
683
684                 MM_ATTR_ITEM_WRITE_LOCK(item);
685
686                 if (mmf_attribute_is_modified(item)) {
687                         if (attrs->commit_func) {
688                                 if (attrs->commit_func(i, item->name,
689                                                                                 &item->tmpval,
690                                                                                 attrs->commit_param)) {
691                                         mmf_attribute_commit(item);
692                                 } else {
693                                         /* without this, there is no way to solve modify when commit_func failed. */
694                                         if (item->flags & MM_ATTRS_FLAG_MODIFIED)
695                                                 item->flags ^= MM_ATTRS_FLAG_MODIFIED;
696                                         ret = -1;
697
698                                         /* Set Error information */
699                                         if (err_attr_name)
700                                                 *err_attr_name = strdup(item->name);
701
702                                         MM_ATTR_ITEM_WRITE_UNLOCK(item);
703                                         break;
704                                 }
705                         } else {
706                                 mmf_attribute_commit(item);
707                         }
708                 }
709
710                 MM_ATTR_ITEM_WRITE_UNLOCK(item);
711         }
712
713         return ret;
714 }
715
716 int mmf_attrs_set_valid_type(MMHandleType h, int idx, int v_type)
717 {
718         return_val_if_fail(h, -1);
719         return_val_if_fail(idx>=0, -1);
720         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
721         return mmf_value_spec_init(&attrs->items[idx].value_spec, v_type);
722 }
723
724 int mmf_attrs_set_valid_range(MMHandleType h, int idx, int min, int max, int dval)
725 {
726         return_val_if_fail(h, -1);
727         return_val_if_fail(idx>=0, -1);
728         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
729         mmf_value_spec_clear(&attrs->items[idx].value_spec);
730         assert(attrs->items[idx].value_spec.type == MMF_VALUE_SPEC_INT_RANGE);
731         return mmf_value_spec_set_int_range(&attrs->items[idx].value_spec, min, max, dval);
732 }
733
734 int mmf_attrs_set_valid_array(MMHandleType h, int idx, const int *array, int count, int dval)
735 {
736         return_val_if_fail(h, -1);
737         return_val_if_fail(idx>=0, -1);
738         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
739         mmf_value_spec_clear(&attrs->items[idx].value_spec);
740         assert(attrs->items[idx].value_spec.type == MMF_VALUE_SPEC_INT_ARRAY);
741         return mmf_value_spec_set_int_array(&attrs->items[idx].value_spec, array, count, dval);
742 }
743
744 int mmf_attrs_set_valid_double_range(MMHandleType h, int idx, double min, double max, double dval)
745 {
746         return_val_if_fail(h, -1);
747         return_val_if_fail(idx>=0, -1);
748         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
749         mmf_value_spec_clear(&attrs->items[idx].value_spec);
750         assert(attrs->items[idx].value_spec.type == MMF_VALUE_SPEC_DOUBLE_RANGE);
751         return mmf_value_spec_set_double_range(&attrs->items[idx].value_spec, min, max, dval);
752 }
753
754 int mmf_attrs_set_valid_double_array(MMHandleType h, int idx, const double *array, int count, double dval)
755 {
756         return_val_if_fail(h, -1);
757         return_val_if_fail(idx>=0, -1);
758         mmf_attrs_t *attrs = (mmf_attrs_t *) h;
759         mmf_value_spec_clear(&attrs->items[idx].value_spec);
760         assert(attrs->items[idx].value_spec.type == MMF_VALUE_SPEC_DOUBLE_ARRAY);
761         return mmf_value_spec_set_double_array(&attrs->items[idx].value_spec, array, count, dval);
762 }