|
@@ -1,131 +1,73 @@
|
|
|
-#include <stdint.h>
|
|
|
|
|
-#include <stddef.h>
|
|
|
|
|
#include <stdbool.h>
|
|
#include <stdbool.h>
|
|
|
|
|
+#include <stddef.h>
|
|
|
|
|
|
|
|
-#ifndef ZIVUOS_USE_STATIC
|
|
|
|
|
|
|
+#ifndef ZIVUOS_DISABLE_DYNAMIC
|
|
|
#include <malloc.h>
|
|
#include <malloc.h>
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
-#include "status.h"
|
|
|
|
|
-#include "models.h"
|
|
|
|
|
#include "process.h"
|
|
#include "process.h"
|
|
|
#include "kernel.h"
|
|
#include "kernel.h"
|
|
|
|
|
+#include "models.h"
|
|
|
|
|
+#include "state.h"
|
|
|
|
|
|
|
|
-/* Dynamic kernel creating. */
|
|
|
|
|
-#ifndef ZIVUOS_USE_STATIC
|
|
|
|
|
-
|
|
|
|
|
-zv_kernel_t * zv_kernel_create() {
|
|
|
|
|
- zv_kernel_t *self = (zv_kernel_t *) malloc(sizeof(zv_kernel_t));
|
|
|
|
|
-
|
|
|
|
|
- if (self == NULL) {
|
|
|
|
|
- return NULL;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- self->is_dynamic = true;
|
|
|
|
|
- self->processes = NULL;
|
|
|
|
|
- self->processes_count = 0;
|
|
|
|
|
- self->current_max_pid = 0;
|
|
|
|
|
-
|
|
|
|
|
- return self;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-void zv_kernel_close(zv_kernel_t *self) {
|
|
|
|
|
- if (!self->is_dynamic) {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- zv_process_t *current = self->processes;
|
|
|
|
|
-
|
|
|
|
|
- while (current->next != NULL) {
|
|
|
|
|
- current = zv_process_remove(current);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- free(self);
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-/* Static kernel creating. */
|
|
|
|
|
-#else
|
|
|
|
|
-
|
|
|
|
|
-zv_kernel_t * zv_kernel_create() {
|
|
|
|
|
- return NULL;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-void zv_kernel_close(zv_kernel_t *self) {
|
|
|
|
|
- /* Not require to free not alloced resources. */
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
|
|
+#ifndef ZV_ALLOC_STEP
|
|
|
|
|
+#define ZV_ALLOC_STEP 0x08
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
-zv_process_t * zv_kernel_get_oldest(zv_kernel_t *self) {
|
|
|
|
|
- zv_process_t *current = self->processes;
|
|
|
|
|
- zv_pid_t count = 0;
|
|
|
|
|
- zv_pid_t max = self->processes_count;
|
|
|
|
|
|
|
+void zv_kernel_init(
|
|
|
|
|
+ zv_kernel_t *self,
|
|
|
|
|
+ zv_process_t **processes,
|
|
|
|
|
+ zv_pid_t processes_count
|
|
|
|
|
+) {
|
|
|
|
|
+ self->processes_stack = processes;
|
|
|
|
|
+ self->processes_stack_size = processes_count;
|
|
|
|
|
+#ifndef ZIVUOS_DISABLE_DYNAMIC
|
|
|
|
|
+ self->is_dynamic = false;
|
|
|
|
|
+#endif
|
|
|
|
|
+}
|
|
|
|
|
|
|
|
- if (current == NULL || max == 0) {
|
|
|
|
|
- return NULL;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+zv_pid_t zv_kernel_count_processes(zv_kernel_t *self) {
|
|
|
|
|
+ zv_pid_t result = 0;
|
|
|
|
|
|
|
|
- while (current->next != NULL) {
|
|
|
|
|
- current = current->next;
|
|
|
|
|
-
|
|
|
|
|
- if (++count >= max) {
|
|
|
|
|
- return current;
|
|
|
|
|
|
|
+ for (zv_pid_t count = 0; count < self->processes_stack_size; ++count) {
|
|
|
|
|
+ if (self->processes_stack[count] == NULL) {
|
|
|
|
|
+ continue;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ ++result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- return current;
|
|
|
|
|
|
|
+ return result;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-zv_process_t * zv_kernel_select_process(zv_kernel_t *self, zv_pid_t target) {
|
|
|
|
|
- zv_process_t *current = self->processes;
|
|
|
|
|
- zv_pid_t count = self->processes_count;
|
|
|
|
|
|
|
+#ifndef ZIVUOS_DISABLE_DYNAMIC
|
|
|
|
|
|
|
|
- while (true) {
|
|
|
|
|
- if (current == NULL || count == 0) {
|
|
|
|
|
- return NULL;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (current->pid == target) {
|
|
|
|
|
- return current;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- --count;
|
|
|
|
|
- current = current->next;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+static inline size_t zv_get_stack_size(zv_pid_t count) {
|
|
|
|
|
+ return (count * sizeof(zv_process_t *));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-zv_pid_t zv_kernel_attach_process(zv_kernel_t *self, zv_process_t *target) {
|
|
|
|
|
- zv_process_t *oldest = zv_kernel_get_oldest(self);
|
|
|
|
|
-
|
|
|
|
|
- ++self->processes_count;
|
|
|
|
|
- ++self->current_max_pid;
|
|
|
|
|
-
|
|
|
|
|
- target->pid = self->current_max_pid;
|
|
|
|
|
- oldest->next = target;
|
|
|
|
|
|
|
+zv_kernel_t * zv_kernel_create() {
|
|
|
|
|
+ zv_kernel_t *self = (zv_kernel_t *) malloc(sizeof(zv_kernel_t));
|
|
|
|
|
|
|
|
- return target->pid;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-/* Dynamic process creating. */
|
|
|
|
|
-#ifndef ZIVUOS_USE_STATIC
|
|
|
|
|
|
|
+ if (self == NULL) {
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
-zv_process_t * zv_kernel_create_process(zv_kernel_t *self) {
|
|
|
|
|
- zv_process_t *target = zv_process_create();
|
|
|
|
|
|
|
+#define STACK_SIZE zv_get_stack_size(ZV_ALLOC_STEP)
|
|
|
|
|
+ zv_process_t **stack = (zv_process_t **) malloc(STACK_SIZE);
|
|
|
|
|
+#undef STACK_SIZE
|
|
|
|
|
|
|
|
- if (target == NULL) {
|
|
|
|
|
|
|
+ if (stack == NULL) {
|
|
|
|
|
+ free(self);
|
|
|
return NULL;
|
|
return NULL;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- target->status = ZV_STATUS_CREATING;
|
|
|
|
|
- zv_kernel_attach_process(self, target);
|
|
|
|
|
-
|
|
|
|
|
- return target;
|
|
|
|
|
-}
|
|
|
|
|
-
|
|
|
|
|
-#else
|
|
|
|
|
|
|
+ zv_kernel_init(self, stack, ZV_ALLOC_STEP);
|
|
|
|
|
+ self->is_dynamic = true;
|
|
|
|
|
|
|
|
-zv_process_t * zv_kernel_create_process(zv_kernel_t *self) {
|
|
|
|
|
- return NULL;
|
|
|
|
|
|
|
+ return self;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
#endif
|
|
|
|
|
+
|
|
|
|
|
+
|