3 Part of the swftools package.
5 Copyright (c) 2001,2002,2003,2004 Matthias Kramm <kramm@quiss.org>
7 This program is rfx_free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the rfx_free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the rfx_free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
31 // ------------------------------- malloc, alloc routines ---------------------
34 char* strdup_n(const char*str, int size)
36 char*m = (char*)rfx_alloc(size+1);
42 char*qstrdup(const char*string)
44 return strdup(string);
46 char*qstrndup(const char*string, int len)
48 return strdup_n(string, len);
51 // ------------------------------- mem_t --------------------------------------
53 void mem_init(mem_t*mem)
55 memset(mem, 0, sizeof(mem_t));
57 void mem_clear(mem_t*mem)
59 rfx_free(mem->buffer);mem->buffer = 0;
61 void mem_destroy(mem_t*mem)
66 static int mem_put_(mem_t*m,const void*data, int length, int null)
69 m->pos += length + (null?1:0);
71 int v1 = (m->pos+63)&~63;
72 int v2 = m->len + m->len / 2;
74 m->buffer = m->buffer?(char*)rfx_realloc(m->buffer,m->len):(char*)rfx_alloc(m->len);
76 assert(n+length <= m->len);
77 memcpy(&m->buffer[n], data, length);
79 m->buffer[n + length] = 0;
82 int mem_put(mem_t*m,void*data, int length)
84 return mem_put_(m, data, length, 0);
86 int mem_putstring(mem_t*m,string_t str)
88 return mem_put_(m, str.str, str.len, 1);
90 int mem_get(mem_t*m, void*data, int length)
92 if(m->read_pos + length > m->pos) {
93 length = m->pos - m->read_pos;
95 memcpy(data, m->buffer+m->read_pos, length);
96 m->read_pos += length;
100 // ------------------------------- ringbuffer_t -------------------------------
102 typedef struct _ringbuffer_internal_t
104 unsigned char*buffer;
108 } ringbuffer_internal_t;
110 void ringbuffer_init(ringbuffer_t*r)
112 ringbuffer_internal_t*i = (ringbuffer_internal_t*)rfx_calloc(sizeof(ringbuffer_internal_t));
113 memset(r, 0, sizeof(ringbuffer_t));
115 i->buffer = (unsigned char*)rfx_alloc(1024);
116 i->buffersize = 1024;
118 int ringbuffer_read(ringbuffer_t*r, void*buf, int len)
120 unsigned char* data = (unsigned char*)buf;
121 ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
122 if(r->available < len)
126 if(i->readpos + len > i->buffersize) {
127 int read1 = i->buffersize-i->readpos;
128 memcpy(data, &i->buffer[i->readpos], read1);
129 memcpy(&data[read1], &i->buffer[0], len - read1);
130 i->readpos = len - read1;
132 memcpy(data, &i->buffer[i->readpos], len);
134 i->readpos %= i->buffersize;
139 void ringbuffer_put(ringbuffer_t*r, void*buf, int len)
141 unsigned char* data = (unsigned char*)buf;
142 ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
144 if(i->buffersize - r->available < len)
147 int newbuffersize = i->buffersize;
148 int oldavailable = r->available;
149 newbuffersize*=3;newbuffersize/=2; /*grow at least by 50% each time */
151 if(newbuffersize < r->available + len)
152 newbuffersize = r->available + len + 1024;
154 buf2 = (unsigned char*)rfx_alloc(newbuffersize);
155 ringbuffer_read(r, buf2, r->available);
158 i->buffersize = newbuffersize;
160 i->writepos = oldavailable;
161 r->available = oldavailable;
163 if(i->writepos + len > i->buffersize) {
164 int read1 = i->buffersize-i->writepos;
165 memcpy(&i->buffer[i->writepos], data, read1);
166 memcpy(&i->buffer[0], &data[read1], len - read1);
167 i->writepos = len - read1;
169 memcpy(&i->buffer[i->writepos], data, len);
171 i->writepos %= i->buffersize;
175 void ringbuffer_clear(ringbuffer_t*r)
177 ringbuffer_internal_t*i = (ringbuffer_internal_t*)r->internal;
178 rfx_free(i->buffer);i->buffer = 0;
182 // ------------------------------- heap_t -------------------------------
184 void heap_init(heap_t*h,int n,int elem_size, int(*compare)(const void *, const void *))
186 memset(h, 0, sizeof(heap_t));
189 h->elem_size = elem_size;
190 h->compare = compare;
191 h->elements = (void**)rfx_calloc(n*sizeof(void*));
192 h->data = (char*)rfx_calloc(h->max_size*h->elem_size);
194 void heap_clear(heap_t*h)
196 rfx_free(h->elements);
200 #define HEAP_NODE_SMALLER(h,node1,node2) ((h)->compare((node1),(node2))>0)
202 static void up(heap_t*h, int node)
204 void*node_p = h->elements[node];
210 h->elements[node] = h->elements[parent];
211 } while(HEAP_NODE_SMALLER(h,h->elements[parent], node_p));
213 h->elements[node] = node_p;
215 static void down(heap_t*h, int node)
217 void*node_p = h->elements[node];
222 /* determine new child's position */
226 if(child+1 < h->size && HEAP_NODE_SMALLER(h,h->elements[child],h->elements[child+1])) // search for bigger child
229 h->elements[node] = h->elements[child];
230 } while(HEAP_NODE_SMALLER(h,node_p, h->elements[child]));
232 h->elements[node] = node_p;
234 void heap_put(heap_t*h, void*e)
237 memcpy(&h->data[pos*h->elem_size],e,h->elem_size);
238 h->elements[pos] = &h->data[pos];
241 int heap_size(heap_t*h)
245 void* heap_max(heap_t*h)
247 return h->elements[0];
249 void* heap_chopmax(heap_t*h)
251 void*p = h->elements[0];
252 h->elements[0] = h->elements[--h->size];
256 void heap_dump(heap_t*h, FILE*fi)
259 for(t=0;t<h->size;t++) {
261 for(s=0;s<=t;s=(s+1)*2-1) {
262 if(s==t) fprintf(fi,"\n");
264 //fprintf(fi,"%d ", h->elements[t]->x); //?
267 void** heap_flatten(heap_t*h)
269 void**nodes = (void**)rfx_alloc(h->size*sizeof(void*));
273 /*printf("Heap Size: %d\n", h->size);
274 heap_print(stdout, h);
276 *p++ = heap_chopmax(h);
281 // ------------------------------- trie --------------------------------------
283 void trie_put(trie_t**t, unsigned const char*id)
286 (*t) = rfx_calloc(sizeof(trie_t));
287 (*t)->rest = (unsigned char*)strdup(id);
290 if((*t)->rest && (*t)->rest[0]) {
291 // shift whatever's currently in here one node down
292 trie_put(&(*t)->row[(*t)->rest[0]], (*t)->rest+1);
296 trie_put(&(*t)->row[id[0]], id+1);
302 int trie_lookup(trie_t*t, unsigned const char*id)
305 if(t->rest && !strcmp(t->rest, id))
315 // ------------------------------- crc32 --------------------------------------
316 static unsigned int*crc32 = 0;
317 static void crc32_init(void)
322 crc32= (unsigned int*)rfx_alloc(sizeof(unsigned int)*256);
323 for(t=0; t<256; t++) {
326 for (s = 0; s < 8; s++) {
327 c = (0xedb88320L*(c&1)) ^ (c >> 1);
332 // ------------------------------- string_t -----------------------------------
334 void string_set2(string_t*str, const char*text, int len)
339 void string_set(string_t*str, const char*text)
342 str->len = strlen(text);
348 string_t string_new(const char*text, int len)
355 string_t string_new2(const char*text)
359 s.len = strlen(text);
366 string_t* string_new3(const char*text, int len)
369 string_t*s = malloc(sizeof(string_t));
374 string_t*s = malloc(sizeof(string_t)+len+1);
376 s->str = (const char*)(s+1);
377 memcpy((char*)s->str, text, len);
378 ((char*)s->str)[len]=0;
382 string_t* string_new4(const char*text)
384 int l = strlen(text);
385 return string_new3(text, l);
388 void string_free(string_t*s)
393 if((string_t*)(s->str) == s+1) {
397 rfx_free((char*)(s->str));
402 char* string_cstr(string_t*str)
404 return strdup_n(str->str, str->len);
406 char* string_escape(string_t*str)
410 for(t=0;t<str->len;t++) {
416 char*s = malloc(len+1);
418 for(t=0;t<str->len;t++) {
419 if(str->str[t]<0x20) {
421 unsigned char c = str->str[t];
422 *p++ = "0123456789abcdef"[c>>4];
423 *p++ = "0123456789abcdef"[c&0x0f];
429 assert(p == &s[len+1]);
433 unsigned int crc32_add_byte(unsigned int checksum, unsigned char b)
437 return checksum>>8 ^ crc32[(b^checksum)&0xff];
439 unsigned int crc32_add_string(unsigned int checksum, const char*s)
446 checksum = checksum>>8 ^ crc32[(*s^checksum)&0xff];
452 unsigned int string_hash(const string_t*str)
455 unsigned int checksum = 0;
458 for(t=0;t<str->len;t++) {
459 checksum = checksum>>8 ^ crc32[(str->str[t]^checksum)&0xff];
463 unsigned int string_hash2(const char*str)
465 unsigned int checksum = 0;
470 checksum = checksum>>8 ^ crc32[(*p^checksum)&0xff];
475 unsigned int string_hash3(const char*str, int len)
480 return string_hash(&s);
482 void string_dup2(string_t*str, const char*text, int len)
485 str->str = strdup_n(text, len);
487 void string_dup(string_t*str, const char*text)
489 str->len = strlen(text);
490 str->str = strdup(text);
492 int string_equals(string_t*str, const char*text)
494 int l = strlen(text);
495 if(str->len == l && !memcmp(str->str, text, l))
499 int string_equals2(string_t*str, string_t*str2)
501 if(str->len == str2->len && !memcmp(str->str, str2->str, str->len))
506 // ------------------------------- stringarray_t ------------------------------
508 typedef struct _stringlist {
510 struct _stringlist*next;
513 typedef struct _stringarray_internal_t
519 } stringarray_internal_t;
521 void stringarray_init(stringarray_t*sa, int hashsize)
523 stringarray_internal_t*s;
525 sa->internal = (stringarray_internal_t*)rfx_calloc(sizeof(stringarray_internal_t));
526 s = (stringarray_internal_t*)sa->internal;
528 s->hash = rfx_calloc(sizeof(stringlist_t*)*hashsize);
529 s->hashsize = hashsize;
531 void stringarray_put(stringarray_t*sa, string_t str)
533 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
535 int hash = string_hash(&str) % s->hashsize;
537 char*ss = string_cstr(&str);
538 mem_put(&s->pos, &ss, sizeof(char*));
540 stringlist_t*l = rfx_alloc(sizeof(stringlist_t));
542 l->next = s->hash[hash];
547 char* stringarray_at(stringarray_t*sa, int pos)
549 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
551 if(pos<0 || pos>=s->num)
553 p = *(char**)&s->pos.buffer[pos*sizeof(char*)];
558 string_t stringarray_at2(stringarray_t*sa, int pos)
561 s.str = stringarray_at(sa, pos);
562 s.len = s.str?strlen(s.str):0;
565 static stringlist_t* stringlist_del(stringarray_t*sa, stringlist_t*l, int index)
568 stringlist_t*old = l;
570 if(index==l->index) {
572 memset(l, 0, sizeof(stringlist_t));
582 fprintf(stderr, "Internal error: did not find string %d in hash\n", index);
586 void stringarray_del(stringarray_t*sa, int pos)
588 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
589 string_t str = stringarray_at2(sa, pos);
590 int hash = string_hash(&str) % s->hashsize;
591 s->hash[hash] = stringlist_del(sa, s->hash[hash], pos);
592 *(char**)&s->pos.buffer[pos*sizeof(char*)] = 0;
594 int stringarray_find(stringarray_t*sa, string_t* str)
596 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
597 int hash = string_hash(str) % s->hashsize;
599 stringlist_t*l = s->hash[hash];
602 string_t s = stringarray_at2(sa, l->index);
603 if(string_equals2(str, &s)) {
610 void stringarray_clear(stringarray_t*sa)
612 stringarray_internal_t*s = (stringarray_internal_t*)sa->internal;
615 for(t=0;t<s->hashsize;t++) {
616 stringlist_t*l = s->hash[t];
618 stringlist_t*next = l->next;
619 memset(l, 0, sizeof(stringlist_t));
624 rfx_free(s->hash);s->hash=0;
627 void stringarray_destroy(stringarray_t*sa)
629 stringarray_clear(sa);
633 // ------------------------------- type_t -------------------------------
635 char ptr_equals(const void*o1, const void*o2)
639 unsigned int ptr_hash(const void*o)
641 return string_hash3((const char*)&o, sizeof(o));
643 void* ptr_dup(const void*o)
647 void ptr_free(void*o)
652 char charptr_equals(const void*o1, const void*o2)
656 return !strcmp(o1,o2);
658 unsigned int charptr_hash(const void*o)
662 return string_hash2(o);
664 void* charptr_dup(const void*o)
670 void charptr_free(void*o)
677 char stringstruct_equals(const void*o1, const void*o2)
681 string_t*s1 = (string_t*)o1;
682 string_t*s2 = (string_t*)o2;
683 int l = s1->len<s2->len?s1->len:s2->len;
684 int r = memcmp(s1->str, s2->str, l);
688 return s1->len==s2->len;
690 unsigned int stringstruct_hash(const void*o)
693 return string_hash(o);
695 string_t*string_dup3(string_t*o)
699 string_t*s = malloc(sizeof(string_t));
704 string_t*s = rfx_alloc(sizeof(string_t)+o->len+1);
706 s->str = (const char*)(s+1);
707 memcpy((char*)s->str, o->str, s->len);
708 ((char*)s->str)[s->len]=0;
711 void stringstruct_free(void*o)
724 type_t charptr_type = {
725 equals: charptr_equals,
731 type_t stringstruct_type = {
732 equals: stringstruct_equals,
733 hash: stringstruct_hash,
734 dup: (dup_func)string_dup3,
735 free: stringstruct_free,
738 // ------------------------------- dictionary_t -------------------------------
740 #define INITIAL_SIZE 1
742 static int max(int x, int y) {
748 dict_t*d = rfx_alloc(sizeof(dict_t));
749 dict_init(d, INITIAL_SIZE);
752 dict_t*dict_new2(type_t*t)
754 dict_t*d = rfx_alloc(sizeof(dict_t));
755 dict_init(d, INITIAL_SIZE);
759 void dict_init(dict_t*h, int size)
761 memset(h, 0, sizeof(dict_t));
763 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
765 h->key_type = &charptr_type;
768 dict_t*dict_clone(dict_t*o)
770 dict_t*h = rfx_alloc(sizeof(dict_t));
771 memcpy(h, o, sizeof(dict_t));
772 h->slots = h->hashsize?(dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*h->hashsize):0;
774 for(t=0;t<o->hashsize;t++) {
775 dictentry_t*e = o->slots[t];
777 dictentry_t*n = (dictentry_t*)rfx_alloc(sizeof(dictentry_t));
778 memcpy(n, e, sizeof(dictentry_t));
779 n->key = h->key_type->dup(e->key);
781 n->next = h->slots[t];
789 static void dict_expand(dict_t*h, int newlen)
791 assert(h->hashsize < newlen);
792 dictentry_t**newslots = (dictentry_t**)rfx_calloc(sizeof(dictentry_t*)*newlen);
794 for(t=0;t<h->hashsize;t++) {
795 dictentry_t*e = h->slots[t];
797 dictentry_t*next = e->next;
798 unsigned int newhash = e->hash%newlen;
799 e->next = newslots[newhash];
800 newslots[newhash] = e;
807 h->hashsize = newlen;
810 dictentry_t* dict_put(dict_t*h, const void*key, void* data)
812 unsigned int hash = h->key_type->hash(key);
813 dictentry_t*e = (dictentry_t*)rfx_alloc(sizeof(dictentry_t));
814 unsigned int hash2 = hash % h->hashsize;
816 e->key = h->key_type->dup(key);
817 e->hash = hash; //for resizing
818 e->next = h->slots[hash2];
824 void dict_put2(dict_t*h, const char*s, void*data)
826 assert(h->key_type == &charptr_type);
827 dict_put(h, s, data);
829 void dict_dump(dict_t*h, FILE*fi, const char*prefix)
832 for(t=0;t<h->hashsize;t++) {
833 dictentry_t*e = h->slots[t];
835 if(h->key_type!=&charptr_type) {
836 fprintf(fi, "%s%08x=%08x\n", prefix, e->key, e->data);
838 fprintf(fi, "%s%s=%08x\n", prefix, e->key, e->data);
845 int dict_count(dict_t*h)
850 static inline dictentry_t* dict_do_lookup(dict_t*h, const void*key)
856 unsigned int ohash = h->key_type->hash(key);
857 unsigned int hash = ohash % h->hashsize;
859 /* check first entry for match */
860 dictentry_t*e = h->slots[hash];
861 if(e && h->key_type->equals(e->key, key)) {
867 /* if dict is 2/3 filled, double the size. Do
868 this the first time we have to actually iterate
869 through a slot to find our data */
870 if(e && h->num*3 >= h->hashsize*2) {
871 int newsize = h->hashsize;
872 while(h->num*3 >= newsize*2) {
873 newsize = newsize<15?15:(newsize+1)*2-1;
875 dict_expand(h, newsize);
876 hash = ohash % h->hashsize;
878 if(e && h->key_type->equals(e->key, key)) {
879 // omit move to front
886 /* check subsequent entries for a match */
887 dictentry_t*last = h->slots[hash];
889 if(h->key_type->equals(e->key, key)) {
890 /* move to front- makes a difference of about 10% in most applications */
891 last->next = e->next;
892 e->next = h->slots[hash];
901 void* dict_lookup(dict_t*h, const void*key)
903 dictentry_t*e = dict_do_lookup(h, key);
908 char dict_contains(dict_t*h, const void*key)
910 dictentry_t*e = dict_do_lookup(h, key);
914 char dict_del(dict_t*h, const void*key)
918 unsigned int hash = h->key_type->hash(key) % h->hashsize;
919 dictentry_t*head = h->slots[hash];
920 dictentry_t*e = head, *prev=0;
922 if(h->key_type->equals(e->key, key)) {
923 dictentry_t*next = e->next;
924 rfx_free((void*)e->key);
925 memset(e, 0, sizeof(dictentry_t));
942 dictentry_t* dict_get_slot(dict_t*h, const void*key)
946 unsigned int ohash = h->key_type->hash(key);
947 unsigned int hash = ohash % h->hashsize;
948 return h->slots[hash];
951 void dict_foreach_keyvalue(dict_t*h, void (*runFunction)(void*data, const void*key, void*val), void*data)
954 for(t=0;t<h->hashsize;t++) {
955 dictentry_t*e = h->slots[t];
957 dictentry_t*next = e->next;
959 runFunction(data, e->key, e->data);
965 void dict_foreach_value(dict_t*h, void (*runFunction)(void*))
968 for(t=0;t<h->hashsize;t++) {
969 dictentry_t*e = h->slots[t];
971 dictentry_t*next = e->next;
973 runFunction(e->data);
980 void dict_free_all(dict_t*h, char free_keys, void (*free_data_function)(void*))
983 for(t=0;t<h->hashsize;t++) {
984 dictentry_t*e = h->slots[t];
986 dictentry_t*next = e->next;
988 h->key_type->free(e->key);
990 if(free_data_function) {
991 free_data_function(e->data);
993 memset(e, 0, sizeof(dictentry_t));
1000 memset(h, 0, sizeof(dict_t));
1003 void dict_clear_shallow(dict_t*h)
1005 dict_free_all(h, 0, 0);
1008 void dict_clear(dict_t*h)
1010 dict_free_all(h, 1, 0);
1013 void dict_destroy_shallow(dict_t*dict)
1015 dict_clear_shallow(dict);
1019 void dict_destroy(dict_t*dict)
1025 // ------------------------------- map_t --------------------------------------
1027 typedef struct _map_internal_t
1032 void map_init(map_t*map)
1035 map->internal = (map_internal_t*)rfx_calloc(sizeof(map_internal_t));
1036 m = (map_internal_t*)map->internal;
1037 dict_init(&m->d, INITIAL_SIZE);
1039 void map_put(map_t*map, string_t t1, string_t t2)
1041 map_internal_t*m = (map_internal_t*)map->internal;
1043 char* s1 = string_cstr(&t1);
1044 dict_put2(&m->d, s1, (void*)string_cstr(&t2));
1047 const char* map_lookup(map_t*map, const char*name)
1049 map_internal_t*m = (map_internal_t*)map->internal;
1050 const char*value = dict_lookup(&m->d, name);
1053 static void freestring(void*data)
1057 static void dumpmapentry(void*data, const void*key, void*value)
1059 FILE*fi = (FILE*)data;
1060 fprintf(fi, "%s=%s\n", key, (char*)value);
1062 void map_dump(map_t*map, FILE*fi, const char*prefix)
1065 map_internal_t*m = (map_internal_t*)map->internal;
1066 dict_foreach_keyvalue(&m->d, dumpmapentry, fi);
1068 void map_clear(map_t*map)
1070 map_internal_t*m = (map_internal_t*)map->internal;
1071 dict_free_all(&m->d, 1, freestring);
1074 void map_destroy(map_t*map)
1080 // ------------------------------- array_t --------------------------------------
1082 array_t* array_new() {
1083 array_t*d = malloc(sizeof(array_t));
1084 memset(d, 0, sizeof(array_t));
1085 d->entry2pos = dict_new();
1088 array_t* array_new2(type_t*type) {
1089 array_t*d = malloc(sizeof(array_t));
1090 memset(d, 0, sizeof(array_t));
1091 d->entry2pos = dict_new2(type);
1094 void*array_getkey(array_t*array, int nr) {
1095 if(nr > array->num || nr<0) {
1096 printf("error: reference to element %d in array[%d]\n", nr, array->num);
1099 return array->d[nr].name;
1101 void*array_getvalue(array_t*array, int nr) {
1102 if(nr > array->num || nr<0) {
1103 printf("error: reference to element %d in array[%d]\n", nr, array->num);
1106 return array->d[nr].data;
1108 int array_append(array_t*array, const void*name, void*data) {
1109 while(array->size <= array->num) {
1112 array->d = malloc(sizeof(array_entry_t)*array->size);
1114 array->d = realloc(array->d, sizeof(array_entry_t)*array->size);
1118 dictentry_t*e = dict_put(array->entry2pos, name, (void*)(ptroff_t)(array->num+1));
1121 array->d[array->num].name = e->key;
1123 array->d[array->num].name = 0;
1125 array->d[array->num].data = (void*)data;
1126 return array->num++;
1128 int array_find(array_t*array, const void*name)
1130 int pos = (int)(ptroff_t)dict_lookup(array->entry2pos, name);
1133 int array_find2(array_t*array, const void*name, void*data)
1135 dict_t*h= array->entry2pos;
1136 dictentry_t*e = dict_get_slot(array->entry2pos, name);
1139 int index = ((int)(ptroff_t)e->data) - 1;
1140 if(h->key_type->equals(e->key, name) && array->d[index].data == data) {
1147 int array_update(array_t*array, const void*name, void*data) {
1148 int pos = array_find(array, name);
1150 array->d[pos].data = data;
1153 return array_append(array, name, data);
1155 int array_append_if_new(array_t*array, const void*name, void*data) {
1156 int pos = array_find(array, name);
1159 return array_append(array, name, data);
1161 void array_free(array_t*array) {
1162 dict_destroy(array->entry2pos);
1164 free(array->d);array->d = 0;
1169 // ------------------------------- list_t --------------------------------------
1172 typedef struct _listinfo {
1174 struct _commonlist*last;
1177 typedef struct _commonlist {
1179 struct _commonlist*next;
1183 int list_length_(void*_list)
1185 commonlist_t*l = (commonlist_t*)_list;
1188 return l->info[0].size;
1190 void list_concat_(void*_l1, void*_l2)
1192 commonlist_t**l1 = (commonlist_t**)_l1;
1193 commonlist_t**l2 = (commonlist_t**)_l2;
1198 (*l1)->info[0].last->next = *l2;
1199 (*l1)->info[0].last = (*l2)->info[0].last;
1200 (*l1)->info[0].size += (*l2)->info[0].size;
1204 void list_append_(void*_list, void*entry)
1206 commonlist_t**list = (commonlist_t**)_list;
1207 commonlist_t* n = 0;
1209 n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
1211 (*list)->info[0].size = 0;
1213 n = malloc(sizeof(commonlist_t));
1214 (*list)->info[0].last->next = n;
1218 (*list)->info[0].last = n;
1219 (*list)->info[0].size++;
1221 /* notice: prepending uses slighly more space than appending */
1222 void list_prepend_(void*_list, void*entry)
1224 commonlist_t**list = (commonlist_t**)_list;
1225 commonlist_t* n = (commonlist_t*)malloc(sizeof(commonlist_t)+sizeof(listinfo_t));
1227 commonlist_t* last = 0;
1229 last = (*list)->info[0].last;
1230 size = (*list)->info[0].size;
1235 (*list)->info[0].last = last;
1236 (*list)->info[0].size = size+1;
1238 void list_free_(void*_list)
1240 commonlist_t**list = (commonlist_t**)_list;
1241 commonlist_t*l = *list;
1243 commonlist_t*next = l->next;
1249 void list_deep_free_(void*_list)
1251 commonlist_t**list = (commonlist_t**)_list;
1252 commonlist_t*l = *list;
1254 commonlist_t*next = l->next;
1256 free(l->entry);l->entry=0;
1263 void*list_clone_(void*_list)
1265 commonlist_t*l = *(commonlist_t**)_list;
1269 commonlist_t*next = l->next;
1270 list_append_(&dest, l->entry);