summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2017-07-09 10:18:26 -0700
committerOri Bernstein <ori@eigenstate.org>2017-07-09 10:18:26 -0700
commit95f4d0b36eb2d25889eed6ab48fb025d5b346b67 (patch)
treed5e206f2a9c6713c193911370958826928c91294 /util
parent25d91f98a97df36e1da381b273219ceedf256a4d (diff)
downloadmc-95f4d0b36eb2d25889eed6ab48fb025d5b346b67.tar.gz
Style(9)-ify.
Diffstat (limited to 'util')
-rw-r--r--util/alloc.c33
-rw-r--r--util/bitset.c53
-rw-r--r--util/htab.c71
-rw-r--r--util/pack.c61
-rw-r--r--util/util.c48
5 files changed, 175 insertions, 91 deletions
diff --git a/util/alloc.c b/util/alloc.c
index 662c452..18d53c9 100644
--- a/util/alloc.c
+++ b/util/alloc.c
@@ -15,7 +15,8 @@
/* Some systems don't have strndup. */
/* malloc wrappers */
-void *zalloc(size_t sz)
+void *
+zalloc(size_t sz)
{
void *mem;
@@ -25,7 +26,8 @@ void *zalloc(size_t sz)
return mem;
}
-void *xalloc(size_t sz)
+void *
+xalloc(size_t sz)
{
void *mem;
@@ -35,7 +37,8 @@ void *xalloc(size_t sz)
return mem;
}
-void *zrealloc(void *mem, size_t oldsz, size_t sz)
+void *
+zrealloc(void *mem, size_t oldsz, size_t sz)
{
char *p;
@@ -45,7 +48,8 @@ void *zrealloc(void *mem, size_t oldsz, size_t sz)
return p;
}
-void *xrealloc(void *mem, size_t sz)
+void *
+xrealloc(void *mem, size_t sz)
{
mem = realloc(mem, sz);
if (!mem && sz)
@@ -54,7 +58,8 @@ void *xrealloc(void *mem, size_t sz)
}
/* lists */
-void lappend(void *l, size_t *len, void *n)
+void
+lappend(void *l, size_t *len, void *n)
{
void ***pl;
@@ -64,7 +69,8 @@ void lappend(void *l, size_t *len, void *n)
(*len)++;
}
-void *lpop(void *l, size_t *len)
+void *
+lpop(void *l, size_t *len)
{
void ***pl;
void *v;
@@ -76,7 +82,8 @@ void *lpop(void *l, size_t *len)
return v;
}
-void linsert(void *p, size_t *len, size_t idx, void *v)
+void
+linsert(void *p, size_t *len, size_t idx, void *v)
{
void ***pl, **l;
@@ -89,7 +96,8 @@ void linsert(void *p, size_t *len, size_t idx, void *v)
(*len)++;
}
-void ldel(void *p, size_t *len, size_t idx)
+void
+ldel(void *p, size_t *len, size_t idx)
{
void ***pl, **l;
@@ -102,7 +110,8 @@ void ldel(void *p, size_t *len, size_t idx)
*pl = xrealloc(l, *len * sizeof(void *));
}
-void lcat(void *dst, size_t *ndst, void *src, size_t nsrc)
+void
+lcat(void *dst, size_t *ndst, void *src, size_t nsrc)
{
size_t i;
void ***d, **s;
@@ -113,7 +122,8 @@ void lcat(void *dst, size_t *ndst, void *src, size_t nsrc)
lappend(d, ndst, s[i]);
}
-void lfree(void *l, size_t *len)
+void
+lfree(void *l, size_t *len)
{
void ***pl;
@@ -124,7 +134,8 @@ void lfree(void *l, size_t *len)
*len = 0;
}
-void *memdup(void *mem, size_t len)
+void *
+memdup(void *mem, size_t len)
{
void *ret;
diff --git a/util/bitset.c b/util/bitset.c
index c69269a..6aa9344 100644
--- a/util/bitset.c
+++ b/util/bitset.c
@@ -17,7 +17,8 @@
* over both without keeping track of the
* minimum size.
*/
-static void eqsz(Bitset *a, Bitset *b)
+static void
+eqsz(Bitset *a, Bitset *b)
{
size_t sz;
size_t i;
@@ -48,7 +49,8 @@ static void eqsz(Bitset *a, Bitset *b)
}
/* Creates a new all-zero bit set */
-Bitset *mkbs()
+Bitset *
+mkbs()
{
Bitset *bs;
@@ -59,7 +61,8 @@ Bitset *mkbs()
}
/* Frees a bitset. Safe to call on NULL. */
-void bsfree(Bitset *bs)
+void
+bsfree(Bitset *bs)
{
if (!bs)
return;
@@ -68,7 +71,8 @@ void bsfree(Bitset *bs)
}
/* Duplicates a bitset. NULL is duplicated to NULL. */
-Bitset *bsdup(Bitset *a)
+Bitset *
+bsdup(Bitset *a)
{
Bitset *bs;
@@ -82,7 +86,8 @@ Bitset *bsdup(Bitset *a)
}
/* Zeroes all values in a bit set */
-Bitset *bsclear(Bitset *bs)
+Bitset *
+bsclear(Bitset *bs)
{
size_t i;
@@ -94,7 +99,8 @@ Bitset *bsclear(Bitset *bs)
}
/* Counts the number of values held in a bit set */
-size_t bscount(Bitset *bs)
+size_t
+bscount(Bitset *bs)
{
size_t i, j, n;
@@ -106,7 +112,8 @@ size_t bscount(Bitset *bs)
return n;
}
-inline static int firstbit(size_t b)
+inline
+static int firstbit(size_t b)
{
int n;
static const char bits[] = {
@@ -134,7 +141,7 @@ inline static int firstbit(size_t b)
return n;
}
-/* A slightly tricky function to iterate over the contents
+/* A slightly tricky function to iterate over the content
* of a bitset. It returns true immediately if 'elt' is in
* the bitset, otherwise it seeks forward to the next value
* held in the bitset and stores it in elt. If there are no
@@ -151,7 +158,8 @@ inline static int firstbit(size_t b)
* to prevent the function from returning the same value
* repeatedly.
*/
-int bsiter(Bitset *bs, size_t *elt)
+int
+bsiter(Bitset *bs, size_t *elt)
{
size_t b, t, i;
@@ -175,9 +183,11 @@ int bsiter(Bitset *bs, size_t *elt)
/* Returns the largest value that the bitset can possibly
* hold. It's conservative, but scanning the entire bitset
* is a bit slow. This is mostly an aid to iterate over it. */
-size_t bsmax(Bitset *bs) { return bs->nchunks * Sizetbits; }
+size_t
+bsmax(Bitset *bs) { return bs->nchunks * Sizetbits; }
-void bsput(Bitset *bs, size_t elt)
+void
+bsput(Bitset *bs, size_t elt)
{
size_t sz;
if (elt >= bs->nchunks * Sizetbits) {
@@ -188,13 +198,15 @@ void bsput(Bitset *bs, size_t elt)
bs->chunks[elt / Sizetbits] |= 1ULL << (elt % Sizetbits);
}
-void bsdel(Bitset *bs, size_t elt)
+void
+bsdel(Bitset *bs, size_t elt)
{
if (elt < bs->nchunks * Sizetbits)
bs->chunks[elt / Sizetbits] &= ~(1ULL << (elt % Sizetbits));
}
-void bsunion(Bitset *a, Bitset *b)
+void
+bsunion(Bitset *a, Bitset *b)
{
size_t i;
@@ -203,7 +215,8 @@ void bsunion(Bitset *a, Bitset *b)
a->chunks[i] |= b->chunks[i];
}
-void bsintersect(Bitset *a, Bitset *b)
+void
+bsintersect(Bitset *a, Bitset *b)
{
size_t i;
@@ -212,7 +225,8 @@ void bsintersect(Bitset *a, Bitset *b)
a->chunks[i] &= b->chunks[i];
}
-void bsdiff(Bitset *a, Bitset *b)
+void
+bsdiff(Bitset *a, Bitset *b)
{
size_t i;
@@ -221,7 +235,8 @@ void bsdiff(Bitset *a, Bitset *b)
a->chunks[i] &= ~b->chunks[i];
}
-int bseq(Bitset *a, Bitset *b)
+int
+bseq(Bitset *a, Bitset *b)
{
size_t i;
@@ -235,7 +250,8 @@ int bseq(Bitset *a, Bitset *b)
return 1;
}
-int bsissubset(Bitset *set, Bitset *sub)
+int
+bsissubset(Bitset *set, Bitset *sub)
{
size_t i;
@@ -246,7 +262,8 @@ int bsissubset(Bitset *set, Bitset *sub)
return 1;
}
-int bsisempty(Bitset *set)
+int
+bsisempty(Bitset *set)
{
size_t i;
diff --git a/util/htab.c b/util/htab.c
index 0fc0ed4..8894579 100644
--- a/util/htab.c
+++ b/util/htab.c
@@ -11,12 +11,13 @@
#define Initsz 16
#define Seed 2928213749
-ulong murmurhash2(void *key, size_t len);
+static ulong murmurhash2(void *key, size_t len);
/* Creates a new empty hash table, using 'hash' as the
* hash funciton, and 'cmp' to verify that there are no
* hash collisions. */
-Htab *mkht(ulong (*hash)(void *key), int (*cmp)(void *k1, void *k2))
+Htab *
+mkht(ulong (*hash)(void *key), int (*cmp)(void *k1, void *k2))
{
Htab *ht;
@@ -36,7 +37,8 @@ Htab *mkht(ulong (*hash)(void *key), int (*cmp)(void *k1, void *k2))
/* Frees a hash table. Passing this function
* NULL is a no-op. */
-void htfree(Htab *ht)
+void
+htfree(Htab *ht)
{
if (!ht)
return;
@@ -49,7 +51,8 @@ void htfree(Htab *ht)
/* Offsets the hash so that '0' can be
* used as a 'no valid value */
-static ulong hash(Htab *ht, void *k)
+static ulong
+hash(Htab *ht, void *k)
{
ulong h;
h = ht->hash(k);
@@ -62,7 +65,8 @@ static ulong hash(Htab *ht, void *k)
/* Resizes the hash table by copying all
* the old keys into the right slots in a
* new table. */
-static void grow(Htab *ht, int sz)
+static void
+grow(Htab *ht, int sz)
{
void **oldk;
void **oldv;
@@ -96,9 +100,10 @@ static void grow(Htab *ht, int sz)
}
/* Inserts 'k' into the hash table, possibly
- * killing any previous key that compares
+ * killing any previous key that compare
* as equal. */
-int htput(Htab *ht, void *k, void *v)
+int
+htput(Htab *ht, void *k, void *v)
{
int i;
ulong h;
@@ -120,7 +125,8 @@ int htput(Htab *ht, void *k, void *v)
i = (h + di) & (ht->sz - 1);
}
ht->nelt++;
-conflicted:
+conflicted
+:
if (ht->dead[i])
ht->ndead--;
ht->hashes[i] = h;
@@ -137,7 +143,8 @@ conflicted:
/* Finds the index that we would insert
* the key into */
-static ssize_t htidx(Htab *ht, void *k)
+static ssize_t
+htidx(Htab *ht, void *k)
{
ssize_t i;
ulong h;
@@ -147,7 +154,8 @@ static ssize_t htidx(Htab *ht, void *k)
h = hash(ht, k);
i = h & (ht->sz - 1);
while (ht->hashes[i] && !ht->dead[i] && ht->hashes[i] != h) {
-searchmore:
+searchmore
+:
di++;
i = (h + di) & (ht->sz - 1);
}
@@ -161,9 +169,10 @@ searchmore:
/* Looks up a key, returning NULL if
* the value is not present. Note,
* if NULL is a valid value, you need
- * to check with hthas() to see if it's
+ * to check with hthas() to see if it'
* not there */
-void *htget(Htab *ht, void *k)
+void *
+htget(Htab *ht, void *k)
{
ssize_t i;
@@ -174,7 +183,8 @@ void *htget(Htab *ht, void *k)
return ht->vals[i];
}
-void htdel(Htab *ht, void *k)
+void
+htdel(Htab *ht, void *k)
{
ssize_t i;
@@ -189,14 +199,16 @@ void htdel(Htab *ht, void *k)
}
/* Tests for 'k's presence in 'ht' */
-int hthas(Htab *ht, void *k) { return htidx(ht, k) >= 0; }
+int
+hthas(Htab *ht, void *k) { return htidx(ht, k) >= 0; }
/* Returns a list of all keys in the hash
* table, storing the size of the returned
* array in 'nkeys'. NB: the value returned
* is allocated on the heap, and it is the
* job of the caller to free it */
-void **htkeys(Htab *ht, size_t *nkeys)
+void **
+htkeys(Htab *ht, size_t *nkeys)
{
void **k;
size_t i, j;
@@ -210,7 +222,8 @@ void **htkeys(Htab *ht, size_t *nkeys)
return k;
}
-ulong strhash(void *_s)
+ulong
+strhash(void *_s)
{
char *s;
@@ -220,7 +233,8 @@ ulong strhash(void *_s)
return murmurhash2(_s, strlen(_s));
}
-int streq(void *a, void *b)
+int
+streq(void *a, void *b)
{
if (a == b)
return 1;
@@ -229,7 +243,8 @@ int streq(void *a, void *b)
return !strcmp(a, b);
}
-ulong strlithash(void *_s)
+ulong
+strlithash(void *_s)
{
Str *s;
@@ -239,7 +254,8 @@ ulong strlithash(void *_s)
return murmurhash2(s->buf, s->len);
}
-int strliteq(void *_a, void *_b)
+int
+strliteq(void *_a, void *_b)
{
Str *a, *b;
@@ -254,27 +270,32 @@ int strliteq(void *_a, void *_b)
return !memcmp(a->buf, b->buf, a->len);
}
-ulong ptrhash(void *key)
-{
+ulong
+ptrhash(void *key)
+{
return inthash((uintptr_t)key);
}
-ulong inthash(uint64_t key)
+ulong
+inthash(uint64_t key)
{
return murmurhash2(&key, sizeof key);
}
-int inteq(uint64_t a, uint64_t b)
+int
+inteq(uint64_t a, uint64_t b)
{
return a == b;
}
-int ptreq(void *a, void *b)
+int
+ptreq(void *a, void *b)
{
return a == b;
}
-ulong murmurhash2 (void *ptr, size_t len)
+static ulong
+murmurhash2 (void *ptr, size_t len)
{
uint32_t m = 0x5bd1e995;
uint32_t r = 24;
diff --git a/util/pack.c b/util/pack.c
index aea907b..6457ba0 100644
--- a/util/pack.c
+++ b/util/pack.c
@@ -14,7 +14,8 @@
#include "util.h"
/* endian packing */
-void be64(vlong v, byte buf[8])
+void
+be64(vlong v, byte buf[8])
{
buf[0] = (v >> 56) & 0xff;
buf[1] = (v >> 48) & 0xff;
@@ -26,7 +27,8 @@ void be64(vlong v, byte buf[8])
buf[7] = (v >> 0) & 0xff;
}
-vlong host64(byte buf[8])
+vlong
+host64(byte buf[8])
{
vlong v = 0;
@@ -41,7 +43,8 @@ vlong host64(byte buf[8])
return v;
}
-void be32(long v, byte buf[4])
+void
+be32(long v, byte buf[4])
{
buf[0] = (v >> 24) & 0xff;
buf[1] = (v >> 16) & 0xff;
@@ -49,7 +52,8 @@ void be32(long v, byte buf[4])
buf[3] = (v >> 0) & 0xff;
}
-long host32(byte buf[4])
+long
+host32(byte buf[4])
{
int32_t v = 0;
v |= ((long)buf[0] << 24);
@@ -59,7 +63,8 @@ long host32(byte buf[4])
return v;
}
-void wrbuf(FILE *fd, void *p, size_t sz)
+void
+wrbuf(FILE *fd, void *p, size_t sz)
{
size_t n;
char *buf;
@@ -75,7 +80,8 @@ void wrbuf(FILE *fd, void *p, size_t sz)
}
}
-void rdbuf(FILE *fd, void *buf, size_t sz)
+void
+rdbuf(FILE *fd, void *buf, size_t sz)
{
size_t n;
@@ -89,13 +95,15 @@ void rdbuf(FILE *fd, void *buf, size_t sz)
}
}
-void wrbyte(FILE *fd, char val)
+void
+wrbyte(FILE *fd, char val)
{
if (fputc(val, fd) == EOF)
die("Unexpected EOF");
}
-char rdbyte(FILE *fd)
+char
+rdbyte(FILE *fd)
{
int c;
c = fgetc(fd);
@@ -104,21 +112,24 @@ char rdbyte(FILE *fd)
return c;
}
-void wrint(FILE *fd, long val)
+void
+wrint(FILE *fd, long val)
{
byte buf[4];
be32(val, buf);
wrbuf(fd, buf, 4);
}
-long rdint(FILE *fd)
+long
+rdint(FILE *fd)
{
byte buf[4];
rdbuf(fd, buf, 4);
return host32(buf);
}
-void wrstr(FILE *fd, char *val)
+void
+wrstr(FILE *fd, char *val)
{
size_t len;
@@ -131,7 +142,8 @@ void wrstr(FILE *fd, char *val)
}
}
-char *rdstr(FILE *fd)
+char *
+rdstr(FILE *fd)
{
ssize_t len;
char *s;
@@ -147,13 +159,15 @@ char *rdstr(FILE *fd)
}
}
-void wrlenstr(FILE *fd, Str str)
+void
+wrlenstr(FILE *fd, Str str)
{
wrint(fd, str.len);
wrbuf(fd, str.buf, str.len);
}
-void rdlenstr(FILE *fd, Str *str)
+void
+rdlenstr(FILE *fd, Str *str)
{
str->len = rdint(fd);
str->buf = xalloc(str->len + 1);
@@ -161,7 +175,8 @@ void rdlenstr(FILE *fd, Str *str)
str->buf[str->len] = '\0';
}
-void wrflt(FILE *fd, double val)
+void
+wrflt(FILE *fd, double val)
{
byte buf[8];
/* Assumption: We have 'val' in 64 bit IEEE format */
@@ -175,7 +190,8 @@ void wrflt(FILE *fd, double val)
wrbuf(fd, buf, 8);
}
-double rdflt(FILE *fd)
+double
+rdflt(FILE *fd)
{
byte buf[8];
union {
@@ -188,7 +204,8 @@ double rdflt(FILE *fd)
return u.fval;
}
-size_t bprintf(char *buf, size_t sz, char *fmt, ...)
+size_t
+bprintf(char *buf, size_t sz, char *fmt, ...)
{
va_list ap;
size_t n;
@@ -202,12 +219,14 @@ size_t bprintf(char *buf, size_t sz, char *fmt, ...)
return n;
}
-void wrbool(FILE *fd, int val)
-{
+void
+wrbool(FILE *fd, int val)
+{
wrbyte(fd, val);
}
-int rdbool(FILE *fd)
+int
+rdbool(FILE *fd)
{
- return rdbyte(fd);
+ return rdbyte(fd);
}
diff --git a/util/util.c b/util/util.c
index 904ef26..cc711ed 100644
--- a/util/util.c
+++ b/util/util.c
@@ -14,7 +14,8 @@
#include "util.h"
/* Some systems don't have strndup. */
-char *strdupn(char *s, size_t len)
+char *
+strdupn(char *s, size_t len)
{
char *ret;
@@ -24,7 +25,8 @@ char *strdupn(char *s, size_t len)
return ret;
}
-char *xstrdup(char *s)
+char *
+xstrdup(char *s)
{
char *p;
@@ -34,7 +36,8 @@ char *xstrdup(char *s)
return p;
}
-char *strjoin(char *u, char *v)
+char *
+strjoin(char *u, char *v)
{
size_t n;
char *s;
@@ -45,7 +48,8 @@ char *strjoin(char *u, char *v)
return s;
}
-char *swapsuffix(char *buf, size_t sz, char *s, char *suf, char *swap)
+char *
+swapsuffix(char *buf, size_t sz, char *s, char *suf, char *swap)
{
size_t slen, suflen, swaplen;
@@ -70,7 +74,8 @@ char *swapsuffix(char *buf, size_t sz, char *s, char *suf, char *swap)
return buf;
}
-size_t max(size_t a, size_t b)
+size_t
+max(size_t a, size_t b)
{
if (a > b)
return a;
@@ -78,7 +83,8 @@ size_t max(size_t a, size_t b)
return b;
}
-size_t min(size_t a, size_t b)
+size_t
+min(size_t a, size_t b)
{
if (a < b)
return a;
@@ -86,7 +92,8 @@ size_t min(size_t a, size_t b)
return b;
}
-size_t align(size_t sz, size_t a)
+size_t
+align(size_t sz, size_t a)
{
/* align to 0 just returns sz */
if (a == 0)
@@ -94,7 +101,8 @@ size_t align(size_t sz, size_t a)
return (sz + a - 1) & ~(a - 1);
}
-void indentf(int depth, char *fmt, ...)
+void
+indentf(int depth, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
@@ -102,7 +110,8 @@ void indentf(int depth, char *fmt, ...)
va_end(ap);
}
-void findentf(FILE *fd, int depth, char *fmt, ...)
+void
+findentf(FILE *fd, int depth, char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
@@ -110,7 +119,8 @@ void findentf(FILE *fd, int depth, char *fmt, ...)
va_end(ap);
}
-void vfindentf(FILE *fd, int depth, char *fmt, va_list ap)
+void
+vfindentf(FILE *fd, int depth, char *fmt, va_list ap)
{
ssize_t i;
@@ -119,7 +129,8 @@ void vfindentf(FILE *fd, int depth, char *fmt, va_list ap)
vfprintf(fd, fmt, ap);
}
-static int optinfo(Optctx *ctx, char arg, int *take, int *mand)
+static int
+optinfo(Optctx *ctx, char arg, int *take, int *mand)
{
char *s;
@@ -144,7 +155,8 @@ static int optinfo(Optctx *ctx, char arg, int *take, int *mand)
return 0;
}
-static int findnextopt(Optctx *ctx)
+static int
+findnextopt(Optctx *ctx)
{
size_t i;
@@ -156,13 +168,15 @@ static int findnextopt(Optctx *ctx)
}
ctx->finished = 1;
return 0;
-foundopt:
+foundopt
+:
ctx->argidx = i;
ctx->curarg = ctx->optargs[i] + 1; /* skip initial '-' */
return 1;
}
-void optinit(Optctx *ctx, char *optstr, char **optargs, size_t noptargs)
+void
+optinit(Optctx *ctx, char *optstr, char **optargs, size_t noptargs)
{
ctx->args = NULL;
ctx->nargs = 0;
@@ -177,7 +191,8 @@ void optinit(Optctx *ctx, char *optstr, char **optargs, size_t noptargs)
findnextopt(ctx);
}
-int optnext(Optctx *ctx)
+int
+optnext(Optctx *ctx)
{
int take, mand;
int c;
@@ -208,4 +223,5 @@ int optnext(Optctx *ctx)
return c;
}
-int optdone(Optctx *ctx) { return *ctx->curarg == '\0' && ctx->finished; }
+int
+optdone(Optctx *ctx) { return *ctx->curarg == '\0' && ctx->finished; }