Skip to content

Commit

Permalink
- Internal cleanups of code and some refactoring for code clarity.
Browse files Browse the repository at this point in the history
- Renamed coroutine *_await_and_return() => *_await_with_return().
  • Loading branch information
tylov committed Oct 23, 2024
1 parent 2375473 commit 132624e
Show file tree
Hide file tree
Showing 18 changed files with 98 additions and 89 deletions.
2 changes: 1 addition & 1 deletion include/c11/fmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ FMT_DEF int _fmt_parse(char* p, int nargs, const char *fmt, ...) {
case 'g': if (empty) memcpy(p, ".8", 2), p += 2; break;
case '@': ++arg; if (empty) memcpy(p, ".16", 3), p += 3; break;
}
if (!strchr("csdioxXufFeEaAgGnp", fmt[-1]))
if (strchr("csdioxXufFeEaAgGnp", fmt[-1]) == NULL)
while (*arg) *p++ = *arg++;
if (p0 && (p[-1] == 's' || p[-1] == 'c')) /* left-align str */
memmove(p0 + 1, p0, (size_t)(p++ - p0)), *p0 = '-';
Expand Down
2 changes: 1 addition & 1 deletion include/stc/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ STC_INLINE Self _c_MEMB(_from)(_m_raw raw)

#if !defined i_no_clone
STC_INLINE Self _c_MEMB(_clone)(Self other) {
if (!other.get) return other;
if (other.get == NULL) return other;
Self out = {_i_malloc(_m_value, 1)};
*out.get = i_keyclone((*other.get));
return out;
Expand Down
2 changes: 1 addition & 1 deletion include/stc/cbits.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ STC_INLINE char* _cbits_to_str(const uintptr_t* set, const isize sz,
for (isize i = 0; i < n; ++i) \
if ((set[i] OPR other[i]) != VAL) \
return false; \
if (!(sz & (_cbits_WB - 1))) \
if ((sz & (_cbits_WB - 1)) == 0) \
return true; \
const uintptr_t i = (uintptr_t)n, m = _cbits_bit(sz) - 1; \
return ((set[i] OPR other[i]) & m) == (VAL & m)
Expand Down
2 changes: 1 addition & 1 deletion include/stc/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ STC_INLINE isize c_next_pow2(isize n) {
// substring in substring?
STC_INLINE char* c_strnstrn(const char *str, isize slen,
const char *needle, isize nlen) {
if (!nlen) return (char *)str;
if (nlen == 0) return (char *)str;
if (nlen > slen) return NULL;
slen -= nlen;
do {
Expand Down
22 changes: 11 additions & 11 deletions include/stc/coroutine.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ enum {
};
typedef enum {
CCO_DONE = 0,
CCO_AWAIT = 1<<29,
CCO_YIELD = 1<<30,
CCO_YIELD = 1<<29,
CCO_AWAIT = 1<<30,
} cco_result;

typedef struct {
Expand Down Expand Up @@ -97,12 +97,12 @@ typedef struct {
return value; \
} while (0)

#define cco_await(promise) cco_await_and_return(promise, CCO_AWAIT)
#define cco_await_and_return(promise, ret) \
#define cco_await(until) cco_await_with_return(until, CCO_AWAIT)
#define cco_await_with_return(until, ret) \
do { \
*_state = __LINE__; \
/* fall through */ \
case __LINE__: if (!(promise)) {return ret; goto _resume;} \
case __LINE__: if (!(until)) {return ret; goto _resume;} \
} while (0)

/* cco_await_coroutine(): assumes coroutine returns a cco_result value (int) */
Expand Down Expand Up @@ -232,10 +232,10 @@ typedef struct cco_runtime {

typedef struct { ptrdiff_t count; } cco_semaphore;

#define cco_await_semaphore(sem) cco_await_semaphore_and_return(sem, CCO_AWAIT)
#define cco_await_semaphore_and_return(sem, ret) \
#define cco_await_semaphore(sem) cco_await_semaphore_with_return(sem, CCO_AWAIT)
#define cco_await_semaphore_with_return(sem, ret) \
do { \
cco_await_and_return((sem)->count > 0, ret); \
cco_await_with_return((sem)->count > 0, ret); \
--(sem)->count; \
} while (0)

Expand Down Expand Up @@ -290,12 +290,12 @@ typedef struct { ptrdiff_t count; } cco_semaphore;

typedef struct { double interval, start; } cco_timer;

#define cco_await_timer(tm, sec) cco_await_timer_and_return(tm, sec, CCO_AWAIT)
#define cco_await_timer(tm, sec) cco_await_timer_with_return(tm, sec, CCO_AWAIT)
#define cco_await_timer_v(...) c_MACRO_OVERLOAD(cco_await_timer_v, __VA_ARGS__)
#define cco_await_timer_and_return(tm, sec, ret) \
#define cco_await_timer_with_return(tm, sec, ret) \
do { \
cco_timer_start(tm, sec); \
cco_await_and_return(cco_timer_expired(tm), ret); \
cco_await_with_return(cco_timer_expired(tm), ret); \
} while (0)

static inline void cco_timer_start(cco_timer* tm, double sec) {
Expand Down
7 changes: 4 additions & 3 deletions include/stc/csview.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,12 @@ STC_INLINE const char* csview_at(csview sv, isize idx)

/* utf8 iterator */
STC_INLINE csview_iter csview_begin(const csview* self) {
return c_literal(csview_iter){.u8 = {{self->buf, utf8_chr_size(self->buf)},
self->buf + self->size}};
csview_iter it = {.u8 = {{self->buf, utf8_chr_size(self->buf)},
self->buf + self->size}};
return it;
}
STC_INLINE csview_iter csview_end(const csview* self) {
return c_literal(csview_iter){.u8 = {{0}, self->buf + self->size}};
(void)self; csview_iter it = {0}; return it;
}
STC_INLINE void csview_next(csview_iter* it) {
it->ref += it->chr.size;
Expand Down
25 changes: 13 additions & 12 deletions include/stc/hmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ STC_INLINE _m_iter _c_MEMB(_advance)(_m_iter it, size_t n) {
STC_INLINE _m_iter
_c_MEMB(_find)(const Self* self, _m_keyraw rkey) {
_m_value* ref;
if (self->size && (ref = _c_MEMB(_bucket_lookup_)(self, &rkey).ref))
if (self->size != 0 && (ref = _c_MEMB(_bucket_lookup_)(self, &rkey).ref) != NULL)
return c_literal(_m_iter){ref,
&self->table[self->bucket_count],
&self->meta[ref - self->table]};
Expand All @@ -257,7 +257,7 @@ _c_MEMB(_get_mut)(Self* self, _m_keyraw rkey)
STC_INLINE int
_c_MEMB(_erase)(Self* self, _m_keyraw rkey) {
_m_value* ref;
if (self->size && (ref = _c_MEMB(_bucket_lookup_)(self, &rkey).ref))
if (self->size != 0 && (ref = _c_MEMB(_bucket_lookup_)(self, &rkey).ref) != NULL)
{ _c_MEMB(_erase_entry)(self, ref); return 1; }
return 0;
}
Expand All @@ -284,10 +284,11 @@ _c_MEMB(_eq)(const Self* self, const Self* other) {
#if defined(i_implement) || defined(i_static)

STC_DEF _m_iter _c_MEMB(_begin)(const Self* self) {
_m_iter it = {self->table, self->table+self->bucket_count, self->meta};
if (it._mref)
while (it._mref->dist == 0)
++it.ref, ++it._mref;
_m_iter it = {self->table, self->table, self->meta};
if (it.ref == NULL) return it;
it._end += self->bucket_count;
while (it._mref->dist == 0)
++it.ref, ++it._mref;
if (it.ref == it._end) it.ref = NULL;
return it;
}
Expand Down Expand Up @@ -351,7 +352,7 @@ STC_DEF void _c_MEMB(_clear)(Self* self) {
if (_res.inserted)
_res.ref->first = i_keyfrom(rkey);
else {
if (!_res.ref) return _res;
if (_res.ref == NULL) return _res;
i_valdrop((&_res.ref->second));
}
_res.ref->second = i_valfrom(rmapped);
Expand Down Expand Up @@ -416,19 +417,19 @@ _c_MEMB(_bucket_insert_)(const Self* self, const _m_keyraw* rkeyptr) {
#if !defined i_no_clone
STC_DEF Self
_c_MEMB(_clone)(Self map) {
if (map.bucket_count) {
if (map.bucket_count != 0) {
_m_value *d = _i_malloc(_m_value, map.bucket_count);
const isize _mbytes = (map.bucket_count + 1)*c_sizeof *map.meta;
struct hmap_meta *m = (struct hmap_meta *)i_malloc(_mbytes);
if (d && m) {
if (d != NULL && m != NULL) {
c_memcpy(m, map.meta, _mbytes);
_m_value *_dst = d, *_end = map.table + map.bucket_count;
for (; map.table != _end; ++map.table, ++map.meta, ++_dst)
if (map.meta->dist)
*_dst = _c_MEMB(_value_clone)(*map.table);
} else {
if (d) i_free(d, map.bucket_count*c_sizeof *d);
if (m) i_free(m, _mbytes);
if (d != NULL) i_free(d, map.bucket_count*c_sizeof *d);
if (m != NULL) i_free(m, _mbytes);
d = 0, m = 0, map.bucket_count = 0;
}
map.table = d, map.meta = m;
Expand Down Expand Up @@ -457,7 +458,7 @@ _c_MEMB(_reserve)(Self* self, const isize _newcap) {
const _m_value* d = self->table;
const struct hmap_meta* m = self->meta;

for (isize i = 0; i < _oldbucks; ++i, ++d) if ((m++)->dist) {
for (isize i = 0; i < _oldbucks; ++i, ++d) if ((m++)->dist != 0) {
_m_keyraw r = i_keytoraw(_i_keyref(d));
_m_result _res = _c_MEMB(_bucket_insert_)(&map, &r);
*_res.ref = *d; // move
Expand Down
18 changes: 9 additions & 9 deletions include/stc/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ STC_INLINE void _c_MEMB(_value_drop)(_m_value* pval) { i_keydrop(pval);
STC_INLINE isize
_c_MEMB(_count)(const Self* self) {
isize n = 1; const _m_node *node = self->last;
if (!node) return 0;
if (node == NULL) return 0;
while ((node = node->next) != self->last) ++n;
return n;
}
Expand Down Expand Up @@ -250,7 +250,7 @@ _c_MEMB(_push_back)(Self* self, _m_value value) {
STC_DEF _m_value*
_c_MEMB(_push_front)(Self* self, _m_value value) {
_c_list_insert_entry_after(self->last, value);
if (!self->last)
if (self->last == NULL)
self->last = entry;
return &entry->value;
}
Expand All @@ -265,7 +265,7 @@ _c_MEMB(_push_back_node)(Self* self, _m_node* node) {
STC_DEF _m_value*
_c_MEMB(_insert_after_node)(Self* self, _m_node* ref, _m_node* node) {
_c_list_insert_after_node(ref, node);
if (!self->last)
if (self->last == NULL)
self->last = node;
return &node->value;
}
Expand All @@ -274,7 +274,7 @@ STC_DEF _m_iter
_c_MEMB(_insert_at)(Self* self, _m_iter it, _m_value value) {
_m_node* node = it.ref ? it.prev : self->last;
_c_list_insert_entry_after(node, value);
if (!self->last || !it.ref) {
if (self->last == NULL || it.ref == NULL) {
it.prev = self->last ? self->last : entry;
self->last = entry;
}
Expand All @@ -295,7 +295,7 @@ _c_MEMB(_erase_range)(Self* self, _m_iter it1, _m_iter it2) {
_m_node *end = it2.ref ? _clist_tonode(it2.ref) : self->last->next;
if (it1.ref != it2.ref) do {
_c_MEMB(_erase_after_node)(self, it1.prev);
if (!self->last) break;
if (self->last == NULL) break;
} while (it1.prev->next != end);
return it2;
}
Expand Down Expand Up @@ -330,14 +330,14 @@ _c_MEMB(_reverse)(Self* self) {

STC_DEF _m_iter
_c_MEMB(_splice)(Self* self, _m_iter it, Self* other) {
if (!self->last)
if (self->last == NULL)
self->last = other->last;
else if (other->last) {
_m_node *p = it.ref ? it.prev : self->last, *next = p->next;
it.prev = other->last;
p->next = it.prev->next;
it.prev->next = next;
if (!it.ref) self->last = it.prev;
if (it.ref == NULL) self->last = it.prev;
}
other->last = NULL;
return it;
Expand Down Expand Up @@ -378,7 +378,7 @@ _c_MEMB(_remove)(Self* self, _m_raw val) {
_m_raw r = i_keytoraw((&node->value));
if (i_eq((&r), (&val))) {
_c_MEMB(_erase_after_node)(self, prev), ++n;
if (!self->last) break;
if (self->last == NULL) break;
} else
prev = node;
} while (node != self->last);
Expand All @@ -396,7 +396,7 @@ STC_DEF bool _c_MEMB(_sort)(Self* self) {
c_foreach (i, Self, *self) {
if (len == cap) {
isize cap_n = cap + cap/2 + 8;
if (!(p = (_m_value *)i_realloc(arr, cap*c_sizeof *p, cap_n*c_sizeof *p)))
if ((p = (_m_value *)i_realloc(arr, cap*c_sizeof *p, cap_n*c_sizeof *p)) == NULL)
goto done;
arr = p, cap = cap_n;
}
Expand Down
6 changes: 3 additions & 3 deletions include/stc/priv/cregex_prv.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ _lexasciiclass(_Parser *par, _Rune *rp) /* assume *rp == '[' and *par->exprp ==
};
int inv = par->exprp[1] == '^', off = 1 + inv;
for (unsigned i = 0; i < (sizeof cls/sizeof *cls); ++i)
if (!strncmp(par->exprp + off, cls[i].c, (size_t)cls[i].n)) {
if (strncmp(par->exprp + off, cls[i].c, (size_t)cls[i].n) == 0) {
*rp = (_Rune)cls[i].r;
par->exprp += off + cls[i].n;
break;
Expand Down Expand Up @@ -685,7 +685,7 @@ _lexutfclass(_Parser *par, _Rune *rp)
};
unsigned inv = (*rp == 'P');
for (unsigned i = 0; i < (sizeof cls/sizeof *cls); ++i) {
if (!strncmp(par->exprp, cls[i].c, (size_t)cls[i].n)) {
if (strncmp(par->exprp, cls[i].c, (size_t)cls[i].n) == 0) {
if (par->rune_type == TOK_IRUNE && (cls[i].r == UTF_ll || cls[i].r == UTF_lu))
*rp = (_Rune)(UTF_lc + inv);
else
Expand Down Expand Up @@ -868,7 +868,7 @@ _regcomp1(_Reprog *pp, _Parser *par, const char *s, int cflags)
isize instcap = 5 + 6*c_strlen(s);
isize new_allocsize = c_sizeof(_Reprog) + instcap*c_sizeof(_Reinst);
pp = (_Reprog *)i_realloc(pp, pp ? pp->allocsize : 0, new_allocsize);
if (! pp) {
if (pp == NULL) {
par->error = CREG_OUTOFMEMORY;
return NULL;
}
Expand Down
8 changes: 4 additions & 4 deletions include/stc/priv/cstr_prv.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ char* cstr_reserve(cstr* self, const isize cap) {
char* cstr_resize(cstr* self, const isize size, const char value) {
cstr_view r = cstr_getview(self);
if (size > r.size) {
if (size > r.cap && !(r.data = cstr_reserve(self, size)))
if (size > r.cap && (r.data = cstr_reserve(self, size)) == NULL)
return NULL;
c_memset(r.data + r.size, value, size - r.size);
}
Expand All @@ -108,7 +108,7 @@ char* cstr_append_n(cstr* self, const char* str, const isize len) {
if (r.size + len > r.cap) {
const size_t off = (size_t)(str - r.data);
r.data = cstr_reserve(self, r.size*3/2 + len);
if (!r.data) return NULL;
if (r.data == NULL) return NULL;
if (off <= (size_t)r.size) str = r.data + off; /* handle self append */
}
c_memcpy(r.data + r.size, str, len);
Expand All @@ -119,7 +119,7 @@ char* cstr_append_n(cstr* self, const char* str, const isize len) {
cstr cstr_from_replace(csview in, csview search, csview repl, int32_t count) {
cstr out = cstr_init();
isize from = 0; char* res;
if (!count) count = INT32_MAX;
if (count == 0) count = INT32_MAX;
if (search.size)
while (count-- && (res = c_strnstrn(in.buf + from, in.size - from, search.buf, search.size))) {
const isize pos = (res - in.buf);
Expand Down Expand Up @@ -160,7 +160,7 @@ void cstr_shrink_to_fit(cstr* self) {

char* cstr_append_uninit(cstr *self, isize len) {
cstr_view r = cstr_getview(self);
if (r.size + len > r.cap && !(r.data = cstr_reserve(self, r.size*3/2 + len)))
if (r.size + len > r.cap && (r.data = cstr_reserve(self, r.size*3/2 + len)) == NULL)
return NULL;
_cstr_set_size(self, r.size + len);
return r.data + r.size;
Expand Down
10 changes: 5 additions & 5 deletions include/stc/priv/cstr_prv.h
Original file line number Diff line number Diff line change
Expand Up @@ -223,22 +223,22 @@ STC_INLINE csview cstr_u8_chr(const cstr* self, isize u8pos) {

STC_INLINE cstr_iter cstr_begin(const cstr* self) {
csview sv = cstr_sv(self);
if (!sv.size) return c_literal(cstr_iter){.ref = NULL};
return c_literal(cstr_iter){.chr = {sv.buf, utf8_chr_size(sv.buf)}};
cstr_iter it = {.chr = {sv.buf, utf8_chr_size(sv.buf)}};
return it;
}
STC_INLINE cstr_iter cstr_end(const cstr* self) {
(void)self; return c_literal(cstr_iter){NULL};
(void)self; cstr_iter it = {0}; return it;
}
STC_INLINE void cstr_next(cstr_iter* it) {
it->ref += it->chr.size;
it->chr.size = utf8_chr_size(it->ref);
if (!*it->ref) it->ref = NULL;
if (*it->ref == '\0') it->ref = NULL;
}

STC_INLINE cstr_iter cstr_advance(cstr_iter it, isize u8pos) {
it.ref = c_const_cast(char *, utf8_offset(it.ref, u8pos));
it.chr.size = utf8_chr_size(it.ref);
if (!*it.ref) it.ref = NULL;
if (*it.ref == '\0') it.ref = NULL;
return it;
}

Expand Down
4 changes: 2 additions & 2 deletions include/stc/priv/queue_prv.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ _c_MEMB(_reserve)(Self* self, const isize n) {
return true;
isize oldpow2 = self->capmask + 1, newpow2 = c_next_pow2(n + 1);
_m_value* d = (_m_value *)i_realloc(self->cbuf, oldpow2*c_sizeof *d, newpow2*c_sizeof *d);
if (!d)
if (d == NULL)
return false;
isize head = oldpow2 - self->start;
if (self->start <= self->end)
Expand Down Expand Up @@ -213,7 +213,7 @@ _c_MEMB(_shrink_to_fit)(Self *self) {
if (sz > self->capmask/2)
return;
Self out = _c_MEMB(_with_capacity)(sz);
if (!out.cbuf)
if (out.cbuf == NULL)
return;
c_foreach (i, Self, *self)
out.cbuf[j++] = *i.ref;
Expand Down
Loading

0 comments on commit 132624e

Please sign in to comment.