about summary refs log tree commit diff
diff options
context:
space:
mode:
authorsternenseemann <sternenseemann@systemli.org>2020-08-08 13:03:01 +0200
committersternenseemann <sternenseemann@systemli.org>2020-08-08 13:03:01 +0200
commit744f0507438bf4224ffc8ca75907a6af33c29541 (patch)
tree2aaf54e850c5515c0995b073ed09038c4ffb356f
parent36285a3d51322b33d83462233d4fcac08f89bfbb (diff)
feat(main): mmap only on demand and unmap asap
This should limit peak memory consumption to sizeof(struct entry) *
amount_of_entries + sizeof largest_file. Previously it was size of
the index plus size of all entries' files as they were all read into
memory first thing and only freed afterwards.
-rw-r--r--main.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/main.c b/main.c
index 41a0a75..6f29d10 100644
--- a/main.c
+++ b/main.c
@@ -77,6 +77,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <dirent.h>
+#include <sys/mman.h>
 #include <sys/types.h>
 #include <string.h>
 #include <errno.h>
@@ -171,7 +172,7 @@ int main(void) {
 void blog_index(char script_name[]) {
     struct entry *entries = NULL;
 
-    int count = make_index(BLOG_DIR, script_name, 1, &entries);
+    int count = make_index(BLOG_DIR, script_name, 0, &entries);
 
     send_header("Content-type", "text/html");
 
@@ -189,7 +190,15 @@ void blog_index(char script_name[]) {
         template_header();
 
         for(size_t i = 0; i < count; i++) {
-            template_index_entry(entries[i]);
+            if(entry_get_text(&entries[i]) != -1) {
+                template_index_entry(entries[i]);
+
+                // unmap file
+                if(munmap(entries[i].text, entries[i].text_size) != -1) {
+                    entries[i].text_size = -1;
+                    entries[i].text = NULL;
+                }
+            }
         }
 
         template_footer();