Taiga Develop 3 bulan lalu
induk
melakukan
359a596c63

+ 1 - 0
headers/zivuos.h

@@ -4,5 +4,6 @@
 #include "zivuos/systypes.h"
 #include "zivuos/zalloc.h"
 #include "zivuos/mutex.h"
+#include "zivuos/vector.h"
 
 #endif

+ 0 - 50
headers/zivuos/.vector.h

@@ -1,50 +0,0 @@
-#ifndef CX_ZIVUOS_VECTOR_H_INCLUDED
-#define CX_ZIVUOS_VECTOR_H_INCLUDED
-
-#include <stdbool.h>
-
-#include "systypes.h"
-#include "memory.h"
-
-typedef struct zv_vector_container_s {
-    struct zv_vector_container_s *previous;
-    struct zv_vector_container_s *next;
-
-    zv_universal_t content;
-} zv_vector_container_t;
-
-typedef struct zv_vector_s {
-    zv_vector_container_t *first;
-    zv_vector_container_t *last;
-
-    zv_size_t size;
-} zv_vector_t;
-
-zv_vector_t * zv_vector_create(void);
-void zv_vector_delete(zv_vector_t *self);
-bool zv_vector_avairable(void);
-
-bool zv_vector_push(zv_vector_t *self, zv_universal_t content);
-zv_universal_t zv_vector_pop(zv_vector_t *self);
-
-zv_universal_t * zv_vector_get_pointer(zv_vector_t *self, zv_size_t target);
-zv_universal_t zv_vector_get(zv_vector_t *self, zv_size_t target);
-
-bool zv_vector_insert(
-    zv_vector_t *self, 
-    zv_universal_t content, 
-    zv_size_t after
-);
-
-zv_universal_t zv_vector_remove(
-    zv_vector_t *self,
-    zv_size_t target
-);
-
-zv_universal_t zv_vector_set(
-    zv_vector_t *self, 
-    zv_universal_t content, 
-    zv_size_t target
-);
-
-#endif

+ 4 - 20
headers/zivuos/mutex.h

@@ -9,25 +9,9 @@ typedef volatile enum zv_mutex_e {
     ZV_MUTEX_LOCKED
 } zv_mutex_t;
 
-static inline bool zv_mutex_try_lock(zv_mutex_t *self) {
-    zv_mutex_t required = ZV_MUTEX_FREE;
-
-    return __atomic_compare_exchange_n(
-        self, 
-        &required, 
-        ZV_MUTEX_LOCKED, 
-        false, 
-        __ATOMIC_ACQ_REL, 
-        __ATOMIC_ACQUIRE
-    );
-}
-
-static inline bool zv_mutex_is_free(zv_mutex_t *self) {
-    return __atomic_load_n(self, __ATOMIC_ACQUIRE) == ZV_MUTEX_FREE; 
-}
-
-static inline void zv_mutex_unlock(zv_mutex_t *self) {
-    __atomic_store_n(self, ZV_MUTEX_FREE, __ATOMIC_RELEASE);
-}
+enum zv_mutex_e zv_mutex_create();
+bool zv_mutex_try_lock(zv_mutex_t *self);
+bool zv_mutex_is_free(zv_mutex_t *self);
+void zv_mutex_unlock(zv_mutex_t *self);
 
 #endif

+ 0 - 11
headers/zivuos/semaphore.h

@@ -1,11 +0,0 @@
-#ifndef CX_ZIVUOS_SEMAPHORE_H_INCLUDED
-#define CX_ZIVUOS_SEMAPHORE_H_INCLUDED
-
-#include "systypes.h"
-
-typedef struct zv_semaphore_s {
-    volatile uint8_t content;
-} zv_semaphore_t;
-
-
-#endif

+ 2 - 0
headers/zivuos/systypes.h

@@ -17,4 +17,6 @@ typedef union zv_universal_u {
     uintptr_t uinteger;
 } zv_universal_t;
 
+#define ZV_UNIVERSAL_NULL ((zv_universal_t) NULL)
+
 #endif

+ 39 - 0
headers/zivuos/vector.h

