Skip to content

Instantly share code, notes, and snippets.

@jstimpfle
Last active July 11, 2018 01:32
Show Gist options
  • Save jstimpfle/4578352e4dc03d20613d5bbfb7857f4b to your computer and use it in GitHub Desktop.
Save jstimpfle/4578352e4dc03d20613d5bbfb7857f4b to your computer and use it in GitHub Desktop.
contest-blurb.c
#define _POSIX_C_SOURCE 200809L
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define UNUSED __attribute__((unused))
#define NORETURN __attribute__((noreturn))
#define DIE(fmt, ...) _die("FATAL ERROR at line %d: " fmt "\n", __LINE__, ##__VA_ARGS__)
#define REALLOC(ptr, nels) _reallocfail((void**)&(ptr), nels, sizeof *(ptr), __FILE__, __LINE__)
#define CLEAR(ptr, nels) memset((ptr), 0, (nels) * sizeof *(ptr))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define MAX(a,b) ((a) > (b) ? (a) : (b))
static UNUSED NORETURN void _die(const char *fmt, ...) { va_list ap; va_start(ap, fmt); vfprintf(stdout, fmt, ap); va_end(ap); abort(); }
static UNUSED void _reallocfail(void **ptr, size_t nels, size_t elsize, const char *filename, int lineno) { size_t nbytes = nels * elsize; void *p = realloc(*ptr, nbytes); if (!p) { DIE("Allocation request for %zu=%zu*%zu bytes from %s line %d failed.", nbytes, nels, elsize, filename, lineno); } *ptr = p; }
static UNUSED int compareint(const void *a, const void *b) { int x = *(const int *)a; int y = *(const int *)b; return (x>y)-(x<y); }
static UNUSED int comparelonglong(const void *a, const void *b) { long long x = *(const long long *)a; long long y = *(const long long *)b; return (x>y)-(x<y); }
static UNUSED int readint(void) { int n, c; n = 0; while ((c = getchar_unlocked()) >= 0 && c < '0') continue; if (c == EOF) { DIE("Failed to read integer"); } for (;;) { if (c < '0') break; n = 10*n + c-'0'; c = getchar_unlocked(); } return n; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment