From 4b8574aa878f0f21ff5ffdcda27ad510f8a2f911 Mon Sep 17 00:00:00 2001 From: Erica Fischer Date: Fri, 2 Feb 2024 14:12:34 -0800 Subject: [PATCH] A few more cases to populate the hash in addpool --- main.cpp | 3 ++- memfile.cpp | 4 +++- memfile.hpp | 2 +- pool.cpp | 25 ++++++++++++++++++------- 4 files changed, 24 insertions(+), 10 deletions(-) diff --git a/main.cpp b/main.cpp index 8748a552..82621b59 100644 --- a/main.cpp +++ b/main.cpp @@ -1336,7 +1336,8 @@ std::pair read_input(std::vector &sources, char *fname, i // To distinguish a null value { struct stringpool p; - memfile_write(r->treefile, &p, sizeof(struct stringpool)); + bool in_memory; + memfile_write(r->treefile, &p, sizeof(struct stringpool), in_memory); } r->file_bbox[0] = r->file_bbox[1] = UINT_MAX; diff --git a/memfile.cpp b/memfile.cpp index a363b0b9..ec2e8adc 100644 --- a/memfile.cpp +++ b/memfile.cpp @@ -49,10 +49,11 @@ int memfile_close(struct memfile *file) { return 0; } -int memfile_write(struct memfile *file, void *s, long long len) { +int memfile_write(struct memfile *file, void *s, long long len, bool &in_memory) { // If it is full, append to the file. // If it is not full yet, append to the string in memory. + in_memory = false; if (file->fp != NULL) { if (fwrite(s, sizeof(char), len, file->fp) != (size_t) len) { return 0; @@ -61,6 +62,7 @@ int memfile_write(struct memfile *file, void *s, long long len) { } else { file->map.append(std::string((char *) s, len)); file->off += len; + in_memory = true; } return len; diff --git a/memfile.hpp b/memfile.hpp index e8ab8c6d..e339120d 100644 --- a/memfile.hpp +++ b/memfile.hpp @@ -15,7 +15,7 @@ struct memfile { struct memfile *memfile_open(int fd); int memfile_close(struct memfile *file); -int memfile_write(struct memfile *file, void *s, long long len); +int memfile_write(struct memfile *file, void *s, long long len, bool &in_memory); void memfile_full(struct memfile *file); #endif diff --git a/pool.cpp b/pool.cpp index bf3059c3..16fe4d49 100644 --- a/pool.cpp +++ b/pool.cpp @@ -64,14 +64,20 @@ long long addpool(struct memfile *poolfile, struct memfile *treefile, const char // the pool is full yet. long long off = poolfile->off; - if (memfile_write(poolfile, &type, 1) < 0) { + bool in_memory = false; + + if (memfile_write(poolfile, &type, 1, in_memory) < 0) { perror("memfile write"); exit(EXIT_WRITE); } - if (memfile_write(poolfile, (void *) s, strlen(s) + 1) < 0) { + if (memfile_write(poolfile, (void *) s, strlen(s) + 1, in_memory) < 0) { perror("memfile write"); exit(EXIT_WRITE); } + + if (in_memory) { + dedup[hash_off] = off; + } return off; } } @@ -94,11 +100,12 @@ long long addpool(struct memfile *poolfile, struct memfile *treefile, const char // to the newly-added strings. long long off = poolfile->off; - if (memfile_write(poolfile, &type, 1) < 0) { + bool in_memory; + if (memfile_write(poolfile, &type, 1, in_memory) < 0) { perror("memfile write"); exit(EXIT_WRITE); } - if (memfile_write(poolfile, (void *) s, strlen(s) + 1) < 0) { + if (memfile_write(poolfile, (void *) s, strlen(s) + 1, in_memory) < 0) { perror("memfile write"); exit(EXIT_WRITE); } @@ -114,14 +121,18 @@ long long addpool(struct memfile *poolfile, struct memfile *treefile, const char } long long off = poolfile->off; - if (memfile_write(poolfile, &type, 1) < 0) { + bool in_memory = false; + if (memfile_write(poolfile, &type, 1, in_memory) < 0) { perror("memfile write"); exit(EXIT_WRITE); } - if (memfile_write(poolfile, (void *) s, strlen(s) + 1) < 0) { + if (memfile_write(poolfile, (void *) s, strlen(s) + 1, in_memory) < 0) { perror("memfile write"); exit(EXIT_WRITE); } + if (in_memory) { + dedup[hash_off] = off; + } if (off >= LONG_MAX || treefile->off >= LONG_MAX) { // Tree or pool is bigger than 2GB @@ -139,7 +150,7 @@ long long addpool(struct memfile *poolfile, struct memfile *treefile, const char tsp.off = off; long long p = treefile->off; - if (memfile_write(treefile, &tsp, sizeof(struct stringpool)) < 0) { + if (memfile_write(treefile, &tsp, sizeof(struct stringpool), in_memory) < 0) { perror("memfile write"); exit(EXIT_WRITE); }