rename all sc_array stuff to vector stuff

This commit is contained in:
2025-05-03 22:47:59 +03:00
parent d1bff36830
commit 487da24e65
11 changed files with 151 additions and 131 deletions

View File

@@ -4,10 +4,10 @@
#include "filewatch_context.h"
#include "filewatch_type.h"
#include "vector.h"
#include "common.h"
#include "logger.h"
#include "types.h"
#include "vector.h"
#include <errno.h>
#include <stdatomic.h>
@@ -19,8 +19,7 @@
#include <time.h>
#include <unistd.h>
sc_array_def(FileEvent, fwevent);
typedef struct sc_array_fwevent VectorFileEvent;
vector_def(FileEvent, FileEvent);
/**
* All context and state required to run a file watcher.
@@ -34,7 +33,6 @@ typedef struct FileWatcher {
VectorFileEvent events;
} FileWatcher;
/*
* Watch Params
*/
@@ -50,8 +48,8 @@ static WatchParams *watch_params_find_by_path(const char *path,
FileWatcherContext *ctx,
size_t *index) {
const VectorWatchParams *params = &ctx->params;
for (size_t i = 0; i < sc_array_size(params); ++i) {
WatchParams *param = &sc_array_at(params, i);
for (size_t i = 0; i < vector_size(params); ++i) {
WatchParams *param = &vector_at(params, i);
if (strcmp(path, param->path) == 0) {
if (index)
*index = i;
@@ -68,7 +66,7 @@ static WatchParams *watch_params_find_by_path(const char *path,
#define NULL_EVENT (FileEvent){0};
static FileEvent file_event_create(const struct inotify_event *inevent,
FileWatcherContext *ctx) {
FileWatcherContext *ctx) {
FileEvent event = {0};
if (!inevent) {
@@ -101,8 +99,8 @@ static FileEvent file_event_create(const struct inotify_event *inevent,
filename[fullpath_size] = '\0';
// find if file is associated with any path
for (size_t i = 0; i < sc_array_size(&fw->files); ++i) {
const char *watched_file = sc_array_at(&fw->files, i);
for (size_t i = 0; i < vector_size(&fw->files); ++i) {
const char *watched_file = vector_at(&fw->files, i);
if (strcmp(watched_file, filename) == 0) {
// check if event mask matches the watcher mask
@@ -147,9 +145,9 @@ FileEvent file_event_pop(FileWatcher *fw) {
mtx_lock(&fw->mutex);
FileEvent e = NULL_EVENT;
if (sc_array_size(&fw->events) > 0) {
e = sc_array_last(&fw->events);
sc_array_del_last(&fw->events);
if (vector_size(&fw->events) > 0) {
e = vector_last(&fw->events);
vector_del_last(&fw->events);
}
mtx_unlock(&fw->mutex);
@@ -167,8 +165,8 @@ static HiResult file_watcher_ctx_init(FileWatcherContext *ctx) {
return HI_FAIL;
}
sc_array_init(&ctx->watches);
sc_array_init(&ctx->params);
vector_init(&ctx->watches);
vector_init(&ctx->params);
return HI_OK;
}
@@ -196,7 +194,7 @@ FileWatcher *file_watcher_create() {
free(fw);
return NULL;
}
sc_array_init(&fw->events);
vector_init(&fw->events);
mtx_init(&fw->mutex, mtx_plain);
cnd_init(&fw->cond);
fw->running = true;
@@ -226,7 +224,7 @@ HiResult file_watcher_add(FileWatcher *fw, const char *filename, u32 flags) {
watch_params_find_by_path(filename, &fw->context, NULL);
if (!params) {
WatchParams params = watch_params_create(filename, flags);
sc_array_add(&fw->context.params, params);
vector_add(&fw->context.params, params);
} else {
params->mask |= flags;
}
@@ -252,7 +250,7 @@ HiResult hi_file_watcher_remove(FileWatcher *fw, const char *filename) {
size_t i = 0;
WatchParams *params = watch_params_find_by_path(filename, &fw->context, &i);
watch_params_destroy(params);
sc_array_del(&fw->context.params, i);
vector_del(&fw->context.params, i);
}
mtx_unlock(&fw->mutex);
@@ -306,11 +304,11 @@ int file_watcher_watch(void *arg) {
event = (const struct inotify_event *)ptr;
FileEvent e = file_event_create(event, &ctx->context);
if (e.type != HI_FILE_NONE) {
sc_array_add(&ctx->events, e);
vector_add(&ctx->events, e);
if (event->len) {
sc_log_debug("Event created: queue-size: %u, %s %s\n",
sc_array_size(&ctx->events), e.pathname,
vector_size(&ctx->events), e.pathname,
hi_file_watch_type_to_str(e.type));
}
}