27 #include <sys/types.h>
33 #ifndef SIMCLIST_NO_DUMPRESTORE
39 # include <arpa/inet.h>
41 # include <winsock2.h>
46 #ifndef SIMCLIST_DEBUG
56 #if defined(_MSC_VER) || defined(__MINGW32__)
58 int gettimeofday(
struct timeval *tp,
void *tzp) {
65 tp->tv_sec = t / 1000;
66 tp->tv_usec = t % 1000;
73 #if !defined(_WIN32) || !defined(_MSC_VER)
74 # include <inttypes.h>
77 typedef UINT8 uint8_t;
78 typedef UINT16 uint16_t;
79 typedef ULONG32 uint32_t;
80 typedef UINT64 uint64_t;
82 typedef INT16 int16_t;
83 typedef LONG32 int32_t;
84 typedef INT64 int64_t;
89 #ifndef SIMCLIST_NO_DUMPRESTORE
91 #define WRITE_ERRCHECK(fd, msgbuf, msglen) do { \
92 if (write(fd, msgbuf, msglen) < 0) return -1; \
95 #define READ_ERRCHECK(fd, msgbuf, msglen) do { \
96 if (read(fd, msgbuf, msglen) != msglen) { \
107 ((uint64_t)((((uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \
108 (((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
109 (((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
110 (((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
111 (((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
112 (((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
113 (((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
114 (((uint64_t)(x) & 0x00000000000000ffULL) << 56))) \
118 #define ntoh64(x) (hton64(x))
126 #ifdef SIMCLIST_WITH_THREADS
130 #define SIMCLIST_MAXTHREADS 2
157 #ifndef SIMCLIST_MAX_SPARE_ELEMS
158 #define SIMCLIST_MAX_SPARE_ELEMS 5
162 #ifdef SIMCLIST_WITH_THREADS
166 #include "simclist.h"
170 #define SIMCLIST_MINQUICKSORTELS 24
174 #define SIMCLIST_DUMPFORMAT_VERSION 1
176 #define SIMCLIST_DUMPFORMAT_HEADERLEN 30
181 int32_t timestamp_sec;
182 int32_t timestamp_usec;
194 static int list_drop_elem(
list_t *restrict l,
struct list_entry_s *tmp,
unsigned int pos);
197 static int list_attributes_setdefaults(
list_t *restrict l);
201 static int list_repOk(
const list_t *restrict l);
204 static int list_attrOk(
const list_t *restrict l);
208 static void list_sort_quicksort(
list_t *restrict l,
int versus,
212 static inline void list_sort_selectionsort(
list_t *restrict l,
int versus,
216 static void *list_get_minmax(
const list_t *restrict l,
int versus);
218 static inline struct list_entry_s *list_findpos(
const list_t *restrict l,
int posstart);
241 #ifdef SIMCLIST_SYSTEM_RNG
243 static unsigned random_seed = 0;
246 static inline void seed_random(
void) {
247 if (random_seed == 0)
248 random_seed = (unsigned)getpid() ^ (unsigned)time(NULL);
251 static inline long get_random(
void) {
252 random_seed = (1664525 * random_seed + 1013904223);
258 # define seed_random()
259 # define get_random() (rand())
264 int list_init(
list_t *restrict l) {
265 if (l == NULL)
return -1;
274 l->head_sentinel->next = l->tail_sentinel;
275 l->tail_sentinel->prev = l->head_sentinel;
276 l->head_sentinel->prev = l->tail_sentinel->next = l->mid = NULL;
277 l->head_sentinel->data = l->tail_sentinel->data = NULL;
282 l->iter_curentry = NULL;
288 #ifdef SIMCLIST_WITH_THREADS
292 list_attributes_setdefaults(l);
294 assert(list_repOk(l));
295 assert(list_attrOk(l));
300 void list_destroy(
list_t *restrict l) {
304 for (i = 0; i < l->spareelsnum; i++) {
305 free(l->spareels[i]);
308 free(l->head_sentinel);
309 free(l->tail_sentinel);
312 int list_attributes_setdefaults(
list_t *restrict l) {
313 l->attrs.comparator = NULL;
314 l->attrs.seeker = NULL;
317 l->attrs.meter = NULL;
318 l->attrs.copy_data = 0;
320 l->attrs.hasher = NULL;
323 l->attrs.serializer = NULL;
324 l->attrs.unserializer = NULL;
326 assert(list_attrOk(l));
332 int list_attributes_comparator(
list_t *restrict l, element_comparator comparator_fun) {
333 if (l == NULL)
return -1;
335 l->attrs.comparator = comparator_fun;
337 assert(list_attrOk(l));
342 int list_attributes_seeker(
list_t *restrict l, element_seeker seeker_fun) {
343 if (l == NULL)
return -1;
345 l->attrs.seeker = seeker_fun;
346 assert(list_attrOk(l));
351 int list_attributes_copy(
list_t *restrict l, element_meter metric_fun,
int copy_data) {
352 if (l == NULL || (metric_fun == NULL && copy_data != 0))
return -1;
354 l->attrs.meter = metric_fun;
355 l->attrs.copy_data = copy_data;
357 assert(list_attrOk(l));
362 int list_attributes_hash_computer(
list_t *restrict l, element_hash_computer hash_computer_fun) {
363 if (l == NULL)
return -1;
365 l->attrs.hasher = hash_computer_fun;
366 assert(list_attrOk(l));
370 int list_attributes_serializer(
list_t *restrict l, element_serializer serializer_fun) {
371 if (l == NULL)
return -1;
373 l->attrs.serializer = serializer_fun;
374 assert(list_attrOk(l));
378 int list_attributes_unserializer(
list_t *restrict l, element_unserializer unserializer_fun) {
379 if (l == NULL)
return -1;
381 l->attrs.unserializer = unserializer_fun;
382 assert(list_attrOk(l));
386 int list_append(
list_t *restrict l,
const void *data) {
387 return list_insert_at(l, data, l->numels);
390 int list_prepend(
list_t *restrict l,
const void *data) {
391 return list_insert_at(l, data, 0);
394 void *list_fetch(
list_t *restrict l) {
395 return list_extract_at(l, 0);
398 void *list_get_at(
const list_t *restrict l,
unsigned int pos) {
401 tmp = list_findpos(l, pos);
403 return (tmp != NULL ? tmp->data : NULL);
406 void *list_get_max(
const list_t *restrict l) {
407 return list_get_minmax(l, +1);
410 void *list_get_min(
const list_t *restrict l) {
411 return list_get_minmax(l, -1);
416 static void *list_get_minmax(
const list_t *restrict l,
int versus) {
420 if (l->attrs.comparator == NULL || l->numels == 0)
423 curminmax = l->head_sentinel->next->data;
424 for (s = l->head_sentinel->next->next; s != l->tail_sentinel; s = s->next) {
425 if (l->attrs.comparator(curminmax, s->data) * versus > 0)
433 static inline struct list_entry_s *list_findpos(
const list_t *restrict l,
int posstart) {
439 if (posstart < -1 || posstart > (
int)l->numels)
return NULL;
441 x = (float)(posstart+1) / l->numels;
444 for (i = -1, ptr = l->head_sentinel; i < posstart; ptr = ptr->next, i++);
445 }
else if (x < 0.5) {
447 for (i = (l->numels-1)/2, ptr = l->mid; i > posstart; ptr = ptr->prev, i--);
448 }
else if (x <= 0.75) {
450 for (i = (l->numels-1)/2, ptr = l->mid; i < posstart; ptr = ptr->next, i++);
453 for (i = l->numels, ptr = l->tail_sentinel; i > posstart; ptr = ptr->prev, i--);
459 void *list_extract_at(
list_t *restrict l,
unsigned int pos) {
463 if (l->iter_active || pos >= l->numels)
return NULL;
465 tmp = list_findpos(l, pos);
469 list_drop_elem(l, tmp, pos);
472 assert(list_repOk(l));
477 int list_insert_at(
list_t *restrict l,
const void *data,
unsigned int pos) {
480 if (l->iter_active || pos > l->numels)
return -1;
483 if (l->spareelsnum > 0) {
484 lent = l->spareels[l->spareelsnum-1];
492 if (l->attrs.copy_data) {
494 size_t datalen = l->attrs.meter(data);
496 memcpy(lent->data, data, datalen);
498 lent->data = (
void*)data;
502 prec = list_findpos(l, pos-1);
513 if (l->numels == 1) {
515 }
else if (l->numels % 2) {
516 if (pos >= (l->numels-1)/2) l->mid = l->mid->next;
518 if (pos <= (l->numels-1)/2) l->mid = l->mid->prev;
521 assert(list_repOk(l));
526 int list_delete(
list_t *restrict l,
const void *data) {
529 pos = list_locate(l, data);
533 r = list_delete_at(l, pos);
537 assert(list_repOk(l));
542 int list_delete_at(
list_t *restrict l,
unsigned int pos) {
546 if (l->iter_active || pos >= l->numels)
return -1;
548 delendo = list_findpos(l, pos);
550 list_drop_elem(l, delendo, pos);
555 assert(list_repOk(l));
560 int list_delete_range(
list_t *restrict l,
unsigned int posstart,
unsigned int posend) {
562 unsigned int numdel, midposafter, i;
565 if (l->iter_active || posend < posstart || posend >= l->numels)
return -1;
567 numdel = posend - posstart + 1;
568 if (numdel == l->numels)
return list_clear(l);
570 tmp = list_findpos(l, posstart);
571 lastvalid = tmp->prev;
573 midposafter = (l->numels-1-numdel)/2;
575 midposafter = midposafter < posstart ? midposafter : midposafter+numdel;
576 movedx = midposafter - (l->numels-1)/2;
579 for (i = 0; i < (
unsigned int)movedx; l->mid = l->mid->next, i++);
582 for (i = 0; i < (
unsigned int)movedx; l->mid = l->mid->prev, i++);
585 assert(posstart == 0 || lastvalid != l->head_sentinel);
587 if (l->attrs.copy_data) {
589 for (; i <= posend; i++) {
592 if (tmp2->data != NULL) free(tmp2->data);
593 if (l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
594 l->spareels[l->spareelsnum++] = tmp2;
601 for (; i <= posend; i++) {
604 if (l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
605 l->spareels[l->spareelsnum++] = tmp2;
611 assert(i == posend+1 && (posend != l->numels || tmp == l->tail_sentinel));
613 lastvalid->next = tmp;
614 tmp->prev = lastvalid;
616 l->numels -= posend - posstart + 1;
618 assert(list_repOk(l));
623 int list_clear(
list_t *restrict l) {
630 if (l->iter_active)
return -1;
632 if (l->attrs.copy_data) {
634 for (s = l->head_sentinel->next; l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS && s != l->tail_sentinel; s = s->next) {
636 if (s->data != NULL) free(s->data);
637 l->spareels[l->spareelsnum++] = s;
639 while (s != l->tail_sentinel) {
641 if (s->data != NULL) free(s->data);
645 l->head_sentinel->next = l->tail_sentinel;
646 l->tail_sentinel->prev = l->head_sentinel;
649 for (s = l->head_sentinel->next; l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS && s != l->tail_sentinel; s = s->next) {
651 l->spareels[l->spareelsnum++] = s;
653 while (s != l->tail_sentinel) {
658 l->head_sentinel->next = l->tail_sentinel;
659 l->tail_sentinel->prev = l->head_sentinel;
664 assert(list_repOk(l));
669 unsigned int list_size(
const list_t *restrict l) {
673 int list_empty(
const list_t *restrict l) {
674 return (l->numels == 0);
677 int list_locate(
const list_t *restrict l,
const void *data) {
681 if (l->attrs.comparator != NULL) {
683 for (el = l->head_sentinel->next; el != l->tail_sentinel; el = el->next, pos++) {
684 if (l->attrs.comparator(data, el->data) == 0)
break;
688 for (el = l->head_sentinel->next; el != l->tail_sentinel; el = el->next, pos++) {
689 if (el->data == data)
break;
692 if (el == l->tail_sentinel)
return -1;
697 void *list_seek(
list_t *restrict l,
const void *indicator) {
700 if (l->attrs.seeker == NULL)
return NULL;
702 for (iter = l->head_sentinel->next; iter != l->tail_sentinel; iter = iter->next) {
703 if (l->attrs.seeker(iter->data, indicator) != 0)
return iter->data;
709 int list_contains(
const list_t *restrict l,
const void *data) {
710 return (list_locate(l, data) >= 0);
719 if (l1 == NULL || l2 == NULL || dest == NULL || l1 == dest || l2 == dest)
724 dest->numels = l1->numels + l2->numels;
725 if (dest->numels == 0)
729 srcel = l1->head_sentinel->next;
730 el = dest->head_sentinel;
731 while (srcel != l1->tail_sentinel) {
735 el->data = srcel->data;
740 srcel = l2->head_sentinel->next;
741 while (srcel != l2->tail_sentinel) {
745 el->data = srcel->data;
748 el->next = dest->tail_sentinel;
749 dest->tail_sentinel->prev = el;
752 err = l2->numels - l1->numels;
755 for (cnt = 0; cnt < (
unsigned int)err; cnt++) dest->mid = dest->mid->next;
756 }
else if (err/2 < 0) {
758 for (cnt = 0; cnt < (
unsigned int)err; cnt++) dest->mid = dest->mid->prev;
761 assert(!(list_repOk(l1) && list_repOk(l2)) || list_repOk(dest));
766 int list_sort(
list_t *restrict l,
int versus) {
767 if (l->iter_active || l->attrs.comparator == NULL)
772 list_sort_quicksort(l, versus, 0, l->head_sentinel->next, l->numels-1, l->tail_sentinel->prev);
773 assert(list_repOk(l));
777 #ifdef SIMCLIST_WITH_THREADS
778 struct list_sort_wrappedparams {
781 unsigned int first, last;
785 static void *list_sort_quicksort_threadwrapper(
void *wrapped_params) {
786 struct list_sort_wrappedparams *wp = (
struct list_sort_wrappedparams *)wrapped_params;
787 list_sort_quicksort(wp->l, wp->versus, wp->first, wp->fel, wp->last, wp->lel);
794 static inline void list_sort_selectionsort(
list_t *restrict l,
int versus,
803 for (firstunsorted = fel; firstunsorted != lel; firstunsorted = firstunsorted->next) {
805 for (toswap = firstunsorted, cursor = firstunsorted->next; cursor != lel->next; cursor = cursor->next)
806 if (l->attrs.comparator(toswap->data, cursor->data) * -versus > 0) toswap = cursor;
807 if (toswap != firstunsorted) {
808 tmpdata = firstunsorted->data;
809 firstunsorted->data = toswap->data;
810 toswap->data = tmpdata;
815 static void list_sort_quicksort(
list_t *restrict l,
int versus,
818 unsigned int pivotid;
823 #ifdef SIMCLIST_WITH_THREADS
832 if (last - first+1 <= SIMCLIST_MINQUICKSORTELS) {
833 list_sort_selectionsort(l, versus, first, fel, last, lel);
838 if (! (last > first))
return;
840 pivotid = (get_random() % (last - first + 1));
844 if (pivotid < (last - first + 1)/2) {
845 for (i = 0, pivot = fel; i < pivotid; pivot = pivot->next, i++);
847 for (i = last - first, pivot = lel; i > pivotid; pivot = pivot->prev, i--);
854 while (left != pivot && right != pivot) {
855 for (; left != pivot && (l->attrs.comparator(left->data, pivot->data) * -versus <= 0); left = left->next);
857 for (; right != pivot && (l->attrs.comparator(right->data, pivot->data) * -versus >= 0); right = right->prev);
859 if (left != pivot && right != pivot) {
861 tmpdata = left->data;
862 left->data = right->data;
863 right->data = tmpdata;
871 if (right == pivot) {
872 while (left != pivot) {
873 if (l->attrs.comparator(left->data, pivot->data) * -versus > 0) {
874 tmpdata = left->data;
875 left->data = pivot->prev->data;
876 pivot->prev->data = pivot->data;
877 pivot->data = tmpdata;
880 if (pivot == left)
break;
886 while (right != pivot) {
887 if (l->attrs.comparator(right->data, pivot->data) * -versus < 0) {
889 tmpdata = right->data;
890 right->data = pivot->next->data;
891 pivot->next->data = pivot->data;
892 pivot->data = tmpdata;
895 if (pivot == right)
break;
904 #ifdef SIMCLIST_WITH_THREADS
908 if (l->threadcount < SIMCLIST_MAXTHREADS-1) {
909 struct list_sort_wrappedparams *wp = (
struct list_sort_wrappedparams *)malloc(
sizeof(
struct list_sort_wrappedparams));
916 wp->last = first+pivotid-1;
917 wp->lel = pivot->prev;
918 if (pthread_create(&tid, NULL, list_sort_quicksort_threadwrapper, wp) != 0) {
921 list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
924 list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
927 if (first + pivotid < last) list_sort_quicksort(l, versus, first+pivotid+1, pivot->next, last, lel);
929 pthread_join(tid, (
void **)NULL);
933 if (pivotid > 0) list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
934 if (first + pivotid < last) list_sort_quicksort(l, versus, first+pivotid+1, pivot->next, last, lel);
938 int list_iterator_start(
list_t *restrict l) {
939 if (l->iter_active)
return 0;
942 l->iter_curentry = l->head_sentinel->next;
946 void *list_iterator_next(
list_t *restrict l) {
949 if (! l->iter_active)
return NULL;
951 toret = l->iter_curentry->data;
952 l->iter_curentry = l->iter_curentry->next;
958 int list_iterator_hasnext(
const list_t *restrict l) {
959 if (! l->iter_active)
return 0;
960 return (l->iter_pos < l->numels);
963 int list_iterator_stop(
list_t *restrict l) {
964 if (! l->iter_active)
return 0;
970 int list_hash(
const list_t *restrict l, list_hash_t *restrict hash) {
974 assert(hash != NULL);
976 tmphash = l->numels * 2 + 100;
977 if (l->attrs.hasher == NULL) {
978 #ifdef SIMCLIST_ALLOW_LOCATIONBASED_HASHES
980 #warning "Memlocation-based hash is consistent only for testing modification in the same program run."
984 for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
985 for (i = 0; i <
sizeof(x->data); i++) {
986 tmphash += (tmphash ^ (uintptr_t)x->data);
988 tmphash += tmphash % l->numels;
995 for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
996 tmphash += tmphash ^ l->attrs.hasher(x->data);
997 tmphash += tmphash % l->numels;
1006 #ifndef SIMCLIST_NO_DUMPRESTORE
1007 int list_dump_getinfo_filedescriptor(
int fd,
list_dump_info_t *restrict info) {
1008 int32_t terminator_head, terminator_tail;
1014 READ_ERRCHECK(fd, & info->version,
sizeof(info->version));
1015 info->version = ntohs(info->version);
1016 if (info->version > SIMCLIST_DUMPFORMAT_VERSION) {
1022 READ_ERRCHECK(fd, & info->timestamp.tv_sec,
sizeof(info->timestamp.tv_sec));
1023 info->timestamp.tv_sec = ntohl(info->timestamp.tv_sec);
1024 READ_ERRCHECK(fd, & info->timestamp.tv_usec,
sizeof(info->timestamp.tv_usec));
1025 info->timestamp.tv_usec = ntohl(info->timestamp.tv_usec);
1028 READ_ERRCHECK(fd, & terminator_head,
sizeof(terminator_head));
1029 terminator_head = ntohl(terminator_head);
1032 READ_ERRCHECK(fd, & info->list_size,
sizeof(info->list_size));
1033 info->list_size = ntohl(info->list_size);
1036 READ_ERRCHECK(fd, & info->list_numels,
sizeof(info->list_numels));
1037 info->list_numels = ntohl(info->list_numels);
1040 READ_ERRCHECK(fd, & elemlen,
sizeof(elemlen));
1041 elemlen = ntohl(elemlen);
1044 READ_ERRCHECK(fd, & info->list_hash,
sizeof(info->list_hash));
1045 info->list_hash = ntohl(info->list_hash);
1050 hop = info->list_size;
1053 hop = info->list_size + elemlen*info->list_numels;
1055 if (lseek(fd, hop, SEEK_CUR) == -1) {
1060 READ_ERRCHECK(fd, & terminator_tail,
sizeof(terminator_tail));
1061 terminator_tail = ntohl(terminator_tail);
1063 if (terminator_head == terminator_tail)
1064 info->consistent = 1;
1066 info->consistent = 0;
1071 int list_dump_getinfo_file(
const char *restrict filename,
list_dump_info_t *restrict info) {
1074 fd = open(filename, O_RDONLY, 0);
1075 if (fd < 0)
return -1;
1077 ret = list_dump_getinfo_filedescriptor(fd, info);
1083 int list_dump_filedescriptor(
const list_t *restrict l,
int fd,
size_t *restrict len) {
1087 struct timeval timeofday;
1090 if (l->attrs.meter == NULL && l->attrs.serializer == NULL) {
1111 header.ver = htons( SIMCLIST_DUMPFORMAT_VERSION );
1114 gettimeofday(&timeofday, NULL);
1115 header.timestamp_sec = htonl(timeofday.tv_sec);
1116 header.timestamp_usec = htonl(timeofday.tv_usec);
1118 header.rndterm = htonl((int32_t)get_random());
1123 header.numels = htonl(l->numels);
1126 if (l->attrs.hasher != NULL) {
1127 if (htonl(list_hash(l, & header.listhash)) != 0) {
1132 header.listhash = htonl(0);
1135 header.totlistlen = header.elemlen = 0;
1138 if (lseek(fd, SIMCLIST_DUMPFORMAT_HEADERLEN, SEEK_SET) < 0) {
1144 if (l->numels > 0) {
1147 if (l->attrs.serializer != NULL) {
1149 ser_buf = l->attrs.serializer(l->head_sentinel->next->data, & header.elemlen);
1152 for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
1153 ser_buf = l->attrs.serializer(x->data, &bufsize);
1154 header.totlistlen += bufsize;
1155 if (header.elemlen != 0) {
1156 if (header.elemlen != bufsize) {
1160 header.totlistlen = 0;
1161 x = l->head_sentinel;
1162 if (lseek(fd, SIMCLIST_DUMPFORMAT_HEADERLEN, SEEK_SET) < 0) {
1170 WRITE_ERRCHECK(fd, ser_buf, bufsize);
1172 WRITE_ERRCHECK(fd, & bufsize,
sizeof(
size_t));
1173 WRITE_ERRCHECK(fd, ser_buf, bufsize);
1177 }
else if (l->attrs.meter != NULL) {
1178 header.elemlen = (uint32_t)l->attrs.meter(l->head_sentinel->next->data);
1181 for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
1182 bufsize = l->attrs.meter(x->data);
1183 header.totlistlen += bufsize;
1184 if (header.elemlen != 0) {
1185 if (header.elemlen != bufsize) {
1188 header.totlistlen = 0;
1189 x = l->head_sentinel;
1193 WRITE_ERRCHECK(fd, x->data, bufsize);
1195 WRITE_ERRCHECK(fd, &bufsize,
sizeof(
size_t));
1196 WRITE_ERRCHECK(fd, x->data, bufsize);
1201 header.elemlen = htonl(header.elemlen);
1202 header.totlistlen = htonl(header.totlistlen);
1206 WRITE_ERRCHECK(fd, & header.rndterm,
sizeof(header.rndterm));
1210 lseek(fd, 0, SEEK_SET);
1212 WRITE_ERRCHECK(fd, & header.ver,
sizeof(header.ver));
1213 WRITE_ERRCHECK(fd, & header.timestamp_sec,
sizeof(header.timestamp_sec));
1214 WRITE_ERRCHECK(fd, & header.timestamp_usec,
sizeof(header.timestamp_usec));
1215 WRITE_ERRCHECK(fd, & header.rndterm,
sizeof(header.rndterm));
1217 WRITE_ERRCHECK(fd, & header.totlistlen,
sizeof(header.totlistlen));
1218 WRITE_ERRCHECK(fd, & header.numels,
sizeof(header.numels));
1219 WRITE_ERRCHECK(fd, & header.elemlen,
sizeof(header.elemlen));
1220 WRITE_ERRCHECK(fd, & header.listhash,
sizeof(header.listhash));
1225 *len =
sizeof(header) + ntohl(header.totlistlen);
1231 int list_restore_filedescriptor(
list_t *restrict l,
int fd,
size_t *restrict len) {
1235 uint32_t elsize, totreadlen, totmemorylen;
1237 memset(& header, 0,
sizeof(header));
1242 READ_ERRCHECK(fd, &header.ver,
sizeof(header.ver));
1243 header.ver = ntohs(header.ver);
1244 if (header.ver != SIMCLIST_DUMPFORMAT_VERSION) {
1250 READ_ERRCHECK(fd, & header.timestamp_sec,
sizeof(header.timestamp_sec));
1251 header.timestamp_sec = ntohl(header.timestamp_sec);
1252 READ_ERRCHECK(fd, & header.timestamp_usec,
sizeof(header.timestamp_usec));
1253 header.timestamp_usec = ntohl(header.timestamp_usec);
1256 READ_ERRCHECK(fd, & header.rndterm,
sizeof(header.rndterm));
1258 header.rndterm = ntohl(header.rndterm);
1261 READ_ERRCHECK(fd, & header.totlistlen,
sizeof(header.totlistlen));
1262 header.totlistlen = ntohl(header.totlistlen);
1265 READ_ERRCHECK(fd, & header.numels,
sizeof(header.numels));
1266 header.numels = ntohl(header.numels);
1269 READ_ERRCHECK(fd, & header.elemlen,
sizeof(header.elemlen));
1270 header.elemlen = ntohl(header.elemlen);
1273 READ_ERRCHECK(fd, & header.listhash,
sizeof(header.listhash));
1274 header.listhash = ntohl(header.listhash);
1278 totreadlen = totmemorylen = 0;
1279 if (header.elemlen > 0) {
1281 if (l->attrs.unserializer != NULL) {
1283 buf = malloc(header.elemlen);
1284 for (cnt = 0; cnt < header.numels; cnt++) {
1285 READ_ERRCHECK(fd, buf, header.elemlen);
1286 list_append(l, l->attrs.unserializer(buf, & elsize));
1287 totmemorylen += elsize;
1291 for (cnt = 0; cnt < header.numels; cnt++) {
1292 buf = malloc(header.elemlen);
1293 READ_ERRCHECK(fd, buf, header.elemlen);
1294 list_append(l, buf);
1296 totmemorylen = header.numels * header.elemlen;
1298 totreadlen = header.numels * header.elemlen;
1301 if (l->attrs.unserializer != NULL) {
1303 for (cnt = 0; cnt < header.numels; cnt++) {
1304 READ_ERRCHECK(fd, & elsize,
sizeof(elsize));
1305 buf = malloc((
size_t)elsize);
1306 READ_ERRCHECK(fd, buf, elsize);
1307 totreadlen += elsize;
1308 list_append(l, l->attrs.unserializer(buf, & elsize));
1309 totmemorylen += elsize;
1313 for (cnt = 0; cnt < header.numels; cnt++) {
1314 READ_ERRCHECK(fd, & elsize,
sizeof(elsize));
1315 buf = malloc(elsize);
1316 READ_ERRCHECK(fd, buf, elsize);
1317 totreadlen += elsize;
1318 list_append(l, buf);
1320 totmemorylen = totreadlen;
1324 READ_ERRCHECK(fd, &elsize,
sizeof(elsize));
1325 elsize = ntohl(elsize);
1337 if (totreadlen != header.totlistlen && (int32_t)elsize == header.rndterm) {
1343 if (lseek(fd, 0, SEEK_CUR) != lseek(fd, 0, SEEK_END)) {
1349 *len = totmemorylen;
1355 int list_dump_file(
const list_t *restrict l,
const char *restrict filename,
size_t *restrict len) {
1356 int fd, oflag, mode;
1359 oflag = O_RDWR | O_CREAT | O_TRUNC;
1360 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1362 oflag = _O_RDWR | _O_CREAT | _O_TRUNC;
1363 mode = _S_IRUSR | _S_IWUSR | _S_IRGRP | _S_IROTH;
1365 fd = open(filename, oflag, mode);
1366 if (fd < 0)
return -1;
1368 list_dump_filedescriptor(l, fd, len);
1374 int list_restore_file(
list_t *restrict l,
const char *restrict filename,
size_t *restrict len) {
1377 fd = open(filename, O_RDONLY, 0);
1378 if (fd < 0)
return -1;
1380 list_restore_filedescriptor(l, fd, len);
1388 static int list_drop_elem(
list_t *restrict l,
struct list_entry_s *tmp,
unsigned int pos) {
1389 if (tmp == NULL)
return -1;
1392 if (l->numels % 2) {
1394 if (l->numels == 1) l->mid = NULL;
1395 else if (pos >= l->numels/2) l->mid = l->mid->prev;
1397 if (pos < l->numels/2) l->mid = l->mid->next;
1400 tmp->prev->next = tmp->next;
1401 tmp->next->prev = tmp->prev;
1404 if (l->attrs.copy_data && tmp->data != NULL)
1407 if (l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
1408 l->spareels[l->spareelsnum++] = tmp;
1417 #define SIMCLIST_NUMBER_COMPARATOR(type) int list_comparator_##type(const void *a, const void *b) { return( *(type *)a < *(type *)b) - (*(type *)a > *(type *)b); }
1419 SIMCLIST_NUMBER_COMPARATOR(int8_t)
1420 SIMCLIST_NUMBER_COMPARATOR(int16_t)
1421 SIMCLIST_NUMBER_COMPARATOR(int32_t)
1422 SIMCLIST_NUMBER_COMPARATOR(int64_t)
1424 SIMCLIST_NUMBER_COMPARATOR(uint8_t)
1425 SIMCLIST_NUMBER_COMPARATOR(uint16_t)
1426 SIMCLIST_NUMBER_COMPARATOR(uint32_t)
1427 SIMCLIST_NUMBER_COMPARATOR(uint64_t)
1429 SIMCLIST_NUMBER_COMPARATOR(
float)
1430 SIMCLIST_NUMBER_COMPARATOR(
double)
1432 int list_comparator_string(const
void *a, const
void *b) {
return strcmp((
const char *)b, (
const char *)a); }
1435 #define SIMCLIST_METER(type) size_t list_meter_##type(const void *el) { if (el) { } return sizeof(type); }
1437 SIMCLIST_METER(int8_t)
1438 SIMCLIST_METER(int16_t)
1439 SIMCLIST_METER(int32_t)
1440 SIMCLIST_METER(int64_t)
1442 SIMCLIST_METER(uint8_t)
1443 SIMCLIST_METER(uint16_t)
1444 SIMCLIST_METER(uint32_t)
1445 SIMCLIST_METER(uint64_t)
1447 SIMCLIST_METER(
float)
1448 SIMCLIST_METER(
double)
1450 size_t list_meter_string(const
void *el) {
return strlen((
const char *)el) + 1; }
1453 #define SIMCLIST_HASHCOMPUTER(type) list_hash_t list_hashcomputer_##type(const void *el) { return (list_hash_t)(*(type *)el); }
1455 SIMCLIST_HASHCOMPUTER(int8_t)
1456 SIMCLIST_HASHCOMPUTER(int16_t)
1457 SIMCLIST_HASHCOMPUTER(int32_t)
1458 SIMCLIST_HASHCOMPUTER(int64_t)
1460 SIMCLIST_HASHCOMPUTER(uint8_t)
1461 SIMCLIST_HASHCOMPUTER(uint16_t)
1462 SIMCLIST_HASHCOMPUTER(uint32_t)
1463 SIMCLIST_HASHCOMPUTER(uint64_t)
1465 SIMCLIST_HASHCOMPUTER(
float)
1466 SIMCLIST_HASHCOMPUTER(
double)
1468 list_hash_t list_hashcomputer_string(const
void *el) {
1470 list_hash_t hash = 123;
1471 const char *str = (
const char *)el;
1474 for (l = 0; str[l] !=
'\0'; l++) {
1475 if (l) plus = hash ^ str[l];
1476 else plus = hash ^ (str[l] - str[0]);
1477 hash += (plus << (CHAR_BIT * (l %
sizeof(list_hash_t))));
1485 static int list_repOk(
const list_t *restrict l) {
1489 ok = (l != NULL) && (
1491 (l->head_sentinel != NULL && l->tail_sentinel != NULL) &&
1492 (l->head_sentinel != l->tail_sentinel) && (l->head_sentinel->prev == NULL && l->tail_sentinel->next == NULL) &&
1494 (l->numels > 0 || (l->mid == NULL && l->head_sentinel->next == l->tail_sentinel && l->tail_sentinel->prev == l->head_sentinel)) &&
1496 l->spareelsnum <= SIMCLIST_MAX_SPARE_ELEMS
1501 if (l->numels >= 1) {
1503 for (i = -1, s = l->head_sentinel; i < (
int)(l->numels-1)/2 && s->next != NULL; i++, s = s->next) {
1504 if (s->next->prev != s)
break;
1506 ok = (i == (int)(l->numels-1)/2 && l->mid == s);
1508 for (; s->next != NULL; i++, s = s->next) {
1509 if (s->next->prev != s)
break;
1511 ok = (i == (int)l->numels && s == l->tail_sentinel);
1517 static int list_attrOk(
const list_t *restrict l) {
1520 ok = (l->attrs.copy_data == 0 || l->attrs.meter != NULL);