From 67edcc906f5570fd72d1e220cd3d5a03506d21e3 Mon Sep 17 00:00:00 2001 From: Fufu Fang Date: Sat, 4 Sep 2021 12:40:37 +0100 Subject: [PATCH] Clean up for the master branch --- Makefile | 2 +- src/link.c | 2 +- src/log.h | 2 +- src/memcache.c | 28 ++++++++++++++++++ src/memcache.h | 35 +++++++++++++++++++++++ src/network.c | 2 +- src/ramcache.c | 77 -------------------------------------------------- src/ramcache.h | 57 ------------------------------------- src/sonic.c | 2 +- 9 files changed, 68 insertions(+), 139 deletions(-) create mode 100644 src/memcache.c create mode 100644 src/memcache.h delete mode 100644 src/ramcache.c delete mode 100644 src/ramcache.h diff --git a/Makefile b/Makefile index 1906edf..b071d6a 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ CFLAGS += -g -O2 -Wall -Wextra -Wshadow \ LDFLAGS += `pkg-config --libs-only-L gumbo libcurl fuse uuid expat` LIBS = -pthread -lgumbo -lcurl -lfuse -lcrypto -lexpat COBJS = main.o network.o fuse_local.o link.o cache.o util.o sonic.o log.o\ - config.o ramcache.o + config.o memcache.o OS := $(shell uname) ifeq ($(OS),Darwin) diff --git a/src/link.c b/src/link.c index eb0e105..8cfe410 100644 --- a/src/link.c +++ b/src/link.c @@ -1,7 +1,7 @@ #include "link.h" #include "log.h" -#include "ramcache.h" +#include "memcache.h" #include "util.h" #include diff --git a/src/log.h b/src/log.h index bd8a5d8..1bdfbfa 100644 --- a/src/log.h +++ b/src/log.h @@ -13,7 +13,7 @@ typedef enum { link_lock_debug = 1 << 5, network_lock_debug = 1 << 6, cache_lock_debug = 1 << 7, - ramcache_debug = 1 << 8, + memcache_debug = 1 << 8, libcurl_debug = 1 << 9, } LogType; diff --git a/src/memcache.c b/src/memcache.c new file mode 100644 index 0000000..386a2c4 --- /dev/null +++ b/src/memcache.c @@ -0,0 +1,28 @@ +#include "memcache.h" + +#include "log.h" +#include "util.h" + +#include +#include + +size_t write_memory_callback(void *recv_data, size_t size, size_t nmemb, + void *userp) +{ + size_t recv_size = size * nmemb; + TransferStruct *ts = (TransferStruct *) userp; + + ts->data = realloc(ts->data, ts->curr_size + recv_size + 1); + if (!ts->data) { + /* + * out of memory! + */ + lprintf(fatal, "realloc failure!\n"); + } + + memmove(&ts->data[ts->curr_size], recv_data, recv_size); + ts->curr_size += recv_size; + ts->data[ts->curr_size] = '\0'; + + return recv_size; +} \ No newline at end of file diff --git a/src/memcache.h b/src/memcache.h new file mode 100644 index 0000000..77511fe --- /dev/null +++ b/src/memcache.h @@ -0,0 +1,35 @@ +#ifndef memcache_H +#define memcache_H +#include "link.h" + +/** + * \brief specify the type of data transfer + */ +typedef enum { + FILESTAT = 's', + DATA = 'd' +} TransferType; + +/** + * \brief For storing transfer data and metadata + */ +struct TransferStruct { + /** \brief The array to store the data */ + char *data; + /** \brief The current size of the array */ + size_t curr_size; + /** \brief The type of transfer being done */ + TransferType type; + /** \brief Whether transfer is in progress */ + volatile int transferring; + /** \brief The link associated with the transfer */ + Link *link; +}; + +/** + * \brief Callback function for file transfer + */ +size_t write_memory_callback(void *contents, size_t size, size_t nmemb, + void *userp); + +#endif \ No newline at end of file diff --git a/src/network.c b/src/network.c index 395591d..b45cf37 100644 --- a/src/network.c +++ b/src/network.c @@ -1,7 +1,7 @@ #include "network.h" #include "log.h" -#include "ramcache.h" +#include "memcache.h" #include "util.h" #include diff --git a/src/ramcache.c b/src/ramcache.c deleted file mode 100644 index e087621..0000000 --- a/src/ramcache.c +++ /dev/null @@ -1,77 +0,0 @@ -#include "ramcache.h" - -#include "log.h" -#include "util.h" - -#include -#include - -size_t write_memory_callback(void *recv_data, size_t size, size_t nmemb, - void *userp) -{ - size_t recv_size = size * nmemb; - TransferStruct *ts = (TransferStruct *) userp; - // lprintf(ramcache_debug, "bg_transfer: %d\n", ts->bg_transfer); - // if (ts->bg_transfer) { - // lprintf(ramcache_debug, "ramcache: thread %x: locking;\n", - // pthread_self()); - // } - // PTHREAD_MUTEX_LOCK(&ts->lock); - - ts->data = realloc(ts->data, ts->curr_size + recv_size + 1); - if (!ts->data) { - /* - * out of memory! - */ - lprintf(fatal, "realloc failure!\n"); - } - - memmove(&ts->data[ts->curr_size], recv_data, recv_size); - ts->curr_size += recv_size; - ts->data[ts->curr_size] = '\0'; - // if (ts->bg_transfer) { - // lprintf(ramcache_debug, "ramcache: thread %x: unlocking;\n", - // pthread_self()); - // } - // PTHREAD_MUTEX_UNLOCK(&ts->lock); - return recv_size; -} - -TransferStruct *TransferStruct_bg_transfer_new() -{ - TransferStruct *ts = CALLOC(1, sizeof(TransferStruct)); - if (pthread_mutexattr_init(&ts->lock_attr)) { - lprintf(fatal, "lock_attr initialisation failed!\n"); - } - - if (pthread_mutexattr_setpshared(&ts->lock_attr, - PTHREAD_PROCESS_SHARED)) { - lprintf(fatal, "could not set lock_attr!\n"); - } - - if (pthread_mutex_init(&ts->lock, &ts->lock_attr)) { - lprintf(fatal, "lock initialisation failed!\n"); - } - - ts->bg_transfer = 1; - - return ts; -} - -void TransferStruct_bg_transfer_free(TransferStruct *ts) -{ - if (ts->bg_transfer) { - if (pthread_mutex_destroy(&ts->lock)) { - lprintf(fatal, "could not destroy lock!\n"); - } - - if (pthread_mutexattr_destroy(&ts->lock_attr)) { - lprintf(fatal, "could not destroy lock_attr!\n"); - } - } - - /* free(NULL) does nothing */ - free(ts->data); - FREE(ts); -} - diff --git a/src/ramcache.h b/src/ramcache.h deleted file mode 100644 index e688fb3..0000000 --- a/src/ramcache.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef RAMCACHE_H -#define RAMCACHE_H -#include "link.h" - -/** - * \brief specify the type of data transfer - */ -typedef enum { - FILESTAT = 's', - DATA = 'd' -} TransferType; - -/** - * \brief For storing transfer data and metadata - */ -struct TransferStruct { - /** \brief The array to store the data */ - char *data; - /** \brief The current size of the array */ - size_t curr_size; - /** \brief The type of transfer being done */ - TransferType type; - /** \brief Whether transfer is in progress */ - volatile int transferring; - /** \brief The link associated with the transfer */ - Link *link; - /** \brief The minium requested size */ - size_t min_req_size; - /** \brief mutex for background transfer */ - pthread_mutex_t lock; - /** \brief attribute associated with the mutex */ - pthread_mutexattr_t lock_attr; - /** \brief Whether this TransferStruct was used for background transfer */ - int bg_transfer; - /** \brief The cache file used for background transfer */ - Cache *cf; - /** \brief The ID of the segment being downloaded */ - off_t seg_id; -}; - -/** - * \brief Callback function for file transfer - */ -size_t write_memory_callback(void *contents, size_t size, size_t nmemb, - void *userp); - -/** - * \brief Create a TransferStruct for background transfer - */ -TransferStruct *TransferStruct_bg_transfer_new(); - -/** - * \brief Free a TransferStruct used for background transfer - */ -void TransferStruct_free(TransferStruct *ts); - -#endif \ No newline at end of file diff --git a/src/sonic.c b/src/sonic.c index 8366d96..8e9ae51 100644 --- a/src/sonic.c +++ b/src/sonic.c @@ -3,7 +3,7 @@ #include "config.h" #include "log.h" #include "link.h" -#include "ramcache.h" +#include "memcache.h" #include "util.h" #include