diff --git a/macro.h b/macro.h new file mode 100644 index 0000000..fae0ee1 --- /dev/null +++ b/macro.h @@ -0,0 +1,15 @@ +#pragma once + +#define ARRSZ(arr) (sizeof(arr) / sizeof(*arr)) + +#define SWAP(a, b, type) do { \ + type tmp; \ + tmp = a; \ + a = b; \ + b = tmp; \ +} while(0) + + +#define ARR_FOREACH(arr, item) \ + for (item = arr; item != arr + ARRSZ(arr) * sizeof(*item); item++) + diff --git a/util.c b/util.c new file mode 100644 index 0000000..1514964 --- /dev/null +++ b/util.c @@ -0,0 +1,48 @@ +#include +#include +#include + +void * +xmalloc(size_t sz) +{ + void *res; + res = malloc(sz); + if (sz != 0 && res == NULL) { + perror("malloc()"); + exit(EXIT_FAILURE); + } + return res; + +} + +void* +xrealloc(void *ptr, size_t size) +{ + void *res; + + res = realloc(ptr, size); + if (size != 0 && res == NULL) { + perror("realloc()"); + exit(EXIT_FAILURE); + } + + return res; +} + +void +xfree(void *ptr) +{ + if (ptr) + free(ptr); +} + +void +xerror(int rc, char *fmt, ...) +{ + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + + exit(rc); +} + diff --git a/util.h b/util.h new file mode 100644 index 0000000..aa141f4 --- /dev/null +++ b/util.h @@ -0,0 +1,9 @@ +#pragma once + +void *xmalloc(size_t sz); + +void* xrealloc(void *ptr, size_t size); + +void xfree(void *ptr); + +void xerror(int rc, char *fmt, ...);