]> git.xolatile.top Git - public-libhl.git/commitdiff
Allocation by chunks of 1KiB...
authorxolatile <xolatile@proton.me>
Tue, 15 Aug 2023 23:27:09 +0000 (19:27 -0400)
committerxolatile <xolatile@proton.me>
Tue, 15 Aug 2023 23:27:09 +0000 (19:27 -0400)
src/main.c

index 1482e3d67662c6b85174a21804decc7a9651d91c..90e0f2d6aa59c1499c03c7ef203b7574ce245529 100644 (file)
@@ -4,6 +4,9 @@
 #include <unistd.h>
 #include <fcntl.h>
 
+#define ALLOCATION_CHUNK (1024UL)
+#define ALLOCATION_LIMIT (1024UL * 1024UL)
+
 enum { NORMAL,     BOLD,       DARKNESS,   ITALIC,     UNDERLINE,  BLINK,      DUNNO_6,    REVERSE,    INVISIBLE    };
 enum { GREY,       RED,        GREEN,      YELLOW,     BLUE,       PINK,       CYAN,       WHITE,      CANCEL       };
 
@@ -245,9 +248,15 @@ static int render_word (
                ++data_offset;
        } while ((is_separator (buffer [data_offset]) == 0) && (buffer [data_offset] != '\0'));
 
+       free (word);
+
        return (data_offset);
 }
-
+/*
+Valgrind: Depends on uninitialized value (buffer [0])...?
+280 : } while ((buffer [buffer_size - 1] != '\0') || (buffer_size != ALLOCATION_LIMIT));
+285 : while (buffer [offset] != '\0') {
+*/
 int main (
        int      argc,
        char * * argv
@@ -260,18 +269,21 @@ int main (
                return (-1);
        }
 
+       buffer = realloc (buffer, ALLOCATION_CHUNK);
+
        do {
+               if ((buffer_size + 1) % ALLOCATION_CHUNK == 0) {
+                       buffer = realloc (buffer, ((buffer_size + 1) / ALLOCATION_CHUNK + 1) * ALLOCATION_CHUNK);
+               }
+               buffer [buffer_size] = '\0'; /* Fixing Valgrind warnings... */
+               read (STDIN_FILENO, & buffer [buffer_size], sizeof (* buffer));
                ++buffer_size;
-               buffer = realloc (buffer, buffer_size);
-               read (STDIN_FILENO, & buffer [buffer_size - 1], sizeof (* buffer));
-       } while ((buffer [buffer_size] != '\0') || (buffer_size != 10000));
+       } while ((buffer [buffer_size - 1] != '\0') /*|| (buffer_size != ALLOCATION_LIMIT)*/);
 
-       ++buffer_size;
-       buffer = realloc (buffer, buffer_size);
-       buffer [buffer_size] = '\0';
+       buffer [buffer_size - 1] = '\0';
 
        /* Checking if long comments work... */
-       // Checking if long comments work...
+       // Checking if short comments work...
 
        while (buffer [offset] != '\0') {
                if ((buffer [offset] == '/') && (buffer [offset + 1] == '/')) {