diff options
author | comex | 2015-08-17 01:48:39 -0400 |
---|---|---|
committer | comex | 2015-08-17 01:48:39 -0400 |
commit | 1099ec5560a3f8e7a44547f657c0eae8bcff17f2 (patch) | |
tree | e16cf06078886c124c3956c80dbc2a85fe26fac6 /lib/cbit/cqueue.h | |
parent | formatting (diff) | |
download | substitute-1099ec5560a3f8e7a44547f657c0eae8bcff17f2.tar.gz |
stuff
Diffstat (limited to 'lib/cbit/cqueue.h')
-rw-r--r-- | lib/cbit/cqueue.h | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/cbit/cqueue.h b/lib/cbit/cqueue.h new file mode 100644 index 0000000..0ab5488 --- /dev/null +++ b/lib/cbit/cqueue.h @@ -0,0 +1,60 @@ +#pragma once +#include "misc.h" + +struct cqueue_internal { + char *start, *end, *read_ptr, *write_ptr; +}; + +#ifdef __cplusplus +extern "C" { +#endif + +void cqueue_realloc_internal(struct cqueue_internal *ci, + size_t new_capacity_bytes); +#ifdef __cplusplus +} +#endif + +#define DECL_CQUEUE(ty, name) \ + typedef ty __CQUEUE_TY_##name; \ + struct cqueue_##name { \ + union { \ + struct cqueue_internal ci; \ + struct { \ + __CQUEUE_TY_##name *start, *end, *read_ptr, *write_ptr; \ + }; \ + }; \ + }; \ + UNUSED_STATIC_INLINE \ + void cqueue_free_storage_##name(struct cqueue_##name *c) { \ + free(c->start); \ + } \ + UNUSED_STATIC_INLINE \ + void cqueue_realloc_##name(struct cqueue_##name *c, size_t new_capacity) { \ + cqueue_realloc_internal(&c->ci, safe_mul(new_capacity, \ + sizeof(__CQUEUE_TY_##name))); \ + } \ + UNUSED_STATIC_INLINE \ + __CQUEUE_TY_##name *cqueue_appendp_##name(struct cqueue_##name *c) { \ + if (c->write_ptr + 1 == c->read_ptr) \ + cqueue_realloc_##name(c, (c->end - c->start) * 2 + 3); \ + __CQUEUE_TY_##name *ret = c->write_ptr++; \ + if (c->write_ptr == c->end) \ + c->write_ptr = c->start; \ + return ret; \ + } \ + UNUSED_STATIC_INLINE \ + __CQUEUE_TY_##name *cqueue_shiftp_##name(struct cqueue_##name *c) { \ + if (c->read_ptr == c->write_ptr) \ + return NULL; \ + __CQUEUE_TY_##name *ret = c->read_ptr++; \ + if (c->read_ptr == c->end) \ + c->read_ptr = c->start; \ + return ret; \ + } \ + UNUSED_STATIC_INLINE \ + size_t cqueue_length_##name(const struct cqueue_name *c) { \ + return ((c->write_ptr - c->start) - (c->read_ptr - c->start)) \ + % (c->end - c->start); \ + } \ + typedef char __plz_end_decl_cqueue_with_semicolon_##name |