diff --git a/lib/upipe/udict_inline.c b/lib/upipe/udict_inline.c index d7ad37b33..451125a2e 100644 --- a/lib/upipe/udict_inline.c +++ b/lib/upipe/udict_inline.c @@ -125,6 +125,9 @@ struct udict_inline { struct umem umem; /** used size */ size_t size; + /** attribute returned by the last iteration step, from which the next + * step resumes, or NULL */ + uint8_t *last; /** common structure */ struct udict udict; @@ -155,6 +158,7 @@ static struct udict *udict_inline_alloc(struct udict_mgr *mgr, size_t size) uint8_t *buffer = umem_buffer(&inl->umem); buffer[0] = UDICT_TYPE_END; inl->size = 1; + inl->last = NULL; return udict; } @@ -251,7 +255,11 @@ static uint8_t *udict_inline_find(struct udict *udict, const char *name, } /** @internal @This finds an attribute (shorthand or not) of the given name - * and type and returns the name and type of the next attribute. + * and type and returns the name and type of the next attribute. When given + * back the name and type it returned last, unchanged, it resumes from that + * attribute instead of looking it up, which makes a whole iteration linear + * rather than quadratic; any change to the udict in between makes it look the + * attribute up again. * * @param udict pointer to the udict * @param name_p reference to the name of the attribute to find, changed during @@ -268,15 +276,25 @@ static void udict_inline_iterate(struct udict *udict, const char **name_p, uint8_t *attr; if (likely(*type_p != UDICT_TYPE_END)) { - attr = udict_inline_find(udict, *name_p, *type_p); + /* the attribute returned last is given back if it has the same type + * and, unless it is a shorthand (unique by type), the same name + * pointer, which points into the buffer */ + if (inl->last != NULL && *inl->last == *type_p && + (*type_p > UDICT_TYPE_SHORTHAND ? *name_p == NULL : + *name_p == (const char *)(inl->last + 3))) + attr = inl->last; + else + attr = udict_inline_find(udict, *name_p, *type_p); if (likely(attr != NULL)) attr = udict_inline_next(attr); } else attr = umem_buffer(&inl->umem); if (unlikely(attr == NULL || *attr == UDICT_TYPE_END)) { + inl->last = NULL; *type_p = UDICT_TYPE_END; return; } + inl->last = attr; *type_p = *attr; *name_p = *attr > UDICT_TYPE_SHORTHAND ? NULL : (const char *)(attr + 3); @@ -361,6 +379,8 @@ static int udict_inline_delete(struct udict *udict, const char *name, { assert(type != UDICT_TYPE_END); struct udict_inline *inl = udict_inline_from_udict(udict); + /* the attributes after it move */ + inl->last = NULL; uint8_t *attr = udict_inline_find(udict, name, type); if (unlikely(attr == NULL)) return UBASE_ERR_INVALID; @@ -385,6 +405,8 @@ static int udict_inline_set(struct udict *udict, const char *name, uint8_t **attr_p) { struct udict_inline *inl = udict_inline_from_udict(udict); + /* the attributes may move */ + inl->last = NULL; const struct inline_shorthand *shorthand = NULL; enum udict_type base_type = type; if (likely(type > UDICT_TYPE_SHORTHAND)) { diff --git a/tests/udict_inline_test.c b/tests/udict_inline_test.c index ac66f5d14..7adc23b27 100644 --- a/tests/udict_inline_test.c +++ b/tests/udict_inline_test.c @@ -30,6 +30,176 @@ #define SALUTATION "Hello everyone, this is just some padding to make the structure bigger, if you don't mind." +/** iterates over a udict and checks that it returns the given attributes in + * order; with copy set, each name is given back as a copy, so that the + * attribute is looked up rather than resumed from */ +static void check_iterate(struct udict *udict, unsigned nb, + const enum udict_type *types, + const char *const *names, bool copy) +{ + const char *name = NULL; + enum udict_type type = UDICT_TYPE_END; + char buffer[16]; + for (unsigned i = 0; i < nb; i++) { + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == types[i]); + if (names[i] == NULL) + assert(name == NULL); + else + assert(name != NULL && !strcmp(name, names[i])); + if (copy && name != NULL) { + snprintf(buffer, sizeof(buffer), "%s", name); + name = buffer; + } + } + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_END); +} + +/** allocates a udict whose second attribute is a shorthand which a change to + * the first attribute moves: its old position then holds a byte of the + * opaque value of the third attribute, equal to its type, so that resuming + * from the old position would go wrong */ +static struct udict *moving_udict(struct udict_mgr *mgr) +{ + struct udict *udict = udict_alloc(mgr, 0); + assert(udict != NULL); + ubase_assert(udict_set_string(udict, "ab", UDICT_TYPE_STRING, "x.s")); + ubase_assert(udict_set_void(udict, NULL, UDICT_TYPE_FLOW_ERROR, NULL)); + uint8_t value[16]; + memset(value, UDICT_TYPE_FLOW_ERROR, sizeof(value)); + struct udict_opaque opaque = { .v = value, .size = sizeof(value) }; + ubase_assert(udict_set_opaque(udict, opaque, UDICT_TYPE_OPAQUE, "x.o")); + + const char *name = NULL; + enum udict_type type = UDICT_TYPE_END; + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_STRING && !strcmp(name, "x.s")); + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_FLOW_ERROR && name == NULL); + return udict; +} + +/** checks that an iteration resumes from the attribute it returned last when + * given it back, and looks the attribute up otherwise */ +static void test_iterate(struct udict_mgr *mgr) +{ + /* room enough for the attribute added below not to move the others */ + struct udict *udict = udict_alloc(mgr, 1024); + assert(udict != NULL); + ubase_assert(udict_set_unsigned(udict, 1, UDICT_TYPE_UNSIGNED, "x.a")); + ubase_assert(udict_set_string(udict, "def.", UDICT_TYPE_FLOW_DEF, NULL)); + ubase_assert(udict_set_unsigned(udict, 2, UDICT_TYPE_UNSIGNED, "x.b")); + ubase_assert(udict_set_void(udict, NULL, UDICT_TYPE_FLOW_ERROR, NULL)); + ubase_assert(udict_set_unsigned(udict, 3, UDICT_TYPE_UNSIGNED, "x.c")); + static const enum udict_type types[] = { + UDICT_TYPE_UNSIGNED, UDICT_TYPE_FLOW_DEF, UDICT_TYPE_UNSIGNED, + UDICT_TYPE_FLOW_ERROR, UDICT_TYPE_UNSIGNED + }; + static const char *const names[] = { "x.a", NULL, "x.b", NULL, "x.c" }; + + /* named and shorthand attributes are returned in order, whether the + * names are given back as returned or as copies, and an iteration can + * be started again once finished */ + check_iterate(udict, 5, types, names, false); + check_iterate(udict, 5, types, names, true); + check_iterate(udict, 5, types, names, false); + + /* a duplicated udict starts afresh: given a name of the original, it + * looks the attribute up among its own, and the original then resumes + * from where it was */ + struct udict *dup = udict_dup(udict); + assert(dup != NULL); + const char *name = NULL; + enum udict_type type = UDICT_TYPE_END; + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_UNSIGNED && !strcmp(name, "x.a")); + const char *dup_name = name; + enum udict_type dup_type = type; + ubase_assert(udict_iterate(dup, &dup_name, &dup_type)); + assert(dup_type == UDICT_TYPE_FLOW_DEF && dup_name == NULL); + ubase_assert(udict_iterate(dup, &dup_name, &dup_type)); + assert(dup_type == UDICT_TYPE_UNSIGNED && !strcmp(dup_name, "x.b")); + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_FLOW_DEF && name == NULL); + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_UNSIGNED && !strcmp(name, "x.b")); + assert(name != dup_name); + udict_free(dup); + + /* given another attribute than the one returned last, the iteration + * carries on after that one */ + ubase_assert(udict_iterate(udict, &name, &type)); + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_UNSIGNED && !strcmp(name, "x.c")); + name = "x.a"; + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_FLOW_DEF && name == NULL); + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_UNSIGNED && !strcmp(name, "x.b")); + + /* deleting the attribute returned last and resuming from the one before + * it carries on with the next one */ + ubase_assert(udict_delete(udict, UDICT_TYPE_UNSIGNED, "x.b")); + name = NULL; + type = UDICT_TYPE_FLOW_DEF; + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_FLOW_ERROR && name == NULL); + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_UNSIGNED && !strcmp(name, "x.c")); + + /* an attribute added during an iteration, without moving the others, is + * returned at its end */ + ubase_assert(udict_set_unsigned(udict, 4, UDICT_TYPE_UNSIGNED, "x.d")); + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_UNSIGNED && !strcmp(name, "x.d")); + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_END); + udict_free(udict); + + /* a deletion moving the attribute returned last makes the iteration + * look it up again */ + udict = moving_udict(mgr); + ubase_assert(udict_delete(udict, UDICT_TYPE_STRING, "x.s")); + name = NULL; + type = UDICT_TYPE_FLOW_ERROR; + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_OPAQUE && !strcmp(name, "x.o")); + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_END); + udict_free(udict); + + /* and so does a change of size, which moves the attribute to the end */ + udict = moving_udict(mgr); + ubase_assert(udict_set_string(udict, "a longer string", UDICT_TYPE_STRING, + "x.s")); + name = NULL; + type = UDICT_TYPE_FLOW_ERROR; + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_OPAQUE && !strcmp(name, "x.o")); + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_STRING && !strcmp(name, "x.s")); + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_END); + udict_free(udict); + + /* and so does an addition reallocating the attributes elsewhere, which + * a memory checker reports otherwise, as the old buffer is freed */ + udict = moving_udict(mgr); + static uint8_t big[4096]; + struct udict_opaque opaque = { .v = big, .size = sizeof(big) }; + ubase_assert(udict_set_opaque(udict, opaque, UDICT_TYPE_OPAQUE, "x.big")); + name = NULL; + type = UDICT_TYPE_FLOW_ERROR; + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_OPAQUE && !strcmp(name, "x.o")); + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_OPAQUE && !strcmp(name, "x.big")); + ubase_assert(udict_iterate(udict, &name, &type)); + assert(type == UDICT_TYPE_END); + udict_free(udict); +} + int main(int argc, char **argv) { struct uprobe *uprobe = uprobe_stdio_alloc(NULL, stdout, UPROBE_LOG_DEBUG); @@ -39,6 +209,8 @@ int main(int argc, char **argv) -1, -1); assert(mgr != NULL); + test_iterate(mgr); + struct udict *udict1 = udict_alloc(mgr, 0); assert(udict1 != NULL);