From d08e2bba951c654cd82c6323aed6cae3fab337fc Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Wed, 6 Sep 2017 21:05:57 +0200 Subject: [PATCH] Add I/O for deque --- m-deque.h | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- m-shared.h | 12 +++++----- 2 files changed, 71 insertions(+), 7 deletions(-) diff --git a/m-deque.h b/m-deque.h index dd7bd108..a944a204 100644 --- a/m-deque.h +++ b/m-deque.h @@ -644,10 +644,72 @@ DEQUEI_CONTRACT(d); \ } \ , /* NO SWAP: TODO */) \ + \ + M_IF_METHOD(GET_STR, oplist)( \ + static inline void \ + M_C(name, _get_str)(string_t str, deque_t deque, bool append) \ + { \ + STRING_CONTRACT(str); \ + DEQUEI_CONTRACT(deque); \ + (append ? string_cat_str : string_set_str) (str, "["); \ + it_t it; \ + for (M_C(name, _it)(it, deque) ; \ + !M_C(name, _end_p)(it); \ + M_C(name, _next)(it)){ \ + const type *item = M_C(name, _cref)(it); \ + M_GET_GET_STR oplist (str, *item, true); \ + if (!M_C(name, _last_p)(it)) \ + string_push_back (str, M_GET_SEPARATOR oplist); \ + } \ + string_push_back (str, ']'); \ + STRING_CONTRACT(str); \ + } \ + , /* no GET_STR */ ) \ + \ + M_IF_METHOD(OUT_STR, oplist)( \ + static inline void \ + M_C(name, _out_str)(FILE *file, const deque_t deque) \ + { \ + DEQUEI_CONTRACT(deque); \ + assert (file != NULL); \ + fputc ('[', file); \ + for (size_t i = 0; i < deque->size; i++) { \ + const type *item = M_C(name, _cget)(deque, i); \ + M_GET_OUT_STR oplist (file, *item); \ + if (i != deque->size-1) \ + fputc (M_GET_SEPARATOR oplist, file); \ + } \ + fputc (']', file); \ + } \ + , /* no OUT_STR */ ) \ + \ + M_IF_METHOD(IN_STR, oplist)( \ + static inline bool \ + M_C(name, _in_str)(deque_t deque, FILE *file) \ + { \ + DEQUEI_CONTRACT(deque); \ + assert (file != NULL); \ + M_C(name,_clean)(deque); \ + int c = fgetc(file); \ + if (c != '[') return false; \ + c = fgetc(file); \ + if (c == ']') return true; \ + ungetc(c, file); \ + type item; \ + M_GET_INIT oplist (item); \ + do { \ + bool b = M_GET_IN_STR oplist (item, file); \ + c = fgetc(file); \ + if (b == false || c == EOF) { break; } \ + M_C(name, _push_back)(deque, item); \ + } while (c == M_GET_SEPARATOR oplist); \ + M_GET_CLEAR oplist (item); \ + DEQUEI_CONTRACT(deque); \ + return c == ']'; \ + } \ + , /* no IN_STR */ ) \ -// TODO: IO [like array] - #define DEQUEI_OPLIST(name, oplist) \ (INIT(M_C(name, _init)) \ ,INIT_SET(M_C(name, _init_set)) \ diff --git a/m-shared.h b/m-shared.h index d642c865..435344da 100644 --- a/m-shared.h +++ b/m-shared.h @@ -66,8 +66,8 @@ #define SHAREDI_PTR_DEF2(name, type, oplist) \ \ typedef struct M_C(name, _s){ \ - type *data; /* Pointer to the data */ \ - atomic_int cpt; /* Counter of how many points to the data */ \ + type *data; /* Pointer to the data */ \ + atomic_int cpt; /* Counter of how many points to the data */ \ bool combineAlloc; /* Does the data and the ptr share the slot? */ \ } *M_C(name, _t)[1]; \ \ @@ -85,13 +85,15 @@ static inline void \ M_C(name, _init2)(M_C(name, _t) shared, type *data) \ { \ + assert (shared != NULL); \ + /* The shared ptr get exclusive access to data */ \ struct M_C(name, _s) *ptr; \ - if (data == NULL) { \ + if (M_UNLIKELY (data == NULL)) { \ *shared = NULL; \ return; \ } \ ptr = M_GET_NEW oplist (struct M_C(name, _s)); \ - if (ptr == NULL) { \ + if (M_UNLIKELY (ptr == NULL)) { \ M_MEMORY_FULL(sizeof(struct M_C(name, _s))); \ return; \ } \ @@ -108,7 +110,7 @@ /* NOTE: Alloc 1 struct with both structures. */ \ struct M_C(name, combine_s) *p = \ M_GET_NEW oplist (struct M_C(name, combine_s)); \ - if (p == NULL) { \ + if (M_UNLIKELY (p == NULL)) { \ M_MEMORY_FULL(sizeof(struct M_C(name, combine_s))); \ return; \ } \