From: Ben Pfaff Date: Mon, 21 Sep 2009 19:38:58 +0000 (-0700) Subject: fatal-signal: Clean up code by using shash. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=411baaacb8672cd9b231328b77a5f1c8a11aad9c;p=openvswitch fatal-signal: Clean up code by using shash. This simplifies the code here and should speed it up, too, when there are lots of files to unlink on a fatal signal. --- diff --git a/lib/fatal-signal.c b/lib/fatal-signal.c index fc126caa..ff011363 100644 --- a/lib/fatal-signal.c +++ b/lib/fatal-signal.c @@ -23,6 +23,7 @@ #include #include #include +#include "shash.h" #include "util.h" /* Signals to catch. */ @@ -165,8 +166,7 @@ call_hooks(int sig_nr) } } -static char **files; -static size_t n_files, max_files; +static struct shash files = SHASH_INITIALIZER(&files); static void unlink_files(void *aux); static void do_unlink_files(void); @@ -183,10 +183,9 @@ fatal_signal_add_file_to_unlink(const char *file) } fatal_signal_block(); - if (n_files >= max_files) { - files = x2nrealloc(files, &max_files, sizeof *files); + if (!shash_find(&files, file)) { + shash_add(&files, file, NULL); } - files[n_files++] = xstrdup(file); fatal_signal_unblock(); } @@ -195,15 +194,12 @@ fatal_signal_add_file_to_unlink(const char *file) void fatal_signal_remove_file_to_unlink(const char *file) { - size_t i; + struct shash_node *node; fatal_signal_block(); - for (i = 0; i < n_files; i++) { - if (!strcmp(files[i], file)) { - free(files[i]); - files[i] = files[--n_files]; - break; - } + node = shash_find(&files, file); + if (node) { + shash_delete(&files, node); } fatal_signal_unblock(); } @@ -217,10 +213,10 @@ unlink_files(void *aux UNUSED) static void do_unlink_files(void) { - size_t i; + struct shash_node *node; - for (i = 0; i < n_files; i++) { - unlink(files[i]); + SHASH_FOR_EACH (node, &files) { + unlink(node->name); } }