Skip to content

Commit 8337b99

Browse files
khachichenglong
authored andcommitted
feat(property): freeze properties after realize
1 parent 6df5892 commit 8337b99

2 files changed

Lines changed: 50 additions & 4 deletions

File tree

src/qom/property.c

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
#include <assert.h>
22
#include <ctype.h>
33
#include <errno.h>
4+
#include <stdbool.h>
45
#include <stdlib.h>
56
#include <string.h>
67

78
#include "miniqom/property.h"
9+
#include "miniqom/error.h"
10+
#include "miniqom/object.h"
811
#include "miniqom/type.h"
912

1013
ObjectClass *object_class_new(Type *type)
@@ -116,9 +119,23 @@ static bool parse_u64(const char *text, uint64_t *value)
116119
bool object_property_set_from_string(Object *obj, const char *name,
117120
const char *text, Error *err)
118121
{
119-
const Property *property = object_property_find(obj, name);
122+
const Property *property;
120123
PropertyValue value = {0};
121124

125+
if (!obj || !name || !text)
126+
{
127+
error_set(err, "invalid property set arguments");
128+
return false;
129+
}
130+
131+
if (object_is_realized(obj))
132+
{
133+
error_set(err, "object is already realized");
134+
return false;
135+
}
136+
137+
property = object_property_find(obj, name);
138+
122139
if (!property)
123140
{
124141
error_set(err, "property not found");
@@ -178,4 +195,4 @@ bool object_property_get(Object *obj,
178195
return false;
179196
}
180197
return property->get(obj, value, err);
181-
}
198+
}

tests/test_qom.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include <assert.h>
2+
#include <stdio.h>
23
#include <string.h>
34

5+
#include "miniqom/error.h"
46
#include "miniqom/memory.h"
57
#include "miniqom/object.h"
68
#include "miniqom/property.h"
@@ -186,9 +188,36 @@ int main(void)
186188
assert(backend->parent_obj.size == 2ULL * 1024 * 1024 * 1024);
187189
assert(!strcmp(backend->parent_obj.swap_storage, "file:///swap"));
188190

189-
// ====== Test property ======
191+
// ====== Freeze properties ======
192+
//
193+
Object *property_object;
194+
property_object = object_new(TYPE_MEMORY_BACKEND_MEMFD);
195+
assert(property_object != NULL);
196+
190197
PropertyValue property_value;
198+
error_clear(&err);
199+
200+
assert(object_property_set_from_string(property_object,
201+
"size",
202+
"2G",
203+
&err));
204+
205+
assert(object_realize(property_object, &err));
206+
assert(object_is_realized(property_object));
207+
208+
assert(!object_property_set_from_string(property_object,
209+
"size", "4G",
210+
&err));
191211

212+
assert(!strcmp(err.message, "object is already realized"));
213+
214+
assert(object_property_get(property_object,
215+
"size",
216+
&property_value,
217+
&err));
218+
219+
assert(property_value.u64 == 2ULL * 1024 * 1024 * 1024);
220+
// ====== Test property ======
192221
assert(object_property_get(mem0, "size",
193222
&property_value, &err));
194223
assert(property_value.u64 == 2ULL * 1024 * 1024 * 1024);
@@ -252,4 +281,4 @@ int main(void)
252281
// ======= Release test resources ======
253282
object_free(root);
254283
return 0;
255-
}
284+
}

0 commit comments

Comments
 (0)