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;
267 memset(l, 0,
sizeof *l);
276 if (NULL == l->tail_sentinel || NULL == l->head_sentinel)
279 l->head_sentinel->next = l->tail_sentinel;
280 l->tail_sentinel->prev = l->head_sentinel;
281 l->head_sentinel->prev = l->tail_sentinel->next = l->mid = NULL;
282 l->head_sentinel->data = l->tail_sentinel->data = NULL;
287 l->iter_curentry = NULL;
292 if (NULL == l->spareels)
295 #ifdef SIMCLIST_WITH_THREADS 299 if (list_attributes_setdefaults(l))
302 assert(list_repOk(l));
303 assert(list_attrOk(l));
308 void list_destroy(
list_t *restrict l) {
312 for (i = 0; i < l->spareelsnum; i++) {
313 free(l->spareels[i]);
316 free(l->head_sentinel);
317 free(l->tail_sentinel);
320 int list_attributes_setdefaults(
list_t *restrict l) {
321 l->attrs.comparator = NULL;
322 l->attrs.seeker = NULL;
325 l->attrs.meter = NULL;
326 l->attrs.copy_data = 0;
328 l->attrs.hasher = NULL;
331 l->attrs.serializer = NULL;
332 l->attrs.unserializer = NULL;
334 assert(list_attrOk(l));
340 int list_attributes_comparator(
list_t *restrict l, element_comparator comparator_fun) {
341 if (l == NULL)
return -1;
343 l->attrs.comparator = comparator_fun;
345 assert(list_attrOk(l));
350 int list_attributes_seeker(
list_t *restrict l, element_seeker seeker_fun) {
351 if (l == NULL)
return -1;
353 l->attrs.seeker = seeker_fun;
354 assert(list_attrOk(l));
359 int list_attributes_copy(
list_t *restrict l, element_meter metric_fun,
int copy_data) {
360 if (l == NULL || (metric_fun == NULL && copy_data != 0))
return -1;
362 l->attrs.meter = metric_fun;
363 l->attrs.copy_data = copy_data;
365 assert(list_attrOk(l));
370 int list_attributes_hash_computer(
list_t *restrict l, element_hash_computer hash_computer_fun) {
371 if (l == NULL)
return -1;
373 l->attrs.hasher = hash_computer_fun;
374 assert(list_attrOk(l));
378 int list_attributes_serializer(
list_t *restrict l, element_serializer serializer_fun) {
379 if (l == NULL)
return -1;
381 l->attrs.serializer = serializer_fun;
382 assert(list_attrOk(l));
386 int list_attributes_unserializer(
list_t *restrict l, element_unserializer unserializer_fun) {
387 if (l == NULL)
return -1;
389 l->attrs.unserializer = unserializer_fun;
390 assert(list_attrOk(l));
394 int list_append(
list_t *restrict l,
const void *data) {
395 return list_insert_at(l, data, l->numels);
398 int list_prepend(
list_t *restrict l,
const void *data) {
399 return list_insert_at(l, data, 0);
402 void *list_fetch(
list_t *restrict l) {
403 return list_extract_at(l, 0);
406 void *list_get_at(
const list_t *restrict l,
unsigned int pos) {
409 tmp = list_findpos(l, pos);
411 return (tmp != NULL ? tmp->data : NULL);
414 void *list_get_max(
const list_t *restrict l) {
415 return list_get_minmax(l, +1);
418 void *list_get_min(
const list_t *restrict l) {
419 return list_get_minmax(l, -1);
424 static void *list_get_minmax(
const list_t *restrict l,
int versus) {
428 if (l->attrs.comparator == NULL || l->numels == 0)
431 curminmax = l->head_sentinel->next->data;
432 for (s = l->head_sentinel->next->next; s != l->tail_sentinel; s = s->next) {
433 if (l->attrs.comparator(curminmax, s->data) * versus > 0)
441 static inline struct list_entry_s *list_findpos(
const list_t *restrict l,
int posstart) {
446 if (NULL == l->head_sentinel || NULL == l->tail_sentinel)
450 if (posstart < -1 || posstart > (
int)l->numels)
return NULL;
452 x = (float)(posstart+1) / l->numels;
455 for (i = -1, ptr = l->head_sentinel; i < posstart; ptr = ptr->next, i++);
456 }
else if (x < 0.5) {
458 for (i = (l->numels-1)/2, ptr = l->mid; i > posstart; ptr = ptr->prev, i--);
459 }
else if (x <= 0.75) {
461 for (i = (l->numels-1)/2, ptr = l->mid; i < posstart; ptr = ptr->next, i++);
464 for (i = l->numels, ptr = l->tail_sentinel; i > posstart; ptr = ptr->prev, i--);
470 void *list_extract_at(
list_t *restrict l,
unsigned int pos) {
474 if (l->iter_active || pos >= l->numels)
return NULL;
476 tmp = list_findpos(l, pos);
483 list_drop_elem(l, tmp, pos);
486 assert(list_repOk(l));
491 int list_insert_at(
list_t *restrict l,
const void *data,
unsigned int pos) {
494 if (l->iter_active || pos > l->numels)
return -1;
497 if (l->spareelsnum > 0) {
498 lent = l->spareels[l->spareelsnum-1];
506 if (l->attrs.copy_data) {
508 size_t datalen = l->attrs.meter(data);
510 if (NULL == lent->data)
515 memcpy(lent->data, data, datalen);
517 lent->data = (
void*)data;
521 prec = list_findpos(l, pos-1);
538 if (l->numels == 1) {
540 }
else if (l->numels % 2) {
541 if (pos >= (l->numels-1)/2) l->mid = l->mid->next;
543 if (pos <= (l->numels-1)/2) l->mid = l->mid->prev;
546 assert(list_repOk(l));
551 int list_delete(
list_t *restrict l,
const void *data) {
554 pos = list_locate(l, data);
558 r = list_delete_at(l, pos);
562 assert(list_repOk(l));
567 int list_delete_at(
list_t *restrict l,
unsigned int pos) {
571 if (l->iter_active || pos >= l->numels)
return -1;
573 delendo = list_findpos(l, pos);
575 list_drop_elem(l, delendo, pos);
580 assert(list_repOk(l));
585 int list_delete_range(
list_t *restrict l,
unsigned int posstart,
unsigned int posend) {
587 unsigned int numdel, midposafter, i;
590 if (l->iter_active || posend < posstart || posend >= l->numels)
return -1;
592 numdel = posend - posstart + 1;
593 if (numdel == l->numels)
return list_clear(l);
595 tmp = list_findpos(l, posstart);
596 lastvalid = tmp->prev;
598 midposafter = (l->numels-1-numdel)/2;
600 midposafter = midposafter < posstart ? midposafter : midposafter+numdel;
601 movedx = midposafter - (l->numels-1)/2;
604 for (i = 0; i < (
unsigned int)movedx; l->mid = l->mid->next, i++);
607 for (i = 0; i < (
unsigned int)movedx; l->mid = l->mid->prev, i++);
610 assert(posstart == 0 || lastvalid != l->head_sentinel);
612 if (l->attrs.copy_data) {
614 for (; i <= posend; i++) {
617 if (tmp2->data != NULL) free(tmp2->data);
618 if (l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
619 l->spareels[l->spareelsnum++] = tmp2;
626 for (; i <= posend; i++) {
629 if (l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
630 l->spareels[l->spareelsnum++] = tmp2;
636 assert(i == posend+1 && (posend != l->numels || tmp == l->tail_sentinel));
638 lastvalid->next = tmp;
639 tmp->prev = lastvalid;
641 l->numels -= posend - posstart + 1;
643 assert(list_repOk(l));
648 int list_clear(
list_t *restrict l) {
655 if (l->iter_active)
return -1;
657 if (l->head_sentinel && l->tail_sentinel) {
658 if (l->attrs.copy_data) {
660 for (s = l->head_sentinel->next; l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS && s != l->tail_sentinel; s = s->next) {
662 if (s->data != NULL) free(s->data);
663 l->spareels[l->spareelsnum++] = s;
665 while (s != l->tail_sentinel) {
667 if (s->data != NULL) free(s->data);
671 l->head_sentinel->next = l->tail_sentinel;
672 l->tail_sentinel->prev = l->head_sentinel;
675 for (s = l->head_sentinel->next; l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS && s != l->tail_sentinel; s = s->next) {
677 l->spareels[l->spareelsnum++] = s;
679 while (s != l->tail_sentinel) {
684 l->head_sentinel->next = l->tail_sentinel;
685 l->tail_sentinel->prev = l->head_sentinel;
691 assert(list_repOk(l));
696 unsigned int list_size(
const list_t *restrict l) {
700 int list_empty(
const list_t *restrict l) {
701 return (l->numels == 0);
704 int list_locate(
const list_t *restrict l,
const void *data) {
708 if (NULL == l->head_sentinel || NULL == l->tail_sentinel)
711 if (l->attrs.comparator != NULL) {
713 for (el = l->head_sentinel->next; el != l->tail_sentinel; el = el->next, pos++) {
714 if (l->attrs.comparator(data, el->data) == 0)
break;
718 for (el = l->head_sentinel->next; el != l->tail_sentinel; el = el->next, pos++) {
719 if (el->data == data)
break;
722 if (el == l->tail_sentinel)
return -1;
727 void *list_seek(
list_t *restrict l,
const void *indicator) {
730 if (l->attrs.seeker == NULL)
return NULL;
732 if (NULL == l->head_sentinel || NULL == l->tail_sentinel)
735 for (iter = l->head_sentinel->next; iter != l->tail_sentinel; iter = iter->next) {
736 if (l->attrs.seeker(iter->data, indicator) != 0)
return iter->data;
742 int list_contains(
const list_t *restrict l,
const void *data) {
743 return (list_locate(l, data) >= 0);
752 if (l1 == NULL || l2 == NULL || dest == NULL || l1 == dest || l2 == dest)
755 if (NULL == l1->head_sentinel || NULL == l1->tail_sentinel
756 || NULL == l2->head_sentinel || NULL == l2->tail_sentinel)
762 dest->numels = l1->numels + l2->numels;
763 if (dest->numels == 0)
767 srcel = l1->head_sentinel->next;
768 el = dest->head_sentinel;
769 while (srcel != l1->tail_sentinel) {
771 if (NULL == el->next)
775 el->data = srcel->data;
780 srcel = l2->head_sentinel->next;
781 while (srcel != l2->tail_sentinel) {
783 if (NULL == el->next)
787 el->data = srcel->data;
790 el->next = dest->tail_sentinel;
791 dest->tail_sentinel->prev = el;
794 err = l2->numels - l1->numels;
797 for (cnt = 0; cnt < (
unsigned int)err; cnt++) dest->mid = dest->mid->next;
798 }
else if (err/2 < 0) {
800 for (cnt = 0; cnt < (
unsigned int)err; cnt++) dest->mid = dest->mid->prev;
803 assert(!(list_repOk(l1) && list_repOk(l2)) || list_repOk(dest));
808 int list_sort(
list_t *restrict l,
int versus) {
809 if (l->iter_active || l->attrs.comparator == NULL)
815 if (NULL == l->head_sentinel || NULL == l->tail_sentinel)
818 list_sort_quicksort(l, versus, 0, l->head_sentinel->next, l->numels-1, l->tail_sentinel->prev);
819 assert(list_repOk(l));
823 #ifdef SIMCLIST_WITH_THREADS 824 struct list_sort_wrappedparams {
827 unsigned int first, last;
831 static void *list_sort_quicksort_threadwrapper(
void *wrapped_params) {
832 struct list_sort_wrappedparams *wp = (
struct list_sort_wrappedparams *)wrapped_params;
833 list_sort_quicksort(wp->l, wp->versus, wp->first, wp->fel, wp->last, wp->lel);
840 static inline void list_sort_selectionsort(
list_t *restrict l,
int versus,
849 for (firstunsorted = fel; firstunsorted != lel; firstunsorted = firstunsorted->next) {
851 for (toswap = firstunsorted, cursor = firstunsorted->next; cursor != lel->next; cursor = cursor->next)
852 if (l->attrs.comparator(toswap->data, cursor->data) * -versus > 0) toswap = cursor;
853 if (toswap != firstunsorted) {
854 tmpdata = firstunsorted->data;
855 firstunsorted->data = toswap->data;
856 toswap->data = tmpdata;
861 static void list_sort_quicksort(
list_t *restrict l,
int versus,
864 unsigned int pivotid;
869 #ifdef SIMCLIST_WITH_THREADS 878 if (last - first+1 <= SIMCLIST_MINQUICKSORTELS) {
879 list_sort_selectionsort(l, versus, first, fel, last, lel);
884 if (! (last > first))
return;
886 pivotid = (get_random() % (last - first + 1));
890 if (pivotid < (last - first + 1)/2) {
891 for (i = 0, pivot = fel; i < pivotid; pivot = pivot->next, i++);
893 for (i = last - first, pivot = lel; i > pivotid; pivot = pivot->prev, i--);
900 while (left != pivot && right != pivot) {
901 for (; left != pivot && (l->attrs.comparator(left->data, pivot->data) * -versus <= 0); left = left->next);
903 for (; right != pivot && (l->attrs.comparator(right->data, pivot->data) * -versus >= 0); right = right->prev);
905 if (left != pivot && right != pivot) {
907 tmpdata = left->data;
908 left->data = right->data;
909 right->data = tmpdata;
917 if (right == pivot) {
918 while (left != pivot) {
919 if (l->attrs.comparator(left->data, pivot->data) * -versus > 0) {
920 tmpdata = left->data;
921 left->data = pivot->prev->data;
922 pivot->prev->data = pivot->data;
923 pivot->data = tmpdata;
926 if (pivot == left)
break;
932 while (right != pivot) {
933 if (l->attrs.comparator(right->data, pivot->data) * -versus < 0) {
935 tmpdata = right->data;
936 right->data = pivot->next->data;
937 pivot->next->data = pivot->data;
938 pivot->data = tmpdata;
941 if (pivot == right)
break;
950 #ifdef SIMCLIST_WITH_THREADS 954 if (l->threadcount < SIMCLIST_MAXTHREADS-1) {
955 struct list_sort_wrappedparams *wp = (
struct list_sort_wrappedparams *)malloc(
sizeof(
struct list_sort_wrappedparams));
964 wp->last = first+pivotid-1;
965 wp->lel = pivot->prev;
966 if (pthread_create(&tid, NULL, list_sort_quicksort_threadwrapper, wp) != 0) {
969 list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
972 list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
975 if (first + pivotid < last) list_sort_quicksort(l, versus, first+pivotid+1, pivot->next, last, lel);
977 pthread_join(tid, (
void **)NULL);
981 if (pivotid > 0) list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
982 if (first + pivotid < last) list_sort_quicksort(l, versus, first+pivotid+1, pivot->next, last, lel);
986 int list_iterator_start(
list_t *restrict l) {
987 if (l->iter_active)
return 0;
988 if (NULL == l->head_sentinel)
992 l->iter_curentry = l->head_sentinel->next;
996 void *list_iterator_next(
list_t *restrict l) {
999 if (! l->iter_active)
return NULL;
1001 toret = l->iter_curentry->data;
1002 l->iter_curentry = l->iter_curentry->next;
1008 int list_iterator_hasnext(
const list_t *restrict l) {
1009 if (! l->iter_active)
return 0;
1010 return (l->iter_pos < l->numels);
1013 int list_iterator_stop(
list_t *restrict l) {
1014 if (! l->iter_active)
return 0;
1020 int list_hash(
const list_t *restrict l, list_hash_t *restrict hash) {
1022 list_hash_t tmphash;
1024 assert(hash != NULL);
1026 tmphash = l->numels * 2 + 100;
1027 if (l->attrs.hasher == NULL) {
1028 #ifdef SIMCLIST_ALLOW_LOCATIONBASED_HASHES 1030 #warning "Memlocation-based hash is consistent only for testing modification in the same program run." 1034 for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
1035 for (i = 0; i <
sizeof(x->data); i++) {
1036 tmphash += (tmphash ^ (uintptr_t)x->data);
1038 tmphash += tmphash % l->numels;
1045 for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
1046 tmphash += tmphash ^ l->attrs.hasher(x->data);
1047 tmphash += tmphash % l->numels;
1056 #ifndef SIMCLIST_NO_DUMPRESTORE 1057 int list_dump_getinfo_filedescriptor(
int fd,
list_dump_info_t *restrict info) {
1058 int32_t terminator_head, terminator_tail;
1064 READ_ERRCHECK(fd, & info->version,
sizeof(info->version));
1065 info->version = ntohs(info->version);
1066 if (info->version > SIMCLIST_DUMPFORMAT_VERSION) {
1072 READ_ERRCHECK(fd, & info->timestamp.tv_sec,
sizeof(info->timestamp.tv_sec));
1073 info->timestamp.tv_sec = ntohl(info->timestamp.tv_sec);
1074 READ_ERRCHECK(fd, & info->timestamp.tv_usec,
sizeof(info->timestamp.tv_usec));
1075 info->timestamp.tv_usec = ntohl(info->timestamp.tv_usec);
1078 READ_ERRCHECK(fd, & terminator_head,
sizeof(terminator_head));
1079 terminator_head = ntohl(terminator_head);
1082 READ_ERRCHECK(fd, & info->list_size,
sizeof(info->list_size));
1083 info->list_size = ntohl(info->list_size);
1086 READ_ERRCHECK(fd, & info->list_numels,
sizeof(info->list_numels));
1087 info->list_numels = ntohl(info->list_numels);
1090 READ_ERRCHECK(fd, & elemlen,
sizeof(elemlen));
1091 elemlen = ntohl(elemlen);
1094 READ_ERRCHECK(fd, & info->list_hash,
sizeof(info->list_hash));
1095 info->list_hash = ntohl(info->list_hash);
1100 hop = info->list_size;
1103 hop = info->list_size + elemlen*info->list_numels;
1105 if (lseek(fd, hop, SEEK_CUR) == -1) {
1110 READ_ERRCHECK(fd, & terminator_tail,
sizeof(terminator_tail));
1111 terminator_tail = ntohl(terminator_tail);
1113 if (terminator_head == terminator_tail)
1114 info->consistent = 1;
1116 info->consistent = 0;
1121 int list_dump_getinfo_file(
const char *restrict filename,
list_dump_info_t *restrict info) {
1124 fd = open(filename, O_RDONLY, 0);
1125 if (fd < 0)
return -1;
1127 ret = list_dump_getinfo_filedescriptor(fd, info);
1133 int list_dump_filedescriptor(
const list_t *restrict l,
int fd,
size_t *restrict len) {
1137 struct timeval timeofday;
1140 if (l->attrs.meter == NULL && l->attrs.serializer == NULL) {
1161 header.ver = htons( SIMCLIST_DUMPFORMAT_VERSION );
1164 gettimeofday(&timeofday, NULL);
1165 header.timestamp_sec = htonl(timeofday.tv_sec);
1166 header.timestamp_usec = htonl(timeofday.tv_usec);
1168 header.rndterm = htonl((int32_t)get_random());
1173 header.numels = htonl(l->numels);
1176 if (l->attrs.hasher != NULL) {
1177 if (htonl(list_hash(l, & header.listhash)) != 0) {
1182 header.listhash = htonl(0);
1185 header.totlistlen = header.elemlen = 0;
1188 if (lseek(fd, SIMCLIST_DUMPFORMAT_HEADERLEN, SEEK_SET) < 0) {
1194 if (l->numels > 0) {
1197 if (l->attrs.serializer != NULL) {
1199 ser_buf = l->attrs.serializer(l->head_sentinel->next->data, & header.elemlen);
1202 for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
1203 ser_buf = l->attrs.serializer(x->data, &bufsize);
1204 header.totlistlen += bufsize;
1205 if (header.elemlen != 0) {
1206 if (header.elemlen != bufsize) {
1210 header.totlistlen = 0;
1211 x = l->head_sentinel;
1212 if (lseek(fd, SIMCLIST_DUMPFORMAT_HEADERLEN, SEEK_SET) < 0) {
1220 WRITE_ERRCHECK(fd, ser_buf, bufsize);
1222 WRITE_ERRCHECK(fd, & bufsize,
sizeof(
size_t));
1223 WRITE_ERRCHECK(fd, ser_buf, bufsize);
1227 }
else if (l->attrs.meter != NULL) {
1228 header.elemlen = (uint32_t)l->attrs.meter(l->head_sentinel->next->data);
1231 for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
1232 bufsize = l->attrs.meter(x->data);
1233 header.totlistlen += bufsize;
1234 if (header.elemlen != 0) {
1235 if (header.elemlen != bufsize) {
1238 header.totlistlen = 0;
1239 x = l->head_sentinel;
1243 WRITE_ERRCHECK(fd, x->data, bufsize);
1245 WRITE_ERRCHECK(fd, &bufsize,
sizeof(
size_t));
1246 WRITE_ERRCHECK(fd, x->data, bufsize);
1251 header.elemlen = htonl(header.elemlen);
1252 header.totlistlen = htonl(header.totlistlen);
1256 WRITE_ERRCHECK(fd, & header.rndterm,
sizeof(header.rndterm));
1260 lseek(fd, 0, SEEK_SET);
1262 WRITE_ERRCHECK(fd, & header.ver,
sizeof(header.ver));
1263 WRITE_ERRCHECK(fd, & header.timestamp_sec,
sizeof(header.timestamp_sec));
1264 WRITE_ERRCHECK(fd, & header.timestamp_usec,
sizeof(header.timestamp_usec));
1265 WRITE_ERRCHECK(fd, & header.rndterm,
sizeof(header.rndterm));
1267 WRITE_ERRCHECK(fd, & header.totlistlen,
sizeof(header.totlistlen));
1268 WRITE_ERRCHECK(fd, & header.numels,
sizeof(header.numels));
1269 WRITE_ERRCHECK(fd, & header.elemlen,
sizeof(header.elemlen));
1270 WRITE_ERRCHECK(fd, & header.listhash,
sizeof(header.listhash));
1275 *len =
sizeof(header) + ntohl(header.totlistlen);
1281 int list_restore_filedescriptor(
list_t *restrict l,
int fd,
size_t *restrict len) {
1285 uint32_t elsize, totreadlen, totmemorylen;
1287 memset(& header, 0,
sizeof(header));
1292 READ_ERRCHECK(fd, &header.ver,
sizeof(header.ver));
1293 header.ver = ntohs(header.ver);
1294 if (header.ver != SIMCLIST_DUMPFORMAT_VERSION) {
1300 READ_ERRCHECK(fd, & header.timestamp_sec,
sizeof(header.timestamp_sec));
1301 header.timestamp_sec = ntohl(header.timestamp_sec);
1302 READ_ERRCHECK(fd, & header.timestamp_usec,
sizeof(header.timestamp_usec));
1303 header.timestamp_usec = ntohl(header.timestamp_usec);
1306 READ_ERRCHECK(fd, & header.rndterm,
sizeof(header.rndterm));
1308 header.rndterm = ntohl(header.rndterm);
1311 READ_ERRCHECK(fd, & header.totlistlen,
sizeof(header.totlistlen));
1312 header.totlistlen = ntohl(header.totlistlen);
1315 READ_ERRCHECK(fd, & header.numels,
sizeof(header.numels));
1316 header.numels = ntohl(header.numels);
1319 READ_ERRCHECK(fd, & header.elemlen,
sizeof(header.elemlen));
1320 header.elemlen = ntohl(header.elemlen);
1323 READ_ERRCHECK(fd, & header.listhash,
sizeof(header.listhash));
1324 header.listhash = ntohl(header.listhash);
1328 totreadlen = totmemorylen = 0;
1329 if (header.elemlen > 0) {
1331 if (l->attrs.unserializer != NULL) {
1333 buf = malloc(header.elemlen);
1336 for (cnt = 0; cnt < header.numels; cnt++) {
1337 READ_ERRCHECK(fd, buf, header.elemlen);
1338 list_append(l, l->attrs.unserializer(buf, & elsize));
1339 totmemorylen += elsize;
1343 for (cnt = 0; cnt < header.numels; cnt++) {
1344 buf = malloc(header.elemlen);
1347 READ_ERRCHECK(fd, buf, header.elemlen);
1348 list_append(l, buf);
1350 totmemorylen = header.numels * header.elemlen;
1352 totreadlen = header.numels * header.elemlen;
1355 if (l->attrs.unserializer != NULL) {
1357 for (cnt = 0; cnt < header.numels; cnt++) {
1358 READ_ERRCHECK(fd, & elsize,
sizeof(elsize));
1359 buf = malloc((
size_t)elsize);
1362 READ_ERRCHECK(fd, buf, elsize);
1363 totreadlen += elsize;
1364 list_append(l, l->attrs.unserializer(buf, & elsize));
1365 totmemorylen += elsize;
1369 for (cnt = 0; cnt < header.numels; cnt++) {
1370 READ_ERRCHECK(fd, & elsize,
sizeof(elsize));
1371 buf = malloc(elsize);
1374 READ_ERRCHECK(fd, buf, elsize);
1375 totreadlen += elsize;
1376 list_append(l, buf);
1378 totmemorylen = totreadlen;
1382 READ_ERRCHECK(fd, &elsize,
sizeof(elsize));
1383 elsize = ntohl(elsize);
1395 if (totreadlen != header.totlistlen && (int32_t)elsize == header.rndterm) {
1401 if (lseek(fd, 0, SEEK_CUR) != lseek(fd, 0, SEEK_END)) {
1407 *len = totmemorylen;
1413 int list_dump_file(
const list_t *restrict l,
const char *restrict filename,
size_t *restrict len) {
1414 int fd, oflag, mode;
1417 oflag = O_RDWR | O_CREAT | O_TRUNC;
1418 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1420 oflag = _O_RDWR | _O_CREAT | _O_TRUNC;
1421 mode = _S_IRUSR | _S_IWUSR | _S_IRGRP | _S_IROTH;
1423 fd = open(filename, oflag, mode);
1424 if (fd < 0)
return -1;
1426 list_dump_filedescriptor(l, fd, len);
1432 int list_restore_file(
list_t *restrict l,
const char *restrict filename,
size_t *restrict len) {
1435 fd = open(filename, O_RDONLY, 0);
1436 if (fd < 0)
return -1;
1438 list_restore_filedescriptor(l, fd, len);
1446 static int list_drop_elem(
list_t *restrict l,
struct list_entry_s *tmp,
unsigned int pos) {
1447 if (tmp == NULL)
return -1;
1450 if (l->numels % 2) {
1452 if (l->numels == 1) l->mid = NULL;
1453 else if (pos >= l->numels/2) l->mid = l->mid->prev;
1455 if (pos < l->numels/2) l->mid = l->mid->next;
1458 tmp->prev->next = tmp->next;
1459 tmp->next->prev = tmp->prev;
1462 if (l->attrs.copy_data && tmp->data != NULL)
1465 if (l->spareels != NULL && l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
1466 l->spareels[l->spareelsnum++] = tmp;
1475 #define SIMCLIST_NUMBER_COMPARATOR(type) int list_comparator_##type(const void *a, const void *b) { return( *(type *)a < *(type *)b) - (*(type *)a > *(type *)b); } 1477 SIMCLIST_NUMBER_COMPARATOR(int8_t)
1478 SIMCLIST_NUMBER_COMPARATOR(int16_t)
1479 SIMCLIST_NUMBER_COMPARATOR(int32_t)
1480 SIMCLIST_NUMBER_COMPARATOR(int64_t)
1482 SIMCLIST_NUMBER_COMPARATOR(uint8_t)
1483 SIMCLIST_NUMBER_COMPARATOR(uint16_t)
1484 SIMCLIST_NUMBER_COMPARATOR(uint32_t)
1485 SIMCLIST_NUMBER_COMPARATOR(uint64_t)
1487 SIMCLIST_NUMBER_COMPARATOR(
float)
1488 SIMCLIST_NUMBER_COMPARATOR(
double)
1490 int list_comparator_string(
const void *a,
const void *b) {
return strcmp((
const char *)b, (
const char *)a); }
1493 #define SIMCLIST_METER(type) size_t list_meter_##type(const void *el) { if (el) { } return sizeof(type); } 1495 SIMCLIST_METER(int8_t)
1496 SIMCLIST_METER(int16_t)
1497 SIMCLIST_METER(int32_t)
1498 SIMCLIST_METER(int64_t)
1500 SIMCLIST_METER(uint8_t)
1501 SIMCLIST_METER(uint16_t)
1502 SIMCLIST_METER(uint32_t)
1503 SIMCLIST_METER(uint64_t)
1505 SIMCLIST_METER(
float)
1506 SIMCLIST_METER(
double)
1508 size_t list_meter_string(
const void *el) {
return strlen((
const char *)el) + 1; }
1511 #define SIMCLIST_HASHCOMPUTER(type) list_hash_t list_hashcomputer_##type(const void *el) { return (list_hash_t)(*(type *)el); } 1513 SIMCLIST_HASHCOMPUTER(int8_t)
1514 SIMCLIST_HASHCOMPUTER(int16_t)
1515 SIMCLIST_HASHCOMPUTER(int32_t)
1516 SIMCLIST_HASHCOMPUTER(int64_t)
1518 SIMCLIST_HASHCOMPUTER(uint8_t)
1519 SIMCLIST_HASHCOMPUTER(uint16_t)
1520 SIMCLIST_HASHCOMPUTER(uint32_t)
1521 SIMCLIST_HASHCOMPUTER(uint64_t)
1523 SIMCLIST_HASHCOMPUTER(
float)
1524 SIMCLIST_HASHCOMPUTER(
double)
1526 list_hash_t list_hashcomputer_string(
const void *el) {
1528 list_hash_t hash = 123;
1529 const char *str = (
const char *)el;
1532 for (l = 0; str[l] !=
'\0'; l++) {
1533 if (l) plus = hash ^ str[l];
1534 else plus = hash ^ (str[l] - str[0]);
1535 hash += (plus << (CHAR_BIT * (l %
sizeof(list_hash_t))));
1543 static int list_repOk(
const list_t *restrict l) {
1547 ok = (l != NULL) && (
1549 (l->head_sentinel != NULL && l->tail_sentinel != NULL) &&
1550 (l->head_sentinel != l->tail_sentinel) && (l->head_sentinel->prev == NULL && l->tail_sentinel->next == NULL) &&
1552 (l->numels > 0 || (l->mid == NULL && l->head_sentinel->next == l->tail_sentinel && l->tail_sentinel->prev == l->head_sentinel)) &&
1554 l->spareelsnum <= SIMCLIST_MAX_SPARE_ELEMS
1559 if (l->numels >= 1) {
1561 for (i = -1, s = l->head_sentinel; i < (
int)(l->numels-1)/2 && s->next != NULL; i++, s = s->next) {
1562 if (s->next->prev != s)
break;
1564 ok = (i == (int)(l->numels-1)/2 && l->mid == s);
1566 for (; s->next != NULL; i++, s = s->next) {
1567 if (s->next->prev != s)
break;
1569 ok = (i == (int)l->numels && s == l->tail_sentinel);
1575 static int list_attrOk(
const list_t *restrict l) {
1578 ok = (l->attrs.copy_data == 0 || l->attrs.meter != NULL);