stdlib.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #include <stdlib.h>
  2. #include <sys/mman.h>
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <alloca.h>
  7. #include <assert.h>
  8. #include <Kernel/Syscall.h>
  9. #include <AK/Assertions.h>
  10. extern "C" {
  11. // FIXME: This is a temporary malloc() implementation. It never frees anything,
  12. // and you can't allocate more than 128 kB total.
  13. static const size_t mallocBudget = 131072;
  14. static byte* nextptr = nullptr;
  15. static byte* endptr = nullptr;
  16. void __malloc_init()
  17. {
  18. nextptr = (byte*)mmap(nullptr, mallocBudget, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
  19. endptr = nextptr + mallocBudget;
  20. int rc = set_mmap_name(nextptr, mallocBudget, "malloc");
  21. if (rc < 0)
  22. perror("set_mmap_name failed");
  23. }
  24. void* malloc(size_t size)
  25. {
  26. if ((nextptr + size) > endptr) {
  27. volatile char* crashme = (char*)0xc007d00d;
  28. *crashme = 0;
  29. }
  30. byte* ret = nextptr;
  31. nextptr += size;
  32. nextptr += 16;
  33. nextptr = (byte*)((dword)nextptr & 0xfffffff0);
  34. return ret;
  35. }
  36. void free(void* ptr)
  37. {
  38. if (!ptr)
  39. return;
  40. #if 0
  41. munmap(ptr, 4096);
  42. #endif
  43. }
  44. void* calloc(size_t nmemb, size_t)
  45. {
  46. (void) nmemb;
  47. ASSERT_NOT_REACHED();
  48. return nullptr;
  49. }
  50. void* realloc(void *ptr, size_t size)
  51. {
  52. // FIXME: This is broken as shit.
  53. auto* new_ptr = malloc(size);
  54. memcpy(new_ptr, ptr, size);
  55. return new_ptr;
  56. }
  57. void exit(int status)
  58. {
  59. Syscall::invoke(Syscall::SC_exit, (dword)status);
  60. for (;;);
  61. }
  62. void abort()
  63. {
  64. // FIXME: Implement proper abort().
  65. exit(253);
  66. }
  67. char* getenv(const char* name)
  68. {
  69. for (size_t i = 0; environ[i]; ++i) {
  70. const char* decl = environ[i];
  71. char* eq = strchr(decl, '=');
  72. if (!eq)
  73. continue;
  74. size_t varLength = eq - decl;
  75. char* var = (char*)alloca(varLength + 1);
  76. memcpy(var, decl, varLength);
  77. var[varLength] = '\0';
  78. if (!strcmp(var, name)) {
  79. char* value = eq + 1;
  80. return value;
  81. }
  82. }
  83. return nullptr;
  84. }
  85. int atoi(const char* str)
  86. {
  87. size_t len = strlen(str);
  88. int value = 0;
  89. bool isNegative = false;
  90. for (size_t i = 0; i < len; ++i) {
  91. if (i == 0 && str[0] == '-') {
  92. isNegative = true;
  93. continue;
  94. }
  95. if (str[i] < '0' || str[i] > '9')
  96. return value;
  97. value = value * 10;
  98. value += str[i] - '0';
  99. }
  100. return isNegative ? -value : value;
  101. }
  102. long atol(const char* str)
  103. {
  104. static_assert(sizeof(int) == sizeof(long));
  105. return atoi(str);
  106. }
  107. void __qsort(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *))
  108. {
  109. (void) base;
  110. (void) nmemb;
  111. (void) size;
  112. (void) compar;
  113. assert(false);
  114. }
  115. }