2019-11-24 17:36:46 +00:00
|
|
|
#ifndef NOTCURSES_EGCPOOL
|
|
|
|
#define NOTCURSES_EGCPOOL
|
|
|
|
|
2019-11-25 18:36:52 +00:00
|
|
|
#include <wchar.h>
|
2019-11-27 19:43:25 +00:00
|
|
|
#include <errno.h>
|
2019-11-24 17:36:46 +00:00
|
|
|
#include <stddef.h>
|
2019-11-27 19:43:25 +00:00
|
|
|
#include <assert.h>
|
2019-11-24 17:36:46 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// cells only provide storage for a single 7-bit character. if there's anything
|
|
|
|
// more than that, it's spilled into the egcpool, and the cell is given an
|
|
|
|
// offset. when a cell is released, the memory it owned is zeroed out, and
|
|
|
|
// recognizable as use for another cell.
|
|
|
|
|
|
|
|
typedef struct egcpool {
|
2019-11-26 02:11:27 +00:00
|
|
|
char* pool; // ringbuffer of attached extension storage
|
|
|
|
int poolsize; // total number of bytes in pool
|
|
|
|
int poolused; // bytes actively used, grow when this gets too large
|
|
|
|
int poolwrite; // next place to *look for* a place to write
|
2019-11-24 17:36:46 +00:00
|
|
|
} egcpool;
|
|
|
|
|
2019-11-27 06:02:05 +00:00
|
|
|
#define POOL_MINIMUM_ALLOC BUFSIZ
|
|
|
|
|
2019-11-24 17:36:46 +00:00
|
|
|
static inline void
|
|
|
|
egcpool_init(egcpool* p){
|
|
|
|
memset(p, 0, sizeof(*p));
|
|
|
|
}
|
|
|
|
|
2019-11-27 06:02:05 +00:00
|
|
|
static inline int
|
|
|
|
egcpool_grow(egcpool* pool, size_t len){
|
|
|
|
size_t newsize = pool->poolsize * 2;
|
|
|
|
if(newsize < POOL_MINIMUM_ALLOC){
|
|
|
|
newsize = POOL_MINIMUM_ALLOC;
|
|
|
|
}
|
|
|
|
while(len > newsize - pool->poolsize){ // ensure we make enough space
|
|
|
|
newsize *= 2;
|
|
|
|
}
|
|
|
|
// offsets only have 24 bits available...
|
|
|
|
if(newsize >= 1u << 24u){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// nasty cast here because c++ source might include this header :/
|
|
|
|
typeof(*pool->pool)* tmp = (typeof(pool->pool))realloc(pool->pool, newsize);
|
|
|
|
if(tmp == NULL){
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
pool->pool = tmp;
|
|
|
|
memset(pool->pool + pool->poolsize, 0, newsize - pool->poolsize);
|
|
|
|
pool->poolsize = newsize;
|
|
|
|
return 0;
|
|
|
|
}
|
2019-11-24 17:36:46 +00:00
|
|
|
|
2019-11-25 18:36:52 +00:00
|
|
|
// Eat an EGC from the UTF-8 string input. This consists of extracting a
|
|
|
|
// multibyte via mbtowc, then continuing to extract any which have zero
|
|
|
|
// width until hitting another spacing character or a NUL terminator. Writes
|
|
|
|
// the number of columns occupied to '*colcount'. Returns the number of bytes
|
|
|
|
// consumed, not including any NUL terminator. Note that neither the number
|
|
|
|
// of bytes nor columns is necessarily equivalent to the number of decoded code
|
|
|
|
// points. Such are the ways of Unicode.
|
2019-11-26 02:11:27 +00:00
|
|
|
static inline int
|
|
|
|
utf8_egc_len(const char* gcluster, int* colcount){
|
2019-11-25 18:36:52 +00:00
|
|
|
size_t ret = 0;
|
|
|
|
*colcount = 0;
|
2019-11-24 19:00:48 +00:00
|
|
|
wchar_t wc;
|
2019-11-25 18:36:52 +00:00
|
|
|
int r;
|
2019-11-27 15:43:03 +00:00
|
|
|
mbstate_t mbt;
|
|
|
|
memset(&mbt, 0, sizeof(mbt));
|
2019-11-25 18:36:52 +00:00
|
|
|
do{
|
2019-11-27 15:43:03 +00:00
|
|
|
r = mbrtowc(&wc, gcluster, MB_CUR_MAX, &mbt);
|
2019-11-25 18:36:52 +00:00
|
|
|
if(r < 0){
|
|
|
|
return -1;
|
|
|
|
}else if(r){
|
|
|
|
int cols = wcwidth(wc);
|
|
|
|
if(cols){
|
|
|
|
if(*colcount){ // this must be starting a new EGC, exit and do not claim
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*colcount += cols;
|
|
|
|
}
|
|
|
|
ret += r;
|
|
|
|
gcluster += r;
|
|
|
|
}
|
|
|
|
}while(r);
|
|
|
|
return ret;
|
2019-11-24 19:00:48 +00:00
|
|
|
}
|
|
|
|
|
2019-11-24 20:26:01 +00:00
|
|
|
// if we're inserting a EGC of |len| bytes, ought we proactively realloc?
|
|
|
|
static inline bool
|
2019-11-26 02:11:27 +00:00
|
|
|
egcpool_alloc_justified(const egcpool* pool, int len){
|
|
|
|
const int poolfree = pool->poolsize - pool->poolused;
|
2019-11-24 20:26:01 +00:00
|
|
|
// proactively get more space if we have less than 10% free. this doesn't
|
|
|
|
// guarantee that we'll have enough space to insert the string -- we could
|
|
|
|
// theoretically have every 10th byte free, and be unable to write even a
|
|
|
|
// two-byte egc -- so we might have to allocate after an expensive search :/.
|
|
|
|
if(poolfree >= len && poolfree * 10 > pool->poolsize){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-24 17:36:46 +00:00
|
|
|
// stash away the provided UTF8, NUL-terminated grapheme cluster. the cluster
|
|
|
|
// should not be less than 2 bytes (such a cluster should be directly stored in
|
|
|
|
// the cell). returns -1 on error, and otherwise a non-negative 24-bit offset.
|
2019-11-27 15:43:03 +00:00
|
|
|
// 'ulen' must be the number of bytes to lift from egc (utf8_egc_len()).
|
2019-11-24 17:36:46 +00:00
|
|
|
static inline int
|
2019-11-27 15:43:03 +00:00
|
|
|
egcpool_stash(egcpool* pool, const char* egc, size_t ulen){
|
|
|
|
int len = ulen + 1; // count the NUL terminator
|
2019-11-24 17:36:46 +00:00
|
|
|
if(len <= 2){ // should never be empty, nor a single byte + NUL
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// the first time through, we don't force a grow unless we expect ourselves
|
|
|
|
// to have too little space. once we've done a search, we do force the grow.
|
|
|
|
// we should thus never have more than two iterations of this loop.
|
|
|
|
bool searched = false;
|
2019-11-24 20:26:01 +00:00
|
|
|
// we might have to realloc our underlying pool. it is possible that this EGC
|
|
|
|
// is actually *in* that pool, in which case our pointer will be invalidated.
|
|
|
|
// to be safe, duplicate prior to a realloc, and free along all paths.
|
|
|
|
char* duplicated = NULL;
|
2019-11-24 17:36:46 +00:00
|
|
|
do{
|
2019-11-24 20:26:01 +00:00
|
|
|
if(egcpool_alloc_justified(pool, len) || searched){
|
|
|
|
if(!duplicated){
|
|
|
|
duplicated = strdup(egc);
|
|
|
|
}
|
|
|
|
if(egcpool_grow(pool, len)){
|
|
|
|
free(duplicated);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
egc = duplicated;
|
2019-11-24 17:36:46 +00:00
|
|
|
}
|
|
|
|
// we now look for a place to lay out this egc. we need |len| zeroes in a
|
|
|
|
// row. starting at pool->poolwrite, look for such a range of unused
|
|
|
|
// memory. if we find it, write it out, and update used count. if we come
|
|
|
|
// back to where we started, force a growth and try again.
|
2019-11-26 02:11:27 +00:00
|
|
|
int curpos = pool->poolwrite;
|
2019-11-24 17:36:46 +00:00
|
|
|
do{
|
|
|
|
if(curpos == pool->poolsize){
|
|
|
|
curpos = 0;
|
|
|
|
}
|
|
|
|
if(pool->pool[curpos]){ // can't write if there's stuff here
|
|
|
|
++curpos;
|
2019-12-01 07:21:13 +00:00
|
|
|
}else if(curpos && pool->pool[curpos - 1]){ // don't kill someone's NUL
|
|
|
|
++curpos;
|
2019-11-24 20:56:22 +00:00
|
|
|
}else if(pool->poolsize - curpos < len){ // can't wrap around
|
|
|
|
if(pool->poolwrite > curpos){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
curpos = 0; // can this skip pool->poolwrite?
|
2019-11-24 17:36:46 +00:00
|
|
|
}else{ // promising! let's see if there's enough space
|
2019-11-26 02:11:27 +00:00
|
|
|
int need = len;
|
2019-11-24 17:36:46 +00:00
|
|
|
size_t trial = curpos;
|
|
|
|
while(--need){
|
2019-11-24 20:56:22 +00:00
|
|
|
if(pool->pool[++trial]){ // alas, not enough space here
|
2019-11-24 17:36:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(need == 0){ // found a suitable space, copy it!
|
2019-11-24 20:56:22 +00:00
|
|
|
memcpy(pool->pool + curpos, egc, len - 1);
|
|
|
|
pool->pool[curpos + len - 1] = '\0';
|
2019-12-01 07:21:13 +00:00
|
|
|
pool->poolwrite = curpos + len;
|
2019-11-24 17:36:46 +00:00
|
|
|
pool->poolused += len;
|
2019-11-24 20:26:01 +00:00
|
|
|
free(duplicated);
|
2019-11-24 17:36:46 +00:00
|
|
|
return curpos;
|
|
|
|
}
|
2019-11-24 20:56:22 +00:00
|
|
|
if(pool->poolwrite > curpos && pool->poolwrite - (len - need) < curpos){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
curpos += len - need;
|
2019-11-24 17:36:46 +00:00
|
|
|
}
|
2019-11-26 02:11:27 +00:00
|
|
|
}while(curpos != pool->poolwrite);
|
2019-11-24 17:36:46 +00:00
|
|
|
}while( (searched = !searched) );
|
2019-11-24 20:26:01 +00:00
|
|
|
free(duplicated);
|
2019-12-01 07:21:13 +00:00
|
|
|
assert(false);
|
2019-11-24 17:36:46 +00:00
|
|
|
return -1; // should never get here
|
|
|
|
}
|
|
|
|
|
2019-11-27 19:43:25 +00:00
|
|
|
// Run a consistency check on the offset; ensure it's a valid, non-empty EGC.
|
|
|
|
static inline bool
|
|
|
|
egcpool_check_validity(const egcpool* pool, int offset){
|
2019-12-01 03:53:24 +00:00
|
|
|
if(offset >= pool->poolsize){
|
2019-12-01 07:21:13 +00:00
|
|
|
fprintf(stderr, "Offset 0x%06x greater than size (%d)\n", offset, pool->poolsize);
|
2019-12-01 03:53:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2019-11-27 19:43:25 +00:00
|
|
|
const char* egc = pool->pool + offset;
|
|
|
|
if(*egc == '\0'){
|
2019-12-01 07:21:13 +00:00
|
|
|
fprintf(stderr, "Bad offset 0x%06x: empty\n", offset);
|
2019-11-27 19:43:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
do{
|
|
|
|
wchar_t wcs;
|
|
|
|
int r = mbtowc(&wcs, egc, strlen(egc));
|
|
|
|
if(r < 0){
|
2019-12-01 07:21:13 +00:00
|
|
|
fprintf(stderr, "Invalid UTF8 at offset 0x%06x, len %zu [%s]\n",
|
2019-11-27 19:43:25 +00:00
|
|
|
offset, strlen(egc), strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
egc += r;
|
|
|
|
}while(*egc);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-11-24 17:36:46 +00:00
|
|
|
// remove the egc from the pool. start at offset, and zero out everything until
|
|
|
|
// we find a zero (our own NUL terminator). remove that number of bytes from
|
|
|
|
// the usedcount.
|
|
|
|
static inline void
|
2019-11-26 02:11:27 +00:00
|
|
|
egcpool_release(egcpool* pool, int offset){
|
2019-11-24 17:36:46 +00:00
|
|
|
size_t freed = 1; // account for free(d) NUL terminator
|
2019-11-27 19:43:25 +00:00
|
|
|
assert(egcpool_check_validity(pool, offset));
|
2019-11-24 17:36:46 +00:00
|
|
|
while(pool->pool[offset]){
|
|
|
|
pool->pool[offset] = '\0';
|
|
|
|
++freed;
|
2019-12-01 07:21:13 +00:00
|
|
|
++offset;
|
|
|
|
assert(offset < pool->poolsize);
|
2019-11-24 17:36:46 +00:00
|
|
|
}
|
|
|
|
pool->poolused -= freed;
|
|
|
|
// FIXME ought we update pool->poolwrite?
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
egcpool_dump(egcpool* pool){
|
|
|
|
free(pool->pool);
|
2019-11-26 00:23:54 +00:00
|
|
|
pool->pool = NULL;
|
2019-11-24 17:36:46 +00:00
|
|
|
pool->poolsize = 0;
|
|
|
|
pool->poolwrite = 0;
|
2019-11-26 00:23:54 +00:00
|
|
|
pool->poolused = 0;
|
2019-11-24 17:36:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|