{
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 (;;) {
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();
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;
#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;
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) {
};
// 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.
//
// 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)