skalibs

Mirror/fork of https://skarnet.org/software/skalibs/
git clone https://ccx.te2000.cz/git/skalibs
Log | Files | Refs | README | LICENSE

commit ed40369c8610914085d568ec57701a035bf8dd29
parent a8e40d0287f05f6e444035431b1cc7b340fe8d79
Author: Laurent Bercot <ska-skaware@skarnet.org>
Date:   Fri, 29 Dec 2017 14:16:27 +0000

 Add genqdyn

Diffstat:
Mpackage/deps.mak | 10+++++++++-
Msrc/include/skalibs/datastruct.h | 1+
Asrc/include/skalibs/genqdyn.h | 33+++++++++++++++++++++++++++++++++
Asrc/libdatastruct/genqdyn-internal.h | 10++++++++++
Asrc/libdatastruct/genqdyn_clean.c | 11+++++++++++
Asrc/libdatastruct/genqdyn_free.c | 10++++++++++
Asrc/libdatastruct/genqdyn_init.c | 13+++++++++++++
Asrc/libdatastruct/genqdyn_pop.c | 13+++++++++++++
Asrc/libdatastruct/genqdyn_push.c | 9+++++++++
Asrc/libdatastruct/genqdyn_zero.c | 5+++++
10 files changed, 114 insertions(+), 1 deletion(-)

