diff options
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 165 |
1 files changed, 157 insertions, 8 deletions
@@ -1,6 +1,8 @@ #include <bh/queue.h> #include <bh/hashmap.h> #include <bh/thread.h> +#include <bh/file.h> +#include <bh/buffer.h> #include <stdio.h> #include <stdint.h> @@ -50,7 +52,7 @@ void factor_task(void *arg) //printf("Factor: %d\n", factor(42)); //fflush(stdout); volatile int i = factor(16); - + //printf("Factor: %d\n", factor(15)); } @@ -61,7 +63,7 @@ void bar() bh_thread_pool_t *pool; bh_task_t *task; size_t i; - + bh_uint64_t start, end; //printf("Pool create\n"); @@ -75,9 +77,9 @@ void bar() //printf("Prepare\n"); //fflush(stdout); - + start = __rdtsc(); - + for (i = 0; i < 128 * 1024; i++) { //printf("Task create\n"); @@ -85,26 +87,173 @@ void bar() task = bh_task_new(factor_task, NULL, BH_THREAD_CLEANUP); bh_thread_pool_add(pool, task); } - + //system("pause"); bh_thread_pool_wait(pool); end = __rdtsc(); bh_thread_pool_free(pool); - - - + + + printf("Elapsed: %llu\n", end - start); //system("pause"); } +int fib(int x) +{ + static int memory[128] = {0}; + int result; + + if (x < 128 && memory[x] != 0) + return memory[x]; + + if (x <= 1) + return 1; + + + result = fib(x - 1) + fib(x - 2); + + if (x < 128) + memory[x] = result; + + return result; +} + +#include <string.h> + +char* itoa (unsigned long long value, char str[], int radix) +{ + char buf [66]; + char* dest = buf + sizeof(buf); + int sign = 0; + + if (value == 0) { + memcpy (str, "0", 2); + return str; + } + + if (radix < 0) { + radix = -radix; + if ( (long long) value < 0) { + value = -value; + sign = 1; + } + } + + *--dest = '\0'; + + switch (radix) + { + case 16: + while (value) { + * --dest = '0' + (value & 0xF); + if (*dest > '9') *dest += 'A' - '9' - 1; + value >>= 4; + } + break; + case 10: + while (value) { + *--dest = '0' + (value % 10); + value /= 10; + } + break; + + case 8: + while (value) { + *--dest = '0' + (value & 7); + value >>= 3; + } + break; + + case 2: + while (value) { + *--dest = '0' + (value & 1); + value >>= 1; + } + break; + + default: // The slow version, but universal + while (value) { + *--dest = '0' + (value % radix); + if (*dest > '9') *dest += 'A' - '9' - 1; + value /= radix; + } + break; + } + + if (sign) *--dest = '-'; + + memcpy (str, dest, buf +sizeof(buf) - dest); + return str; +} + +void test_api(bh_io_t *io) +{ + int i; + + for (i = 0; i < 128; i++) + { + char str[65]; + + itoa(i, str, 10); + bh_io_write(io, str, strlen(str)); + bh_io_write(io, " ", 1); + itoa(fib(i), str, 10); + bh_io_write(io, str, strlen(str)); + bh_io_write(io, "\n", 1); + } +} + +void test(void) +{ + bh_file_t *file = bh_file_new("log.txt"); + + if (!file) + return; + + if (bh_file_open(file, BH_IO_APPEND | BH_IO_WRITE)) + { + printf("File not opened?"); + bh_file_free(file); + return; + } + + bh_file_write(file, "Hello, world!\n", 14); + test_api((bh_io_t *)file); + bh_file_free(file); +} + +void ddump() +{ + bh_buffer_t *buffer; + bh_file_t *file; + + buffer = bh_buffer_new(); + bh_buffer_open(buffer, BH_IO_WRITE); + + bh_buffer_write(buffer, "Hello, world!", 13); + bh_buffer_close(buffer); + + file = bh_file_new("debug.txt"); + bh_file_open(file, BH_IO_WRITE | BH_IO_TRUNCATE); + bh_file_write(file, bh_buffer_data(buffer), bh_buffer_size(buffer)); + + bh_file_free(file); + bh_buffer_free(buffer); +} + int main() { bh_queue_t *queue; void *iter; size_t i, j; + ddump(); + return 0; + //foo(); + test(); printf("Thread?\n"); fflush(stdout); |
