From 1dd24ca6d2daeaeb0b9d90f432809508a98b259b Mon Sep 17 00:00:00 2001 From: Yinan Zhang Date: Thu, 2 Apr 2020 11:11:08 -0700 Subject: [PATCH] Add rotate functionality to ql module --- include/jemalloc/internal/ql.h | 10 ++++++++++ test/unit/ql.c | 25 ++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/include/jemalloc/internal/ql.h b/include/jemalloc/internal/ql.h index 93ddce58..3b780609 100644 --- a/include/jemalloc/internal/ql.h +++ b/include/jemalloc/internal/ql.h @@ -98,6 +98,16 @@ struct { \ ql_first(a_head_b) = (a_elm); \ } while (0) +/* + * An optimized version of: + * a_type *t = ql_first(a_head); + * ql_remove((a_head), t, a_field); + * ql_tail_insert((a_head), t, a_field); + */ +#define ql_rotate(a_head, a_field) do { \ + ql_first(a_head) = qr_next(ql_first(a_head), a_field); \ +} while (0) + #define ql_foreach(a_var, a_head, a_field) \ qr_foreach((a_var), ql_first(a_head), a_field) diff --git a/test/unit/ql.c b/test/unit/ql.c index c2b19812..662d1e8b 100644 --- a/test/unit/ql.c +++ b/test/unit/ql.c @@ -258,6 +258,28 @@ TEST_BEGIN(test_ql_concat_split) { } TEST_END +TEST_BEGIN(test_ql_rotate) { + list_head_t head; + list_t entries[NENTRIES]; + unsigned i; + + ql_new(&head); + init_entries(entries, sizeof(entries)/sizeof(list_t)); + for (i = 0; i < NENTRIES; i++) { + ql_tail_insert(&head, &entries[i], link); + } + + char head_id = ql_first(&head)->id; + for (i = 0; i < NENTRIES; i++) { + assert_c_eq(ql_first(&head)->id, head_id, ""); + ql_rotate(&head, link); + assert_c_eq(ql_last(&head, link)->id, head_id, ""); + head_id++; + } + test_entries_list(&head, entries, NENTRIES); +} +TEST_END + int main(void) { return test( @@ -267,5 +289,6 @@ main(void) { test_ql_head_insert, test_ql_head_remove, test_ql_insert, - test_ql_concat_split); + test_ql_concat_split, + test_ql_rotate); }