Cixo Develop 8 месяцев назад
Родитель
Сommit
186f800370

+ 1 - 1
build-generic/make.sh

@@ -1,7 +1,7 @@
 #!/bin/bash
 
 CC=gcc
-CC_FLAGS="-O2"
+CC_FLAGS="-O2 -Wall -Wextra"
 
 AR=ar
 AR_FLAGS="rcs"

+ 14 - 0
headers/zivuos/hash.h

@@ -0,0 +1,14 @@
+#ifndef CX_ZIVUOS_HASH_H_INCLUDED
+#define CX_ZIVUOS_HASH_H_INCLUDED
+
+#include <stdint.h>
+
+#include "zivu_types.h"
+
+typedef uint32_t zv_hash_t;
+
+#define ZV_HASH_MAX UINT32_MAX
+
+zv_hash_t zv_hash_calculate(void *, zv_size_t);
+
+#endif

+ 2 - 1
headers/zivuos/kernel.h

@@ -25,7 +25,8 @@ void zv_kernel_init(zv_kernel_t *, zv_process_t *, zv_pid_t);
 #ifndef ZV_DISABLE_DYNAMIC
 
 zv_kernel_t * zv_kernel_create();
-#define ZV_DYNAMIC_PROCESSES_STEP 0x08
+#define ZV_PROCESSES_STEP 0x08
+#define ZV_PROCESSES_STEP_SIZE (ZV_PROCESSES_STEP * sizeof(zv_process_t))
 
 #endif
 

+ 12 - 12
headers/zivuos/latch.h

@@ -1,22 +1,22 @@
 #ifndef CX_ZIVUOS_LATCH_H_INCLUDED
 #define CX_ZIVUOS_LATCH_H_INCLUDED
 
+#include <stdint.h>
+#include <stdbool.h>
+
 #include "zivu_types.h"
+#include "hash.h"
 
