]> Sergey Matveev's repositories - nnn.git/commitdiff
Invert optimization: allocate in a go
authorArun Prakash Jana <engineerarun@gmail.com>
Thu, 15 Jul 2021 13:25:03 +0000 (18:55 +0530)
committerArun Prakash Jana <engineerarun@gmail.com>
Thu, 15 Jul 2021 13:29:00 +0000 (18:59 +0530)
src/nnn.c

index 50200422f6f213e6fdf918e7a7ba7dd190a67364..01f512b83b9d1e7704250e26cabec6442dd24b12 100644 (file)
--- a/src/nnn.c
+++ b/src/nnn.c
@@ -1562,7 +1562,8 @@ static inline void findmarkentry(char *path, struct entry *dentp)
 
 static void invertselbuf(char *path)
 {
-       size_t len, endpos, offset = 0;
+       size_t len, endpos, shrinklen = 0, alloclen = 0;
+       size_t const pathlen = xstrlen(path);
        char *found;
        int nmarked = 0, prev = 0;
        struct entry *dentp;
@@ -1580,6 +1581,7 @@ static void invertselbuf(char *path)
                        } else {
                                ++nselected;
                                dentp->flags |= FILE_SELECTED;
+                               alloclen += pathlen + dentp->nlen;
                        }
                } else {
                        dentp->flags |= FILE_SCANNED;
@@ -1603,10 +1605,11 @@ static void invertselbuf(char *path)
                                }
 
                                --nselected;
-                               offset += len; /* buffer size adjustment */
+                               shrinklen += len; /* buffer size adjustment */
                        } else {
                                ++nselected;
                                dentp->flags |= FILE_SELECTED;
+                               alloclen += pathlen + dentp->nlen;
                        }
                        scan = FALSE;
                }
@@ -1658,7 +1661,13 @@ static void invertselbuf(char *path)
        }
 
        /* Buffer size adjustment */
-       selbufpos -= offset;
+       selbufpos -= shrinklen;
+
+       if (alloclen > shrinklen) {
+               pselbuf = xrealloc(pselbuf, selbuflen + (alloclen - shrinklen));
+               if (!pselbuf)
+                       errexit();
+       }
 
        free(marked);