33#ifndef SIMCLIST_NO_DUMPRESTORE
39# include <arpa/inet.h>
58#if defined(_MSC_VER) || defined(__MINGW32__)
60int gettimeofday(
struct timeval *tp,
void *tzp) {
67 tp->tv_sec = t / 1000;
68 tp->tv_usec = t % 1000;
75#if !defined(_WIN32) || !defined(_MSC_VER)
80typedef UINT16 uint16_t;
81typedef ULONG32 uint32_t;
82typedef UINT64 uint64_t;
85typedef LONG32 int32_t;
91#ifndef SIMCLIST_NO_DUMPRESTORE
93#define WRITE_ERRCHECK(fd, msgbuf, msglen) do { \
94 if (write(fd, msgbuf, msglen) < 0) return -1; \
97#define READ_ERRCHECK(fd, msgbuf, msglen) do { \
98 if (read(fd, msgbuf, msglen) != msglen) { \
109 ((uint64_t)((((uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \
110 (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
111 (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
112 (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
113 (((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
114 (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
115 (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
116 (((uint64_t)(x) & 0x00000000000000ffULL) << 56))) \
120#define ntoh64(x) (hton64(x))
128#ifdef SIMCLIST_WITH_THREADS
132#define SIMCLIST_MAXTHREADS 2
159#ifndef SIMCLIST_MAX_SPARE_ELEMS
160#define SIMCLIST_MAX_SPARE_ELEMS 5
164#ifdef SIMCLIST_WITH_THREADS
172#define SIMCLIST_MINQUICKSORTELS 24
176#define SIMCLIST_DUMPFORMAT_VERSION 1
178#define SIMCLIST_DUMPFORMAT_HEADERLEN 30
183 int32_t timestamp_sec;
184 int32_t timestamp_usec;
196static int list_drop_elem(
list_t *restrict l,
struct list_entry_s *tmp,
unsigned int pos);
199static int list_attributes_setdefaults(
list_t *restrict l);
203static int list_repOk(
const list_t *restrict l);
206static int list_attrOk(
const list_t *restrict l);
210static void list_sort_quicksort(
list_t *restrict l,
int versus,
214static inline void list_sort_selectionsort(
list_t *restrict l,
int versus,
218static void *list_get_minmax(
const list_t *restrict l,
int versus);
220static inline struct list_entry_s *list_findpos(
const list_t *restrict l,
int posstart);
243#ifdef SIMCLIST_SYSTEM_RNG
245static unsigned random_seed = 0;
248static inline void seed_random(
void) {
249 if (random_seed == 0)
250 random_seed = (unsigned)getpid() ^ (unsigned)time(NULL);
253static inline long get_random(
void) {
254 random_seed = (1664525 * random_seed + 1013904223);
260# define seed_random()
261# define get_random() (rand())
266int list_init(
list_t *restrict l) {
267 if (l == NULL)
return -1;
269 memset(l, 0,
sizeof *l);
278 if (NULL == l->tail_sentinel || NULL == l->head_sentinel)
281 l->head_sentinel->next = l->tail_sentinel;
282 l->tail_sentinel->prev = l->head_sentinel;
283 l->head_sentinel->prev = l->tail_sentinel->next = l->mid = NULL;
284 l->head_sentinel->data = l->tail_sentinel->data = NULL;
289 l->iter_curentry = NULL;
294 if (NULL == l->spareels)
297#ifdef SIMCLIST_WITH_THREADS
301 if (list_attributes_setdefaults(l))
304 assert(list_repOk(l));
305 assert(list_attrOk(l));
310void list_destroy(
list_t *restrict l) {
314 for (i = 0; i < l->spareelsnum; i++) {
315 free(l->spareels[i]);
318 free(l->head_sentinel);
319 free(l->tail_sentinel);
322int list_attributes_setdefaults(
list_t *restrict l) {
323 l->attrs.comparator = NULL;
324 l->attrs.seeker = NULL;
327 l->attrs.meter = NULL;
328 l->attrs.copy_data = 0;
330 l->attrs.hasher = NULL;
333 l->attrs.serializer = NULL;
334 l->attrs.unserializer = NULL;
336 assert(list_attrOk(l));
342int list_attributes_comparator(
list_t *restrict l, element_comparator comparator_fun) {
343 if (l == NULL)
return -1;
345 l->attrs.comparator = comparator_fun;
347 assert(list_attrOk(l));
352int list_attributes_seeker(
list_t *restrict l, element_seeker seeker_fun) {
353 if (l == NULL)
return -1;
355 l->attrs.seeker = seeker_fun;
356 assert(list_attrOk(l));
361int list_attributes_copy(
list_t *restrict l, element_meter metric_fun,
int copy_data) {
362 if (l == NULL || (metric_fun == NULL && copy_data != 0))
return -1;
364 l->attrs.meter = metric_fun;
365 l->attrs.copy_data = copy_data;
367 assert(list_attrOk(l));
372int list_attributes_hash_computer(
list_t *restrict l, element_hash_computer hash_computer_fun) {
373 if (l == NULL)
return -1;
375 l->attrs.hasher = hash_computer_fun;
376 assert(list_attrOk(l));
380int list_attributes_serializer(
list_t *restrict l, element_serializer serializer_fun) {
381 if (l == NULL)
return -1;
383 l->attrs.serializer = serializer_fun;
384 assert(list_attrOk(l));
388int list_attributes_unserializer(
list_t *restrict l, element_unserializer unserializer_fun) {
389 if (l == NULL)
return -1;
391 l->attrs.unserializer = unserializer_fun;
392 assert(list_attrOk(l));
396int list_append(
list_t *restrict l,
const void *data) {
397 return list_insert_at(l, data, l->numels);
400int list_prepend(
list_t *restrict l,
const void *data) {
401 return list_insert_at(l, data, 0);
404void *list_fetch(
list_t *restrict l) {
405 return list_extract_at(l, 0);
408void *list_get_at(
const list_t *restrict l,
unsigned int pos) {
411 tmp = list_findpos(l, pos);
413 return (tmp != NULL ? tmp->data : NULL);
416void *list_get_max(
const list_t *restrict l) {
417 return list_get_minmax(l, +1);
420void *list_get_min(
const list_t *restrict l) {
421 return list_get_minmax(l, -1);
426static void *list_get_minmax(
const list_t *restrict l,
int versus) {
430 if (l->attrs.comparator == NULL || l->numels == 0)
433 curminmax = l->head_sentinel->next->data;
434 for (s = l->head_sentinel->next->next; s != l->tail_sentinel; s = s->next) {
435 if (l->attrs.comparator(curminmax, s->data) * versus > 0)
443static inline struct list_entry_s *list_findpos(
const list_t *restrict l,
int posstart) {
448 if (NULL == l->head_sentinel || NULL == l->tail_sentinel)
452 if (posstart < -1 || posstart > (
int)l->numels)
return NULL;
455 x = (float)(posstart+1) / l->numels;
460 for (i = -1, ptr = l->head_sentinel; i < posstart; ptr = ptr->next, i++);
461 }
else if (x < 0.5) {
463 for (i = (l->numels-1)/2, ptr = l->mid; i > posstart; ptr = ptr->prev, i--);
464 }
else if (x <= 0.75) {
466 for (i = (l->numels-1)/2, ptr = l->mid; i < posstart; ptr = ptr->next, i++);
469 for (i = l->numels, ptr = l->tail_sentinel; i > posstart; ptr = ptr->prev, i--);
475void *list_extract_at(
list_t *restrict l,
unsigned int pos) {
479 if (l->iter_active || pos >= l->numels)
return NULL;
481 tmp = list_findpos(l, pos);
488 list_drop_elem(l, tmp, pos);
491 assert(list_repOk(l));
496int list_insert_at(
list_t *restrict l,
const void *data,
unsigned int pos) {
499 if (l->iter_active || pos > l->numels)
return -1;
502 if (l->spareelsnum > 0) {
503 lent = l->spareels[l->spareelsnum-1];
511 if (l->attrs.copy_data) {
513 size_t datalen = l->attrs.meter(data);
515 if (NULL == lent->data)
520 memcpy(lent->data, data, datalen);
522 lent->data = (
void*)data;
526 prec = list_findpos(l, pos-1);
543 if (l->numels == 1) {
545 }
else if (l->numels % 2) {
546 if (pos >= (l->numels-1)/2) l->mid = l->mid->next;
548 if (pos <= (l->numels-1)/2) l->mid = l->mid->prev;
551 assert(list_repOk(l));
556int list_delete(
list_t *restrict l,
const void *data) {
559 pos = list_locate(l, data);
563 r = list_delete_at(l, pos);
567 assert(list_repOk(l));
572int list_delete_at(
list_t *restrict l,
unsigned int pos) {
576 if (l->iter_active || pos >= l->numels)
return -1;
578 delendo = list_findpos(l, pos);
580 list_drop_elem(l, delendo, pos);
585 assert(list_repOk(l));
590int list_delete_range(
list_t *restrict l,
unsigned int posstart,
unsigned int posend) {
592 unsigned int numdel, midposafter, i;
595 if (l->iter_active || posend < posstart || posend >= l->numels)
return -1;
597 numdel = posend - posstart + 1;
598 if (numdel == l->numels)
return list_clear(l);
600 tmp = list_findpos(l, posstart);
601 lastvalid = tmp->prev;
603 midposafter = (l->numels-1-numdel)/2;
605 midposafter = midposafter < posstart ? midposafter : midposafter+numdel;
606 movedx = midposafter - (l->numels-1)/2;
609 for (i = 0; i < (
unsigned int)movedx; l->mid = l->mid->next, i++);
612 for (i = 0; i < (
unsigned int)movedx; l->mid = l->mid->prev, i++);
615 assert(posstart == 0 || lastvalid != l->head_sentinel);
617 if (l->attrs.copy_data) {
619 for (; i <= posend; i++) {
622 if (tmp2->data != NULL) free(tmp2->data);
623 if (l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
624 l->spareels[l->spareelsnum++] = tmp2;
631 for (; i <= posend; i++) {
634 if (l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
635 l->spareels[l->spareelsnum++] = tmp2;
641 assert(i == posend+1 && (posend != l->numels || tmp == l->tail_sentinel));
643 lastvalid->next = tmp;
644 tmp->prev = lastvalid;
646 l->numels -= posend - posstart + 1;
648 assert(list_repOk(l));
653int list_clear(
list_t *restrict l) {
660 if (l->iter_active)
return -1;
662 if (l->head_sentinel && l->tail_sentinel) {
663 if (l->attrs.copy_data) {
665 for (s = l->head_sentinel->next; l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS && s != l->tail_sentinel; s = s->next) {
667 if (s->data != NULL) free(s->data);
668 l->spareels[l->spareelsnum++] = s;
670 while (s != l->tail_sentinel) {
672 if (s->data != NULL) free(s->data);
676 l->head_sentinel->next = l->tail_sentinel;
677 l->tail_sentinel->prev = l->head_sentinel;
680 for (s = l->head_sentinel->next; l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS && s != l->tail_sentinel; s = s->next) {
682 l->spareels[l->spareelsnum++] = s;
684 while (s != l->tail_sentinel) {
689 l->head_sentinel->next = l->tail_sentinel;
690 l->tail_sentinel->prev = l->head_sentinel;
696 assert(list_repOk(l));
701unsigned int list_size(
const list_t *restrict l) {
705int list_empty(
const list_t *restrict l) {
706 return (l->numels == 0);
709int list_locate(
const list_t *restrict l,
const void *data) {
713 if (NULL == l->head_sentinel || NULL == l->tail_sentinel)
716 if (l->attrs.comparator != NULL) {
718 for (el = l->head_sentinel->next; el != l->tail_sentinel; el = el->next, pos++) {
719 if (l->attrs.comparator(data, el->data) == 0)
break;
723 for (el = l->head_sentinel->next; el != l->tail_sentinel; el = el->next, pos++) {
724 if (el->data == data)
break;
727 if (el == l->tail_sentinel)
return -1;
732void *list_seek(
list_t *restrict l,
const void *indicator) {
735 if (l->attrs.seeker == NULL)
return NULL;
737 if (NULL == l->head_sentinel || NULL == l->tail_sentinel)
740 for (iter = l->head_sentinel->next; iter != l->tail_sentinel; iter = iter->next) {
741 if (l->attrs.seeker(iter->data, indicator) != 0)
return iter->data;
747int list_contains(
const list_t *restrict l,
const void *data) {
748 return (list_locate(l, data) >= 0);
757 if (l1 == NULL || l2 == NULL || dest == NULL || l1 == dest || l2 == dest)
760 if (NULL == l1->head_sentinel || NULL == l1->tail_sentinel
761 || NULL == l2->head_sentinel || NULL == l2->tail_sentinel)
767 dest->numels = l1->numels + l2->numels;
768 if (dest->numels == 0)
772 srcel = l1->head_sentinel->next;
773 el = dest->head_sentinel;
774 while (srcel != l1->tail_sentinel) {
776 if (NULL == el->next)
780 el->data = srcel->data;
785 srcel = l2->head_sentinel->next;
786 while (srcel != l2->tail_sentinel) {
788 if (NULL == el->next)
792 el->data = srcel->data;
795 el->next = dest->tail_sentinel;
796 dest->tail_sentinel->prev = el;
799 err = l2->numels - l1->numels;
802 for (cnt = 0; cnt < (
unsigned int)err; cnt++) dest->mid = dest->mid->next;
803 }
else if (err/2 < 0) {
805 for (cnt = 0; cnt < (
unsigned int)err; cnt++) dest->mid = dest->mid->prev;
808 assert(!(list_repOk(l1) && list_repOk(l2)) || list_repOk(dest));
813int list_sort(
list_t *restrict l,
int versus) {
814 if (l->iter_active || l->attrs.comparator == NULL)
820 if (NULL == l->head_sentinel || NULL == l->tail_sentinel)
823 list_sort_quicksort(l, versus, 0, l->head_sentinel->next, l->numels-1, l->tail_sentinel->prev);
824 assert(list_repOk(l));
828#ifdef SIMCLIST_WITH_THREADS
829struct list_sort_wrappedparams {
832 unsigned int first, last;
836static void *list_sort_quicksort_threadwrapper(
void *wrapped_params) {
837 struct list_sort_wrappedparams *wp = (
struct list_sort_wrappedparams *)wrapped_params;
838 list_sort_quicksort(wp->l, wp->versus, wp->first, wp->fel, wp->last, wp->lel);
845static inline void list_sort_selectionsort(
list_t *restrict l,
int versus,
854 for (firstunsorted = fel; firstunsorted != lel; firstunsorted = firstunsorted->next) {
856 for (toswap = firstunsorted, cursor = firstunsorted->next; cursor != lel->next; cursor = cursor->next)
857 if (l->attrs.comparator(toswap->data, cursor->data) * -versus > 0) toswap = cursor;
858 if (toswap != firstunsorted) {
859 tmpdata = firstunsorted->data;
860 firstunsorted->data = toswap->data;
861 toswap->data = tmpdata;
866static void list_sort_quicksort(
list_t *restrict l,
int versus,
869 unsigned int pivotid;
874#ifdef SIMCLIST_WITH_THREADS
883 if (last - first+1 <= SIMCLIST_MINQUICKSORTELS) {
884 list_sort_selectionsort(l, versus, first, fel, last, lel);
889 if (! (last > first))
return;
891 pivotid = (get_random() % (last - first + 1));
895 if (pivotid < (last - first + 1)/2) {
896 for (i = 0, pivot = fel; i < pivotid; pivot = pivot->next, i++);
898 for (i = last - first, pivot = lel; i > pivotid; pivot = pivot->prev, i--);
905 while (left != pivot && right != pivot) {
906 for (; left != pivot && (l->attrs.comparator(left->data, pivot->data) * -versus <= 0); left = left->next);
908 for (; right != pivot && (l->attrs.comparator(right->data, pivot->data) * -versus >= 0); right = right->prev);
910 if (left != pivot && right != pivot) {
912 tmpdata = left->data;
913 left->data = right->data;
914 right->data = tmpdata;
922 if (right == pivot) {
923 while (left != pivot) {
924 if (l->attrs.comparator(left->data, pivot->data) * -versus > 0) {
925 tmpdata = left->data;
926 left->data = pivot->prev->data;
927 pivot->prev->data = pivot->data;
928 pivot->data = tmpdata;
931 if (pivot == left)
break;
937 while (right != pivot) {
938 if (l->attrs.comparator(right->data, pivot->data) * -versus < 0) {
940 tmpdata = right->data;
941 right->data = pivot->next->data;
942 pivot->next->data = pivot->data;
943 pivot->data = tmpdata;
946 if (pivot == right)
break;
955#ifdef SIMCLIST_WITH_THREADS
959 if (l->threadcount < SIMCLIST_MAXTHREADS-1) {
960 struct list_sort_wrappedparams *wp = (
struct list_sort_wrappedparams *)malloc(
sizeof(
struct list_sort_wrappedparams));
969 wp->last = first+pivotid-1;
970 wp->lel = pivot->prev;
971 if (pthread_create(&tid, NULL, list_sort_quicksort_threadwrapper, wp) != 0) {
974 list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
977 list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
980 if (first + pivotid < last) list_sort_quicksort(l, versus, first+pivotid+1, pivot->next, last, lel);
982 pthread_join(tid, (
void **)NULL);
986 if (pivotid > 0) list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
987 if (first + pivotid < last) list_sort_quicksort(l, versus, first+pivotid+1, pivot->next, last, lel);
991int list_iterator_start(
list_t *restrict l) {
992 if (l->iter_active)
return 0;
993 if (NULL == l->head_sentinel)
997 l->iter_curentry = l->head_sentinel->next;
1001void *list_iterator_next(
list_t *restrict l) {
1004 if (! l->iter_active)
return NULL;
1006 toret = l->iter_curentry->data;
1007 l->iter_curentry = l->iter_curentry->next;
1013int list_iterator_hasnext(
const list_t *restrict l) {
1014 if (! l->iter_active)
return 0;
1015 return (l->iter_pos < l->numels);
1018int list_iterator_stop(
list_t *restrict l) {
1019 if (! l->iter_active)
return 0;
1025int list_hash(
const list_t *restrict l, list_hash_t *restrict hash) {
1027 list_hash_t tmphash;
1029 assert(hash != NULL);
1031 tmphash = l->numels * 2 + 100;
1032 if (l->attrs.hasher == NULL) {
1033#ifdef SIMCLIST_ALLOW_LOCATIONBASED_HASHES
1035#warning "Memlocation-based hash is consistent only for testing modification in the same program run."
1039 for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
1040 for (i = 0; i <
sizeof(x->data); i++) {
1041 tmphash += (tmphash ^ (uintptr_t)x->data);
1043 tmphash += tmphash % l->numels;
1050 for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
1051 tmphash += tmphash ^ l->attrs.hasher(x->data);
1052 tmphash += tmphash % l->numels;
1061#ifndef SIMCLIST_NO_DUMPRESTORE
1062int list_dump_getinfo_filedescriptor(
int fd,
list_dump_info_t *restrict info) {
1063 int32_t terminator_head, terminator_tail;
1069 READ_ERRCHECK(fd, & info->version,
sizeof(info->version));
1070 info->version = ntohs(info->version);
1071 if (info->version > SIMCLIST_DUMPFORMAT_VERSION) {
1077 READ_ERRCHECK(fd, & info->timestamp.tv_sec,
sizeof(info->timestamp.tv_sec));
1078 info->timestamp.tv_sec = ntohl(info->timestamp.tv_sec);
1079 READ_ERRCHECK(fd, & info->timestamp.tv_usec,
sizeof(info->timestamp.tv_usec));
1080 info->timestamp.tv_usec = ntohl(info->timestamp.tv_usec);
1083 READ_ERRCHECK(fd, & terminator_head,
sizeof(terminator_head));
1084 terminator_head = ntohl(terminator_head);
1087 READ_ERRCHECK(fd, & info->list_size,
sizeof(info->list_size));
1088 info->list_size = ntohl(info->list_size);
1091 READ_ERRCHECK(fd, & info->list_numels,
sizeof(info->list_numels));
1092 info->list_numels = ntohl(info->list_numels);
1095 READ_ERRCHECK(fd, & elemlen,
sizeof(elemlen));
1096 elemlen = ntohl(elemlen);
1099 READ_ERRCHECK(fd, & info->list_hash,
sizeof(info->list_hash));
1100 info->list_hash = ntohl(info->list_hash);
1105 hop = info->list_size;
1108 hop = info->list_size + elemlen*info->list_numels;
1110 if (lseek(fd, hop, SEEK_CUR) == -1) {
1115 READ_ERRCHECK(fd, & terminator_tail,
sizeof(terminator_tail));
1116 terminator_tail = ntohl(terminator_tail);
1118 if (terminator_head == terminator_tail)
1119 info->consistent = 1;
1121 info->consistent = 0;
1126int list_dump_getinfo_file(
const char *restrict filename,
list_dump_info_t *restrict info) {
1129 fd = open(filename, O_RDONLY, 0);
1130 if (fd < 0)
return -1;
1132 ret = list_dump_getinfo_filedescriptor(fd, info);
1138int list_dump_filedescriptor(
const list_t *restrict l,
int fd,
size_t *restrict len) {
1142 struct timeval timeofday;
1145 if (l->attrs.meter == NULL && l->attrs.serializer == NULL) {
1166 header.ver = htons( SIMCLIST_DUMPFORMAT_VERSION );
1169 gettimeofday(&timeofday, NULL);
1170 header.timestamp_sec = htonl(timeofday.tv_sec);
1171 header.timestamp_usec = htonl(timeofday.tv_usec);
1173 header.rndterm = htonl((int32_t)get_random());
1178 header.numels = htonl(l->numels);
1181 if (l->attrs.hasher != NULL) {
1182 if (htonl(list_hash(l, & header.listhash)) != 0) {
1187 header.listhash = htonl(0);
1190 header.totlistlen = header.elemlen = 0;
1193 if (lseek(fd, SIMCLIST_DUMPFORMAT_HEADERLEN, SEEK_SET) < 0) {
1199 if (l->numels > 0) {
1202 if (l->attrs.serializer != NULL) {
1204 ser_buf = l->attrs.serializer(l->head_sentinel->next->data, & header.elemlen);
1207 for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
1208 ser_buf = l->attrs.serializer(x->data, &bufsize);
1209 header.totlistlen += bufsize;
1210 if (header.elemlen != 0) {
1211 if (header.elemlen != bufsize) {
1215 header.totlistlen = 0;
1216 x = l->head_sentinel;
1217 if (lseek(fd, SIMCLIST_DUMPFORMAT_HEADERLEN, SEEK_SET) < 0) {
1225 WRITE_ERRCHECK(fd, ser_buf, bufsize);
1227 WRITE_ERRCHECK(fd, &bufsize,
sizeof(bufsize));
1228 WRITE_ERRCHECK(fd, ser_buf, bufsize);
1232 }
else if (l->attrs.meter != NULL) {
1233 header.elemlen = (uint32_t)l->attrs.meter(l->head_sentinel->next->data);
1236 for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
1237 bufsize = l->attrs.meter(x->data);
1238 header.totlistlen += bufsize;
1239 if (header.elemlen != 0) {
1240 if (header.elemlen != bufsize) {
1243 header.totlistlen = 0;
1244 x = l->head_sentinel;
1248 WRITE_ERRCHECK(fd, x->data, bufsize);
1250 WRITE_ERRCHECK(fd, &bufsize,
sizeof(bufsize));
1251 WRITE_ERRCHECK(fd, x->data, bufsize);
1256 header.elemlen = htonl(header.elemlen);
1257 header.totlistlen = htonl(header.totlistlen);
1261 WRITE_ERRCHECK(fd, & header.rndterm,
sizeof(header.rndterm));
1265 lseek(fd, 0, SEEK_SET);
1267 WRITE_ERRCHECK(fd, & header.ver,
sizeof(header.ver));
1268 WRITE_ERRCHECK(fd, & header.timestamp_sec,
sizeof(header.timestamp_sec));
1269 WRITE_ERRCHECK(fd, & header.timestamp_usec,
sizeof(header.timestamp_usec));
1270 WRITE_ERRCHECK(fd, & header.rndterm,
sizeof(header.rndterm));
1272 WRITE_ERRCHECK(fd, & header.totlistlen,
sizeof(header.totlistlen));
1273 WRITE_ERRCHECK(fd, & header.numels,
sizeof(header.numels));
1274 WRITE_ERRCHECK(fd, & header.elemlen,
sizeof(header.elemlen));
1275 WRITE_ERRCHECK(fd, & header.listhash,
sizeof(header.listhash));
1280 *len =
sizeof(header) + ntohl(header.totlistlen);
1286int list_restore_filedescriptor(
list_t *restrict l,
int fd,
size_t *restrict len) {
1290 uint32_t elsize, totreadlen, totmemorylen;
1292 memset(& header, 0,
sizeof(header));
1297 READ_ERRCHECK(fd, &header.ver,
sizeof(header.ver));
1298 header.ver = ntohs(header.ver);
1299 if (header.ver != SIMCLIST_DUMPFORMAT_VERSION) {
1305 READ_ERRCHECK(fd, & header.timestamp_sec,
sizeof(header.timestamp_sec));
1306 header.timestamp_sec = ntohl(header.timestamp_sec);
1307 READ_ERRCHECK(fd, & header.timestamp_usec,
sizeof(header.timestamp_usec));
1308 header.timestamp_usec = ntohl(header.timestamp_usec);
1311 READ_ERRCHECK(fd, & header.rndterm,
sizeof(header.rndterm));
1313 header.rndterm = ntohl(header.rndterm);
1316 READ_ERRCHECK(fd, & header.totlistlen,
sizeof(header.totlistlen));
1317 header.totlistlen = ntohl(header.totlistlen);
1320 READ_ERRCHECK(fd, & header.numels,
sizeof(header.numels));
1321 header.numels = ntohl(header.numels);
1324 READ_ERRCHECK(fd, & header.elemlen,
sizeof(header.elemlen));
1325 header.elemlen = ntohl(header.elemlen);
1328 READ_ERRCHECK(fd, & header.listhash,
sizeof(header.listhash));
1329 header.listhash = ntohl(header.listhash);
1333 totreadlen = totmemorylen = 0;
1334 if (header.elemlen > 0) {
1336 if (l->attrs.unserializer != NULL) {
1338 buf = malloc(header.elemlen);
1341 for (cnt = 0; cnt < header.numels; cnt++) {
1342 READ_ERRCHECK(fd, buf, (ssize_t) header.elemlen);
1343 list_append(l, l->attrs.unserializer(buf, & elsize));
1344 totmemorylen += elsize;
1348 for (cnt = 0; cnt < header.numels; cnt++) {
1349 buf = malloc(header.elemlen);
1352 READ_ERRCHECK(fd, buf, (ssize_t) header.elemlen);
1353 list_append(l, buf);
1355 totmemorylen = header.numels * header.elemlen;
1357 totreadlen = header.numels * header.elemlen;
1360 if (l->attrs.unserializer != NULL) {
1362 for (cnt = 0; cnt < header.numels; cnt++) {
1363 READ_ERRCHECK(fd, & elsize,
sizeof(elsize));
1364 buf = malloc((
size_t)elsize);
1367 READ_ERRCHECK(fd, buf, (ssize_t) elsize);
1368 totreadlen += elsize;
1369 list_append(l, l->attrs.unserializer(buf, & elsize));
1370 totmemorylen += elsize;
1374 for (cnt = 0; cnt < header.numels; cnt++) {
1375 READ_ERRCHECK(fd, & elsize,
sizeof(elsize));
1376 buf = malloc(elsize);
1379 READ_ERRCHECK(fd, buf, (ssize_t) elsize);
1380 totreadlen += elsize;
1381 list_append(l, buf);
1383 totmemorylen = totreadlen;
1387 READ_ERRCHECK(fd, &elsize,
sizeof(elsize));
1388 elsize = ntohl(elsize);
1400 if (totreadlen != header.totlistlen && (int32_t)elsize == header.rndterm) {
1406 if (lseek(fd, 0, SEEK_CUR) != lseek(fd, 0, SEEK_END)) {
1412 *len = totmemorylen;
1418int list_dump_file(
const list_t *restrict l,
const char *restrict filename,
size_t *restrict len) {
1419 int fd, oflag, mode;
1422 oflag = O_RDWR | O_CREAT | O_TRUNC;
1423 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1425 oflag = _O_RDWR | _O_CREAT | _O_TRUNC;
1426 mode = _S_IRUSR | _S_IWUSR | _S_IRGRP | _S_IROTH;
1428 fd = open(filename, oflag, mode);
1429 if (fd < 0)
return -1;
1431 list_dump_filedescriptor(l, fd, len);
1437int list_restore_file(
list_t *restrict l,
const char *restrict filename,
size_t *restrict len) {
1440 fd = open(filename, O_RDONLY, 0);
1441 if (fd < 0)
return -1;
1443 list_restore_filedescriptor(l, fd, len);
1451static int list_drop_elem(
list_t *restrict l,
struct list_entry_s *tmp,
unsigned int pos) {
1452 if (tmp == NULL)
return -1;
1455 if (l->numels % 2) {
1457 if (l->numels == 1) l->mid = NULL;
1458 else if (pos >= l->numels/2) l->mid = l->mid->prev;
1460 if (pos < l->numels/2) l->mid = l->mid->next;
1463 tmp->prev->next = tmp->next;
1464 tmp->next->prev = tmp->prev;
1467 if (l->attrs.copy_data && tmp->data != NULL)
1470 if (l->spareels != NULL && l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
1471 l->spareels[l->spareelsnum++] = tmp;
1480#define SIMCLIST_NUMBER_COMPARATOR(type) int list_comparator_##type(const void *a, const void *b) { return( *(type *)a < *(type *)b) - (*(type *)a > *(type *)b); }
1482SIMCLIST_NUMBER_COMPARATOR(int8_t)
1483SIMCLIST_NUMBER_COMPARATOR(int16_t)
1484SIMCLIST_NUMBER_COMPARATOR(int32_t)
1485SIMCLIST_NUMBER_COMPARATOR(int64_t)
1487SIMCLIST_NUMBER_COMPARATOR(uint8_t)
1488SIMCLIST_NUMBER_COMPARATOR(uint16_t)
1489SIMCLIST_NUMBER_COMPARATOR(uint32_t)
1490SIMCLIST_NUMBER_COMPARATOR(uint64_t)
1492SIMCLIST_NUMBER_COMPARATOR(
float)
1493SIMCLIST_NUMBER_COMPARATOR(
double)
1495int list_comparator_string(
const void *a,
const void *b) {
return strcmp((
const char *)b, (
const char *)a); }
1498#define SIMCLIST_METER(type) size_t list_meter_##type(const void *el) { if (el) { } return sizeof(type); }
1500SIMCLIST_METER(int8_t)
1501SIMCLIST_METER(int16_t)
1502SIMCLIST_METER(int32_t)
1503SIMCLIST_METER(int64_t)
1505SIMCLIST_METER(uint8_t)
1506SIMCLIST_METER(uint16_t)
1507SIMCLIST_METER(uint32_t)
1508SIMCLIST_METER(uint64_t)
1510SIMCLIST_METER(
float)
1511SIMCLIST_METER(
double)
1513size_t list_meter_string(
const void *el) {
return strlen((
const char *)el) + 1; }
1516#define SIMCLIST_HASHCOMPUTER(type) list_hash_t list_hashcomputer_##type(const void *el) { return (list_hash_t)(*(type *)el); }
1518SIMCLIST_HASHCOMPUTER(int8_t)
1519SIMCLIST_HASHCOMPUTER(int16_t)
1520SIMCLIST_HASHCOMPUTER(int32_t)
1521SIMCLIST_HASHCOMPUTER(int64_t)
1523SIMCLIST_HASHCOMPUTER(uint8_t)
1524SIMCLIST_HASHCOMPUTER(uint16_t)
1525SIMCLIST_HASHCOMPUTER(uint32_t)
1526SIMCLIST_HASHCOMPUTER(uint64_t)
1528SIMCLIST_HASHCOMPUTER(
float)
1529SIMCLIST_HASHCOMPUTER(
double)
1531list_hash_t list_hashcomputer_string(
const void *el) {
1533 list_hash_t hash = 123;
1534 const char *str = (
const char *)el;
1537 for (l = 0; str[l] !=
'\0'; l++) {
1538 if (l) plus = hash ^ str[l];
1539 else plus = hash ^ (str[l] - str[0]);
1540 hash += (plus << (CHAR_BIT * (l %
sizeof(list_hash_t))));
1548static int list_repOk(
const list_t *restrict l) {
1552 ok = (l != NULL) && (
1554 (l->head_sentinel != NULL && l->tail_sentinel != NULL) &&
1555 (l->head_sentinel != l->tail_sentinel) && (l->head_sentinel->prev == NULL && l->tail_sentinel->next == NULL) &&
1557 (l->numels > 0 || (l->mid == NULL && l->head_sentinel->next == l->tail_sentinel && l->tail_sentinel->prev == l->head_sentinel)) &&
1559 l->spareelsnum <= SIMCLIST_MAX_SPARE_ELEMS
1564 if (l->numels >= 1) {
1566 for (i = -1, s = l->head_sentinel; i < (int)(l->numels-1)/2 && s->next != NULL; i++, s = s->next) {
1567 if (s->next->prev != s)
break;
1569 ok = (i == (int)(l->numels-1)/2 && l->mid == s);
1571 for (; s->next != NULL; i++, s = s->next) {
1572 if (s->next->prev != s)
break;
1574 ok = (i == (int)l->numels && s == l->tail_sentinel);
1580static int list_attrOk(
const list_t *restrict l) {
1583 ok = (l->attrs.copy_data == 0 || l->attrs.meter != NULL);