rework some (minor) things

This commit is contained in:
2025-08-11 15:49:29 +02:00
parent 2731f908b3
commit bdccc05440
6 changed files with 111 additions and 28 deletions

59
src/atrb.h Normal file
View File

@@ -0,0 +1,59 @@
// Copyright (c) 2025 Quinn
// Licensed under the MIT Licence. See LICENSE for details
#pragma once
#if defined(__GNUC__)
#if __has_attribute(__pure__)
#define PURE __attribute__((__pure__))
#else
#define PURE
#endif
#if __has_attribute(__const__)
#define CONST __attribute__((__const__))
#else
#define CONST
#endif
#if __has_attribute(__noreturn__)
#define NORET __attribute__((__noreturn__))
#else
#define NORET
#endif
#if __has_attribute(__malloc__)
#define MALLOC __attribute__((__malloc__))
#else
#define MALLOC
#endif
#if __has_attribute(__used__)
#define USED __attribute__((__used__))
#else
#define USED
#endif
#if __has_attribute(__unused__)
#define UNUSED __attribute__((__unused__))
#else
#define UNUSED
#endif
#if __has_attribute(__deprecated__)
#define DEPRECATED __attribute__((__deprecated__))
#else
#define DEPRECATED
#endif
#if __has_attribute(__format__)
#define FORMAT(...) __attribute__((format(__VA_ARGS__)))
#else
#define FORMAT(...)
#endif
#if __has_attribute(__nonnull__)
#define NONNULL(...) __attribute__((nonnull(__VA_ARGS__)))
#else
#define NONNULL(...)
#endif
#endif

View File

@@ -1,18 +0,0 @@
#include "error.h"
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdnoreturn.h>
noreturn void fatal(const char *fmt, ...) {
char buf[128];
va_list args;
va_start(args, fmt);
vsnprintf(buf, 128, fmt, args);
va_end(args);
fprintf(stderr, "\033[91mE: %s\033[0m\n", buf);
exit(1);
}

View File

@@ -1,7 +1,21 @@
// Copyright (c) 2025 Quinn
// Licensed under the MIT Licence. See LICENSE for details
#pragma once
#include <stdarg.h>
#include <stdint.h>
#include <stdnoreturn.h>
// prints an error message and aborts execution
noreturn void fatal(const char *, ...);
#if __INCLUDE_LEVEL__ > 0
#include <stdio.h>
#include <stdlib.h>
#include "util/macro.h"
#endif
#define debug(s, ...) printf("\033[95m" __FILE__ ":" MACRO_STR2(__LINE__) ": [DBG]: " s "\033[0m\n", ##__VA_ARGS__)
#define info(s, ...) printf(__FILE__ ":" MACRO_STR2(__LINE__) ": [INF]: " s "\n", ##__VA_ARGS__)
#define warn(s, ...) fprintf(stderr, "\033[93m" __FILE__ ":" MACRO_STR2(__LINE__) ": [WAR]: " s "\033[0m\n", ##__VA_ARGS__)
#define error(s, ...) fprintf(stderr, "\033[91m" __FILE__ ":" MACRO_STR2(__LINE__) ": [ERR]: " s "\033[0m\n", ##__VA_ARGS__)
#define fatal(s, ...) \
do { \
printf("\033[101m" __FILE__ ":" MACRO_STR2(__LINE__) ": [FAT]: " s "\033[0m\n", ##__VA_ARGS__); \
exit(EXIT_FAILURE); \
} while (0)

View File

@@ -10,23 +10,22 @@ uint8_t getoptions(int32_t argc, char *const *argv, int32_t *ncpus) {
uint8_t opts = 0;
char opt;
const char *const msg = "-%c has already been set";
while ((opt = getopt(argc, argv, "lavi")) != -1) {
switch (opt) {
case 'l':
if (opts & OPT_LIST_CORES) fatal(msg, 'l');
if (opts & OPT_LIST_CORES) fatal("-%c has already been set", 'l');
opts |= OPT_LIST_CORES;
break;
case 'a':
if (opts & OPT_SET_ALL) fatal(msg, 'a');
if (opts & OPT_SET_ALL) fatal("-%c has already been set", 'a');
opts |= OPT_SET_ALL;
break;
case 'v':
if (opts & OPT_VERBOSE) fatal(msg, 'v');
if (opts & OPT_VERBOSE) fatal("-%c has already been set", 'v');
opts |= OPT_VERBOSE | OPT_LIST_CORES;
break;
case 'i':
if (opts & OPT_INVERT) fatal(msg, 'i');
if (opts & OPT_INVERT) fatal("-%c has already been set", 'i');
opts |= OPT_INVERT;
break;
case '?':

20
src/util/intdef.h Normal file
View File

@@ -0,0 +1,20 @@
// Copyright (c) 2025 Quinn
// Licensed under the MIT Licence. See LICENSE for details
#pragma once
/* variable-width integer types */
typedef unsigned int uint; // ≥16 bit unsigned integer
typedef unsigned long ulong; // ≥32 bit unsigned integer
typedef signed long long llong; // ≥64 bit signed integer
typedef unsigned long long ullong; // ≥64 bit unsigned integer
/* fixed-width integer types */
#include <stdint.h>
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;

9
src/util/macro.h Normal file
View File

@@ -0,0 +1,9 @@
// Copyright (c) 2025 Quinn
// Licensed under the MIT Licence. See LICENSE for details
#pragma once
#define WIDTHOF(t) (sizeof(t) * 8) // gets the bit width of a type
#define MACRO_CAT(x, y) x##y // concatenate two macro variables together
#define MACRO_CAT2(x, y) MACRO_CAT(x, y) // concatenate two macro variables together
#define MACRO_STR(v) #v // for converting macro variable into a string
#define MACRO_STR2(v) MACRO_STR(v) // for a recursive string generation