]> Cypherpunks repositories - keks.git/commitdiff
Explicitly set items initial capacity
authorSergey Matveev <stargrave@stargrave.org>
Mon, 16 Dec 2024 15:21:42 +0000 (18:21 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Mon, 16 Dec 2024 15:21:42 +0000 (18:21 +0300)
c/cmd/print-items/print-items.c
c/lib/items.c
c/lib/items.h

index 9d6eaa64e7e1c99ebf60aa2981fa6e4b28865daa3bf3e5e5d713902e16a77063..d3ad628e58aeef0432e842cbd18bad292082ff90041a58590a6a98d4f059f681 100644 (file)
@@ -271,12 +271,14 @@ main(int argc, char **argv)
 {
     bool doEncode = false;
     bool noTotals = false;
+    ptrdiff_t itemsInitialLen = 2048;
     NoColour = getenv("NO_COLOR") != NULL;
     struct option longopts[] = {
         {"max-str-len", required_argument, NULL, 'a'},
         {"do-encode", no_argument, NULL, 'b'},
         {"no-offsets", no_argument, NULL, 'c'},
         {"no-totals", no_argument, NULL, 'd'},
+        {"items-initial-len", required_argument, NULL, 'f'},
         {NULL, 0, NULL, 0}};
     int ch = 0;
     for (;;) {
@@ -308,6 +310,20 @@ main(int argc, char **argv)
         case 'd': // no-totals
             noTotals = true;
             break;
+        case 'f': { // items-initial-len
+            errno = 0;
+            long tmp = strtol(optarg, NULL, 10);
+            if (errno != 0) {
+                fprintf(stderr, "--items-initial-len: %s\n", strerror(errno));
+                exit(EXIT_FAILURE);
+            }
+            if (tmp < 0) {
+                fputs("--items-initial-len: is negative\n", stderr);
+                exit(EXIT_FAILURE);
+            }
+            itemsInitialLen = (ptrdiff_t)tmp;
+            break;
+        }
         case '?':
         default:
             usage();
@@ -328,7 +344,7 @@ main(int argc, char **argv)
     snprintf(OffFmt, sizeof OffFmt, "%%s%%0%dzd%%s ", OffDigits);
 
     struct KEKSItems items;
-    enum KEKSErr err = KEKSItemsInit(&items);
+    enum KEKSErr err = KEKSItemsInit(&items, itemsInitialLen);
     if (err != KEKSErrNo) {
         fprintf(stderr, "err: %s\n", KEKSErr2Str(err));
         return EXIT_FAILURE;
index 015a5682c56765b64cfd1787f8b99fa4103f7e6ffddee2206cfa85d4794bf378..c32574a82e2345d34880fc504ac0f6587c5d21808027d6beb6a36f14f38cdcb5 100644 (file)
 #include "err.h"
 #include "items.h"
 
-static const ptrdiff_t keksItemsPoolGrowLen = 64;
 static const size_t parseMaxRecursionDepth = 1024;
 
 enum KEKSErr
-KEKSItemsInit(struct KEKSItems *items)
+KEKSItemsInit(struct KEKSItems *items, const ptrdiff_t initialLen)
 {
     items->len = 0;
-    items->cap = keksItemsPoolGrowLen;
+    items->cap = initialLen;
     items->list = calloc((size_t)(items->cap), sizeof(struct KEKSItem));
     if (items->list == NULL) {
         return KEKSErrNoMem;
@@ -50,10 +49,10 @@ KEKSItemsGrow(struct KEKSItems *items)
     if (items->cap == -1) {
         return KEKSErrNoMem;
     }
-    if ((SIZE_MAX - keksItemsPoolGrowLen) < (size_t)(items->cap)) {
+    if ((size_t)(items->cap) >= (SIZE_MAX / 2)) {
         return KEKSErrNoMem;
     }
-    items->cap += keksItemsPoolGrowLen;
+    items->cap *= 2;
     {
         const ptrdiff_t possibleN = SIZE_MAX / sizeof(struct KEKSItem);
         if (items->cap > possibleN) {
index 8c0962431d5839c317fd79dde2a44820aeec3670f0621d65d9cc50bf49d4f1bf..c085484ae28e8e3194e109031d9c354c2e4811123f956dba899d7ed688e77aa0 100644 (file)
@@ -56,7 +56,8 @@ struct KEKSItems {
 };
 
 // TEXINFO: KEKSItemsInit
-// @deftypefun {enum KEKSErr} KEKSItemsInit (struct KEKSItems *items)
+// @deftypefun {enum KEKSErr} KEKSItemsInit @
+//     (struct KEKSItems *items, const ptrdiff_t initialLen)
 // Initialise the @ref{KEKSItems} structure by allocating an initial
 // capacity for the underlying storage.
 //
@@ -69,7 +70,7 @@ struct KEKSItems {
 // to NULL (do not forget to free it, if it was initialised before).
 // @end deftypefun
 enum KEKSErr
-KEKSItemsInit(struct KEKSItems *);
+KEKSItemsInit(struct KEKSItems *, const ptrdiff_t initialLen);
 
 // TEXINFO: KEKSItemsGrow
 // @deftypefun {enum KEKSErr} KEKSItemsGrow (struct KEKSItems *items)