klibc.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*!
  2. * Copyright (C) Fraunhofer-Institut for Photonic Microsystems (IPMS)
  3. * Maria-Reiche-Str. 2
  4. * 01109 Dresden
  5. *
  6. * Unauthorized copying of this file, via any medium is strictly prohibited
  7. * Proprietary and confidential
  8. *
  9. * \file klibc.c
  10. * \author fra11385
  11. * \date 12.04.2021
  12. * \brief <insert file brief description>
  13. *
  14. */
  15. #include "klibc.h"
  16. #include <kernel/base/kernel.h>
  17. #include <stddef.h>
  18. list_data_t *list_create()
  19. {
  20. list_data_t *top = (list_data_t *)kallocz(sizeof(list_data_t));
  21. top->data = NULL;
  22. top->idx = 0;
  23. top->prev = top;
  24. top->next = top;
  25. return top;
  26. }
  27. void list_free(list_data_t *top, bool free_data)
  28. {
  29. if (top == NULL)
  30. return;
  31. list_data_t *last = top->prev;
  32. while (last != top) {
  33. list_data_t *temp = last;
  34. last = last->prev;
  35. if (free_data)
  36. kfree(temp->data);
  37. kfree(temp);
  38. }
  39. if (free_data)
  40. kfree(top->data);
  41. kfree(top);
  42. }
  43. uint8_t list_size(list_data_t *top)
  44. {
  45. return top == NULL ? 0 : top->prev->idx;
  46. }
  47. list_data_t *list_next(list_data_t *top, list_data_t *data)
  48. {
  49. if (top == NULL || data == NULL)
  50. return NULL;
  51. list_data_t *ret = data->next;
  52. return ret == top ? NULL : ret;
  53. }
  54. list_data_t *list_prev(list_data_t *top, list_data_t *data)
  55. {
  56. if (top == NULL || data == NULL)
  57. return NULL;
  58. list_data_t *ret = data->prev;
  59. return ret == top ? NULL : ret;
  60. }
  61. bool list_end(list_data_t *top, list_data_t *data)
  62. {
  63. return (top == NULL) || (data == NULL) || (data == top);
  64. }
  65. list_data_t *list_add(list_data_t *top, void *data)
  66. {
  67. if (top->data == NULL) {
  68. top->data = data;
  69. top->idx = 1;
  70. return top;
  71. }
  72. list_data_t *next = (list_data_t *)kallocz(sizeof(list_data_t));
  73. next->data = data;
  74. next->idx = top->prev->idx + 1;
  75. next->next = top;
  76. next->prev = top->prev;
  77. top->prev->next = next;
  78. top->prev = next;
  79. return next;
  80. }
  81. list_data_t *list_get(list_data_t *top, uint8_t idx)
  82. {
  83. list_data_t *el = top;
  84. do {
  85. if (el->idx == idx)
  86. return el;
  87. el = el->next;
  88. } while (el != top);
  89. return NULL;
  90. }
  91. void list_del(list_data_t *top, void *data)
  92. {
  93. if (top == NULL || data == NULL)
  94. return;
  95. list_data_t *el = top;
  96. do {
  97. if (el->data == data) {
  98. list_del_entry(top, el);
  99. return;
  100. }
  101. el = el->next;
  102. } while (el != top);
  103. return;
  104. }
  105. void list_del_entry(list_data_t *top, list_data_t *entry)
  106. {
  107. if (top == NULL || entry == NULL)
  108. return;
  109. list_data_t *el = top;
  110. bool update_idx = false;
  111. if (entry == top) {
  112. if (entry->next == top) {
  113. // el == top && list_size == 1
  114. top->data = NULL;
  115. top->idx = 0;
  116. return;
  117. } else {
  118. // el == top && list_size > 1
  119. top->data = top->next->data;
  120. el = top->next;
  121. top->next->next->prev = top;
  122. top->next = top->next->next;
  123. kfree(el);
  124. top->idx = 1;
  125. update_idx = true;
  126. }
  127. }
  128. el = top->next;
  129. while (el != top) {
  130. if (el == entry) {
  131. el = el->next;
  132. entry->prev->next = entry->next;
  133. entry->next->prev = entry->prev;
  134. kfree(entry);
  135. update_idx = true;
  136. continue;
  137. }
  138. if (update_idx)
  139. el->idx = el->prev->idx + 1;
  140. el = el->next;
  141. }
  142. return;
  143. }
  144. char *ltrim(const char *str)
  145. {
  146. char *ret = (char *)str;
  147. while (*ret == ' ')
  148. ret++;
  149. return ret;
  150. }
  151. list_data_t *split(const char *str, const char *delim)
  152. {
  153. list_data_t *ret = list_create();
  154. char *part = strtok((char *)str, delim);
  155. while (part != NULL) {
  156. list_add(ret, part);
  157. part = strtok(NULL, delim);
  158. }
  159. return ret;
  160. }