kernel.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #include <stdbool.h>
  2. #include <stddef.h>
  3. #ifndef ZIVUOS_DISABLE_DYNAMIC
  4. #include <malloc.h>
  5. #endif
  6. #include "process.h"
  7. #include "kernel.h"
  8. #include "models.h"
  9. #include "state.h"
  10. #ifndef ZV_ALLOC_STEP
  11. #define ZV_ALLOC_STEP 0x08
  12. #endif
  13. void zv_kernel_init(
  14. zv_kernel_t *self,
  15. zv_process_t **processes,
  16. zv_pid_t processes_count
  17. ) {
  18. self->processes_stack = processes;
  19. self->processes_stack_size = processes_count;
  20. #ifndef ZIVUOS_DISABLE_DYNAMIC
  21. self->is_dynamic = false;
  22. #endif
  23. }
  24. zv_pid_t zv_kernel_count_processes(zv_kernel_t *self) {
  25. zv_pid_t result = 0;
  26. for (zv_pid_t count = 0; count < self->processes_stack_size; ++count) {
  27. if (self->processes_stack[count] == NULL) {
  28. continue;
  29. }
  30. ++result;
  31. }
  32. return result;
  33. }
  34. #ifndef ZIVUOS_DISABLE_DYNAMIC
  35. static inline size_t zv_get_stack_size(zv_pid_t count) {
  36. return (count * sizeof(zv_process_t *));
  37. }
  38. zv_kernel_t * zv_kernel_create() {
  39. zv_kernel_t *self = (zv_kernel_t *) malloc(sizeof(zv_kernel_t));
  40. if (self == NULL) {
  41. return NULL;
  42. }
  43. #define STACK_SIZE zv_get_stack_size(ZV_ALLOC_STEP)
  44. zv_process_t **stack = (zv_process_t **) malloc(STACK_SIZE);
  45. #undef STACK_SIZE
  46. if (stack == NULL) {
  47. free(self);
  48. return NULL;
  49. }
  50. zv_kernel_init(self, stack, ZV_ALLOC_STEP);
  51. self->is_dynamic = true;
  52. return self;
  53. }
  54. #endif