Skip to content

Commit

Permalink
fix stupid console parsing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ougi committed Mar 23, 2024
1 parent 1410825 commit e438dda
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 22 deletions.
25 changes: 12 additions & 13 deletions lib/arena.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,23 @@ static inline uintptr_t alignForward(uintptr_t ptr, size_t alignment) {
}

static inline Arena *ArenaAlloc(size_t bytes) {
if (bytes == 0) {
fprintf(stderr, "FATAL: requested new arena of 0 bytes!");
exit(-1);
}

#ifdef _WIN32
Arena *arena = VirtualAlloc(
NULL, sizeof(*arena), MEM_COMMIT, PAGE_READWRITE
);
arena->buf = VirtualAlloc(
NULL, bytes, MEM_COMMIT, PAGE_READWRITE
NULL, sizeof(*arena) + bytes, MEM_COMMIT, PAGE_READWRITE
);
#else
Arena *arena = mmap(
NULL, sizeof(*arena), PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0
);
arena->buf = mmap(
NULL, bytes, PROT_READ | PROT_WRITE,
NULL, sizeof(*arena) + bytes, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0
);
#endif

arena->buf = (char*)(arena + sizeof(*arena));

if (!arena || !arena->buf) {
fprintf(stderr, "FATAL: unable to allocate %zuB\n", bytes);
exit(errno);
Expand All @@ -88,10 +87,10 @@ static inline void *ArenaPush(Arena *arena, size_t bytes) {

if (arena->pos + bytes > arena->size) {
/* need more memory! (make growable arena linked list) */
if (bytes > arena->size) {
if (bytes > arena->size * ARENA_GROWTH_FACTOR) {
fprintf(
stderr, " FATAL: requested block is too big for one arena! "
"(Bl: %zuB, Ar: %zuB)\n", bytes, arena->size
stderr, "FATAL: requested block won't fit in a single arena!"
" (Block: %zuB, Arena: %zuB)\n", bytes, arena->size
);
exit(-1);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ typedef struct Arguments {
char **inFormats;
const char *outFormat;
size_t numberOfThreads;
InputMode mode;

union OutPath {
char *customFolder;
char *customPath;
} outPath;

uint16_t options; // fields for the optional arguments

} Arguments;

typedef struct Thread {
Expand Down Expand Up @@ -145,7 +145,7 @@ typedef struct Thread {

#define printErr(msg, dsc) \
fprintf(stderr, \
"%s ERROR: %s%s: %s%s%s\n\n", \
"%s ERROR: %s%s: %s'%s'%s\n\n", \
COLOR_ERROR, \
COLOR_DEFAULT, msg, \
COLOR_INPUT, dsc, \
Expand Down
5 changes: 3 additions & 2 deletions src/convert.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ int convertFiles(const char **files, Arguments *args, ProcessInfo *stats) {
#endif

char *outPath = NULL;
size_t numberOfThreads = args->numberOfThreads ?
args->numberOfThreads : getNumberOfOnlineThreads();
size_t defaultThreads = getNumberOfOnlineThreads() / 2;
size_t numberOfThreads = args->numberOfThreads > 0 ?
args->numberOfThreads : (defaultThreads > 0 ? defaultThreads : 1);
Thread *threads = GlobalArenaPush(numberOfThreads * sizeof(*threads));
size_t fileIdx = 0;

Expand Down
6 changes: 2 additions & 4 deletions src/data.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,10 @@ extern inline size_t getNumberOfOnlineThreads(void) {
#ifdef _WIN32
SYSTEM_INFO sysInfo = {0};
GetSystemInfo(&sysInfo);
size_t n = (size_t)sysInfo.dwNumberOfProcessors;
return (size_t)sysInfo.dwNumberOfProcessors;
#else
size_t n = (size_t)sysconf(_SC_NPROCESSORS_ONLN);
return (size_t)sysconf(_SC_NPROCESSORS_ONLN);
#endif

return n > 1 ? n : 2;
}

extern char *getAbsolutePath(const char *dir) {
Expand Down
4 changes: 3 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ int main(int argc, char *argv[]) {
int state = PARSE_STATE_OK;

if (inputMode == ARGUMENTS) {
parsedArgs->mode = ARGUMENTS;
state = parseArgs(argc - 1, argv + 1, parsedArgs);

if (parsedArgs->options & OPT_DISPLAYHELP) {
Expand All @@ -83,6 +84,7 @@ int main(int argc, char *argv[]) {
goto exit;
}
} else {
parsedArgs->mode = CONSOLE;
state = parseConsoleInput(parsedArgs);
}

Expand Down Expand Up @@ -125,7 +127,7 @@ int main(int argc, char *argv[]) {
if (exitCode != EXIT_FAILURE) _displayEndDialog(&stats);
} else {
printf(" found no matching files (%saborting%s)\n\n",
COLOR_ACCENT, COLOR_DEFAULT);
COLOR_ERROR, COLOR_DEFAULT);
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ int parseArgs(const int listSize, char *args[], Arguments *parsedArgs) {
continue;
}

if (parsedArgs->mode != ARGUMENTS) continue;

if (!isDirectory(args[i])) {
printErr("invalid/incomplete option", args[i]);
printf(" (run with %s--help%s for info)\n\n",
Expand Down

0 comments on commit e438dda

Please sign in to comment.