@@ -0,0 +1,39 @@
+#ifndef CX_ZIVUOS_VECTOR_H_INCLUDED
+#define CX_ZIVUOS_VECTOR_H_INCLUDED
+
+#include <stdint.h>
+#include <stddef.h>
+#include <stdbool.h>
+
+#include "systypes.h"
+#include "zalloc.h"
+
+typedef struct zv_vector_container_s {
+    zv_universal_t content;
+    
+    struct zv_vector_container_s *next;
+    struct zv_vector_container_s *previous;
+} zv_vector_container_t;
+
+typedef struct zv_vector_s {
+    struct zv_vector_container_s *first;
+    struct zv_vector_container_s *last;
+
+    size_t length;
+} zv_vector_t;
+
+zv_vector_t * zv_vector_create(void);
+void zv_vector_clear(zv_vector_t *self);
+void zv_vector_remove(zv_vector_t *self);
+
+zv_universal_t zv_vector_pop(zv_vector_t *self);
+zv_universal_t zv_vector_indexed_pop(zv_vector_t *self, size_t index);
+
+bool zv_vector_insert(
+    zv_vector_t *self, 
+    zv_universal_t content, 
+    size_t index
+);
+bool zv_vector_append(zv_vector_t *self, zv_universal_t content);
+
+#endif

+ 0 - 96
sources/.vector.c

@@ -1,96 +0,0 @@
-#include <stddef.h>
-#include <stdint.h>
-#include <stdbool.h>
-
-#include "zalloc.h"
-#include "vector.h"
-#include "systypes.h"
-
-
-zv_vector_t * zv_vector_create(void) {
-    if (!zv_malloc_avairable()) {
-        return NULL
-    }
-
-    zv_vector_t *self = (zv_vector_t *) zv_malloc(sizeof(zv_vector_t));
-   
-    if (self == NULL) {
-        return NULL;
-    }
-
-    self->first = NULL;
-    self->last = NULL;
-    self->size = 0;
-
-    return self;
-}
-
-void zv_vector_delete(zv_vector_t *self) {
-    zv_vector_container_t *current = self->first;
-
-    while (current != NULL) {
-        zv_vector_container_t *next = current->next;
-        zv_free(current);
-        current = next;
-    }
-
-    zv_free(self);
-}
-
-bool zv_vector_avairable(void) {
-    return zv_zalloc_avairable();
-
-}
-
-bool zv_vector_push(zv_vector_t *self, zv_universal_t content) {
-    zv_vector_container_t *target = zv_malloc(sizeof(zv_vector_container_t));
-
-    if (target == NULL) {
-        return false;
-    }
-
-    target->next = NULL;
-    target->content = content;
-    target->previous = self->last;
-    self->last = target;
-
-    return true;
-}
-
-zv_universal_t zv_vector_pop(zv_vector_t *self) {
-    zv_universal_t result = self->last->
-    return result; 
-}
-
-zv_universal_t * zv_vector_get_pointer(zv_vector_t *self, zv_size_t target) {
-
-}
-
-zv_universal_t zv_vector_get(zv_vector_t *self, zv_size_t target) {
-
-}
-
-bool zv_vector_insert(
-    zv_vector_t *self, 
-    zv_universal_t content, 
-    zv_size_t after
-) {
-
-}
-
-zv_universal_t zv_vector_remove(
-    zv_vector_t *self,
-    zv_size_t target
-) {
-
-}
-
-zv_universal_t zv_vector_set(
-    zv_vector_t *self, 
-    zv_universal_t content, 
-    zv_size_t target
-) {
-
-}
-
-

+ 31 - 0
sources/mutex.c

@@ -0,0 +1,31 @@
+#include <stdint.h>
+#include <stdbool.h>
+
+#include "mutex.h"
+
+enum zv_mutex_e zv_mutex_create() {
+    return ZV_MUTEX_FREE;
+}
+
+bool zv_mutex_try_lock(zv_mutex_t *self) {
+    zv_mutex_t required = ZV_MUTEX_FREE;
+
+    return __atomic_compare_exchange_n(
+        self, 
+        (void *) &required, 
+        ZV_MUTEX_LOCKED, 
+        false, 
+        __ATOMIC_ACQ_REL, 
+        __ATOMIC_ACQUIRE
+    );
+}
+
+bool zv_mutex_is_free(zv_mutex_t *self) {
+    return __atomic_load_n(self, __ATOMIC_ACQUIRE) == ZV_MUTEX_FREE; 
+}
+
+void zv_mutex_unlock(zv_mutex_t *self) {
+    __atomic_store_n(self, ZV_MUTEX_FREE, __ATOMIC_RELEASE);
+}
+
+

