|
@@ -1,70 +1,131 @@
|
|
|
-#ifndef ZV_DISABLE_DYNAMIC
|
|
|
|
|
|
|
+#include <stdint.h>
|
|
|
|
|
+#include <stddef.h>
|
|
|
|
|
+#include <stdbool.h>
|
|
|
|
|
+
|
|
|
|
|
+#ifndef ZIVUOS_USE_STATIC
|
|
|
#include <malloc.h>
|
|
#include <malloc.h>
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
-#include "zivu_types.h"
|
|
|
|
|
-#include "kernel.h"
|
|
|
|
|
|
|
+#include "status.h"
|
|
|
|
|
+#include "models.h"
|
|
|
#include "process.h"
|
|
#include "process.h"
|
|
|
|
|
+#include "kernel.h"
|
|
|
|
|
|
|
|
-#ifndef ZV_DISABLE_DYNAMIC
|
|
|
|
|
|
|
+/* Dynamic kernel creating. */
|
|
|
|
|
+#ifndef ZIVUOS_USE_STATIC
|
|
|
|
|
|
|
|
zv_kernel_t * zv_kernel_create() {
|
|
zv_kernel_t * zv_kernel_create() {
|
|
|
zv_kernel_t *self = (zv_kernel_t *) malloc(sizeof(zv_kernel_t));
|
|
zv_kernel_t *self = (zv_kernel_t *) malloc(sizeof(zv_kernel_t));
|
|
|
-
|
|
|
|
|
- if (self != NULL) {
|
|
|
|
|
- zv_kernel_init(self, NULL, 0x00);
|
|
|
|
|
|
|
+
|
|
|
|
|
+ if (self == NULL) {
|
|
|
|
|
+ return NULL;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ self->is_dynamic = true;
|
|
|
|
|
+ self->processes = NULL;
|
|
|
|
|
+ self->processes_count = 0;
|
|
|
|
|
+ self->current_max_pid = 0;
|
|
|
|
|
+
|
|
|
return self;
|
|
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
|
|
#else
|
|
|
|
|
|
|
|
zv_kernel_t * zv_kernel_create() {
|
|
zv_kernel_t * zv_kernel_create() {
|
|
|
- return (zv_kernel_t *) NULL;
|
|
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+void zv_kernel_close(zv_kernel_t *self) {
|
|
|
|
|
+ /* Not require to free not alloced resources. */
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
-/* For dynamic also kernel. */
|
|
|
|
|
-#ifndef ZV_DISABLE_DYNAMIC
|
|
|
|
|
|
|
+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 *container,
|
|
|
|
|
- zv_pid_t processes_count
|
|
|
|
|
-) {
|
|
|
|
|
- self->current_process = ZV_IDLE_PROCESS;
|
|
|
|
|
|
|
+ if (current == NULL || max == 0) {
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- if (container != NULL) {
|
|
|
|
|
- self->processes = container;
|
|
|
|
|
- self->processes_count = processes_count;
|
|
|
|
|
- self->is_dynamic = false;
|
|
|
|
|
|
|
+ while (current->next != NULL) {
|
|
|
|
|
+ current = current->next;
|
|
|
|
|
|
|
|
- return;
|
|
|
|
|
|
|
+ if (++count >= max) {
|
|
|
|
|
+ return current;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- self->processes_count = ZV_PROCESSES_STEP * 1;
|
|
|
|
|
- self->processes = (zv_process_t *) malloc(ZV_PROCESSES_STEP_SIZE * 1);
|
|
|
|
|
- self->is_dynamic = true;
|
|
|
|
|
|
|
+ return current;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+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;
|
|
|
|
|
+
|
|
|
|
|
+ while (true) {
|
|
|
|
|
+ if (current == NULL || count == 0) {
|
|
|
|
|
+ return NULL;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (current->pid == target) {
|
|
|
|
|
+ return current;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ --count;
|
|
|
|
|
+ current = current->next;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+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;
|
|
|
|
|
|
|
|
- /* When cannot alloc, make kernel unuseable. */
|
|
|
|
|
- if (self->processes == NULL) {
|
|
|
|
|
- self->processes_count = 0x00;
|
|
|
|
|
- self->is_dynamic = false;
|
|
|
|
|
|
|
+ target->pid = self->current_max_pid;
|
|
|
|
|
+ oldest->next = target;
|
|
|
|
|
+
|
|
|
|
|
+ return target->pid;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+/* Dynamic process creating. */
|
|
|
|
|
+#ifndef ZIVUOS_USE_STATIC
|
|
|
|
|
+
|
|
|
|
|
+zv_process_t * zv_kernel_create_process(zv_kernel_t *self) {
|
|
|
|
|
+ zv_process_t *target = zv_process_create();
|
|
|
|
|
+
|
|
|
|
|
+ if (target == NULL) {
|
|
|
|
|
+ return NULL;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ target->status = ZV_STATUS_CREATING;
|
|
|
|
|
+ zv_kernel_attach_process(self, target);
|
|
|
|
|
+
|
|
|
|
|
+ return target;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-#else
|
|
|
|
|
|
|
+#else
|
|
|
|
|
|
|
|
-void zv_kernel_init(
|
|
|
|
|
- zv_kernel_t *self,
|
|
|
|
|
- zv_process_t *container,
|
|
|
|
|
- zv_pid_t processes_count
|
|
|
|
|
-) {
|
|
|
|
|
- self->current_process = ZV_IDLE_PROCESS;
|
|
|
|
|
- self->processes = container;
|
|
|
|
|
- self->processes_count = processes_count;
|
|
|
|
|
|
|
+zv_process_t * zv_kernel_create_process(zv_kernel_t *self) {
|
|
|
|
|
+ return NULL;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-#endif
|
|
|
|
|
|
|
+#endif
|