-typedef enum zv_latch_state_e {
-    SAME,
-    CHANGED
-} zv_latch_state_t;
+typedef struct zv_latch_s {
+    zv_universal_param_t data[1];
+    zv_hash_t hash;
+    zv_size_t size;
+    bool changed;
 
-typedef union zv_latch_updater_u {
-    intptr_t exists;
-    zv_latch_state_t (*callback) (struct zv_latch_s *);
-} zv_latch_updater_t;
+#ifndef ZV_DISABLE_DYNAMIC
+    bool is_dynamic;
+#endif
 
-typedef struct zv_latch_s {
-    zv_universal_param_t data;
-    zv_latch_state_t state;
-    zv_latch_updater_t updater;
 } zv_latch_t;
 
 #endif

+ 53 - 0
sources/hash.c

@@ -0,0 +1,53 @@
+#include <stdint.h>
+
+#include "hash.h"
+#include "zivu_types.h"
+
+inline static zv_hash_t zv_hash_calculate_intptr(intptr_t target) {
+    zv_hash_t left_part = (zv_hash_t) (target >> (sizeof(zv_hash_t) * 8));
+    zv_hash_t right_part = (zv_hash_t) (target & ZV_HASH_MAX);
+
+    /* Splited left part is left part of target, where bytes are fliped. */
+    /* Example: 0xAABBCCDD -> 0xCCDDAABB */
+
+    const uint8_t half_part = sizeof(zv_hash_t) * 8 / 2;
+    zv_hash_t splited_left_part;
+    splited_left_part = left_part >> half_part;
+    splited_left_part = splited_left_part | (left_part << half_part);
+
+    zv_hash_t result;
+    result = left_part ^ right_part;
+    result = result ^ splited_left_part;
+
+    return result;
+}
+
+inline static zv_hash_t zv_hash_calculate_array(
+    uint8_t *content, 
+    zv_size_t amount
+) {
+    zv_hash_t first = 1;
+    zv_hash_t second = 0;
+    
+    while (amount > 0) {
+        first = (first + *content) % 65521;
+        second = (first + second) % 65521;
+
+        ++content;
+        --amount;
+    }
+    
+    return (second << 16) | first;
+}
+
+zv_hash_t zv_hash_calculate(void *target, zv_size_t amount) {
+    if (amount == 0) {
+        if (sizeof(zv_hash_t) <= sizeof(intptr_t)) {
+            return zv_hash_calculate_intptr((intptr_t) target);
+        }
+
+        return (zv_hash_t) ((intptr_t) target);
+    }
+
+    return zv_hash_calculate_array((uint8_t *) target, amount);
+}

+ 11 - 3
sources/kernel.c

@@ -6,7 +6,7 @@
 #include "kernel.h"
 #include "process.h"
 
-zv_kernel_t * zv_kernel_create();
+#ifndef ZV_DISABLE_DYNAMIC
 
 zv_kernel_t * zv_kernel_create() {
     zv_kernel_t *self = (zv_kernel_t *) malloc(sizeof(zv_kernel_t));
@@ -18,6 +18,14 @@ zv_kernel_t * zv_kernel_create() {
     return self;
 }
 
+#else
+
+zv_kernel_t * zv_kernel_create() {
+    return (zv_kernel_t *) NULL;
+}
+
+#endif
+
 /* For dynamic also kernel. */
 #ifndef ZV_DISABLE_DYNAMIC
 
@@ -36,8 +44,8 @@ void zv_kernel_init(
         return;
     }
 
-    self->processes_count = ZV_DYNAMIC_PROCESSES_STEP * 1;
-    self->processes = (zv_process_t *) malloc(ZV_DYNAMIC_PROCESSES_STEP * 1);
+    self->processes_count = ZV_PROCESSES_STEP * 1;
+    self->processes = (zv_process_t *) malloc(ZV_PROCESSES_STEP_SIZE * 1);
     self->is_dynamic = true;
 
     /* When cannot alloc, make kernel unuseable. */

+ 76 - 0
sources/latch.c

@@ -0,0 +1,76 @@
+#include "hash.h"
+#include "latch.h"
+#include "zivu_types.h"
+
+#ifndef ZV_DISABLE_DYNAMIC
+#include <malloc.h>
+#endif
+
+static inline zv_hash_t zv_latch_calculate_hash(zv_latch_t *self) {
+    return zv_hash_calculate(self->data->pointer, self->size);
+}
+
+static inline void zv_latch_regenerate_hash(zv_latch_t *self) {
+    self->hash = zv_latch_calculate_hash(self);
+}
+
+bool zv_latch_changed(zv_latch_t *self) {
+    if (self->changed) {
+        return true;
+    }
+
+    if (self->hash == zv_latch_calculate_hash(self)) {
+        return false;
+    }
+
+    return self->changed = true;
+}
+
+zv_universal_param_t zv_latch_read(zv_latch_t *self) {
+    self->changed = false;
+    self->hash = zv_latch_calculate_hash(self);
+    
+    return *self->data;
+}
+
+void zv_latch_write(zv_latch_t *self, zv_universal_param_t content) {
+    self->changed = true;
+    *self->data = content;
+}
+
+#ifndef ZV_DISABLE_DYNAMIC
+
+zv_latch_t * zv_latch_create(zv_size_t size) {
+    zv_latch_t *self = (zv_latch_t *) malloc(sizeof(zv_latch_t));
+    
+    if (self == NULL) {
+        return NULL;
+    }
+
+    self->size = size;
+    self->is_dynamic = true;
+    self->changed = false;
+    self->data->pointer = (void *) malloc(size);
+
+    if (self->data->pointer == NULL) {
+        free(self);
+        return NULL;
+    }
+
+    zv_latch_regenerate_hash(self);
+
+    return self;
+}
+
+void zv_latch_remove(zv_latch_t *self) {
+    if (!self->is_dynamic) {
+        return;
+    }
+    
+    free(self->data->pointer);
+    free(self);
+}
+
+#endif
+
+

+ 0 - 0
tests/001-pipe/run.sh → tests/001-latch/run.sh


+ 0 - 0
tests/001-pipe/testing.c → tests/001-latch/testing.c