...
 
Commits (8)
......@@ -13,8 +13,8 @@ void bitmap_zero_range(bitmap *bitmap, unsigned long n, unsigned long m)
{
unsigned long an = BIT_ALIGN_UP(n);
unsigned long am = BIT_ALIGN_DOWN(m);
bitmap_word headmask = -1ULL >> (n % BITMAP_WORD_BITS);
bitmap_word tailmask = ~(-1ULL >> (m % BITMAP_WORD_BITS));
bitmap_word headmask = BITMAP_WORD_1 >> (n % BITMAP_WORD_BITS);
bitmap_word tailmask = ~(BITMAP_WORD_1 >> (m % BITMAP_WORD_BITS));
assert(m >= n);
......@@ -38,8 +38,8 @@ void bitmap_fill_range(bitmap *bitmap, unsigned long n, unsigned long m)
{
unsigned long an = BIT_ALIGN_UP(n);
unsigned long am = BIT_ALIGN_DOWN(m);
bitmap_word headmask = -1ULL >> (n % BITMAP_WORD_BITS);
bitmap_word tailmask = ~(-1ULL >> (m % BITMAP_WORD_BITS));
bitmap_word headmask = BITMAP_WORD_1 >> (n % BITMAP_WORD_BITS);
bitmap_word tailmask = ~(BITMAP_WORD_1 >> (m % BITMAP_WORD_BITS));
assert(m >= n);
......@@ -65,7 +65,7 @@ static int bitmap_clz(bitmap_word w)
return __builtin_clzl(w);
#else
int lz = 0;
bitmap_word mask = 1UL << (BITMAP_WORD_BITS - 1);
bitmap_word mask = (bitmap_word)1 << (BITMAP_WORD_BITS - 1);
while (!(w & mask)) {
lz++;
......@@ -81,8 +81,8 @@ unsigned long bitmap_ffs(const bitmap *bitmap,
{
unsigned long an = BIT_ALIGN_UP(n);
unsigned long am = BIT_ALIGN_DOWN(m);
bitmap_word headmask = -1ULL >> (n % BITMAP_WORD_BITS);
bitmap_word tailmask = ~(-1ULL >> (m % BITMAP_WORD_BITS));
bitmap_word headmask = BITMAP_WORD_1 >> (n % BITMAP_WORD_BITS);
bitmap_word tailmask = ~(BITMAP_WORD_1 >> (m % BITMAP_WORD_BITS));
assert(m >= n);
......
......@@ -15,10 +15,13 @@ typedef unsigned long bitmap_word;
#define BITMAP_NWORDS(_n) \
(((_n) + BITMAP_WORD_BITS - 1) / BITMAP_WORD_BITS)
#define BITMAP_WORD_0 (0)
#define BITMAP_WORD_1 ((bitmap_word)-1UL)
/*
* We wrap each word in a structure for type checking.
*/
typedef struct {
typedef struct bitmap {
bitmap_word w;
} bitmap;
......
......@@ -29,7 +29,7 @@ int main(void)
}
/* Check it is read-write. */
ok1(write(fd, buf, strlen(buf)) == strlen(buf));
lseek(fd, SEEK_SET, 0);
lseek(fd, 0, SEEK_SET);
ok1(read(fd, buf, strlen("Hello world!")) == strlen("Hello world!"));
ok1(strcmp(buf, "Hello world!") == 0);
......
......@@ -75,6 +75,15 @@ bool htable_init_sized(struct htable *ht,
size_t (*rehash)(const void *elem, void *priv),
void *priv, size_t size);
/**
* htable_count - count number of entries in a hash table.
* @ht: the hash table
*/
static inline size_t htable_count(const struct htable *ht)
{
return ht->elems;
}
/**
* htable_clear - empty a hash table.
* @ht: the hash table to clear
......
......@@ -25,6 +25,9 @@
* void <name>_clear(struct <name> *);
* bool <name>_copy(struct <name> *dst, const struct <name> *src);
*
* Count entries:
* size_t <name>_count(const struct <name> *ht);
*
* Add function only fails if we run out of memory:
* bool <name>_add(struct <name> *ht, const <type> *e);
*
......@@ -69,6 +72,10 @@
{ \
return htable_init_sized(&ht->raw, name##_hash, NULL, s); \
} \
static inline UNNEEDED size_t name##_count(const struct name *ht) \
{ \
return htable_count(&ht->raw); \
} \
static inline UNNEEDED void name##_clear(struct name *ht) \
{ \
htable_clear(&ht->raw); \
......
......@@ -65,7 +65,7 @@ static void find_vals(const struct htable_obj *ht,
return;
}
}
pass("Found %u numbers in hash", i);
ok1(htable_obj_count(ht) == i);
}
static void del_vals(struct htable_obj *ht,
......@@ -116,12 +116,13 @@ int main(void)
void *p;
struct htable_obj_iter iter;
plan_tests(29);
plan_tests(32);
for (i = 0; i < NUM_VALS; i++)
val[i].key = i;
dne = i;
htable_obj_init(&ht);
ok1(htable_obj_count(&ht) == 0);
ok1(ht_max(&ht.raw) == 0);
ok1(ht.raw.bits == 0);
......@@ -205,6 +206,8 @@ int main(void)
}
htable_obj_clear(&ht);
ok1(htable_obj_count(&ht) == 0);
htable_obj_clear(&ht2);
ok1(htable_obj_count(&ht2) == 0);
return exit_status();
}
......@@ -67,7 +67,7 @@ static void find_vals(struct htable *ht,
return;
}
}
pass("Found %llu numbers in hash", (long long)i);
ok1(htable_count(ht) == i);
}
static void del_vals(struct htable *ht,
......@@ -105,12 +105,13 @@ int main(void)
void *p;
struct htable_iter iter;
plan_tests(36);
plan_tests(38);
for (i = 0; i < NUM_VALS; i++)
val[i] = i;
dne = i;
htable_init(&ht, hash, NULL);
ok1(htable_count(&ht) == 0);
ok1(ht_max(&ht) == 0);
ok1(ht.bits == 0);
......@@ -207,6 +208,8 @@ int main(void)
ok1(htable_init_sized(&ht, hash, NULL, 1025));
ok1(ht_max(&ht) >= 1025);
htable_clear(&ht);
ok1(htable_count(&ht) == 0);
return exit_status();
}
......@@ -231,6 +231,63 @@ none_left:
return NULL;
}
void *intmap_before_(const struct intmap *map, intmap_index_t *indexp)
{
const struct intmap *n, *prev = NULL;
intmap_index_t index = (*indexp) - 1;
/* Special case of overflow */
if (index == (intmap_index_t)-1ULL)
goto none_left;
/* Special case of empty map */
if (intmap_empty_(map))
goto none_left;
/* Follow down, until prefix differs. */
n = map;
while (!n->v) {
int crit = critbit(n);
u8 direction;
intmap_index_t prefix, idx;
idx = (index >> crit);
direction = idx & 1;
/* Leave critbit in place: we can't shift by 64 anyway */
idx |= 1;
prefix = n->u.n->prefix_and_critbit >> crit;
/* If this entire tree is less than index, take last */
if (idx > prefix)
return intmap_last_(n, indexp);
/* If this entire tree is greater than index, we're past it. */
else if (idx < prefix)
goto try_lesser_tree;
/* Remember lesser tree for backtracking */
if (direction)
prev = n;
n = &n->u.n->child[direction];
}
/* Found a predecessor? */
if (n->u.i <= index) {
errno = 0;
*indexp = n->u.i;
return n->v;
}
try_lesser_tree:
/* If we ever took a lesser branch, go back to lesser branch */
if (prev)
return intmap_last_(&prev->u.n->child[0], indexp);
none_left:
errno = ENOENT;
return NULL;
}
void *intmap_last_(const struct intmap *map, intmap_index_t *indexp)
{
const struct intmap *n;
......
......@@ -309,6 +309,32 @@ void *intmap_after_(const struct intmap *map, intmap_index_t *indexp);
tcon_cast((smap), sintmap_canary, \
sintmap_after_(sintmap_unwrap_(smap), (indexp)))
/**
* uintmap_before - get the closest preceding index in an unsigned intmap
* @umap: the typed intmap to iterate through.
* @indexp: the succeeding index (may not exist)
*
* Returns NULL if the there is no entry < @indexp, otherwise
* populates *@indexp and returns the highest entry < @indexp.
*/
#define uintmap_before(umap, indexp) \
tcon_cast((umap), uintmap_canary, \
intmap_before_(uintmap_unwrap_(umap), (indexp)))
void *intmap_before_(const struct intmap *map, intmap_index_t *indexp);
/**
* sintmap_before - get the closest preceding index in a signed intmap
* @smap: the typed intmap to iterate through.
* @indexp: the succeeding index (may not exist)
*
* Returns NULL if the there is no entry < @indexp, otherwise
* populates *@indexp and returns the highest entry < @indexp.
*/
#define sintmap_before(smap, indexp) \
tcon_cast((smap), sintmap_canary, \
sintmap_before_(sintmap_unwrap_(smap), (indexp)))
/**
* uintmap_last - get last value in an unsigned intmap
* @umap: the typed intmap to iterate through.
......@@ -455,6 +481,15 @@ static inline void *sintmap_after_(const struct intmap *map,
return ret;
}
static inline void *sintmap_before_(const struct intmap *map,
sintmap_index_t *indexp)
{
intmap_index_t i = SINTMAP_OFF(*indexp);
void *ret = intmap_before_(map, &i);
*indexp = SINTMAP_UNOFF(i);
return ret;
}
static inline void *sintmap_last_(const struct intmap *map,
sintmap_index_t *indexp)
{
......
......@@ -19,6 +19,15 @@ static bool check_umap(const umap *map)
bool last = true;
/* Must be in order, must contain value. */
prev = 256;
for (v = uintmap_last(map, &i); v; v = uintmap_before(map, &i)) {
if (i >= prev)
return false;
if (*v != i)
return false;
prev = i;
}
prev = -1;
for (v = uintmap_first(map, &i); v; v = uintmap_after(map, &i)) {
if (i <= prev)
......@@ -40,6 +49,15 @@ static bool check_smap(const smap *map)
bool last = true;
/* Must be in order, must contain value. */
prev = 0x80000000ULL;
for (v = sintmap_last(map, &i); v; v = sintmap_before(map, &i)) {
if (i >= prev)
return false;
if (*v != i)
return false;
prev = i;
}
prev = -0x80000001ULL;
for (v = sintmap_first(map, &i); v; v = sintmap_after(map, &i)) {
if (i <= prev)
......@@ -52,7 +70,7 @@ static bool check_smap(const smap *map)
return last;
}
int main(void)
int main(int argc, char *argv[])
{
umap umap;
smap smap;
......@@ -64,6 +82,9 @@ int main(void)
uintmap_init(&umap);
sintmap_init(&smap);
if (argc > 1)
srandom(atoi(argv[1]));
for (i = 0; i < NUM; i++) {
urandoms[i] = random();
srandoms[i] = random();
......
......@@ -36,6 +36,19 @@ static bool check_umap(const umap *map)
last = (uintmap_last(map, &last_idx) == v);
}
if (!last)
return false;
prev = INT_MAX;
for (v = uintmap_last(map, &i); v; v = uintmap_before(map, &i)) {
if ((int64_t)i >= prev)
return false;
if (*v != i)
return false;
prev = i;
last = (uintmap_first(map, &last_idx) == v);
}
if (!last)
return false;
......@@ -71,6 +84,18 @@ static bool check_smap(const smap *map)
prev = i;
}
if (!last)
return false;
prev = 0x80000001ULL;
for (v = sintmap_last(map, &i); v; v = sintmap_before(map, &i)) {
if (i >= prev)
return false;
if (*v != i)
return false;
prev = i;
last = (sintmap_first(map, &last_idx) == v);
}
if (!last)
return false;
......
......@@ -137,6 +137,13 @@ pid_t pipecmdarr(int *fd_tochild, int *fd_fromchild, int *fd_errfromchild,
goto child_errno_fail;
close(errfromchild[1]);
}
/* Make (fairly!) sure all other fds are closed. */
int max = sysconf(_SC_OPEN_MAX);
for (int i = 3; i < max; i++)
if (i != execfail[1])
close(i);
execvp(arr[0], arr);
child_errno_fail:
......
......@@ -20,7 +20,8 @@
* If @errfd == @outfd (and non-NULL) they will be shared.
* If @infd, @outfd or @errfd is &pipecmd_preserve, it is unchanged.
*
* The return value is the pid of the child, or -1.
* The return value is the pid of the child, or -1. All other file-descriptors
* are closed in the child.
*/
pid_t pipecmd(int *infd, int *outfd, int *errfd, const char *cmd, ...);
......
......@@ -31,7 +31,7 @@ int main(void)
/* Grab ourselves for comparison. */
buf[full_read(fd, buf, sizeof(buf))] = '\0';
lseek(fd, SEEK_SET, 0);
lseek(fd, 0, SEEK_SET);
for (i = 0, p = buf; *p; i++) {
lines[i] = p;
......
......@@ -27,7 +27,7 @@ int main(void)
/* Grab ourselves for comparison. */
len = read(fd, buf, sizeof(buf));
buf[len] = '\0';
lseek(fd, SEEK_SET, 0);
lseek(fd, 0, SEEK_SET);
/* We have exact-size buffer, which causes problems adding term. */
rbuf_init(&in, fd, malloc(len), len, test_realloc);
......@@ -44,7 +44,7 @@ int main(void)
free(rbuf_cleanup(&in));
/* Try again. */
lseek(fd, SEEK_SET, 0);
lseek(fd, 0, SEEK_SET);
rbuf_init(&in, fd, malloc(len), len, test_realloc);
p = rbuf_read_str(&in, 64);
ok1(p);
......@@ -53,7 +53,7 @@ int main(void)
free(rbuf_cleanup(&in));
/* Normal case, we get rbuf_start after nul */
lseek(fd, SEEK_SET, 0);
lseek(fd, 0, SEEK_SET);
rbuf_init(&in, fd, NULL, 0, test_realloc);
p = rbuf_read_str(&in, '^');
ok1(p);
......
......@@ -28,7 +28,7 @@ int main(void)
/* Grab ourselves for comparison. */
len = read(fd, buf, sizeof(buf));
buf[len] = '\0';
lseek(fd, SEEK_SET, 0);
lseek(fd, 0, SEEK_SET);
for (i = 0, p = buf; *p; i++) {
lines[i] = p;
......@@ -62,7 +62,7 @@ int main(void)
free(rbuf_cleanup(&in));
/* Another way of reading the entire (text) file. */
lseek(fd, SEEK_SET, 0);
lseek(fd, 0, SEEK_SET);
rbuf_init(&in, fd, NULL, 0, test_realloc);
p = rbuf_read_str(&in, 0);
ok1(p);
......
......@@ -302,7 +302,7 @@ fail_take_to:
char *path_readlink(const tal_t *ctx, const char *linkname)
{
ssize_t len, maxlen = 64; /* good first guess. */
ssize_t maxlen = 64; /* good first guess. */
char *ret = NULL;
if (unlikely(!linkname) && is_taken(linkname))
......@@ -311,19 +311,20 @@ fail_take_to:
ret = tal_arr(ctx, char, maxlen + 1);
while (ret) {
len = readlink(linkname, ret, maxlen);
ssize_t len = readlink(linkname, ret, maxlen);
if (len < 0)
goto fail;
if (len < maxlen)
if (len < maxlen) {
ret[len] = '\0';
break;
}
if (!tal_resize(&ret, maxlen *= 2 + 1))
goto fail;
}
if (ret)
ret[len] = '\0';
out:
if (taken(linkname))
tal_free(linkname);
......
......@@ -431,7 +431,7 @@ static const struct test base_tests[] = {
" return i + 1;\n"
"}" },
{ "HAVE_OPENMP", "#pragma omp and -fopenmp support",
"INSIDE_MAIN", NULL, NULL,
"INSIDE_MAIN|EXECUTE|MAY_NOT_COMPILE", NULL, NULL,
"int i;\n"
"#pragma omp parallel for\n"
"for(i = 0; i < 0; i++) {};\n"
......