aboutsummaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorMikhail Romanko <me@blankhex.com>2024-06-03 22:11:05 +0300
committerMikhail Romanko <me@blankhex.com>2024-06-03 22:11:05 +0300
commit79874622a28c081abe155dc01860ddba746abd3b (patch)
tree9dea56ea33951b7c851b534eb6f9d1ff1e1dcfe8 /main.c
parentfdbabab0e04fac2b5a84ea8e8088cd4767034a45 (diff)
downloadbhlib-old-79874622a28c081abe155dc01860ddba746abd3b.tar.gz
Add documentation, expand error handling, implement file and buffer io
Diffstat (limited to 'main.c')
-rw-r--r--main.c165
1 files changed, 157 insertions, 8 deletions
diff --git a/main.c b/main.c
index d4f4dba..607b949 100644
--- a/main.c
+++ b/main.c
@@ -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);