+ 175 - 0
sources/vector.c

@@ -0,0 +1,175 @@
+#include <stdint.h>
+#include <stddef.h>
+#include <stdbool.h>
+
+#include "zalloc.h"
+#include "systypes.h"
+
+#include "vector.h"
+
+
+static zv_vector_container_t * zv__vector_container_create(void) {
+    zv_vector_container_t *created 
+    = (zv_vector_container_t *) zv_malloc(sizeof(zv_vector_container_t));
+
+    created->next = NULL;
+    created->previous = NULL;
+
+    return created;
+}
+
+static void zv__vector_container_drop(zv_vector_container_t *self) {
+    zv_free(self); 
+}
+
+static zv_vector_container_t * zv__vector_get_container(
+    zv_vector_t *self, 
+    size_t index
+) {
+    if (self->length <= index) {
+        return NULL;
+    }   
+
+    if (index < self->length / 2) {
+        zv_vector_container_t *current = self->first;
+
+        while (index > 0) {
+            current = current->next;
+            --index;
+        }
+
+        return current;
+    }
+
+    zv_vector_container_t *current = self->last;
+
+    index = self->length - index;
+
+    while (index > 0) {
+        current = current->previous;
+        --index;
+    }
+
+    return current;
+}
+
+zv_vector_t * zv_vector_create(void) {
+    zv_vector_t *self = (zv_vector_t *) zv_malloc(sizeof(zv_vector_t));
+
+    if (self == NULL) {
+        return NULL;
+    }
+
+    self->length = 0x00;
+    self->first = NULL;
+    self->last = NULL;
+
+    return self;
+}
+
+bool zv_vector_append(zv_vector_t *self, zv_universal_t content) {
+    return zv_vector_insert(self, content, self->length);
+}
+
+zv_universal_t zv_vector_pop(zv_vector_t *self) {
+    return zv_vector_indexed_pop(self, self->length - 1);
+}
+
+bool zv_vector_insert(
+    zv_vector_t *self, 
+    zv_universal_t content, 
+    size_t index
+) {
+    if (index > self->length) {
+        return false;
+    }
+
+    zv_vector_container_t *target = zv__vector_container_create();
+    
+    if (target == NULL) {
+        return false;
+    }
+
+    target->content = content;
+
+    if (index == 0) {
+        target->previous = NULL;
+        target->next = self->first;
+        self->first = target;
+
+        ++self->length;
+        return true;
+    }
+
+    if (index == self->length) {
+        target->next = NULL;
+        target->previous = self->last;
+        self->last = target;
+
+        ++self->length;
+        return true;
+    }
+
+    target->previous = zv__vector_get_container(self, index - 1);
+    target->next = target->previous->next;
+    
+    target->previous->next = target;
+    target->next->previous = target;
+    
+    ++self->length;
+    return true;
+}
+
+zv_universal_t zv_vector_indexed_pop(zv_vector_t *self, size_t index) {
+    zv_vector_container_t *container = zv__vector_get_container(self, index);
+    
+    if (container == NULL) {
+        return ZV_UNIVERSAL_NULL;
+    }
+
+    zv_universal_t content = container->content;
+    
+    if (index == 0) {
+        self->first = container->next; 
+        self->first->previous = NULL;
+    }
+
+    if (index == self->length - 1) {
+        self->last = container->previous;
+        self->last->next = NULL;
+    }
+
+    if (index != 0 && index != self->length - 1) {
+        container->next->previous = container->previous;
+        container->previous->next = container->next;
+    }
+    
+    --self->length;
+    zv__vector_container_drop(container);
+    
+    return content;
+}
+
+void zv_vector_clear(zv_vector_t *self) {
+    if (self->length == 0) {
+        return;
+    }
+
+    zv_vector_container_t *current = self->first;
+
+    while (current != NULL) {
+        zv_vector_container_t *previous = current;
+        current = current->next;
+
+        zv__vector_container_drop(previous);
+    }
+
+    self->first = NULL;
+    self->last = NULL;
+    self->length = 0;
+}
+
+void zv_vector_remove(zv_vector_t *self) {
+    zv_vector_clear(self);
+    zv_free(self);
+}

