#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#ifndef _WIN32
# include <unistd.h>
# include <stdint.h>
#endif
#ifndef SIMCLIST_NO_DUMPRESTORE
# include <time.h>
# include <sys/uio.h>
# include <fcntl.h>
# ifndef _WIN32
# include <arpa/inet.h>
# else
# include <winsock2.h>
# endif
#endif
#ifndef SIMCLIST_DEBUG
#ifndef NDEBUG
#define NDEBUG
#endif
#endif
#include <assert.h>
#include <sys/stat.h>
#include <limits.h>
#if defined(_MSC_VER) || defined(__MINGW32__)
int gettimeofday(struct timeval *tp, void *tzp) {
DWORD t;
assert(tzp == NULL);
t = timeGetTime();
tp->tv_sec = t / 1000;
tp->tv_usec = t % 1000;
return 0;
}
#endif
#if !defined(_WIN32) || !defined(_MSC_VER)
# include <inttypes.h>
#else
# include <basetsd.h>
typedef UINT8 uint8_t;
typedef UINT16 uint16_t;
typedef ULONG32 uint32_t;
typedef UINT64 uint64_t;
typedef INT8 int8_t;
typedef INT16 int16_t;
typedef LONG32 int32_t;
typedef INT64 int64_t;
#endif
#ifndef SIMCLIST_NO_DUMPRESTORE
#define WRITE_ERRCHECK(fd, msgbuf, msglen) do { \
if (write(fd, msgbuf, msglen) < 0) return -1; \
} while (0);
#define READ_ERRCHECK(fd, msgbuf, msglen) do { \
if (read(fd, msgbuf, msglen) != msglen) { \
\
return -1; \
} \
} while (0);
#define hton64(x) (\
htons(1) == 1 ? \
(uint64_t)x \
: \
((uint64_t)((((uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \
(((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
(((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
(((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
(((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
(((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
(((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
(((uint64_t)(x) & 0x00000000000000ffULL) << 56))) \
)
#define ntoh64(x) (hton64(x))
#endif
#ifndef EPROTO
#define EPROTO EIO
#endif
#ifdef SIMCLIST_WITH_THREADS
#define SIMCLIST_MAXTHREADS 2
#endif
#ifndef SIMCLIST_MAX_SPARE_ELEMS
#define SIMCLIST_MAX_SPARE_ELEMS 5
#endif
#ifdef SIMCLIST_WITH_THREADS
#include <pthread.h>
#endif
#include "simclist.h"
#define SIMCLIST_MINQUICKSORTELS 24
#define SIMCLIST_DUMPFORMAT_VERSION 1
#define SIMCLIST_DUMPFORMAT_HEADERLEN 30
struct list_dump_header_s {
uint16_t ver;
int32_t timestamp_sec;
int32_t timestamp_usec;
int32_t rndterm;
uint32_t totlistlen;
uint32_t numels;
uint32_t elemlen;
int32_t listhash;
};
static int list_drop_elem(list_t *restrict l, struct list_entry_s *tmp, unsigned int pos);
static int list_attributes_setdefaults(list_t *restrict l);
#ifndef NDEBUG
static int list_repOk(const list_t *restrict l);
static int list_attrOk(const list_t *restrict l);
#endif
static void list_sort_quicksort(list_t *restrict l, int versus,
unsigned int first, struct list_entry_s *fel,
unsigned int last, struct list_entry_s *lel);
static inline void list_sort_selectionsort(list_t *restrict l, int versus,
unsigned int first, struct list_entry_s *fel,
unsigned int last, struct list_entry_s *lel);
static void *list_get_minmax(const list_t *restrict l, int versus);
static inline struct list_entry_s *list_findpos(const list_t *restrict l, int posstart);
#ifdef SIMCLIST_SYSTEM_RNG
static unsigned random_seed = 0;
static inline void seed_random(void) {
if (random_seed == 0)
random_seed = (unsigned)getpid() ^ (unsigned)time(NULL);
}
static inline long get_random(void) {
random_seed = (1664525 * random_seed + 1013904223);
return random_seed;
}
#else
# define seed_random()
# define get_random() (rand())
#endif
int list_init(list_t *restrict l) {
if (l == NULL) return -1;
memset(l, 0, sizeof *l);
seed_random();
l->numels = 0;
l->head_sentinel = (struct list_entry_s *)malloc(sizeof(struct list_entry_s));
l->tail_sentinel = (struct list_entry_s *)malloc(sizeof(struct list_entry_s));
if (NULL == l->tail_sentinel || NULL == l->head_sentinel)
return -1;
l->head_sentinel->next = l->tail_sentinel;
l->tail_sentinel->prev = l->head_sentinel;
l->head_sentinel->prev = l->tail_sentinel->next = l->mid = NULL;
l->head_sentinel->data = l->tail_sentinel->data = NULL;
l->iter_active = 0;
l->iter_pos = 0;
l->iter_curentry = NULL;
l->spareels = (struct list_entry_s **)malloc(SIMCLIST_MAX_SPARE_ELEMS * sizeof(struct list_entry_s *));
l->spareelsnum = 0;
if (NULL == l->spareels)
return -1;
#ifdef SIMCLIST_WITH_THREADS
l->threadcount = 0;
#endif
if (list_attributes_setdefaults(l))
return -1;
assert(list_repOk(l));
assert(list_attrOk(l));
return 0;
}
void list_destroy(list_t *restrict l) {
unsigned int i;
list_clear(l);
for (i = 0; i < l->spareelsnum; i++) {
free(l->spareels[i]);
}
free(l->spareels);
free(l->head_sentinel);
free(l->tail_sentinel);
}
int list_attributes_setdefaults(list_t *restrict l) {
l->attrs.comparator = NULL;
l->attrs.seeker = NULL;
l->attrs.meter = NULL;
l->attrs.copy_data = 0;
l->attrs.hasher = NULL;
l->attrs.serializer = NULL;
l->attrs.unserializer = NULL;
assert(list_attrOk(l));
return 0;
}
int list_attributes_comparator(list_t *restrict l, element_comparator comparator_fun) {
if (l == NULL) return -1;
l->attrs.comparator = comparator_fun;
assert(list_attrOk(l));
return 0;
}
int list_attributes_seeker(list_t *restrict l, element_seeker seeker_fun) {
if (l == NULL) return -1;
l->attrs.seeker = seeker_fun;
assert(list_attrOk(l));
return 0;
}
int list_attributes_copy(list_t *restrict l, element_meter metric_fun, int copy_data) {
if (l == NULL || (metric_fun == NULL && copy_data != 0)) return -1;
l->attrs.meter = metric_fun;
l->attrs.copy_data = copy_data;
assert(list_attrOk(l));
return 0;
}
int list_attributes_hash_computer(list_t *restrict l, element_hash_computer hash_computer_fun) {
if (l == NULL) return -1;
l->attrs.hasher = hash_computer_fun;
assert(list_attrOk(l));
return 0;
}
int list_attributes_serializer(list_t *restrict l, element_serializer serializer_fun) {
if (l == NULL) return -1;
l->attrs.serializer = serializer_fun;
assert(list_attrOk(l));
return 0;
}
int list_attributes_unserializer(list_t *restrict l, element_unserializer unserializer_fun) {
if (l == NULL) return -1;
l->attrs.unserializer = unserializer_fun;
assert(list_attrOk(l));
return 0;
}
int list_append(list_t *restrict l, const void *data) {
return list_insert_at(l, data, l->numels);
}
int list_prepend(list_t *restrict l, const void *data) {
return list_insert_at(l, data, 0);
}
void *list_fetch(list_t *restrict l) {
return list_extract_at(l, 0);
}
void *list_get_at(const list_t *restrict l, unsigned int pos) {
struct list_entry_s *tmp;
tmp = list_findpos(l, pos);
return (tmp != NULL ? tmp->data : NULL);
}
void *list_get_max(const list_t *restrict l) {
return list_get_minmax(l, +1);
}
void *list_get_min(const list_t *restrict l) {
return list_get_minmax(l, -1);
}
static void *list_get_minmax(const list_t *restrict l, int versus) {
void *curminmax;
struct list_entry_s *s;
if (l->attrs.comparator == NULL || l->numels == 0)
return NULL;
curminmax = l->head_sentinel->next->data;
for (s = l->head_sentinel->next->next; s != l->tail_sentinel; s = s->next) {
if (l->attrs.comparator(curminmax, s->data) * versus > 0)
curminmax = s->data;
}
return curminmax;
}
static inline struct list_entry_s *list_findpos(const list_t *restrict l, int posstart) {
struct list_entry_s *ptr;
float x;
int i;
if (NULL == l->head_sentinel || NULL == l->tail_sentinel)
return NULL;
if (posstart < -1 || posstart > (int)l->numels) return NULL;
if( l->numels != 0 )
x = (float)(posstart+1) / l->numels;
else
x = 1;
if (x <= 0.25) {
for (i = -1, ptr = l->head_sentinel; i < posstart; ptr = ptr->next, i++);
} else if (x < 0.5) {
for (i = (l->numels-1)/2, ptr = l->mid; i > posstart; ptr = ptr->prev, i--);
} else if (x <= 0.75) {
for (i = (l->numels-1)/2, ptr = l->mid; i < posstart; ptr = ptr->next, i++);
} else {
for (i = l->numels, ptr = l->tail_sentinel; i > posstart; ptr = ptr->prev, i--);
}
return ptr;
}
void *list_extract_at(list_t *restrict l, unsigned int pos) {
struct list_entry_s *tmp;
void *data;
if (l->iter_active || pos >= l->numels) return NULL;
tmp = list_findpos(l, pos);
if (NULL == tmp)
return NULL;
data = tmp->data;
tmp->data = NULL;
list_drop_elem(l, tmp, pos);
l->numels--;
assert(list_repOk(l));
return data;
}
int list_insert_at(list_t *restrict l, const void *data, unsigned int pos) {
struct list_entry_s *lent, *succ, *prec;
if (l->iter_active || pos > l->numels) return -1;
if (l->spareelsnum > 0) {
lent = l->spareels[l->spareelsnum-1];
l->spareelsnum--;
} else {
lent = (struct list_entry_s *)malloc(sizeof(struct list_entry_s));
if (lent == NULL)
return -1;
}
if (l->attrs.copy_data) {
size_t datalen = l->attrs.meter(data);
lent->data = (struct list_entry_s *)malloc(datalen);
if (NULL == lent->data)
{
free(lent);
return -1;
}
memcpy(lent->data, data, datalen);
} else {
lent->data = (void*)data;
}
prec = list_findpos(l, pos-1);
if (NULL == prec)
{
free(lent->data);
free(lent);
return -1;
}
succ = prec->next;
prec->next = lent;
lent->prev = prec;
lent->next = succ;
succ->prev = lent;
l->numels++;
if (l->numels == 1) {
l->mid = lent;
} else if (l->numels % 2) {
if (pos >= (l->numels-1)/2) l->mid = l->mid->next;
} else {
if (pos <= (l->numels-1)/2) l->mid = l->mid->prev;
}
assert(list_repOk(l));
return 1;
}
int list_delete(list_t *restrict l, const void *data) {
int pos, r;
pos = list_locate(l, data);
if (pos < 0)
return -1;
r = list_delete_at(l, pos);
if (r < 0)
return -1;
assert(list_repOk(l));
return 0;
}
int list_delete_at(list_t *restrict l, unsigned int pos) {
struct list_entry_s *delendo;
if (l->iter_active || pos >= l->numels) return -1;
delendo = list_findpos(l, pos);
list_drop_elem(l, delendo, pos);
l->numels--;
assert(list_repOk(l));
return 0;
}
int list_delete_range(list_t *restrict l, unsigned int posstart, unsigned int posend) {
struct list_entry_s *lastvalid, *tmp, *tmp2;
unsigned int numdel, midposafter, i;
int movedx;
if (l->iter_active || posend < posstart || posend >= l->numels) return -1;
numdel = posend - posstart + 1;
if (numdel == l->numels) return list_clear(l);
tmp = list_findpos(l, posstart);
lastvalid = tmp->prev;
midposafter = (l->numels-1-numdel)/2;
midposafter = midposafter < posstart ? midposafter : midposafter+numdel;
movedx = midposafter - (l->numels-1)/2;
if (movedx > 0) {
for (i = 0; i < (unsigned int)movedx; l->mid = l->mid->next, i++);
} else {
movedx = -movedx;
for (i = 0; i < (unsigned int)movedx; l->mid = l->mid->prev, i++);
}
assert(posstart == 0 || lastvalid != l->head_sentinel);
i = posstart;
if (l->attrs.copy_data) {
for (; i <= posend; i++) {
tmp2 = tmp;
tmp = tmp->next;
if (tmp2->data != NULL) free(tmp2->data);
if (l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
l->spareels[l->spareelsnum++] = tmp2;
} else {
free(tmp2);
}
}
} else {
for (; i <= posend; i++) {
tmp2 = tmp;
tmp = tmp->next;
if (l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
l->spareels[l->spareelsnum++] = tmp2;
} else {
free(tmp2);
}
}
}
assert(i == posend+1 && (posend != l->numels || tmp == l->tail_sentinel));
lastvalid->next = tmp;
tmp->prev = lastvalid;
l->numels -= posend - posstart + 1;
assert(list_repOk(l));
return numdel;
}
int list_clear(list_t *restrict l) {
struct list_entry_s *s;
unsigned int numels;
numels = l->numels;
if (l->iter_active) return -1;
if (l->head_sentinel && l->tail_sentinel) {
if (l->attrs.copy_data) {
for (s = l->head_sentinel->next; l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS && s != l->tail_sentinel; s = s->next) {
if (s->data != NULL) free(s->data);
l->spareels[l->spareelsnum++] = s;
}
while (s != l->tail_sentinel) {
if (s->data != NULL) free(s->data);
s = s->next;
free(s->prev);
}
l->head_sentinel->next = l->tail_sentinel;
l->tail_sentinel->prev = l->head_sentinel;
} else {
for (s = l->head_sentinel->next; l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS && s != l->tail_sentinel; s = s->next) {
l->spareels[l->spareelsnum++] = s;
}
while (s != l->tail_sentinel) {
s = s->next;
free(s->prev);
}
l->head_sentinel->next = l->tail_sentinel;
l->tail_sentinel->prev = l->head_sentinel;
}
}
l->numels = 0;
l->mid = NULL;
assert(list_repOk(l));
return numels;
}
unsigned int list_size(const list_t *restrict l) {
return l->numels;
}
int list_empty(const list_t *restrict l) {
return (l->numels == 0);
}
int list_locate(const list_t *restrict l, const void *data) {
struct list_entry_s *el;
int pos = 0;
if (NULL == l->head_sentinel || NULL == l->tail_sentinel)
return -1;
if (l->attrs.comparator != NULL) {
for (el = l->head_sentinel->next; el != l->tail_sentinel; el = el->next, pos++) {
if (l->attrs.comparator(data, el->data) == 0) break;
}
} else {
for (el = l->head_sentinel->next; el != l->tail_sentinel; el = el->next, pos++) {
if (el->data == data) break;
}
}
if (el == l->tail_sentinel) return -1;
return pos;
}
void *list_seek(list_t *restrict l, const void *indicator) {
const struct list_entry_s *iter;
if (l->attrs.seeker == NULL) return NULL;
if (NULL == l->head_sentinel || NULL == l->tail_sentinel)
return NULL;
for (iter = l->head_sentinel->next; iter != l->tail_sentinel; iter = iter->next) {
if (l->attrs.seeker(iter->data, indicator) != 0) return iter->data;
}
return NULL;
}
int list_contains(const list_t *restrict l, const void *data) {
return (list_locate(l, data) >= 0);
}
int list_concat(const list_t *l1, const list_t *l2, list_t *restrict dest) {
struct list_entry_s *el, *srcel;
unsigned int cnt;
int err;
if (l1 == NULL || l2 == NULL || dest == NULL || l1 == dest || l2 == dest)
return -1;
if (NULL == l1->head_sentinel || NULL == l1->tail_sentinel
|| NULL == l2->head_sentinel || NULL == l2->tail_sentinel)
return -1;
if (list_init(dest))
return -1;
dest->numels = l1->numels + l2->numels;
if (dest->numels == 0)
return 0;
srcel = l1->head_sentinel->next;
el = dest->head_sentinel;
while (srcel != l1->tail_sentinel) {
el->next = (struct list_entry_s *)malloc(sizeof(struct list_entry_s));
if (NULL == el->next)
return -1;
el->next->prev = el;
el = el->next;
el->data = srcel->data;
srcel = srcel->next;
}
dest->mid = el;
srcel = l2->head_sentinel->next;
while (srcel != l2->tail_sentinel) {
el->next = (struct list_entry_s *)malloc(sizeof(struct list_entry_s));
if (NULL == el->next)
return -1;
el->next->prev = el;
el = el->next;
el->data = srcel->data;
srcel = srcel->next;
}
el->next = dest->tail_sentinel;
dest->tail_sentinel->prev = el;
err = l2->numels - l1->numels;
if ((err+1)/2 > 0) {
err = (err+1)/2;
for (cnt = 0; cnt < (unsigned int)err; cnt++) dest->mid = dest->mid->next;
} else if (err/2 < 0) {
err = -err/2;
for (cnt = 0; cnt < (unsigned int)err; cnt++) dest->mid = dest->mid->prev;
}
assert(!(list_repOk(l1) && list_repOk(l2)) || list_repOk(dest));
return 0;
}
int list_sort(list_t *restrict l, int versus) {
if (l->iter_active || l->attrs.comparator == NULL)
return -1;
if (l->numels <= 1)
return 0;
if (NULL == l->head_sentinel || NULL == l->tail_sentinel)
return -1;
list_sort_quicksort(l, versus, 0, l->head_sentinel->next, l->numels-1, l->tail_sentinel->prev);
assert(list_repOk(l));
return 0;
}
#ifdef SIMCLIST_WITH_THREADS
struct list_sort_wrappedparams {
list_t *restrict l;
int versus;
unsigned int first, last;
struct list_entry_s *fel, *lel;
};
static void *list_sort_quicksort_threadwrapper(void *wrapped_params) {
struct list_sort_wrappedparams *wp = (struct list_sort_wrappedparams *)wrapped_params;
list_sort_quicksort(wp->l, wp->versus, wp->first, wp->fel, wp->last, wp->lel);
free(wp);
pthread_exit(NULL);
return NULL;
}
#endif
static inline void list_sort_selectionsort(list_t *restrict l, int versus,
unsigned int first, struct list_entry_s *fel,
unsigned int last, struct list_entry_s *lel) {
struct list_entry_s *cursor, *toswap, *firstunsorted;
void *tmpdata;
if (last <= first)
return;
for (firstunsorted = fel; firstunsorted != lel; firstunsorted = firstunsorted->next) {
for (toswap = firstunsorted, cursor = firstunsorted->next; cursor != lel->next; cursor = cursor->next)
if (l->attrs.comparator(toswap->data, cursor->data) * -versus > 0) toswap = cursor;
if (toswap != firstunsorted) {
tmpdata = firstunsorted->data;
firstunsorted->data = toswap->data;
toswap->data = tmpdata;
}
}
}
static void list_sort_quicksort(list_t *restrict l, int versus,
unsigned int first, struct list_entry_s *fel,
unsigned int last, struct list_entry_s *lel) {
unsigned int pivotid;
unsigned int i;
register struct list_entry_s *pivot;
struct list_entry_s *left, *right;
void *tmpdata;
#ifdef SIMCLIST_WITH_THREADS
pthread_t tid;
int traised;
#endif
if (last <= first)
return;
if (last - first+1 <= SIMCLIST_MINQUICKSORTELS) {
list_sort_selectionsort(l, versus, first, fel, last, lel);
return;
}
if (! (last > first)) return;
pivotid = (get_random() % (last - first + 1));
if (pivotid < (last - first + 1)/2) {
for (i = 0, pivot = fel; i < pivotid; pivot = pivot->next, i++);
} else {
for (i = last - first, pivot = lel; i > pivotid; pivot = pivot->prev, i--);
}
left = fel;
right = lel;
while (left != pivot && right != pivot) {
for (; left != pivot && (l->attrs.comparator(left->data, pivot->data) * -versus <= 0); left = left->next);
for (; right != pivot && (l->attrs.comparator(right->data, pivot->data) * -versus >= 0); right = right->prev);
if (left != pivot && right != pivot) {
tmpdata = left->data;
left->data = right->data;
right->data = tmpdata;
left = left->next;
right = right->prev;
}
}
if (right == pivot) {
while (left != pivot) {
if (l->attrs.comparator(left->data, pivot->data) * -versus > 0) {
tmpdata = left->data;
left->data = pivot->prev->data;
pivot->prev->data = pivot->data;
pivot->data = tmpdata;
pivot = pivot->prev;
pivotid--;
if (pivot == left) break;
} else {
left = left->next;
}
}
} else {
while (right != pivot) {
if (l->attrs.comparator(right->data, pivot->data) * -versus < 0) {
tmpdata = right->data;
right->data = pivot->next->data;
pivot->next->data = pivot->data;
pivot->data = tmpdata;
pivot = pivot->next;
pivotid++;
if (pivot == right) break;
} else {
right = right->prev;
}
}
}
#ifdef SIMCLIST_WITH_THREADS
traised = 0;
if (pivotid > 0) {
if (l->threadcount < SIMCLIST_MAXTHREADS-1) {
struct list_sort_wrappedparams *wp = (struct list_sort_wrappedparams *)malloc(sizeof(struct list_sort_wrappedparams));
if (NULL == wp)
return -1;
l->threadcount++;
traised = 1;
wp->l = l;
wp->versus = versus;
wp->first = first;
wp->fel = fel;
wp->last = first+pivotid-1;
wp->lel = pivot->prev;
if (pthread_create(&tid, NULL, list_sort_quicksort_threadwrapper, wp) != 0) {
free(wp);
traised = 0;
list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
}
} else {
list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
}
}
if (first + pivotid < last) list_sort_quicksort(l, versus, first+pivotid+1, pivot->next, last, lel);
if (traised) {
pthread_join(tid, (void **)NULL);
l->threadcount--;
}
#else
if (pivotid > 0) list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
if (first + pivotid < last) list_sort_quicksort(l, versus, first+pivotid+1, pivot->next, last, lel);
#endif
}
int list_iterator_start(list_t *restrict l) {
if (l->iter_active) return 0;
if (NULL == l->head_sentinel)
return -1;
l->iter_pos = 0;
l->iter_active = 1;
l->iter_curentry = l->head_sentinel->next;
return 1;
}
void *list_iterator_next(list_t *restrict l) {
void *toret;
if (! l->iter_active) return NULL;
toret = l->iter_curentry->data;
l->iter_curentry = l->iter_curentry->next;
l->iter_pos++;
return toret;
}
int list_iterator_hasnext(const list_t *restrict l) {
if (! l->iter_active) return 0;
return (l->iter_pos < l->numels);
}
int list_iterator_stop(list_t *restrict l) {
if (! l->iter_active) return 0;
l->iter_pos = 0;
l->iter_active = 0;
return 1;
}
int list_hash(const list_t *restrict l, list_hash_t *restrict hash) {
struct list_entry_s *x;
list_hash_t tmphash;
assert(hash != NULL);
tmphash = l->numels * 2 + 100;
if (l->attrs.hasher == NULL) {
#ifdef SIMCLIST_ALLOW_LOCATIONBASED_HASHES
#warning "Memlocation-based hash is consistent only for testing modification in the same program run."
int i;
for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
for (i = 0; i < sizeof(x->data); i++) {
tmphash += (tmphash ^ (uintptr_t)x->data);
}
tmphash += tmphash % l->numels;
}
#else
return -1;
#endif
} else {
for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
tmphash += tmphash ^ l->attrs.hasher(x->data);
tmphash += tmphash % l->numels;
}
}
*hash = tmphash;
return 0;
}
#ifndef SIMCLIST_NO_DUMPRESTORE
int list_dump_getinfo_filedescriptor(int fd, list_dump_info_t *restrict info) {
int32_t terminator_head, terminator_tail;
uint32_t elemlen;
off_t hop;
READ_ERRCHECK(fd, & info->version, sizeof(info->version));
info->version = ntohs(info->version);
if (info->version > SIMCLIST_DUMPFORMAT_VERSION) {
errno = EILSEQ;
return -1;
}
READ_ERRCHECK(fd, & info->timestamp.tv_sec, sizeof(info->timestamp.tv_sec));
info->timestamp.tv_sec = ntohl(info->timestamp.tv_sec);
READ_ERRCHECK(fd, & info->timestamp.tv_usec, sizeof(info->timestamp.tv_usec));
info->timestamp.tv_usec = ntohl(info->timestamp.tv_usec);
READ_ERRCHECK(fd, & terminator_head, sizeof(terminator_head));
terminator_head = ntohl(terminator_head);
READ_ERRCHECK(fd, & info->list_size, sizeof(info->list_size));
info->list_size = ntohl(info->list_size);
READ_ERRCHECK(fd, & info->list_numels, sizeof(info->list_numels));
info->list_numels = ntohl(info->list_numels);
READ_ERRCHECK(fd, & elemlen, sizeof(elemlen));
elemlen = ntohl(elemlen);
READ_ERRCHECK(fd, & info->list_hash, sizeof(info->list_hash));
info->list_hash = ntohl(info->list_hash);
if (elemlen > 0) {
hop = info->list_size;
} else {
hop = info->list_size + elemlen*info->list_numels;
}
if (lseek(fd, hop, SEEK_CUR) == -1) {
return -1;
}
READ_ERRCHECK(fd, & terminator_tail, sizeof(terminator_tail));
terminator_tail = ntohl(terminator_tail);
if (terminator_head == terminator_tail)
info->consistent = 1;
else
info->consistent = 0;
return 0;
}
int list_dump_getinfo_file(const char *restrict filename, list_dump_info_t *restrict info) {
int fd, ret;
fd = open(filename, O_RDONLY, 0);
if (fd < 0) return -1;
ret = list_dump_getinfo_filedescriptor(fd, info);
close(fd);
return ret;
}
int list_dump_filedescriptor(const list_t *restrict l, int fd, size_t *restrict len) {
struct list_entry_s *x;
void *ser_buf;
uint32_t bufsize;
struct timeval timeofday;
struct list_dump_header_s header;
if (l->attrs.meter == NULL && l->attrs.serializer == NULL) {
errno = ENOTTY;
return -1;
}
header.ver = htons( SIMCLIST_DUMPFORMAT_VERSION );
gettimeofday(&timeofday, NULL);
header.timestamp_sec = htonl(timeofday.tv_sec);
header.timestamp_usec = htonl(timeofday.tv_usec);
header.rndterm = htonl((int32_t)get_random());
header.numels = htonl(l->numels);
if (l->attrs.hasher != NULL) {
if (htonl(list_hash(l, & header.listhash)) != 0) {
return -1;
}
} else {
header.listhash = htonl(0);
}
header.totlistlen = header.elemlen = 0;
if (lseek(fd, SIMCLIST_DUMPFORMAT_HEADERLEN, SEEK_SET) < 0) {
return -1;
}
if (l->numels > 0) {
if (l->attrs.serializer != NULL) {
ser_buf = l->attrs.serializer(l->head_sentinel->next->data, & header.elemlen);
free(ser_buf);
for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
ser_buf = l->attrs.serializer(x->data, &bufsize);
header.totlistlen += bufsize;
if (header.elemlen != 0) {
if (header.elemlen != bufsize) {
free(ser_buf);
header.elemlen = 0;
header.totlistlen = 0;
x = l->head_sentinel;
if (lseek(fd, SIMCLIST_DUMPFORMAT_HEADERLEN, SEEK_SET) < 0) {
return -1;
}
continue;
}
WRITE_ERRCHECK(fd, ser_buf, bufsize);
} else {
WRITE_ERRCHECK(fd, &bufsize, sizeof(bufsize));
WRITE_ERRCHECK(fd, ser_buf, bufsize);
}
free(ser_buf);
}
} else if (l->attrs.meter != NULL) {
header.elemlen = (uint32_t)l->attrs.meter(l->head_sentinel->next->data);
for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
bufsize = l->attrs.meter(x->data);
header.totlistlen += bufsize;
if (header.elemlen != 0) {
if (header.elemlen != bufsize) {
header.elemlen = 0;
header.totlistlen = 0;
x = l->head_sentinel;
continue;
}
WRITE_ERRCHECK(fd, x->data, bufsize);
} else {
WRITE_ERRCHECK(fd, &bufsize, sizeof(bufsize));
WRITE_ERRCHECK(fd, x->data, bufsize);
}
}
}
header.elemlen = htonl(header.elemlen);
header.totlistlen = htonl(header.totlistlen);
}
WRITE_ERRCHECK(fd, & header.rndterm, sizeof(header.rndterm));
lseek(fd, 0, SEEK_SET);
WRITE_ERRCHECK(fd, & header.ver, sizeof(header.ver));
WRITE_ERRCHECK(fd, & header.timestamp_sec, sizeof(header.timestamp_sec));
WRITE_ERRCHECK(fd, & header.timestamp_usec, sizeof(header.timestamp_usec));
WRITE_ERRCHECK(fd, & header.rndterm, sizeof(header.rndterm));
WRITE_ERRCHECK(fd, & header.totlistlen, sizeof(header.totlistlen));
WRITE_ERRCHECK(fd, & header.numels, sizeof(header.numels));
WRITE_ERRCHECK(fd, & header.elemlen, sizeof(header.elemlen));
WRITE_ERRCHECK(fd, & header.listhash, sizeof(header.listhash));
if (len != NULL) {
*len = sizeof(header) + ntohl(header.totlistlen);
}
return 0;
}
int list_restore_filedescriptor(list_t *restrict l, int fd, size_t *restrict len) {
struct list_dump_header_s header;
unsigned long cnt;
void *buf;
uint32_t elsize, totreadlen, totmemorylen;
memset(& header, 0, sizeof(header));
READ_ERRCHECK(fd, &header.ver, sizeof(header.ver));
header.ver = ntohs(header.ver);
if (header.ver != SIMCLIST_DUMPFORMAT_VERSION) {
errno = EILSEQ;
return -1;
}
READ_ERRCHECK(fd, & header.timestamp_sec, sizeof(header.timestamp_sec));
header.timestamp_sec = ntohl(header.timestamp_sec);
READ_ERRCHECK(fd, & header.timestamp_usec, sizeof(header.timestamp_usec));
header.timestamp_usec = ntohl(header.timestamp_usec);
READ_ERRCHECK(fd, & header.rndterm, sizeof(header.rndterm));
header.rndterm = ntohl(header.rndterm);
READ_ERRCHECK(fd, & header.totlistlen, sizeof(header.totlistlen));
header.totlistlen = ntohl(header.totlistlen);
READ_ERRCHECK(fd, & header.numels, sizeof(header.numels));
header.numels = ntohl(header.numels);
READ_ERRCHECK(fd, & header.elemlen, sizeof(header.elemlen));
header.elemlen = ntohl(header.elemlen);
READ_ERRCHECK(fd, & header.listhash, sizeof(header.listhash));
header.listhash = ntohl(header.listhash);
totreadlen = totmemorylen = 0;
if (header.elemlen > 0) {
if (l->attrs.unserializer != NULL) {
buf = malloc(header.elemlen);
if (NULL == buf)
return -1;
for (cnt = 0; cnt < header.numels; cnt++) {
READ_ERRCHECK(fd, buf, (ssize_t) header.elemlen);
list_append(l, l->attrs.unserializer(buf, & elsize));
totmemorylen += elsize;
}
} else {
for (cnt = 0; cnt < header.numels; cnt++) {
buf = malloc(header.elemlen);
if (NULL == buf)
return -1;
READ_ERRCHECK(fd, buf, (ssize_t) header.elemlen);
list_append(l, buf);
}
totmemorylen = header.numels * header.elemlen;
}
totreadlen = header.numels * header.elemlen;
} else {
if (l->attrs.unserializer != NULL) {
for (cnt = 0; cnt < header.numels; cnt++) {
READ_ERRCHECK(fd, & elsize, sizeof(elsize));
buf = malloc((size_t)elsize);
if (NULL == buf)
return -1;
READ_ERRCHECK(fd, buf, (ssize_t) elsize);
totreadlen += elsize;
list_append(l, l->attrs.unserializer(buf, & elsize));
totmemorylen += elsize;
}
} else {
for (cnt = 0; cnt < header.numels; cnt++) {
READ_ERRCHECK(fd, & elsize, sizeof(elsize));
buf = malloc(elsize);
if (NULL == buf)
return -1;
READ_ERRCHECK(fd, buf, (ssize_t) elsize);
totreadlen += elsize;
list_append(l, buf);
}
totmemorylen = totreadlen;
}
}
READ_ERRCHECK(fd, &elsize, sizeof(elsize));
elsize = ntohl(elsize);
if (totreadlen != header.totlistlen && (int32_t)elsize == header.rndterm) {
errno = EPROTO;
return -1;
}
if (lseek(fd, 0, SEEK_CUR) != lseek(fd, 0, SEEK_END)) {
errno = EPROTO;
return -1;
}
if (len != NULL) {
*len = totmemorylen;
}
return 0;
}
int list_dump_file(const list_t *restrict l, const char *restrict filename, size_t *restrict len) {
int fd, oflag, mode;
#ifndef _WIN32
oflag = O_RDWR | O_CREAT | O_TRUNC;
mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
#else
oflag = _O_RDWR | _O_CREAT | _O_TRUNC;
mode = _S_IRUSR | _S_IWUSR | _S_IRGRP | _S_IROTH;
#endif
fd = open(filename, oflag, mode);
if (fd < 0) return -1;
list_dump_filedescriptor(l, fd, len);
close(fd);
return 0;
}
int list_restore_file(list_t *restrict l, const char *restrict filename, size_t *restrict len) {
int fd;
fd = open(filename, O_RDONLY, 0);
if (fd < 0) return -1;
list_restore_filedescriptor(l, fd, len);
close(fd);
return 0;
}
#endif
static int list_drop_elem(list_t *restrict l, struct list_entry_s *tmp, unsigned int pos) {
if (tmp == NULL) return -1;
if (l->numels % 2) {
if (l->numels == 1) l->mid = NULL;
else if (pos >= l->numels/2) l->mid = l->mid->prev;
} else {
if (pos < l->numels/2) l->mid = l->mid->next;
}
tmp->prev->next = tmp->next;
tmp->next->prev = tmp->prev;
if (l->attrs.copy_data && tmp->data != NULL)
free(tmp->data);
if (l->spareels != NULL && l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
l->spareels[l->spareelsnum++] = tmp;
} else {
free(tmp);
}
return 0;
}
#define SIMCLIST_NUMBER_COMPARATOR(type) int list_comparator_##type(const void *a, const void *b) { return( *(type *)a < *(type *)b) - (*(type *)a > *(type *)b); }
SIMCLIST_NUMBER_COMPARATOR(int8_t)
SIMCLIST_NUMBER_COMPARATOR(int16_t)
SIMCLIST_NUMBER_COMPARATOR(int32_t)
SIMCLIST_NUMBER_COMPARATOR(int64_t)
SIMCLIST_NUMBER_COMPARATOR(uint8_t)
SIMCLIST_NUMBER_COMPARATOR(uint16_t)
SIMCLIST_NUMBER_COMPARATOR(uint32_t)
SIMCLIST_NUMBER_COMPARATOR(uint64_t)
SIMCLIST_NUMBER_COMPARATOR(float)
SIMCLIST_NUMBER_COMPARATOR(double)
int list_comparator_string(const void *a, const void *b) { return strcmp((const char *)b, (const char *)a); }
#define SIMCLIST_METER(type) size_t list_meter_##type(const void *el) { if (el) { } return sizeof(type); }
SIMCLIST_METER(int8_t)
SIMCLIST_METER(int16_t)
SIMCLIST_METER(int32_t)
SIMCLIST_METER(int64_t)
SIMCLIST_METER(uint8_t)
SIMCLIST_METER(uint16_t)
SIMCLIST_METER(uint32_t)
SIMCLIST_METER(uint64_t)
SIMCLIST_METER(float)
SIMCLIST_METER(double)
size_t list_meter_string(const void *el) { return strlen((const char *)el) + 1; }
#define SIMCLIST_HASHCOMPUTER(type) list_hash_t list_hashcomputer_##type(const void *el) { return (list_hash_t)(*(type *)el); }
SIMCLIST_HASHCOMPUTER(int8_t)
SIMCLIST_HASHCOMPUTER(int16_t)
SIMCLIST_HASHCOMPUTER(int32_t)
SIMCLIST_HASHCOMPUTER(int64_t)
SIMCLIST_HASHCOMPUTER(uint8_t)
SIMCLIST_HASHCOMPUTER(uint16_t)
SIMCLIST_HASHCOMPUTER(uint32_t)
SIMCLIST_HASHCOMPUTER(uint64_t)
SIMCLIST_HASHCOMPUTER(float)
SIMCLIST_HASHCOMPUTER(double)
list_hash_t list_hashcomputer_string(const void *el) {
size_t l;
list_hash_t hash = 123;
const char *str = (const char *)el;
char plus;
for (l = 0; str[l] != '\0'; l++) {
if (l) plus = hash ^ str[l];
else plus = hash ^ (str[l] - str[0]);
hash += (plus << (CHAR_BIT * (l % sizeof(list_hash_t))));
}
return hash;
}
#ifndef NDEBUG
static int list_repOk(const list_t *restrict l) {
int ok, i;
struct list_entry_s *s;
ok = (l != NULL) && (
(l->head_sentinel != NULL && l->tail_sentinel != NULL) &&
(l->head_sentinel != l->tail_sentinel) && (l->head_sentinel->prev == NULL && l->tail_sentinel->next == NULL) &&
(l->numels > 0 || (l->mid == NULL && l->head_sentinel->next == l->tail_sentinel && l->tail_sentinel->prev == l->head_sentinel)) &&
l->spareelsnum <= SIMCLIST_MAX_SPARE_ELEMS
);
if (!ok) return 0;
if (l->numels >= 1) {
for (i = -1, s = l->head_sentinel; i < (int)(l->numels-1)/2 && s->next != NULL; i++, s = s->next) {
if (s->next->prev != s) break;
}
ok = (i == (int)(l->numels-1)/2 && l->mid == s);
if (!ok) return 0;
for (; s->next != NULL; i++, s = s->next) {
if (s->next->prev != s) break;
}
ok = (i == (int)l->numels && s == l->tail_sentinel);
}
return ok;
}
static int list_attrOk(const list_t *restrict l) {
int ok;
ok = (l->attrs.copy_data == 0 || l->attrs.meter != NULL);
return ok;
}
#endif