*
*/
-
+
#include <stdio.h>
#include <string.h>
dest->value.d_val = src->value.d_val;
break;
case MM_ATTRS_TYPE_STRING:
- if (src->value.s_val)
- {
- dest->value.s_val = strdup(src->value.s_val);
- dest->size = src->size;
- }
- else
- {
+ if (dest->value.s_val) {
+ free(dest->value.s_val);
dest->value.s_val = NULL;
dest->size = 0;
}
+ if (src->value.s_val) {
+ dest->value.s_val = strdup(src->value.s_val);
+ dest->size = src->size;
+ }
break;
case MM_ATTRS_TYPE_DATA:
dest->value.p_val = src->value.p_val;
return 0;
}
-const char* mmf_value_get_string(const mmf_value_t *v, int *size)
+char* mmf_value_get_string(const mmf_value_t *v, int *size)
{
return_val_if_fail(v && v->type == MMF_VALUE_TYPE_STRING, NULL);
*size=v->size;
return_if_fail(value);
switch (value->type) {
case MMF_VALUE_TYPE_INT:
- mmf_debug(MMF_DEBUG_LOG, "value[int]: %d\n", value->value.i_val);
+ //mmf_debug(MMF_DEBUG_LOG, "value[int]: %d\n", value->value.i_val);
break;
case MMF_VALUE_TYPE_DOUBLE:
- mmf_debug(MMF_DEBUG_LOG, "value[double]: %f\n", value->value.d_val);
+ //mmf_debug(MMF_DEBUG_LOG, "value[double]: %f\n", value->value.d_val);
break;
case MMF_VALUE_TYPE_STRING:
- mmf_debug(MMF_DEBUG_LOG, "value[string]: %s\n", value->value.s_val);
+ //mmf_debug(MMF_DEBUG_LOG, "value[string]: %s\n", value->value.s_val);
break;
case MMF_VALUE_TYPE_DATA:
- mmf_debug(MMF_DEBUG_LOG, "value[data]: %p\n", value->value.p_val);
+ //mmf_debug(MMF_DEBUG_LOG, "value[data]: %p\n", value->value.p_val);
break;
default:
- mmf_debug(MMF_DEBUG_LOG, "value invalid!!\n");
+ //mmf_debug(MMF_DEBUG_LOG, "value invalid!!\n");
break;
}
}
{
return_val_if_fail(item, false);
return_val_if_fail(item->value.type == MMF_VALUE_TYPE_INT, false);
-
+
bool valid = true;
int i = 0;
-
+
switch (item->value_spec.type) {
case MMF_VALUE_SPEC_INT_RANGE:
- if (val < item->value_spec.spec.int_spec.range.min ||
+ if (val < item->value_spec.spec.int_spec.range.min ||
val > item->value_spec.spec.int_spec.range.max) {
valid = false;
- mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of range\n", item->name);
+ //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of range\n", item->name);
}
break;
case MMF_VALUE_SPEC_INT_ARRAY:
}
}
if (!valid) {
- mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of array\n", item->name);
+ //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of array\n", item->name);
}
break;
default:
- break;
+ break;
}
-
+
return valid;
}
{
return_val_if_fail(item, false);
return_val_if_fail(item->value.type == MMF_VALUE_TYPE_DOUBLE, false);
-
+
bool valid = true;
int i = 0;
-
+
switch (item->value_spec.type) {
case MMF_VALUE_SPEC_DOUBLE_RANGE:
- if (val < item->value_spec.spec.double_spec.range.min ||
+ if (val < item->value_spec.spec.double_spec.range.min ||
val > item->value_spec.spec.double_spec.range.max) {
valid = false;
- mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of range\n", item->name);
+ //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of range\n", item->name);
}
break;
case MMF_VALUE_SPEC_DOUBLE_ARRAY:
}
}
if (!valid) {
- mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of array\n", item->name);
+ //mmf_debug(MMF_DEBUG_LOG, "[mmf_attribute:%s] out of array\n", item->name);
}
break;
default:
- break;
+ break;
}
-
+
return valid;
}
mmf_value_clear(&item->tmpval);
mmf_value_clear(&item->value);
mmf_value_spec_clear(&item->value_spec);
+ pthread_mutex_destroy(&item->write_lock);
}
bool mmf_attribute_is_modified(mmf_attribute_t *item)
return_val_if_fail(item, -1);
if (mmf_value_set_string(&item->tmpval, string,size) == 0) {
- if (string)
- item->flags |= MM_ATTRS_FLAG_MODIFIED;
-
+ if (string)
+ item->flags |= MM_ATTRS_FLAG_MODIFIED;
+
return 0;
}
return -1;
return_val_if_fail(count > 0, 0);
mmf_attrs_t *attrs;
attrs = (mmf_attrs_t *) malloc (sizeof(mmf_attrs_t));
+
+ if (attrs == NULL) {
+ debug_error("malloc failed");
+ return 0;
+ }
+
attrs->count = count;
attrs->items = (mmf_attribute_t *) malloc (sizeof(mmf_attribute_t) * count);
+ if(attrs->items == NULL) {
+ debug_error("Failed to malloc for attrs->items.");
+ free(attrs);
+ attrs=NULL;
+ return 0;
+ }
+
memset(attrs->items, 0, sizeof(mmf_attribute_t) * count);
+
return (MMHandleType) attrs;
}
mmf_attrs_t *attrs;
h = mmf_attrs_new(count);
+ if(!h) {
+ return 0;
+ }
mmf_attrs_init(h, info, count);
attrs = (mmf_attrs_t *) h;
attrs->name = NULL;
{
return_val_if_fail(h && info && count > 0, -1);
mmf_attrs_t *attrs = (mmf_attrs_t *) h;
+ mmf_attribute_t *item = NULL;
int i;
int size = 0;
for (i = 0; i < count; i++) {
- mmf_attribute_init(&attrs->items[i],
+ item = &attrs->items[i];
+
+ pthread_mutex_init(&item->write_lock, NULL);
+
+ mmf_attribute_init(item,
info[i].name,
info[i].value_type,
info[i].flags);
switch (info[i].value_type) {
case MMF_VALUE_TYPE_INT:
- assert(attrs->items[i].value.value.i_val == 0);
- mmf_value_set_int(&attrs->items[i].value,
- (int)info[i].default_value);
+ assert(item->value.value.i_val == 0);
+ mmf_value_set_int(&item->value,
+ (intptr_t)info[i].default_value);
break;
case MMF_VALUE_TYPE_DOUBLE:
{
- int i_val = (int)info[i].default_value;
+ int i_val = (intptr_t)info[i].default_value;
double d_val = (double) i_val;
- assert(attrs->items[i].value.value.d_val == 0);
- mmf_value_set_double(&attrs->items[i].value, d_val);
+ assert(item->value.value.d_val == 0);
+ mmf_value_set_double(&item->value, d_val);
break;
}
case MMF_VALUE_TYPE_STRING:
- assert(attrs->items[i].value.value.s_val == NULL);
+ assert(item->value.value.s_val == NULL);
if (info[i].default_value) {
size = strlen(info[i].default_value)+1;
}
- mmf_value_set_string(&attrs->items[i].value,
+ mmf_value_set_string(&item->value,
(const char *)info[i].default_value,size);
break;
case MMF_VALUE_TYPE_DATA:
- assert(attrs->items[i].value.value.p_val == NULL);
+ assert(item->value.value.p_val == NULL);
if (info[i].default_value) {
size = sizeof(info[i].default_value)+1;
}
- mmf_value_set_data(&attrs->items[i].value, info[i].default_value,size);
+ mmf_value_set_data(&item->value, info[i].default_value,size);
break;
default:
- mmf_debug(MMF_DEBUG_LOG, "ERROR: Invalid MMF_VALUE_TYPE\n");
+ //mmf_debug(MMF_DEBUG_LOG, "ERROR: Invalid MMF_VALUE_TYPE\n");
assert(0);
break;
}
int mmf_attrs_commit(MMHandleType h)
{
return_val_if_fail(h, -1);
-
+
mmf_attrs_t *attrs = (mmf_attrs_t * )h;
+ mmf_attribute_t *item = NULL;
int i;
int ret = 0;
for (i = 0; i < attrs->count; ++i) {
- if (mmf_attribute_is_modified(&attrs->items[i])) {
+ item = &attrs->items[i];
+
+ MM_ATTR_ITEM_WRITE_LOCK(item);
+
+ if (mmf_attribute_is_modified(item)) {
if (attrs->commit_func) {
- if (attrs->commit_func(i, attrs->items[i].name,
- &attrs->items[i].tmpval,
+ if (attrs->commit_func(i, item->name,
+ &item->tmpval,
attrs->commit_param)) {
- mmf_attribute_commit(&attrs->items[i]);
+ mmf_attribute_commit(item);
} else {
/* without this, there is no way to solve modify when commit_func failed. */
- if (attrs->items[i].flags & MM_ATTRS_FLAG_MODIFIED)
- attrs->items[i].flags ^= MM_ATTRS_FLAG_MODIFIED;
+ if (item->flags & MM_ATTRS_FLAG_MODIFIED)
+ item->flags ^= MM_ATTRS_FLAG_MODIFIED;
ret = -1;
}
} else {
- mmf_attribute_commit(&attrs->items[i]);
+ mmf_attribute_commit(item);
}
}
+
+ MM_ATTR_ITEM_WRITE_UNLOCK(item);
}
+
return ret;
}
int mmf_attrs_commit_err(MMHandleType h, char **err_attr_name)
{
mmf_attrs_t *attrs = (mmf_attrs_t * )h;
+ mmf_attribute_t *item = NULL;
int i;
int ret = 0;
return_val_if_fail(h, -1);
for (i = 0; i < attrs->count; ++i) {
- if (mmf_attribute_is_modified(&attrs->items[i])) {
+ item = &attrs->items[i];
+
+ MM_ATTR_ITEM_WRITE_LOCK(item);
+
+ if (mmf_attribute_is_modified(item)) {
if (attrs->commit_func) {
- if (attrs->commit_func(i, attrs->items[i].name,
- &attrs->items[i].tmpval,
+ if (attrs->commit_func(i, item->name,
+ &item->tmpval,
attrs->commit_param)) {
- mmf_attribute_commit(&attrs->items[i]);
+ mmf_attribute_commit(item);
} else {
/* without this, there is no way to solve modify when commit_func failed. */
- if (attrs->items[i].flags & MM_ATTRS_FLAG_MODIFIED)
- attrs->items[i].flags ^= MM_ATTRS_FLAG_MODIFIED;
+ if (item->flags & MM_ATTRS_FLAG_MODIFIED)
+ item->flags ^= MM_ATTRS_FLAG_MODIFIED;
ret = -1;
/* Set Error information */
if (err_attr_name)
- *err_attr_name = strdup(attrs->items[i].name);
+ *err_attr_name = strdup(item->name);
+ MM_ATTR_ITEM_WRITE_UNLOCK(item);
break;
}
} else {
- mmf_attribute_commit(&attrs->items[i]);
+ mmf_attribute_commit(item);
}
}
+
+ MM_ATTR_ITEM_WRITE_UNLOCK(item);
}
+
return ret;
}
assert(attrs->items[idx].value_spec.type == MMF_VALUE_SPEC_DOUBLE_ARRAY);
return mmf_value_spec_set_double_array(&attrs->items[idx].value_spec, array, count, dval);
}
-