+ 24 - 0
tests/001-mutex/run.sh

@@ -0,0 +1,24 @@
+#!/bin/bash
+
+CC=gcc
+CC_FLAGS="-O2"
+
+OWN=$(dirname $(realpath $0))
+TEST_SOURCE=$OWN/testing.c
+TEST_OUTPUT=$OWN/testing.app
+HEADERS=$OWN/../../headers
+LIBRARY=$OWN/../../build-generic
+LIBRARY_MAKE=$OWN/../../build-generic/make.sh
+
+rm -f $TEST_OUTPUT
+
+echo "Rebuilding..."
+$LIBRARY_MAKE
+
+echo ""
+echo "Build test..."
+$CC $CC_FLAGS $TEST_SOURCE -o $TEST_OUTPUT -I$HEADERS -L$LIBRARY -lzivuos 
+
+echo "Running..."
+echo ""
+$TEST_OUTPUT

+ 39 - 0
tests/001-mutex/testing.c

@@ -0,0 +1,39 @@
+#include <stdio.h>
+#include <zivuos.h>
+
+void check(zv_mutex_t *self) {
+    if (zv_mutex_is_free(self)) {
+        puts("Mutex is free.");
+    } else {
+        puts("Mutex is locked.");
+    }
+}
+
+void catch(zv_mutex_t *self) {
+    if (zv_mutex_try_lock(self)) {
+        puts("Locking mutex.");
+    } else {
+        puts("Mutex is already locker.");
+    }
+}
+
+int main() {
+    puts("ZivuOS default test unit.");
+
+    zv_mutex_t lock = zv_mutex_create();
+
+    check(&lock);
+    catch(&lock);
+    catch(&lock);
+    check(&lock);
+
+    zv_mutex_unlock(&lock);
+
+    check(&lock);
+    catch(&lock);
+    check(&lock);
+
+    zv_mutex_unlock(&lock);
+
+    return 0;
+}

+ 24 - 0
tests/002-vector/run.sh

@@ -0,0 +1,24 @@
+#!/bin/bash
+
+CC=gcc
+CC_FLAGS="-O2"
+
+OWN=$(dirname $(realpath $0))
+TEST_SOURCE=$OWN/testing.c
+TEST_OUTPUT=$OWN/testing.app
+HEADERS=$OWN/../../headers
+LIBRARY=$OWN/../../build-generic
+LIBRARY_MAKE=$OWN/../../build-generic/make.sh
+
+rm -f $TEST_OUTPUT
+
+echo "Rebuilding..."
+$LIBRARY_MAKE
+
+echo ""
+echo "Build test..."
+$CC $CC_FLAGS $TEST_SOURCE -o $TEST_OUTPUT -I$HEADERS -L$LIBRARY -lzivuos 
+
+echo "Running..."
+echo ""
+$TEST_OUTPUT

+ 35 - 0
tests/002-vector/testing.c

@@ -0,0 +1,35 @@
+#include <stdio.h>
+#include <zivuos.h>
+
+void dump(zv_vector_t *self) {
+    printf("Vector length: %u\n", self->length);
+    
+    zv_vector_container_t *current = self->first;
+    size_t count = 0;
+
+    while (current != NULL) {
+        printf("Item %u: %s\n", count++, current->content.string);
+        current = current->next;        
+    }
+    
+    puts("Dump end");
+    puts("");
+}
+
+int main() {
+    puts("ZivuOS vector test unit.");
+
+    zv_universal_t first = { .string = "UwU" };
+    zv_universal_t second = { .string = "OwO" };
+
+    zv_vector_t *test = zv_vector_create();
+
+    dump(test);
+
+    zv_vector_append(test, first); 
+    zv_vector_append(test, second);
+
+    dump(test);
+
+    return 0;
+}