From: yu.dongliang <18588496441@163.com> Date: Mon, 27 Nov 2023 04:50:02 +0000 (+0800) Subject: tmp X-Git-Url: http://baseworks.info/?a=commitdiff_plain;h=a5c091d828f80587d06c6ada114bda0527dd211f;p=scf.git tmp --- diff --git a/test/list_test.c b/test/list_test.c deleted file mode 100644 index f5d179c..0000000 --- a/test/list_test.c +++ /dev/null @@ -1,39 +0,0 @@ -include "../lib/scf_list.c"; -include "../lib/scf_capi.c"; - -struct Data -{ - list list; - int a; -}; - -int main() -{ - Data* d; - list h; - int i; - - list_init(&h); - - for (i = 0; i < 10; i++) { - d = malloc(sizeof(Data)); - - d->a = i; - list_add_tail(&h, &d->list); - } - - list* l; - - for (l = list_head(&h); l != list_sentinel(&h); ) { - - d = container(l, Data, list); - l = list_next(l); - - printf("%d\n", d->a); - - list_del(&d->list); - free(d); - } - - return 0; -} diff --git a/test/rbtree.c b/test/rbtree.c deleted file mode 100644 index 26d08b1..0000000 --- a/test/rbtree.c +++ /dev/null @@ -1,94 +0,0 @@ - -include "../lib/scf_rbtree.c"; -include "../lib/scf_capi.c"; - -struct rbtree_test_t -{ - scf_rbtree_node_t node; - int d; -}; - -static int test_cmp(scf_rbtree_node_t* node0, void* data) -{ - rbtree_test_t* v0 = (rbtree_test_t*)node0; - rbtree_test_t* v1 = (rbtree_test_t*)data; - - if (v0->d < v1->d) - return -1; - else if (v0->d > v1->d) - return 1; - return 0; -} - -static int test_find(scf_rbtree_node_t* node0, void* data) -{ - rbtree_test_t* v0 = (rbtree_test_t*)node0; - int d1 = (intptr_t)data; - - if (v0->d < d1) - return -1; - else if (v0->d > d1) - return 1; - return 0; -} - -static int test_print(scf_rbtree_node_t* node0, void* data) -{ - rbtree_test_t* v0 = (rbtree_test_t*)node0; - - printf("v0->d: %d\n", v0->d); - return 0; -} - -const int N = 17; - -int main() -{ - scf_rbtree_t tree; - scf_rbtree_init(&tree); - - rbtree_test_t* d; - - int i; - for (i = 0; i < N; i++) { - d = calloc(1, sizeof(rbtree_test_t)); - - d->d = i; - - int ret = scf_rbtree_insert(&tree, &d->node, test_cmp); - if (ret < 0) - return -1; - } - - scf_rbtree_foreach(&tree, tree->root, NULL, test_print); - - scf_rbtree_depth(&tree, tree->root); - - for (i = 0; i < N / 2; i++) { - d = (rbtree_test_t*) scf_rbtree_find(&tree, (void*)(intptr_t)i, test_find); - - int ret = scf_rbtree_delete(&tree, &d->node); - - free(d); - d = NULL; - } - - scf_rbtree_foreach(&tree, tree->root, NULL, test_print); - - scf_rbtree_depth(&tree, tree->root); - - for (i = 0; i < N / 2; i++) { - d = calloc(1, sizeof(rbtree_test_t)); - - d->d = i; - - int ret = scf_rbtree_insert(&tree, &d->node, test_cmp); - if (ret < 0) - return -1; - } - scf_rbtree_foreach_reverse(&tree, tree->root, NULL, test_print); - - scf_rbtree_depth(&tree, tree->root); - return 0; -} - diff --git a/test/scf_malloc.c b/test/scf_malloc.c deleted file mode 100644 index bf204b0..0000000 --- a/test/scf_malloc.c +++ /dev/null @@ -1,364 +0,0 @@ - -int brk (uint8_t* addr); -uint8_t* sbrk(uintptr_t inc); - -int printf(const char* fmt, ...); - -uint8_t* memset(uint8_t* dst, int c, uintptr_t n); -uint8_t* memcpy(uint8_t* dst, uint8_t* src, uintptr_t n); - -// min size of block is 64, so low 6 bits can be used for flags -// flags in prev_size: -// Free Last First -// 2 1 0 -struct scf_mblock_t -{ - uintptr_t magic; - uintptr_t prev_size; - uintptr_t cur_size; - uintptr_t free; -}; - -scf_mblock_t* scf__mblocks[30]; -scf_mblock_t* scf__free_blocks = NULL; -uint8_t* scf__last_brk = NULL; - -uint8_t* scf__malloc(uintptr_t size) -{ - scf_mblock_t* b; - scf_mblock_t* b2; - - uintptr_t bytes = (sizeof(scf_mblock_t) + size + 63) >> 6 << 6; - intptr_t nblocks = sizeof(scf__mblocks) / sizeof(scf__mblocks[0]); - - uintptr_t rest; - uint8_t* addr = NULL; - uint8_t* p = NULL; - intptr_t i; - - for (i = 0; i < nblocks; i++) { - - if ((64 << i) < bytes) - continue; - - if (scf__mblocks[i]) - break; - } - - if (i == nblocks) { - - uintptr_t pages = (bytes + 4095) >> 12; - - p = sbrk(pages << 12); - if (!p) - return NULL; - scf__last_brk = p + (pages << 12); - - rest = (pages << 12) - bytes; - - b = (scf_mblock_t*)p; - - b->prev_size = 0x3; - b->cur_size = bytes; - b->magic = 0x10f0; - } else { - b = scf__mblocks[i]; - scf__mblocks[i] = (scf_mblock_t*)b->free; - - p = (uint8_t*)b; - - rest = b->cur_size - bytes; - - b->prev_size &= ~0x4; - b->magic = 0x10f0; - } - - addr = p + sizeof(scf_mblock_t); - - if (0 == rest) { - printf("%s(),%d, b: %p, scf__last_brk: %p\n", __func__, __LINE__, b, scf__last_brk); - return addr; - } - - p += bytes; - b2 = (scf_mblock_t*)p; - - for (; i >= 0; i--) { - - if (rest >= (64 << i)) { - b2->free = (uintptr_t)scf__mblocks[i]; - scf__mblocks[i] = b2; - break; - } - } - - b ->cur_size = bytes; - b2->cur_size = rest; - b2->prev_size = bytes | 0x4; - b2->magic = 0xf010; - - if (b->prev_size & 0x2) { - b->prev_size &= ~0x2; - b2->prev_size |= 0x2; - } - - printf("%s(),%d, b: %p, scf__last_brk: %p\n", __func__, __LINE__, b, scf__last_brk); - return addr; -} - -int scf__free(uint8_t* p) -{ - p -= sizeof(scf_mblock_t); - - scf_mblock_t* b = (scf_mblock_t*)p; - scf_mblock_t* b2; - scf_mblock_t* b3; - scf_mblock_t** pb; - - uintptr_t bytes = b->cur_size; - intptr_t nblocks = sizeof(scf__mblocks) / sizeof(scf__mblocks[0]); - intptr_t i; - - if (b->prev_size & 0x4) { - printf("%s(), %d, error: double free: %p\n", __func__, __LINE__, p); - return -1; - } - - if (0x10f0 != b->magic) { - printf("%s(), %d, error: corruption free: %p\n", __func__, __LINE__, p); - return -1; - } - - b->prev_size |= 0x4; - b->magic = 0xf010; - - while (!(b->prev_size & 0x2)) { - - b2 = (scf_mblock_t*)((uintptr_t)b + bytes); - - uintptr_t bytes2 = b2->cur_size; - - for (i = nblocks - 1; i >= 0; i--) { - - if (bytes2 >= (64 << i)) - break; - } - - for (pb = &scf__mblocks[i]; *pb; pb = (scf_mblock_t**)&(*pb)->free) { - - if (*pb == b2) - break; - } - if (!*pb) - break; - - *pb = (scf_mblock_t*)b2->free; - - bytes += bytes2; - b->cur_size = bytes; - - if (b2->prev_size & 0x2) - b->prev_size |= 0x2; - else { - b3 = (scf_mblock_t*)((uintptr_t)b2 + bytes2); - - b3->prev_size = bytes | (b3->prev_size & 0x7); - } - } - - while (!(b->prev_size & 0x1)) { - - uintptr_t bytes2 = b->prev_size & ~0x7; - - b2 = (scf_mblock_t*)((uintptr_t)b - bytes2); - - bytes2 = b2->cur_size; - - for (i = nblocks - 1; i >= 0; i--) { - - if (bytes2 >= (64 << i)) - break; - } - - for (pb = &scf__mblocks[i]; *pb; pb = (scf_mblock_t**)&(*pb)->free) { - if (*pb == b2) - break; - } - if (!*pb) - break; - - *pb = (scf_mblock_t*)b2->free; - - bytes += bytes2; - b2->cur_size = bytes; - - if (b->prev_size & 0x2) - b2->prev_size |= 0x2; - else { - b3 = (scf_mblock_t*)((uintptr_t)b2 + bytes); - - b3->prev_size = bytes | (b3->prev_size & 0x7); - } - - b = b2; - } - - bytes = b->cur_size; - - if (0x7 == (b->prev_size) & 0x7) { - - if (scf__last_brk == (uint8_t*)b + bytes) { - - printf("%s(), %d, b: %p, scf__last_brk: %p\n", __func__, __LINE__, b, scf__last_brk); - scf__last_brk = (uint8_t*)b; - brk((uint8_t*)b); - - int flag = 1; - while (flag) { - flag = 0; - - pb = &scf__free_blocks; - while (*pb) { - - b = *pb; - bytes = b->cur_size; - - if (scf__last_brk != (uint8_t*)b + bytes) { - - pb = (scf_mblock_t**)&b->free; - continue; - } - *pb = (scf_mblock_t*)b->free; - - printf("%s(), %d, b: %p, scf__last_brk: %p\n", __func__, __LINE__, b, scf__last_brk); - scf__last_brk = (uint8_t*)b; - brk((uint8_t*)b); - flag = 1; - } - } - } else { - b->free = (uintptr_t)scf__free_blocks; - scf__free_blocks = b; - printf("%s(), %d, b: %p\n", __func__, __LINE__, b); - } - return 0; - } - - for (i = nblocks - 1; i >= 0; i--) { - if (bytes >= (64 << i)) - break; - } - - b->free = (uintptr_t)scf__mblocks[i]; - scf__mblocks[i] = b; - - printf("%s(), %d, b: %p\n", __func__, __LINE__, b); - return 0; -} - -uint8_t* scf__calloc(uintptr_t n, uintptr_t size) -{ - scf_mblock_t* b; - uintptr_t bytes; - uint8_t* p; - - size *= n; - - p = scf__malloc(size); - if (!p) - return NULL; - - b = (scf_mblock_t*)(p - sizeof(scf_mblock_t)); - - bytes = b->cur_size; - bytes -= sizeof(scf_mblock_t); - printf("%s(),%d, calloc, b: %p, bytes: %ld\n", __func__, __LINE__, b, bytes); - - memset(p, 0, bytes); - - return p; -} - -uint8_t* scf__realloc(uint8_t* p, uintptr_t size) -{ - scf_mblock_t* b; - scf_mblock_t* b2; - scf_mblock_t* b3; - uintptr_t bytes; - uint8_t* p2; - - b = (scf_mblock_t*)(p - sizeof(scf_mblock_t)); - - bytes = b->cur_size; - bytes -= sizeof(scf_mblock_t); - - if (bytes < size) { - p2 = scf__malloc(size); - if (!p2) - return NULL; - - memcpy(p2, p, bytes); - scf__free(p); - - printf("%s(), %d, realloc: %p->%p, size: %ld\n", __func__, __LINE__, p, p2, size); - return p2; - } - - size = (sizeof(scf_mblock_t) + 63 + size) >> 6 << 6; - bytes += sizeof(scf_mblock_t); - - if (bytes < size + 64) - return p; - - b2 = (scf_mblock_t*)((uintptr_t)b + size); - - b ->cur_size = size; - b2->cur_size = bytes - size; - - if (b->prev_size & 0x2) { - b->prev_size &= ~0x2; - b2->prev_size = size | 0x2; - } else { - b3 = (scf_mblock_t*)((uintptr_t)b + bytes); - - b2->prev_size = size; - b3->prev_size = (bytes - size) | (b3->prev_size & 0x7); - } - - b2->magic = 0x10f0; - - p2 = (uint8_t*)b2 + sizeof(scf_mblock_t); - scf__free(p2); - - printf("%s(), %d, realloc: %p, free b2: %p, size: %ld\n", __func__, __LINE__, p, p2, size); - return p; -} - -int main() -{ - uint8_t* p0 = scf__malloc(1000); - uint8_t* p1 = scf__malloc(1320); - uint8_t* p2 = scf__malloc(2510); - uint8_t* p3 = scf__malloc(4510); - uint8_t* p4 = scf__malloc(510); - uint8_t* p5 = scf__malloc(6510); - uint8_t* p6 = scf__malloc(510); - uint8_t* p7 = scf__malloc(11510); - - scf__free(p0); - - *p1 = 1; - scf__free(p1); - - *p2 = 2; - *p4 = 4; - - scf__free(p4); - scf__free(p5); - scf__free(p2); - scf__free(p6); - scf__free(p3); - - scf__free(p7); - return 0; -} diff --git a/test/scf_printf.c b/test/scf_printf.c deleted file mode 100644 index 5ffc7ef..0000000 --- a/test/scf_printf.c +++ /dev/null @@ -1,242 +0,0 @@ -struct va_list -{ - uint8_t* iptr; - uint8_t* fptr; - uint8_t* optr; - - intptr_t ireg; - intptr_t freg; - intptr_t others; -}; - -int scf_ulong2a(char* buf, int* pn, int size, uint64_t num) -{ - int n = *pn; - int i = n; - - while (n < size - 1) { - - buf[n++] = num % 10 + '0'; - - num /= 10; - if (0 == num) - break; - } - - *pn = n--; - - while (i < n) { - char c = buf[i]; - buf[i++] = buf[n]; - buf[n--] = c; - } - return *pn; -} - -int scf_long2a(char* buf, int* pn, int size, int64_t num) -{ - int n = *pn; - - if (n < size - 1 && num < 0) { - buf[n++] = '-'; - num = -num; - } - - *pn = n; - return scf_ulong2a(buf, pn, size, num); -} - -int scf_hex2a(char* buf, int* pn, int size, uint64_t num) -{ - int n = *pn; - int i = n; - - while (n < size - 1) { - - uint8_t x = num % 16; - - if (x > 9) - buf[n++] = x - 10 + 'a'; - else - buf[n++] = x + '0'; - - num /= 16; - if (0 == num) - break; - } - - *pn = n--; - - while (i < n) { - char c = buf[i]; - buf[i++] = buf[n]; - buf[n--] = c; - } - return *pn; -} - -int scf_hex2a_prefix(char* buf, int* pn, int size, uint64_t num) -{ - int n = *pn; - - if (n < size - 1 - 2) { - buf[n++] = '0'; - buf[n++] = 'x'; - } - - *pn = n; - return scf_hex2a(buf, pn, size, num); -} - -int scf_p2a(char* buf, int* pn, int size, uint64_t num) -{ - if (0 == num) { - int n = *pn; - char* p = "null"; - - while (n < size - 1 && *p) - buf[n++] = *p++; - - *pn = n; - return n; - } - - return scf_hex2a_prefix(buf, pn, size, num); -} - -int scf_str2a(char* buf, int* pn, int size, const char* str) -{ - int n = *pn; - - while (n < size - 1 && *str) - buf[n++] = *str++; - - *pn = n; - return n; -} - -int scf_double2a(char* buf, int* pn, int size, double num) -{ - if (*pn < size - 1 && num < 0.0) { - buf[(*pn)++] = '-'; - num = -num; - } - - int64_t l = (int64_t)num; - int64_t diff = (int64_t)((num - l) * 1000000); - - scf_ulong2a(buf, pn, size, l); - - if (*pn < size - 1) - buf[(*pn)++] = '.'; - - return scf_ulong2a(buf, pn, size, diff); -} - -int scf_vsnprintf(char* buf, int size, const char* fmt, va_list* ap) -{ - int n = 0; - - while (*fmt) { - - if ('%' != *fmt) { - buf[n++] = *fmt++; - continue; - } - - fmt++; - if ('%' == *fmt) { - buf[n++] = *fmt++; - continue; - } - - int prefix = 0; - if ('#' == *fmt) { - prefix++; - fmt++; - } - - int l = 0; - if ('l' == *fmt) { - l++; - fmt++; - } - - if ('c' == *fmt) - buf[n++] = va_arg(ap, int32_t); - - else if ('u' == *fmt) { - if (l) - scf_ulong2a(buf, &n, size, va_arg(ap, uint64_t)); - else - scf_ulong2a(buf, &n, size, va_arg(ap, uint32_t)); - - } else if ('d' == *fmt) { - if (l) - scf_long2a(buf, &n, size, va_arg(ap, int64_t)); - else - scf_long2a(buf, &n, size, va_arg(ap, int32_t)); - - } else if ('x' == *fmt) { - if (prefix) { - if (l) - scf_hex2a_prefix(buf, &n, size, va_arg(ap, uint64_t)); - else - scf_hex2a_prefix(buf, &n, size, va_arg(ap, uint32_t)); - } else { - if (l) - scf_hex2a(buf, &n, size, va_arg(ap, uint64_t)); - else - scf_hex2a(buf, &n, size, va_arg(ap, uint32_t)); - } - } else if ('p' == *fmt) - scf_p2a(buf, &n, size, va_arg(ap, uint64_t)); - - else if ('s' == *fmt) - scf_str2a(buf, &n, size, va_arg(ap, char*)); - - else if ('f' == *fmt) { - if (l) - scf_double2a(buf, &n, size, va_arg(ap, double)); - else - scf_double2a(buf, &n, size, va_arg(ap, float)); - } else - return -1; - - fmt++; - } - - buf[n] = '\0'; - return n; -} - -int scf_printf(const char* fmt, ...) -{ - va_list ap; - - char buf[1024]; - - va_start(ap, fmt); - int ret = scf_vsnprintf(buf, sizeof(buf) - 1, fmt, &ap); - va_end(ap); - - if (ret > 0) { - ret = scf__write(1, buf, ret); - } - - return ret; -} - -int main() -{ - char buf[1024]; - - float f = 2.71828; - double d = -3.1415926; - int64_t i64 = -255; - - int ret = scf_printf("i: %d, ld: %ld, x: %x, x: %#lx, p: %p, s: %s, f: %f, d: %lf\n", - 100, i64, 254, 253, buf, "hello", f, -3.14); - - return ret; -} diff --git a/test/string.c b/test/string.c deleted file mode 100644 index 81b522b..0000000 --- a/test/string.c +++ /dev/null @@ -1,136 +0,0 @@ -include "../lib/scf_capi.c"; - -struct string -{ - uint8_t* data; - int64_t len; - int64_t capacity; - - int __init(string* this) - { - this->data = scf__auto_malloc(16); - if (!this->data) - return -1; - - this->data[0] = '\0'; - this->capacity = 16; - this->len = 0; - - return 0; - } - - int __init(string* this, const char* s) - { - int64_t len = strlen(s); - - this->data = scf__auto_malloc(len + 1); - if (!this->data) - return -1; - - memcpy(this->data, s, len); - - this->data[len] = '\0'; - this->capacity = len; - this->len = len; - - return 0; - } - - int __init(string* this, const char* s, int64_t len) - { - this->data = scf__auto_malloc(len + 1); - if (!this->data) - return -1; - - memcpy(this->data, s, len); - - this->data[len] = '\0'; - this->capacity = len; - this->len = len; - - return 0; - } - - int operator+=(string* this, string* that) - { - int64_t len = this->len + that->len; - - if (len >= this->capacity) { - uint8_t* p = scf__auto_malloc(len + 16); - if (!p) - return -1; - - memcpy(p, this->data, this->len); - - this->data = p; - this->capacity = len + 15; - } - - memcpy(this->data + this->len, that->data, that->len); - - this->data[len] = '\0'; - this->len = len; - - return 0; - } - - string*, int operator+(string* this, string* that) - { - string* s; - - s = create string(); - if (!s) - return NULL, -1; - - int64_t len = this->len + that->len; - - if (s->len < len) { - s->data = scf__auto_malloc(len + 1); - if (!s->data) - return NULL, -1; - } - - memcpy(s->data, this->data, this->len); - memcpy(s->data + this->len, that->data, that->len); - - s->data[len] = '\0'; - s->capacity = len; - s->len = len; - - return s, 0; - } - - int operator==(string* this, string* that) - { - if (this->len < that->len) - return -1; - else if (this->len > that->len) - return 1; - return memcmp(this->data, that->data, this->len); - } - - void __release(string* this) - { - if (this->data) - scf__auto_freep(&this->data, NULL); - } -}; - -int main() -{ - string* s0; - string* s1; - string* s2; - string* s3; - int err; - - s0 = "hello"; - s1 = " world"; - - s2 = s0 + s1; - s3 = "hello"; - - printf("### s0 == s3: %d, s0->data: %s, s2: %s\n", s0 == s3, s0->data, s2->data); - return 0; -} -