From 5cdeb6d759c4ac5184ebe31267358a36e98d20ca Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sat, 11 Jul 2020 00:17:03 +0200 Subject: [PATCH] refactor: remove jansson implementation of dirbuffer --- lib/webfuse_provider/impl/dirbuffer.c | 47 ++++++++++++++----- lib/webfuse_provider/impl/dirbuffer.h | 18 +++++-- lib/webfuse_provider/impl/message_writer.c | 13 ++--- .../provider/test_dirbuffer.cc | 16 +++---- 4 files changed, 62 insertions(+), 32 deletions(-) diff --git a/lib/webfuse_provider/impl/dirbuffer.c b/lib/webfuse_provider/impl/dirbuffer.c index 0d4f51c..e031adb 100644 --- a/lib/webfuse_provider/impl/dirbuffer.c +++ b/lib/webfuse_provider/impl/dirbuffer.c @@ -1,10 +1,15 @@ #include "webfuse_provider/impl/dirbuffer.h" #include +#include + +#define WFP_IMPL_DIRBUFFER_DEFAULT_CAPACITY 8 struct wfp_dirbuffer * wfp_impl_dirbuffer_create(void) { struct wfp_dirbuffer * buffer = malloc(sizeof(struct wfp_dirbuffer)); - buffer->entries = json_array(); + buffer->size = 0; + buffer->capacity = WFP_IMPL_DIRBUFFER_DEFAULT_CAPACITY; + buffer->entries = malloc(sizeof(struct wfp_dirbuffer_entry) * buffer->capacity); return buffer; } @@ -12,11 +17,12 @@ struct wfp_dirbuffer * wfp_impl_dirbuffer_create(void) void wfp_impl_dirbuffer_dispose( struct wfp_dirbuffer * buffer) { - if (NULL != buffer->entries) + for (size_t i = 0; i < buffer->size; i++) { - json_decref(buffer->entries); + free(buffer->entries[i].name); } + free(buffer->entries); free(buffer); } @@ -25,18 +31,35 @@ void wfp_impl_dirbuffer_add( char const * name, ino_t inode) { - json_t * entry = json_object(); - json_object_set_new(entry, "name", json_string(name)); - json_object_set_new(entry, "inode", json_integer(inode)); + if (buffer->size >= buffer->capacity) + { + buffer->capacity *= 2; + buffer->entries = realloc(buffer->entries, (sizeof(struct wfp_dirbuffer_entry) * buffer->capacity)); + } - json_array_append_new(buffer->entries, entry); + buffer->entries[buffer->size].name = strdup(name); + buffer->entries[buffer->size].inode = inode; + buffer->size++; } -json_t * wfp_impl_dirbuffer_take( +size_t +wfp_impl_dirbuffer_size( struct wfp_dirbuffer * buffer) { - json_t * entries = buffer->entries; - - buffer->entries = NULL; - return entries; + return buffer->size; +} + +struct wfp_dirbuffer_entry const * +wfp_impl_dirbuffer_entry_at( + struct wfp_dirbuffer * buffer, + size_t pos) +{ + struct wfp_dirbuffer_entry const * entry = NULL; + + if (pos < buffer->size) + { + entry = &(buffer->entries[pos]); + } + + return entry; } diff --git a/lib/webfuse_provider/impl/dirbuffer.h b/lib/webfuse_provider/impl/dirbuffer.h index eaa0278..efd76e7 100644 --- a/lib/webfuse_provider/impl/dirbuffer.h +++ b/lib/webfuse_provider/impl/dirbuffer.h @@ -4,16 +4,23 @@ #include #include #include -#include #ifdef __cplusplus extern "C" { #endif +struct wfp_dirbuffer_entry +{ + char * name; + ino_t inode; +}; + struct wfp_dirbuffer { - json_t * entries; + struct wfp_dirbuffer_entry * entries; + size_t size; + size_t capacity; }; extern struct wfp_dirbuffer * wfp_impl_dirbuffer_create(void); @@ -26,9 +33,14 @@ extern void wfp_impl_dirbuffer_add( char const * name, ino_t inode); -extern json_t * wfp_impl_dirbuffer_take( +extern size_t +wfp_impl_dirbuffer_size( struct wfp_dirbuffer * buffer); +extern struct wfp_dirbuffer_entry const * +wfp_impl_dirbuffer_entry_at( + struct wfp_dirbuffer * buffer, + size_t pos); #ifdef __cplusplus } diff --git a/lib/webfuse_provider/impl/message_writer.c b/lib/webfuse_provider/impl/message_writer.c index 8ef9314..ebc51dc 100644 --- a/lib/webfuse_provider/impl/message_writer.c +++ b/lib/webfuse_provider/impl/message_writer.c @@ -99,22 +99,19 @@ wfp_impl_message_writer_add_dirbuffer( wfp_impl_json_writer_object_key(writer->json_writer, "result"); wfp_impl_json_writer_array_begin(writer->json_writer); - json_t * entries = wfp_impl_dirbuffer_take(dirbuffer); - for (size_t i = 0; i < json_array_size(entries); i++) + size_t const count = wfp_impl_dirbuffer_size(dirbuffer); + for (size_t i = 0; i < count; i++) { - json_t * entry = json_array_get(entries, i); - char const * name = json_string_value(json_object_get(entry, "name")); - int inode = json_integer_value(json_object_get(entry, "inode")); + struct wfp_dirbuffer_entry const * entry = wfp_impl_dirbuffer_entry_at(dirbuffer, i); wfp_impl_json_writer_object_begin(writer->json_writer); wfp_impl_json_writer_object_key(writer->json_writer, "name"); - wfp_impl_json_writer_write_string(writer->json_writer, name); + wfp_impl_json_writer_write_string(writer->json_writer, entry->name); wfp_impl_json_writer_object_key(writer->json_writer, "inode"); - wfp_impl_json_writer_write_int(writer->json_writer, inode); + wfp_impl_json_writer_write_int(writer->json_writer, entry->inode); wfp_impl_json_writer_object_end(writer->json_writer); } - json_decref(entries); wfp_impl_json_writer_array_end(writer->json_writer); diff --git a/test/webfuse_provider/provider/test_dirbuffer.cc b/test/webfuse_provider/provider/test_dirbuffer.cc index 639b0ef..6912879 100644 --- a/test/webfuse_provider/provider/test_dirbuffer.cc +++ b/test/webfuse_provider/provider/test_dirbuffer.cc @@ -12,21 +12,19 @@ TEST(DirBuffer, Add) wfp_dirbuffer * buffer = wfp_impl_dirbuffer_create(); wfp_impl_dirbuffer_add(buffer, "answer", 42); - ASSERT_EQ(1, json_array_size(buffer->entries)); + ASSERT_EQ(1, wfp_impl_dirbuffer_size(buffer)); - json_t * entry = json_array_get(buffer->entries, 0); - ASSERT_STREQ("answer", json_string_value(json_object_get(entry, "name"))); - ASSERT_EQ(42, json_integer_value(json_object_get(entry, "inode"))); + auto * entry = wfp_impl_dirbuffer_entry_at(buffer, 0); + ASSERT_STREQ("answer", entry->name); + ASSERT_EQ(42, entry->inode); wfp_impl_dirbuffer_dispose(buffer); } -TEST(DirBuffer, Take) +TEST(DirBuffer, EntryAt) { wfp_dirbuffer * buffer = wfp_impl_dirbuffer_create(); - json_t * entries = wfp_impl_dirbuffer_take(buffer); + auto * entry = wfp_impl_dirbuffer_entry_at(buffer, 0); + ASSERT_EQ(nullptr, entry); wfp_impl_dirbuffer_dispose(buffer); - - ASSERT_TRUE(json_is_array(entries)); - json_decref(entries); }