|
|
@@ -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
|
|
|
+
|
|
|
+
|