diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-08-18 23:59:13 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-08-18 23:59:13 -0400 |
| commit | 8363d53bd27c621cb342fea15736a3b11231f2a4 (patch) | |
| tree | 094530cdd947f57197171f1dda58057739d63151 /builtins | |
| parent | 9e07c6adc7a0616eec40e78024a8501ec7d96559 (diff) | |
Update channel API to take a Where parameter
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/channel.c | 23 | ||||
| -rw-r--r-- | builtins/channel.h | 17 | ||||
| -rw-r--r-- | builtins/text.c | 14 | ||||
| -rw-r--r-- | builtins/where.c | 2 | ||||
| -rw-r--r-- | builtins/where.h | 2 |
5 files changed, 33 insertions, 25 deletions
diff --git a/builtins/channel.c b/builtins/channel.c index 9360bcfc..c2e2cf82 100644 --- a/builtins/channel.c +++ b/builtins/channel.c @@ -17,6 +17,7 @@ #include "integers.h" #include "types.h" #include "util.h" +#include "where.h" public channel_t *Channel$new(Int_t max_size) { @@ -30,50 +31,54 @@ public channel_t *Channel$new(Int_t max_size) return channel; } -public void Channel$give(channel_t *channel, const void *item, int64_t padded_item_size) +public void Channel$give(channel_t *channel, const void *item, Where_t where, int64_t padded_item_size) { (void)pthread_mutex_lock(&channel->mutex); while (channel->items.length >= channel->max_size) pthread_cond_wait(&channel->cond, &channel->mutex); - Array$insert(&channel->items, item, I(0), padded_item_size); + Int_t index = (where.tag == $tag$Where$Start) ? I(1) : I(0); + Array$insert(&channel->items, item, index, padded_item_size); (void)pthread_mutex_unlock(&channel->mutex); (void)pthread_cond_signal(&channel->cond); } -public void Channel$give_all(channel_t *channel, array_t to_give, int64_t padded_item_size) +public void Channel$give_all(channel_t *channel, array_t to_give, Where_t where, int64_t padded_item_size) { if (to_give.length == 0) return; (void)pthread_mutex_lock(&channel->mutex); + Int_t index = (where.tag == $tag$Where$Start) ? I(1) : I(0); if (channel->items.length + to_give.length >= channel->max_size) { for (int64_t i = 0; i < to_give.length; i++) { while (channel->items.length >= channel->max_size) pthread_cond_wait(&channel->cond, &channel->mutex); - Array$insert(&channel->items, to_give.data + i*to_give.stride, I(0), padded_item_size); + Array$insert(&channel->items, to_give.data + i*to_give.stride, index, padded_item_size); } } else { - Array$insert_all(&channel->items, to_give, I(0), padded_item_size); + Array$insert_all(&channel->items, to_give, index, padded_item_size); } (void)pthread_mutex_unlock(&channel->mutex); (void)pthread_cond_signal(&channel->cond); } -public void Channel$get(channel_t *channel, void *out, int64_t item_size, int64_t padded_item_size) +public void Channel$get(channel_t *channel, void *out, Where_t where, int64_t item_size, int64_t padded_item_size) { (void)pthread_mutex_lock(&channel->mutex); while (channel->items.length == 0) pthread_cond_wait(&channel->cond, &channel->mutex); memcpy(out, channel->items.data, item_size); - Array$remove_at(&channel->items, I(1), I(1), padded_item_size); + Int_t index = (where.tag == $tag$Where$End) ? I(0) : I(1); + Array$remove_at(&channel->items, index, I(1), padded_item_size); (void)pthread_mutex_unlock(&channel->mutex); (void)pthread_cond_signal(&channel->cond); } -public void Channel$peek(channel_t *channel, void *out, int64_t item_size) +public void Channel$peek(channel_t *channel, void *out, Where_t where, int64_t item_size) { (void)pthread_mutex_lock(&channel->mutex); while (channel->items.length == 0) pthread_cond_wait(&channel->cond, &channel->mutex); - memcpy(out, channel->items.data, item_size); + int64_t index = (where.tag == $tag$Where$End) ? channel->items.length-1 : 0; + memcpy(out, channel->items.data + channel->items.stride*index, item_size); (void)pthread_mutex_unlock(&channel->mutex); (void)pthread_cond_signal(&channel->cond); } diff --git a/builtins/channel.h b/builtins/channel.h index b8282374..241e7ec9 100644 --- a/builtins/channel.h +++ b/builtins/channel.h @@ -8,15 +8,18 @@ #include "datatypes.h" #include "types.h" #include "util.h" +#include "where.h" channel_t *Channel$new(Int_t max_size); -void Channel$give(channel_t *channel, const void *item, int64_t padded_item_size); -#define Channel$give_value(channel, item, padded_item_size) ({ __typeof(item) _item = item; Channel$give(channel, &_item, padded_item_size); }) -void Channel$give_all(channel_t *channel, array_t to_give, int64_t padded_item_size); -void Channel$get(channel_t *channel, void *out, int64_t item_size, int64_t padded_item_size); -#define Channel$get_value(channel, t, padded_item_size) ({ t _val; Channel$get(channel, &_val, sizeof(t), padded_item_size); _val; }) -void Channel$peek(channel_t *channel, void *out, int64_t item_size); -#define Channel$peek_value(channel, t) ({ t _val; Channel$peek(channel, &_val, sizeof(t)); _val; }) +void Channel$give(channel_t *channel, const void *item, Where_t where, int64_t padded_item_size); +#define Channel$give_value(channel, item, where, padded_item_size) \ + ({ __typeof(item) _item = item; Channel$give(channel, &_item, where, padded_item_size); }) +void Channel$give_all(channel_t *channel, array_t to_give, Where_t where, int64_t padded_item_size); +void Channel$get(channel_t *channel, void *out, Where_t where, int64_t item_size, int64_t padded_item_size); +#define Channel$get_value(channel, where, t, padded_item_size) \ + ({ t _val; Channel$get(channel, &_val, where, sizeof(t), padded_item_size); _val; }) +void Channel$peek(channel_t *channel, void *out, Where_t where, int64_t item_size); +#define Channel$peek_value(channel, where, t) ({ t _val; Channel$peek(channel, &_val, where, sizeof(t)); _val; }) void Channel$clear(channel_t *channel); array_t Channel$view(channel_t *channel); uint32_t Channel$hash(const channel_t **channel, const TypeInfo *type); diff --git a/builtins/text.c b/builtins/text.c index 22784a34..d9e15d03 100644 --- a/builtins/text.c +++ b/builtins/text.c @@ -171,12 +171,12 @@ public bool Text$has(CORD str, CORD target, Where_t where) if (target_norm_len > str_norm_len) return false; bool ret; - if (where.$tag == $tag$Where$Start) { + if (where.tag == $tag$Where$Start) { ret = (u8_strncmp(str_normalized, target_normalized, target_norm_len-1) == 0); - } else if (where.$tag == $tag$Where$End) { + } else if (where.tag == $tag$Where$End) { ret = (u8_strcmp(str_normalized + str_norm_len - target_norm_len, target_normalized) == 0); } else { - assert(where.$tag == $tag$Where$Anywhere); + assert(where.tag == $tag$Where$Anywhere); ret = (u8_strstr(str_normalized, target_normalized) != NULL); } @@ -191,11 +191,11 @@ public CORD Text$without(CORD str, CORD target, Where_t where) size_t target_len = CORD_len(target); size_t str_len = CORD_len(str); - if (where.$tag == $tag$Where$Start) { + if (where.tag == $tag$Where$Start) { if (CORD_ncmp(str, 0, target, 0, target_len) == 0) return CORD_substr(str, target_len, str_len - target_len); return str; - } else if (where.$tag == $tag$Where$End) { + } else if (where.tag == $tag$Where$End) { if (CORD_ncmp(str, str_len-target_len, target, 0, target_len) == 0) return CORD_substr(str, 0, str_len - target_len); return str; @@ -222,10 +222,10 @@ public CORD Text$trimmed(CORD str, CORD skip, Where_t where) const uint8_t *ustr = (const uint8_t*)CORD_to_const_char_star(str); const uint8_t *uskip = (const uint8_t*)CORD_to_const_char_star(skip); // TODO: implement proper reverse iteration with u8_prev() - if (where.$tag == $tag$Where$Start) { + if (where.tag == $tag$Where$Start) { size_t span = u8_strspn(ustr, uskip); return (CORD)ustr + span; - } else if (where.$tag == $tag$Where$End) { + } else if (where.tag == $tag$Where$End) { size_t len = u8_strlen(ustr); const uint8_t *back = ustr + len; size_t back_span = 0; diff --git a/builtins/where.c b/builtins/where.c index 5447d8c9..b69166e0 100644 --- a/builtins/where.c +++ b/builtins/where.c @@ -31,7 +31,7 @@ static CORD Where$as_text(Where_t *obj, bool use_color) { if (!obj) return "Where"; - switch (obj->$tag) { + switch (obj->tag) { case $tag$Where$Anywhere: return use_color ? "\x1b[36;1mWhere.Anywhere\x1b[m" : "Where.Anywhere"; case $tag$Where$Start: diff --git a/builtins/where.h b/builtins/where.h index 75de22a3..cfb45c62 100644 --- a/builtins/where.h +++ b/builtins/where.h @@ -22,7 +22,7 @@ struct Where$Anywhere_s {}; struct Where$Start_s {}; struct Where$End_s {}; struct Where_s { - enum { $tag$Where$Anywhere = 0, $tag$Where$Start = 1, $tag$Where$End = 2 } $tag; + enum { $tag$Where$Anywhere = 0, $tag$Where$Start = 1, $tag$Where$End = 2 } tag; union { Where$Anywhere_t Anywhere; Where$Start_t Start; |
