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)
512 memcpy(lent->data, data, datalen);
514 lent->data = (
void*)data;
518 prec = list_findpos(l, pos-1);
531 if (l->numels == 1) {
533 }
else if (l->numels % 2) {
534 if (pos >= (l->numels-1)/2) l->mid = l->mid->next;
536 if (pos <= (l->numels-1)/2) l->mid = l->mid->prev;
539 assert(list_repOk(l));
544 int list_delete(
list_t *restrict l,
const void *data) {
547 pos = list_locate(l, data);
551 r = list_delete_at(l, pos);
555 assert(list_repOk(l));
560 int list_delete_at(
list_t *restrict l,
unsigned int pos) {
564 if (l->iter_active || pos >= l->numels)
return -1;
566 delendo = list_findpos(l, pos);
568 list_drop_elem(l, delendo, pos);
573 assert(list_repOk(l));
578 int list_delete_range(
list_t *restrict l,
unsigned int posstart,
unsigned int posend) {
580 unsigned int numdel, midposafter, i;
583 if (l->iter_active || posend < posstart || posend >= l->numels)
return -1;
585 numdel = posend - posstart + 1;
586 if (numdel == l->numels)
return list_clear(l);
588 tmp = list_findpos(l, posstart);
589 lastvalid = tmp->prev;
591 midposafter = (l->numels-1-numdel)/2;
593 midposafter = midposafter < posstart ? midposafter : midposafter+numdel;
594 movedx = midposafter - (l->numels-1)/2;
597 for (i = 0; i < (
unsigned int)movedx; l->mid = l->mid->next, i++);
600 for (i = 0; i < (
unsigned int)movedx; l->mid = l->mid->prev, i++);
603 assert(posstart == 0 || lastvalid != l->head_sentinel);
605 if (l->attrs.copy_data) {
607 for (; i <= posend; i++) {
610 if (tmp2->data != NULL) free(tmp2->data);
611 if (l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
612 l->spareels[l->spareelsnum++] = tmp2;
619 for (; i <= posend; i++) {
622 if (l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
623 l->spareels[l->spareelsnum++] = tmp2;
629 assert(i == posend+1 && (posend != l->numels || tmp == l->tail_sentinel));
631 lastvalid->next = tmp;
632 tmp->prev = lastvalid;
634 l->numels -= posend - posstart + 1;
636 assert(list_repOk(l));
641 int list_clear(
list_t *restrict l) {
648 if (l->iter_active)
return -1;
650 if (l->head_sentinel && l->tail_sentinel) {
651 if (l->attrs.copy_data) {
653 for (s = l->head_sentinel->next; l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS && s != l->tail_sentinel; s = s->next) {
655 if (s->data != NULL) free(s->data);
656 l->spareels[l->spareelsnum++] = s;
658 while (s != l->tail_sentinel) {
660 if (s->data != NULL) free(s->data);
664 l->head_sentinel->next = l->tail_sentinel;
665 l->tail_sentinel->prev = l->head_sentinel;
668 for (s = l->head_sentinel->next; l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS && s != l->tail_sentinel; s = s->next) {
670 l->spareels[l->spareelsnum++] = s;
672 while (s != l->tail_sentinel) {
677 l->head_sentinel->next = l->tail_sentinel;
678 l->tail_sentinel->prev = l->head_sentinel;
684 assert(list_repOk(l));
689 unsigned int list_size(
const list_t *restrict l) {
693 int list_empty(
const list_t *restrict l) {
694 return (l->numels == 0);
697 int list_locate(
const list_t *restrict l,
const void *data) {
701 if (NULL == l->head_sentinel || NULL == l->tail_sentinel)
704 if (l->attrs.comparator != NULL) {
706 for (el = l->head_sentinel->next; el != l->tail_sentinel; el = el->next, pos++) {
707 if (l->attrs.comparator(data, el->data) == 0)
break;
711 for (el = l->head_sentinel->next; el != l->tail_sentinel; el = el->next, pos++) {
712 if (el->data == data)
break;
715 if (el == l->tail_sentinel)
return -1;
720 void *list_seek(
list_t *restrict l,
const void *indicator) {
723 if (l->attrs.seeker == NULL)
return NULL;
725 if (NULL == l->head_sentinel || NULL == l->tail_sentinel)
728 for (iter = l->head_sentinel->next; iter != l->tail_sentinel; iter = iter->next) {
729 if (l->attrs.seeker(iter->data, indicator) != 0)
return iter->data;
735 int list_contains(
const list_t *restrict l,
const void *data) {
736 return (list_locate(l, data) >= 0);
745 if (l1 == NULL || l2 == NULL || dest == NULL || l1 == dest || l2 == dest)
748 if (NULL == l1->head_sentinel || NULL == l1->tail_sentinel
749 || NULL == l2->head_sentinel || NULL == l2->tail_sentinel)
755 dest->numels = l1->numels + l2->numels;
756 if (dest->numels == 0)
760 srcel = l1->head_sentinel->next;
761 el = dest->head_sentinel;
762 while (srcel != l1->tail_sentinel) {
764 if (NULL == el->next)
768 el->data = srcel->data;
773 srcel = l2->head_sentinel->next;
774 while (srcel != l2->tail_sentinel) {
776 if (NULL == el->next)
780 el->data = srcel->data;
783 el->next = dest->tail_sentinel;
784 dest->tail_sentinel->prev = el;
787 err = l2->numels - l1->numels;
790 for (cnt = 0; cnt < (
unsigned int)err; cnt++) dest->mid = dest->mid->next;
791 }
else if (err/2 < 0) {
793 for (cnt = 0; cnt < (
unsigned int)err; cnt++) dest->mid = dest->mid->prev;
796 assert(!(list_repOk(l1) && list_repOk(l2)) || list_repOk(dest));
801 int list_sort(
list_t *restrict l,
int versus) {
802 if (l->iter_active || l->attrs.comparator == NULL)
808 if (NULL == l->head_sentinel || NULL == l->tail_sentinel)
811 list_sort_quicksort(l, versus, 0, l->head_sentinel->next, l->numels-1, l->tail_sentinel->prev);
812 assert(list_repOk(l));
816 #ifdef SIMCLIST_WITH_THREADS 817 struct list_sort_wrappedparams {
820 unsigned int first, last;
824 static void *list_sort_quicksort_threadwrapper(
void *wrapped_params) {
825 struct list_sort_wrappedparams *wp = (
struct list_sort_wrappedparams *)wrapped_params;
826 list_sort_quicksort(wp->l, wp->versus, wp->first, wp->fel, wp->last, wp->lel);
833 static inline void list_sort_selectionsort(
list_t *restrict l,
int versus,
842 for (firstunsorted = fel; firstunsorted != lel; firstunsorted = firstunsorted->next) {
844 for (toswap = firstunsorted, cursor = firstunsorted->next; cursor != lel->next; cursor = cursor->next)
845 if (l->attrs.comparator(toswap->data, cursor->data) * -versus > 0) toswap = cursor;
846 if (toswap != firstunsorted) {
847 tmpdata = firstunsorted->data;
848 firstunsorted->data = toswap->data;
849 toswap->data = tmpdata;
854 static void list_sort_quicksort(
list_t *restrict l,
int versus,
857 unsigned int pivotid;
862 #ifdef SIMCLIST_WITH_THREADS 871 if (last - first+1 <= SIMCLIST_MINQUICKSORTELS) {
872 list_sort_selectionsort(l, versus, first, fel, last, lel);
877 if (! (last > first))
return;
879 pivotid = (get_random() % (last - first + 1));
883 if (pivotid < (last - first + 1)/2) {
884 for (i = 0, pivot = fel; i < pivotid; pivot = pivot->next, i++);
886 for (i = last - first, pivot = lel; i > pivotid; pivot = pivot->prev, i--);
893 while (left != pivot && right != pivot) {
894 for (; left != pivot && (l->attrs.comparator(left->data, pivot->data) * -versus <= 0); left = left->next);
896 for (; right != pivot && (l->attrs.comparator(right->data, pivot->data) * -versus >= 0); right = right->prev);
898 if (left != pivot && right != pivot) {
900 tmpdata = left->data;
901 left->data = right->data;
902 right->data = tmpdata;
910 if (right == pivot) {
911 while (left != pivot) {
912 if (l->attrs.comparator(left->data, pivot->data) * -versus > 0) {
913 tmpdata = left->data;
914 left->data = pivot->prev->data;
915 pivot->prev->data = pivot->data;
916 pivot->data = tmpdata;
919 if (pivot == left)
break;
925 while (right != pivot) {
926 if (l->attrs.comparator(right->data, pivot->data) * -versus < 0) {
928 tmpdata = right->data;
929 right->data = pivot->next->data;
930 pivot->next->data = pivot->data;
931 pivot->data = tmpdata;
934 if (pivot == right)
break;
943 #ifdef SIMCLIST_WITH_THREADS 947 if (l->threadcount < SIMCLIST_MAXTHREADS-1) {
948 struct list_sort_wrappedparams *wp = (
struct list_sort_wrappedparams *)malloc(
sizeof(
struct list_sort_wrappedparams));
957 wp->last = first+pivotid-1;
958 wp->lel = pivot->prev;
959 if (pthread_create(&tid, NULL, list_sort_quicksort_threadwrapper, wp) != 0) {
962 list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
965 list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
968 if (first + pivotid < last) list_sort_quicksort(l, versus, first+pivotid+1, pivot->next, last, lel);
970 pthread_join(tid, (
void **)NULL);
974 if (pivotid > 0) 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);
979 int list_iterator_start(
list_t *restrict l) {
980 if (l->iter_active)
return 0;
981 if (NULL == l->head_sentinel)
985 l->iter_curentry = l->head_sentinel->next;
989 void *list_iterator_next(
list_t *restrict l) {
992 if (! l->iter_active)
return NULL;
994 toret = l->iter_curentry->data;
995 l->iter_curentry = l->iter_curentry->next;
1001 int list_iterator_hasnext(
const list_t *restrict l) {
1002 if (! l->iter_active)
return 0;
1003 return (l->iter_pos < l->numels);
1006 int list_iterator_stop(
list_t *restrict l) {
1007 if (! l->iter_active)
return 0;
1013 int list_hash(
const list_t *restrict l, list_hash_t *restrict hash) {
1015 list_hash_t tmphash;
1017 assert(hash != NULL);
1019 tmphash = l->numels * 2 + 100;
1020 if (l->attrs.hasher == NULL) {
1021 #ifdef SIMCLIST_ALLOW_LOCATIONBASED_HASHES 1023 #warning "Memlocation-based hash is consistent only for testing modification in the same program run." 1027 for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
1028 for (i = 0; i <
sizeof(x->data); i++) {
1029 tmphash += (tmphash ^ (uintptr_t)x->data);
1031 tmphash += tmphash % l->numels;
1038 for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
1039 tmphash += tmphash ^ l->attrs.hasher(x->data);
1040 tmphash += tmphash % l->numels;
1049 #ifndef SIMCLIST_NO_DUMPRESTORE 1050 int list_dump_getinfo_filedescriptor(
int fd,
list_dump_info_t *restrict info) {
1051 int32_t terminator_head, terminator_tail;
1057 READ_ERRCHECK(fd, & info->version,
sizeof(info->version));
1058 info->version = ntohs(info->version);
1059 if (info->version > SIMCLIST_DUMPFORMAT_VERSION) {
1065 READ_ERRCHECK(fd, & info->timestamp.tv_sec,
sizeof(info->timestamp.tv_sec));
1066 info->timestamp.tv_sec = ntohl(info->timestamp.tv_sec);
1067 READ_ERRCHECK(fd, & info->timestamp.tv_usec,
sizeof(info->timestamp.tv_usec));
1068 info->timestamp.tv_usec = ntohl(info->timestamp.tv_usec);
1071 READ_ERRCHECK(fd, & terminator_head,
sizeof(terminator_head));
1072 terminator_head = ntohl(terminator_head);
1075 READ_ERRCHECK(fd, & info->list_size,
sizeof(info->list_size));
1076 info->list_size = ntohl(info->list_size);
1079 READ_ERRCHECK(fd, & info->list_numels,
sizeof(info->list_numels));
1080 info->list_numels = ntohl(info->list_numels);
1083 READ_ERRCHECK(fd, & elemlen,
sizeof(elemlen));
1084 elemlen = ntohl(elemlen);
1087 READ_ERRCHECK(fd, & info->list_hash,
sizeof(info->list_hash));
1088 info->list_hash = ntohl(info->list_hash);
1093 hop = info->list_size;
1096 hop = info->list_size + elemlen*info->list_numels;
1098 if (lseek(fd, hop, SEEK_CUR) == -1) {
1103 READ_ERRCHECK(fd, & terminator_tail,
sizeof(terminator_tail));
1104 terminator_tail = ntohl(terminator_tail);
1106 if (terminator_head == terminator_tail)
1107 info->consistent = 1;
1109 info->consistent = 0;
1114 int list_dump_getinfo_file(
const char *restrict filename,
list_dump_info_t *restrict info) {
1117 fd = open(filename, O_RDONLY, 0);
1118 if (fd < 0)
return -1;
1120 ret = list_dump_getinfo_filedescriptor(fd, info);
1126 int list_dump_filedescriptor(
const list_t *restrict l,
int fd,
size_t *restrict len) {
1130 struct timeval timeofday;
1133 if (l->attrs.meter == NULL && l->attrs.serializer == NULL) {
1154 header.ver = htons( SIMCLIST_DUMPFORMAT_VERSION );
1157 gettimeofday(&timeofday, NULL);
1158 header.timestamp_sec = htonl(timeofday.tv_sec);
1159 header.timestamp_usec = htonl(timeofday.tv_usec);
1161 header.rndterm = htonl((int32_t)get_random());
1166 header.numels = htonl(l->numels);
1169 if (l->attrs.hasher != NULL) {
1170 if (htonl(list_hash(l, & header.listhash)) != 0) {
1175 header.listhash = htonl(0);
1178 header.totlistlen = header.elemlen = 0;
1181 if (lseek(fd, SIMCLIST_DUMPFORMAT_HEADERLEN, SEEK_SET) < 0) {
1187 if (l->numels > 0) {
1190 if (l->attrs.serializer != NULL) {
1192 ser_buf = l->attrs.serializer(l->head_sentinel->next->data, & header.elemlen);
1195 for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
1196 ser_buf = l->attrs.serializer(x->data, &bufsize);
1197 header.totlistlen += bufsize;
1198 if (header.elemlen != 0) {
1199 if (header.elemlen != bufsize) {
1203 header.totlistlen = 0;
1204 x = l->head_sentinel;
1205 if (lseek(fd, SIMCLIST_DUMPFORMAT_HEADERLEN, SEEK_SET) < 0) {
1213 WRITE_ERRCHECK(fd, ser_buf, bufsize);
1215 WRITE_ERRCHECK(fd, & bufsize,
sizeof(
size_t));
1216 WRITE_ERRCHECK(fd, ser_buf, bufsize);
1220 }
else if (l->attrs.meter != NULL) {
1221 header.elemlen = (uint32_t)l->attrs.meter(l->head_sentinel->next->data);
1224 for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
1225 bufsize = l->attrs.meter(x->data);
1226 header.totlistlen += bufsize;
1227 if (header.elemlen != 0) {
1228 if (header.elemlen != bufsize) {
1231 header.totlistlen = 0;
1232 x = l->head_sentinel;
1236 WRITE_ERRCHECK(fd, x->data, bufsize);
1238 WRITE_ERRCHECK(fd, &bufsize,
sizeof(
size_t));
1239 WRITE_ERRCHECK(fd, x->data, bufsize);
1244 header.elemlen = htonl(header.elemlen);
1245 header.totlistlen = htonl(header.totlistlen);
1249 WRITE_ERRCHECK(fd, & header.rndterm,
sizeof(header.rndterm));
1253 lseek(fd, 0, SEEK_SET);
1255 WRITE_ERRCHECK(fd, & header.ver,
sizeof(header.ver));
1256 WRITE_ERRCHECK(fd, & header.timestamp_sec,
sizeof(header.timestamp_sec));
1257 WRITE_ERRCHECK(fd, & header.timestamp_usec,
sizeof(header.timestamp_usec));
1258 WRITE_ERRCHECK(fd, & header.rndterm,
sizeof(header.rndterm));
1260 WRITE_ERRCHECK(fd, & header.totlistlen,
sizeof(header.totlistlen));
1261 WRITE_ERRCHECK(fd, & header.numels,
sizeof(header.numels));
1262 WRITE_ERRCHECK(fd, & header.elemlen,
sizeof(header.elemlen));
1263 WRITE_ERRCHECK(fd, & header.listhash,
sizeof(header.listhash));
1268 *len =
sizeof(header) + ntohl(header.totlistlen);
1274 int list_restore_filedescriptor(
list_t *restrict l,
int fd,
size_t *restrict len) {
1278 uint32_t elsize, totreadlen, totmemorylen;
1280 memset(& header, 0,
sizeof(header));
1285 READ_ERRCHECK(fd, &header.ver,
sizeof(header.ver));
1286 header.ver = ntohs(header.ver);
1287 if (header.ver != SIMCLIST_DUMPFORMAT_VERSION) {
1293 READ_ERRCHECK(fd, & header.timestamp_sec,
sizeof(header.timestamp_sec));
1294 header.timestamp_sec = ntohl(header.timestamp_sec);
1295 READ_ERRCHECK(fd, & header.timestamp_usec,
sizeof(header.timestamp_usec));
1296 header.timestamp_usec = ntohl(header.timestamp_usec);
1299 READ_ERRCHECK(fd, & header.rndterm,
sizeof(header.rndterm));
1301 header.rndterm = ntohl(header.rndterm);
1304 READ_ERRCHECK(fd, & header.totlistlen,
sizeof(header.totlistlen));
1305 header.totlistlen = ntohl(header.totlistlen);
1308 READ_ERRCHECK(fd, & header.numels,
sizeof(header.numels));
1309 header.numels = ntohl(header.numels);
1312 READ_ERRCHECK(fd, & header.elemlen,
sizeof(header.elemlen));
1313 header.elemlen = ntohl(header.elemlen);
1316 READ_ERRCHECK(fd, & header.listhash,
sizeof(header.listhash));
1317 header.listhash = ntohl(header.listhash);
1321 totreadlen = totmemorylen = 0;
1322 if (header.elemlen > 0) {
1324 if (l->attrs.unserializer != NULL) {
1326 buf = malloc(header.elemlen);
1329 for (cnt = 0; cnt < header.numels; cnt++) {
1330 READ_ERRCHECK(fd, buf, header.elemlen);
1331 list_append(l, l->attrs.unserializer(buf, & elsize));
1332 totmemorylen += elsize;
1336 for (cnt = 0; cnt < header.numels; cnt++) {
1337 buf = malloc(header.elemlen);
1340 READ_ERRCHECK(fd, buf, header.elemlen);
1341 list_append(l, buf);
1343 totmemorylen = header.numels * header.elemlen;
1345 totreadlen = header.numels * header.elemlen;
1348 if (l->attrs.unserializer != NULL) {
1350 for (cnt = 0; cnt < header.numels; cnt++) {
1351 READ_ERRCHECK(fd, & elsize,
sizeof(elsize));
1352 buf = malloc((
size_t)elsize);
1355 READ_ERRCHECK(fd, buf, elsize);
1356 totreadlen += elsize;
1357 list_append(l, l->attrs.unserializer(buf, & elsize));
1358 totmemorylen += elsize;
1362 for (cnt = 0; cnt < header.numels; cnt++) {
1363 READ_ERRCHECK(fd, & elsize,
sizeof(elsize));
1364 buf = malloc(elsize);
1367 READ_ERRCHECK(fd, buf, elsize);
1368 totreadlen += elsize;
1369 list_append(l, buf);
1371 totmemorylen = totreadlen;
1375 READ_ERRCHECK(fd, &elsize,
sizeof(elsize));
1376 elsize = ntohl(elsize);
1388 if (totreadlen != header.totlistlen && (int32_t)elsize == header.rndterm) {
1394 if (lseek(fd, 0, SEEK_CUR) != lseek(fd, 0, SEEK_END)) {
1400 *len = totmemorylen;
1406 int list_dump_file(
const list_t *restrict l,
const char *restrict filename,
size_t *restrict len) {
1407 int fd, oflag, mode;
1410 oflag = O_RDWR | O_CREAT | O_TRUNC;
1411 mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1413 oflag = _O_RDWR | _O_CREAT | _O_TRUNC;
1414 mode = _S_IRUSR | _S_IWUSR | _S_IRGRP | _S_IROTH;
1416 fd = open(filename, oflag, mode);
1417 if (fd < 0)
return -1;
1419 list_dump_filedescriptor(l, fd, len);
1425 int list_restore_file(
list_t *restrict l,
const char *restrict filename,
size_t *restrict len) {
1428 fd = open(filename, O_RDONLY, 0);
1429 if (fd < 0)
return -1;
1431 list_restore_filedescriptor(l, fd, len);
1439 static int list_drop_elem(
list_t *restrict l,
struct list_entry_s *tmp,
unsigned int pos) {
1440 if (tmp == NULL)
return -1;
1443 if (l->numels % 2) {
1445 if (l->numels == 1) l->mid = NULL;
1446 else if (pos >= l->numels/2) l->mid = l->mid->prev;
1448 if (pos < l->numels/2) l->mid = l->mid->next;
1451 tmp->prev->next = tmp->next;
1452 tmp->next->prev = tmp->prev;
1455 if (l->attrs.copy_data && tmp->data != NULL)
1458 if (l->spareels != NULL && l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
1459 l->spareels[l->spareelsnum++] = tmp;
1468 #define SIMCLIST_NUMBER_COMPARATOR(type) int list_comparator_##type(const void *a, const void *b) { return( *(type *)a < *(type *)b) - (*(type *)a > *(type *)b); } 1470 SIMCLIST_NUMBER_COMPARATOR(int8_t)
1471 SIMCLIST_NUMBER_COMPARATOR(int16_t)
1472 SIMCLIST_NUMBER_COMPARATOR(int32_t)
1473 SIMCLIST_NUMBER_COMPARATOR(int64_t)
1475 SIMCLIST_NUMBER_COMPARATOR(uint8_t)
1476 SIMCLIST_NUMBER_COMPARATOR(uint16_t)
1477 SIMCLIST_NUMBER_COMPARATOR(uint32_t)
1478 SIMCLIST_NUMBER_COMPARATOR(uint64_t)
1480 SIMCLIST_NUMBER_COMPARATOR(
float)
1481 SIMCLIST_NUMBER_COMPARATOR(
double)
1483 int list_comparator_string(
const void *a,
const void *b) {
return strcmp((
const char *)b, (
const char *)a); }
1486 #define SIMCLIST_METER(type) size_t list_meter_##type(const void *el) { if (el) { } return sizeof(type); } 1488 SIMCLIST_METER(int8_t)
1489 SIMCLIST_METER(int16_t)
1490 SIMCLIST_METER(int32_t)
1491 SIMCLIST_METER(int64_t)
1493 SIMCLIST_METER(uint8_t)
1494 SIMCLIST_METER(uint16_t)
1495 SIMCLIST_METER(uint32_t)
1496 SIMCLIST_METER(uint64_t)
1498 SIMCLIST_METER(
float)
1499 SIMCLIST_METER(
double)
1501 size_t list_meter_string(
const void *el) {
return strlen((
const char *)el) + 1; }
1504 #define SIMCLIST_HASHCOMPUTER(type) list_hash_t list_hashcomputer_##type(const void *el) { return (list_hash_t)(*(type *)el); } 1506 SIMCLIST_HASHCOMPUTER(int8_t)
1507 SIMCLIST_HASHCOMPUTER(int16_t)
1508 SIMCLIST_HASHCOMPUTER(int32_t)
1509 SIMCLIST_HASHCOMPUTER(int64_t)
1511 SIMCLIST_HASHCOMPUTER(uint8_t)
1512 SIMCLIST_HASHCOMPUTER(uint16_t)
1513 SIMCLIST_HASHCOMPUTER(uint32_t)
1514 SIMCLIST_HASHCOMPUTER(uint64_t)
1516 SIMCLIST_HASHCOMPUTER(
float)
1517 SIMCLIST_HASHCOMPUTER(
double)
1519 list_hash_t list_hashcomputer_string(
const void *el) {
1521 list_hash_t hash = 123;
1522 const char *str = (
const char *)el;
1525 for (l = 0; str[l] !=
'\0'; l++) {
1526 if (l) plus = hash ^ str[l];
1527 else plus = hash ^ (str[l] - str[0]);
1528 hash += (plus << (CHAR_BIT * (l %
sizeof(list_hash_t))));
1536 static int list_repOk(
const list_t *restrict l) {
1540 ok = (l != NULL) && (
1542 (l->head_sentinel != NULL && l->tail_sentinel != NULL) &&
1543 (l->head_sentinel != l->tail_sentinel) && (l->head_sentinel->prev == NULL && l->tail_sentinel->next == NULL) &&
1545 (l->numels > 0 || (l->mid == NULL && l->head_sentinel->next == l->tail_sentinel && l->tail_sentinel->prev == l->head_sentinel)) &&
1547 l->spareelsnum <= SIMCLIST_MAX_SPARE_ELEMS
1552 if (l->numels >= 1) {
1554 for (i = -1, s = l->head_sentinel; i < (
int)(l->numels-1)/2 && s->next != NULL; i++, s = s->next) {
1555 if (s->next->prev != s)
break;
1557 ok = (i == (int)(l->numels-1)/2 && l->mid == s);
1559 for (; s->next != NULL; i++, s = s->next) {
1560 if (s->next->prev != s)
break;
1562 ok = (i == (int)l->numels && s == l->tail_sentinel);
1568 static int list_attrOk(
const list_t *restrict l) {
1571 ok = (l->attrs.copy_data == 0 || l->attrs.meter != NULL);