Reduces memory occupation with many files.

This commit is contained in:
Andrea Mazzoleni 2013-02-26 21:26:45 +01:00
parent a41443b6b6
commit b7bedc8b60
5 changed files with 43 additions and 3 deletions

9
elem.c
View file

@ -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, '/');

7
elem.h
View file

@ -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.
*/

2
scan.c
View file

@ -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));

22
util.c
View file

@ -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)

6
util.h
View file

@ -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 */