diff --git a/package/deps.mak b/package/deps.mak @@ -16,7 +16,7 @@ src/include/skalibs/bytestr.h: src/include/skalibs/gccattributes.h src/include/s src/include/skalibs/cbuffer.h: src/include/skalibs/gccattributes.h src/include/skalibs/cdb.h: src/include/skalibs/gccattributes.h src/include/skalibs/cdb_make.h: src/include/skalibs/allreadwrite.h src/include/skalibs/buffer.h src/include/skalibs/diuint32.h src/include/skalibs/genalloc.h -src/include/skalibs/datastruct.h: src/include/skalibs/avlnode.h src/include/skalibs/avltree.h src/include/skalibs/avltreen.h src/include/skalibs/genset.h src/include/skalibs/gensetdyn.h +src/include/skalibs/datastruct.h: src/include/skalibs/avlnode.h src/include/skalibs/avltree.h src/include/skalibs/avltreen.h src/include/skalibs/genqdyn.h src/include/skalibs/genset.h src/include/skalibs/gensetdyn.h src/include/skalibs/djbtime.h: src/include/skalibs/tai.h src/include/skalibs/uint64.h src/include/skalibs/djbunix.h: src/include/skalibs/env.h src/include/skalibs/envalloc.h src/include/skalibs/gccattributes.h src/include/skalibs/posixplz.h src/include/skalibs/stralloc.h src/include/skalibs/env.h: src/include/skalibs/gccattributes.h src/include/skalibs/stralloc.h @@ -24,6 +24,7 @@ src/include/skalibs/envalloc.h: src/include/skalibs/genalloc.h src/include/skalibs/environ.h: src/include/skalibs/posixplz.h src/include/skalibs/fmtscan.h: src/include/skalibs/gccattributes.h src/include/skalibs/genalloc.h: src/include/skalibs/functypes.h src/include/skalibs/stralloc.h +src/include/skalibs/genqdyn.h: src/include/skalibs/stralloc.h src/include/skalibs/genset.h: src/include/skalibs/functypes.h src/include/skalibs/gensetdyn.h: src/include/skalibs/functypes.h src/include/skalibs/genalloc.h src/include/skalibs/stralloc.h src/include/skalibs/getpeereid.h: src/include/skalibs/posixplz.h @@ -57,6 +58,7 @@ src/include/skalibs/unixmessage.h: src/include/skalibs/buffer.h src/include/skal src/include/skalibs/unixonacid.h: src/include/skalibs/kolbak.h src/include/skalibs/skaclient.h src/include/skalibs/textclient.h src/include/skalibs/textmessage.h src/include/skalibs/unix-timed.h src/include/skalibs/unix-transactional.h src/include/skalibs/unixconnection.h src/include/skalibs/unixmessage.h src/include/skalibs/webipc.h: src/include/skalibs/djbunix.h src/include/skalibs/posixplz.h src/include/skalibs/tai.h src/libdatastruct/avlnode-internal.h: src/include/skalibs/avlnode.h +src/libdatastruct/genqdyn-internal.h: src/include/skalibs/genqdyn.h src/librandom/random-internal.h: src/include/skalibs/surf.h src/libstdcrypto/md5-internal.h: src/include/skalibs/md5.h src/libstdcrypto/sha1-internal.h: src/include/skalibs/sha1.h @@ -117,6 +119,12 @@ src/libdatastruct/avltreen_delete.o src/libdatastruct/avltreen_delete.lo: src/li src/libdatastruct/avltreen_init.o src/libdatastruct/avltreen_init.lo: src/libdatastruct/avltreen_init.c src/include/skalibs/avlnode.h src/include/skalibs/avltreen.h src/include/skalibs/genset.h src/libdatastruct/avltreen_insert.o src/libdatastruct/avltreen_insert.lo: src/libdatastruct/avltreen_insert.c src/include/skalibs/avltreen.h src/libdatastruct/avltreen_newnode.o src/libdatastruct/avltreen_newnode.lo: src/libdatastruct/avltreen_newnode.c src/include/skalibs/avlnode.h src/include/skalibs/avltreen.h src/include/skalibs/genset.h +src/libdatastruct/genqdyn_clean.o src/libdatastruct/genqdyn_clean.lo: src/libdatastruct/genqdyn_clean.c src/libdatastruct/genqdyn-internal.h +src/libdatastruct/genqdyn_free.o src/libdatastruct/genqdyn_free.lo: src/libdatastruct/genqdyn_free.c src/include/skalibs/genqdyn.h src/include/skalibs/stralloc.h +src/libdatastruct/genqdyn_init.o src/libdatastruct/genqdyn_init.lo: src/libdatastruct/genqdyn_init.c src/include/skalibs/genqdyn.h src/include/skalibs/stralloc.h +src/libdatastruct/genqdyn_pop.o src/libdatastruct/genqdyn_pop.lo: src/libdatastruct/genqdyn_pop.c src/libdatastruct/genqdyn-internal.h src/include/skalibs/genqdyn.h +src/libdatastruct/genqdyn_push.o src/libdatastruct/genqdyn_push.lo: src/libdatastruct/genqdyn_push.c src/include/skalibs/genqdyn.h src/include/skalibs/stralloc.h +src/libdatastruct/genqdyn_zero.o src/libdatastruct/genqdyn_zero.lo: src/libdatastruct/genqdyn_zero.c src/include/skalibs/genqdyn.h src/libdatastruct/genset.o src/libdatastruct/genset.lo: src/libdatastruct/genset.c src/include/skalibs/genset.h src/libdatastruct/genset_iter_nocancel.o src/libdatastruct/genset_iter_nocancel.lo: src/libdatastruct/genset_iter_nocancel.c src/include/skalibs/bitarray.h src/include/skalibs/genset.h src/libdatastruct/genset_iter_withcancel.o src/libdatastruct/genset_iter_withcancel.lo: src/libdatastruct/genset_iter_withcancel.c src/include/skalibs/genset.h diff --git a/src/include/skalibs/datastruct.h b/src/include/skalibs/datastruct.h @@ -3,6 +3,7 @@ #ifndef DATASTRUCT_H #define DATASTRUCT_H +#include <skalibs/genqdyn.h> #include <skalibs/genset.h> #include <skalibs/gensetdyn.h> #include <skalibs/avlnode.h> diff --git a/src/include/skalibs/genqdyn.h b/src/include/skalibs/genqdyn.h @@ -0,0 +1,33 @@ +/* ISC license. */ + +#ifndef SKALIBS_GENQDYN_H +#define SKALIBS_GENQDYN_H + +#include <sys/types.h> +#include <skalibs/stralloc.h> + +typedef struct genqdyn_s genqdyn, *genqdyn_ref ; +struct genqdyn_s +{ + stralloc queue ; + size_t esize ; + size_t head ; + unsigned int num ; + unsigned int den ; +} ; + +#define GENQDYN_ZERO { .queue = STRALLOC_ZERO, .esize = 1, .head = 0, .num = 0, .den = 1 } +extern genqdyn const genqdyn_zero ; + +#define GENQDYN_INIT(type, n, d) { .queue = STRALLOC_ZERO, .esize = sizeof(type), .head = 0, .num = n, .den = d } +extern void genqdyn_init (genqdyn *, size_t, unsigned int, unsigned int) ; + +#define genqdyn_n(g) ((g)->queue.len / (g)->esize - (g)->head) ; + +extern void genqdyn_free (genqdyn *) ; +extern int genqdyn_push (genqdyn *, void const *) ; +#define GENQDYN_PEEK(type, g) ((type *)((g)->queue.s + (g)->esize * (g)->head)) +#define genqdyn_peek(g) GENQDYN_PEEK(void, (g)) +extern int genqdyn_pop(genqdyn *) ; + +#endif diff --git a/src/libdatastruct/genqdyn-internal.h b/src/libdatastruct/genqdyn-internal.h @@ -0,0 +1,10 @@ +/* ISC license. */ + +#ifndef SKALIBS_GENQDYN_INTERNAL_H +#define SKALIBS_GENQDYN_INTERNAL_H + +#include <skalibs/genqdyn.h> + +extern void genqdyn_clean (genqdyn *) ; + +#endif diff --git a/src/libdatastruct/genqdyn_clean.c b/src/libdatastruct/genqdyn_clean.c @@ -0,0 +1,11 @@ +/* ISC license. */ + +#include <string.h> +#include "genqdyn-internal.h" + +void genqdyn_clean (genqdyn *g) +{ + memmove(g->queue.s, g->queue.s + g->head, g->queue.len - g->head) ; + g->queue.len -= g->head ; + g->head = 0 ; +} diff --git a/src/libdatastruct/genqdyn_free.c b/src/libdatastruct/genqdyn_free.c @@ -0,0 +1,10 @@ +/* ISC license. */ + +#include <skalibs/stralloc.h> +#include <skalibs/genqdyn.h> + +void genqdyn_free (genqdyn *g) +{ + stralloc_free(&g->queue) ; + *g = genqdyn_zero ; +} diff --git a/src/libdatastruct/genqdyn_init.c b/src/libdatastruct/genqdyn_init.c @@ -0,0 +1,13 @@ +/* ISC license. */ + +#include <skalibs/stralloc.h> +#include <skalibs/genqdyn.h> + +void genqdyn_init (genqdyn *g, size_t esize, unsigned int num, unsigned int den) +{ + g->queue = stralloc_zero ; + g->esize = esize ; + g->head = 0 ; + g->num = num ; + g->den = den ; +} diff --git a/src/libdatastruct/genqdyn_pop.c b/src/libdatastruct/genqdyn_pop.c @@ -0,0 +1,13 @@ +/* ISC license. */ + +#include <errno.h> +#include <skalibs/genqdyn.h> +#include "genqdyn-internal.h" + +int genqdyn_pop (genqdyn *g) +{ + if (g->head >= g->queue.len) return (errno = EINVAL, 0) ; + g->head += g->esize ; + if (g->den * (g->queue.len - g->head) <= g->num * g->queue.len) genqdyn_clean(g) ; + return 1 ; +} diff --git a/src/libdatastruct/genqdyn_push.c b/src/libdatastruct/genqdyn_push.c @@ -0,0 +1,9 @@ +/* ISC license. */ + +#include <skalibs/stralloc.h> +#include <skalibs/genqdyn.h> + +int genqdyn_push (genqdyn *g, void const *p) +{ + return stralloc_catb(&g->queue, (char const *)p, g->esize) ; +} diff --git a/src/libdatastruct/genqdyn_zero.c b/src/libdatastruct/genqdyn_zero.c @@ -0,0 +1,5 @@ +/* ISC license. */ + +#include <skalibs/genqdyn.h> + +genqdyn const genqdyn_zero = GENQDYN_ZERO ;