commit 87c37666a2a5a9a407554e48f76c2f2323618ad8
parent e6e6d01acbed0dfbb90359fbd42af27c410cd512
Author: vaplv <vaplv@free.fr>
Date: Wed, 27 Nov 2013 16:46:11 +0100
Add an `get item id` function to the free list
Diffstat:
2 files changed, 32 insertions(+), 20 deletions(-)
diff --git a/src/free_list.h b/src/free_list.h
@@ -17,6 +17,8 @@ struct fid {
static const struct fid FID_NULL = { UINT32_MAX, UINT32_MAX };
#define IS_FID_NULL(Fid) ((Fid).index == UINT32_MAX)
+#define FID_EQ(Fid0, Fid1) \
+ ((Fid0).index == (Fid1).index && (Fid0).name == (Fid1).name)
# endif /* FREE_LIST_H */
#else
@@ -53,19 +55,19 @@ FLIST_FUNC__(release)(struct FLIST_TYPE__* list)
}
static FINLINE bool
-FLIST_FUNC__(hold)(struct FLIST_TYPE__* list, struct fid* id)
+FLIST_FUNC__(hold)(struct FLIST_TYPE__* list, struct fid id)
{
- ASSERT(list && id);
- return id->index < list->nitems
- && list->items[id->index].fitem__.id.name == id->name;
+ ASSERT(list);
+ return id.index < list->nitems
+ && list->items[id.index].fitem__.id.name == id.name;
}
static FINLINE struct FITEM_TYPE*
-FLIST_FUNC__(get)(struct FLIST_TYPE__* list, struct fid* id)
+FLIST_FUNC__(get)(struct FLIST_TYPE__* list, struct fid id)
{
- ASSERT(list && id);
+ ASSERT(list);
if(FLIST_FUNC__(hold)(list, id)) {
- return list->items + id->index;
+ return list->items + id.index;
} else {
return NULL;
}
@@ -104,9 +106,9 @@ FLIST_FUNC__(add)(struct FLIST_TYPE__* list)
}
static FINLINE void
-FLIST_FUNC__(del)(struct FLIST_TYPE__* list, struct fid* id)
+FLIST_FUNC__(del)(struct FLIST_TYPE__* list, struct fid id)
{
- ASSERT(list && id);
+ ASSERT(list);
if(FLIST_FUNC__(hold)(list, id)) {
struct FITEM_TYPE* item = FLIST_FUNC__(get)(list, id);
item->fitem__.id.name = UINT32_MAX;
@@ -115,6 +117,13 @@ FLIST_FUNC__(del)(struct FLIST_TYPE__* list, struct fid* id)
}
}
+static FINLINE struct fid
+CONCAT(FITEM_TYPE,_id_get)(const struct FITEM_TYPE* item)
+{
+ ASSERT(item);
+ return item->fitem__.id;
+}
+
#undef FLIST_TYPE__
#undef FLIST_FUNC__
#undef FITEM_TYPE
diff --git a/src/test_free_list.c b/src/test_free_list.c
@@ -23,13 +23,16 @@ main(int argc, char** argv)
}
flist_object_init(&list);
- CHECK(flist_object_hold(&list, &id[0]), false);
- CHECK(flist_object_get(&list, &id[0]), NULL);
+ CHECK(flist_object_hold(&list, id[0]), false);
+ CHECK(flist_object_get(&list, id[0]), NULL);
FOR_EACH(int, i, 0, NB_OBJ / 2) {
+ struct fid tmp_id;
id[i] = flist_object_add(&list);
- CHECK(flist_object_hold(&list, &id[i]), true);
- obj = flist_object_get(&list, &id[i]);
+ CHECK(flist_object_hold(&list, id[i]), true);
+ obj = flist_object_get(&list, id[i]);
+ tmp_id = object_id_get(obj);
+ CHECK(FID_EQ(tmp_id, id[i]), true);
NCHECK(obj, NULL);
obj->i = 0xDECAF000 + (unsigned)i;
}
@@ -37,25 +40,25 @@ main(int argc, char** argv)
FOR_EACH(int, i, 0, NB_OBJ * 2 / 3) {
const float rand_f /* in [0, 1] */ = (float)rand() / (float)RAND_MAX;
const int i = (int)(rand_f * (NB_OBJ - 1));
- flist_object_del(&list, &id[i]);
+ flist_object_del(&list, id[i]);
id[i] = FID_NULL;
}
FOR_EACH(int, i, NB_OBJ / 2, NB_OBJ) {
id[i] = flist_object_add(&list);
- CHECK(flist_object_hold(&list, &id[i]), true);
- obj = flist_object_get(&list, &id[i]);
+ CHECK(flist_object_hold(&list, id[i]), true);
+ obj = flist_object_get(&list, id[i]);
NCHECK(obj, NULL);
obj->i = 0xDECAF000 + (unsigned)i;
}
FOR_EACH(int, i, 0, NB_OBJ) {
if(IS_FID_NULL(id[i])) {
- CHECK(flist_object_hold(&list, &id[i]), false);
- CHECK(flist_object_get(&list, &id[i]), NULL);
+ CHECK(flist_object_hold(&list, id[i]), false);
+ CHECK(flist_object_get(&list, id[i]), NULL);
} else {
- CHECK(flist_object_hold(&list, &id[i]), true);
- obj = flist_object_get(&list, &id[i]);
+ CHECK(flist_object_hold(&list, id[i]), true);
+ obj = flist_object_get(&list, id[i]);
CHECK(obj->i, 0xDECAF000 + (unsigned)i);
}
}