From b7bedc8b60ccfa4ca33bf5b1f7d870c24e8725ef Mon Sep 17 00:00:00 2001 From: Andrea Mazzoleni Date: Tue, 26 Feb 2013 21:26:45 +0100 Subject: [PATCH] Reduces memory occupation with many files. --- elem.c | 9 ++++++++- elem.h | 7 ++++++- scan.c | 2 +- util.c | 22 ++++++++++++++++++++++ util.h | 6 ++++++ 5 files changed, 43 insertions(+), 3 deletions(-) diff --git a/elem.c b/elem.c index 291ea4ce..e3c00954 100644 --- a/elem.c +++ b/elem.c @@ -331,7 +331,7 @@ struct snapraid_file* file_alloc(unsigned block_size, const char* sub, uint64_t block_off_t i; file = malloc_nofail(sizeof(struct snapraid_file)); - pathcpy(file->sub, sizeof(file->sub), sub); + file->sub = strdup_nofail(sub); file->size = size; file->blockmax = (size + block_size - 1) / block_size; file->mtime_sec = mtime_sec; @@ -352,10 +352,17 @@ struct snapraid_file* file_alloc(unsigned block_size, const char* sub, uint64_t void file_free(struct snapraid_file* file) { + free(file->sub); free(file->blockvec); free(file); } +void file_rename(struct snapraid_file* file, const char* sub) +{ + free(file->sub); + file->sub = strdup_nofail(sub); +} + const char* file_name(struct snapraid_file* file) { const char* r = strrchr(file->sub, '/'); diff --git a/elem.h b/elem.h index d2f12e0c..6e73cae5 100644 --- a/elem.h +++ b/elem.h @@ -159,7 +159,7 @@ struct snapraid_file { int mtime_nsec; /**< Modification time nanoseconds. In the range 0 <= x < 1,000,000,000, or FILE_MTIME_NSEC_INVALID. */ block_off_t blockmax; /**< Number of blocks. */ unsigned flag; /**< FILE_IS_* flags. */ - char sub[PATH_MAX]; /**< Sub path of the file. Without the disk dir. The disk is implicit. */ + char* sub; /**< Sub path of the file. Without the disk dir. The disk is implicit. */ /* nodes for data structures */ tommy_node nodelist; @@ -464,6 +464,11 @@ struct snapraid_file* file_alloc(unsigned block_size, const char* sub, uint64_t */ void file_free(struct snapraid_file* file); +/** + * Rename a file. + */ +void file_rename(struct snapraid_file* file, const char* sub); + /** * Returns the name of the file, without the dir. */ diff --git a/scan.c b/scan.c index ceb4289c..816491c4 100644 --- a/scan.c +++ b/scan.c @@ -339,7 +339,7 @@ static void scan_file(struct snapraid_scan* scan, struct snapraid_state* state, tommy_hashdyn_remove_existing(&disk->pathset, &file->pathset); /* save the new name */ - pathcpy(file->sub, sizeof(file->sub), sub); + file_rename(file, sub); /* reinsert in the set */ tommy_hashdyn_insert(&disk->pathset, &file->pathset, file, file_path_hash(file->sub)); diff --git a/util.c b/util.c index 38a085cc..8c608bf8 100644 --- a/util.c +++ b/util.c @@ -755,6 +755,28 @@ void* malloc_nofail_align(size_t size, void** freeptr) return ptr; } +char* strdup_nofail(const char* str) +{ + size_t size; + char* ptr; + + size = strlen(str) + 1; + + ptr = malloc(size); + + if (!ptr) { + /* don't use printf to avoid any possible extra allocation */ + write(2, "Low Memory\n", 11); + exit(EXIT_FAILURE); + } + + memcpy(ptr, str, size); + + mcounter += size; + + return ptr; +} + #include "murmurhash3.c" void memhash(unsigned kind, void* digest, const void* src, unsigned size) diff --git a/util.h b/util.h index 987c967f..f2a26cb4 100644 --- a/util.h +++ b/util.h @@ -369,6 +369,12 @@ void* malloc_nofail(size_t size); */ void* malloc_nofail_align(size_t size, void** freeptr); +/** + * Safe strdup. + * If no memory is available, it aborts. + */ +char* strdup_nofail(const char* str); + /****************************************************************************/ /* hash */