| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "mos_stdlib.hpp" |
| 4 | |
| 5 | #ifdef __MOS_KERNEL__ |
| 6 | #include <mos/allocator.hpp> |
| 7 | #endif |
| 8 | |
| 9 | #include <mos/types.h> |
| 10 | #include <mos_stdio.hpp> |
| 11 | #include <mos_string.hpp> |
| 12 | |
| 13 | unsigned char tolower(unsigned char c) |
| 14 | { |
| 15 | if (c >= 'A' && c <= 'Z') |
| 16 | return c - 'A' + 'a'; |
| 17 | return c; |
| 18 | } |
| 19 | |
| 20 | static int isspace(int _c) |
| 21 | { |
| 22 | return ((_c > 8 && _c < 14) || (_c == 32)); |
| 23 | } |
| 24 | |
| 25 | s32 abs(s32 x) |
| 26 | { |
| 27 | return (x < 0) ? -x : x; |
| 28 | } |
| 29 | |
| 30 | long labs(long x) |
| 31 | { |
| 32 | return (x < 0) ? -x : x; |
| 33 | } |
| 34 | |
| 35 | s64 llabs(s64 x) |
| 36 | { |
| 37 | return (x < 0) ? -x : x; |
| 38 | } |
| 39 | |
| 40 | s32 atoi(const char *nptr) |
| 41 | { |
| 42 | s32 c; |
| 43 | s32 neg = 0; |
| 44 | s32 val = 0; |
| 45 | |
| 46 | while (isspace(c: *nptr)) |
| 47 | nptr++; |
| 48 | |
| 49 | if (*nptr == '-') |
| 50 | { |
| 51 | neg = 1; |
| 52 | nptr++; |
| 53 | } |
| 54 | else if (*nptr == '+') |
| 55 | { |
| 56 | nptr++; |
| 57 | } |
| 58 | |
| 59 | while ((c = *nptr++) >= '0' && c <= '9') |
| 60 | { |
| 61 | val = val * 10 + (c - '0'); |
| 62 | } |
| 63 | |
| 64 | return neg ? -val : val; |
| 65 | } |
| 66 | |
| 67 | unsigned long strtoul(const char *__restrict nptr, char **__restrict endptr, int base) |
| 68 | { |
| 69 | return strtoll(str: nptr, endptr, base); |
| 70 | } |
| 71 | |
| 72 | s64 strtoll(const char *str, char **endptr, int base) |
| 73 | { |
| 74 | return strntoll(str, endptr, base, n: strlen(str)); |
| 75 | } |
| 76 | |
| 77 | s64 strntoll(const char *str, char **endptr, int base, size_t n) |
| 78 | { |
| 79 | s64 result = 0; |
| 80 | bool negative = false; |
| 81 | size_t i = 0; |
| 82 | |
| 83 | if (*str == '-') |
| 84 | negative = true, str++, i++; |
| 85 | else if (*str == '+') |
| 86 | str++, i++; |
| 87 | |
| 88 | while (i < n && *str) |
| 89 | { |
| 90 | char c = *str; |
| 91 | if (c >= '0' && c <= '9') |
| 92 | result *= base, result += c - '0'; |
| 93 | else if (c >= 'a' && c <= 'z') |
| 94 | result *= base, result += c - 'a' + 10; |
| 95 | else if (c >= 'A' && c <= 'Z') |
| 96 | result *= base, result += c - 'A' + 10; |
| 97 | else |
| 98 | break; |
| 99 | str++; |
| 100 | i++; |
| 101 | } |
| 102 | if (endptr) |
| 103 | *endptr = (char *) str; |
| 104 | return negative ? -result : result; |
| 105 | } |
| 106 | |
| 107 | void format_size(char *buf, size_t buf_size, u64 size) |
| 108 | { |
| 109 | static const char *const units[] = { "B" , "KiB" , "MiB" , "GiB" , "TiB" , "PiB" , "EiB" , "ZiB" , "YiB" }; |
| 110 | size_t i = 0; |
| 111 | size_t diff = 0; |
| 112 | while (size >= 1024 && i < MOS_ARRAY_SIZE(units) - 1) |
| 113 | { |
| 114 | diff = size % 1024; |
| 115 | size /= 1024; |
| 116 | i++; |
| 117 | } |
| 118 | if (unlikely(diff == 0 || i == 0)) |
| 119 | { |
| 120 | snprintf(str: buf, size: buf_size, format: "%llu %s" , size, units[i]); |
| 121 | } |
| 122 | else |
| 123 | { |
| 124 | snprintf(str: buf, size: buf_size, format: "%llu %s + %zu %s" , size, units[i], diff, units[i - 1]); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | char *string_trim(char *in) |
| 129 | { |
| 130 | if (in == NULL) |
| 131 | return NULL; |
| 132 | |
| 133 | char *end; |
| 134 | |
| 135 | // Trim leading space |
| 136 | while (*in == ' ') |
| 137 | in++; |
| 138 | |
| 139 | if (*in == 0) // All spaces? |
| 140 | return in; |
| 141 | |
| 142 | // Trim trailing space |
| 143 | end = in + strlen(str: in) - 1; |
| 144 | while (end > in && *end == ' ') |
| 145 | end--; |
| 146 | |
| 147 | // Write new null terminator |
| 148 | *(end + 1) = '\0'; |
| 149 | return in; |
| 150 | } |
| 151 | |
| 152 | #ifdef __MOS_KERNEL__ |
| 153 | void *do_kmalloc(size_t size) |
| 154 | { |
| 155 | return slab_alloc(size); |
| 156 | } |
| 157 | |
| 158 | void *do_kcalloc(size_t nmemb, size_t size) |
| 159 | { |
| 160 | return slab_calloc(nmemb, size); |
| 161 | } |
| 162 | |
| 163 | void *do_krealloc(void *ptr, size_t size) |
| 164 | { |
| 165 | return slab_realloc(addr: ptr, size); |
| 166 | } |
| 167 | |
| 168 | void do_kfree(const void *ptr) |
| 169 | { |
| 170 | slab_free(addr: ptr); |
| 171 | } |
| 172 | |
| 173 | void *malloc(size_t size) |
| 174 | { |
| 175 | return do_kmalloc(size); |
| 176 | } |
| 177 | |
| 178 | void *calloc(size_t nmemb, size_t size) |
| 179 | { |
| 180 | return do_kcalloc(nmemb, size); |
| 181 | } |
| 182 | |
| 183 | void *realloc(void *ptr, size_t size) |
| 184 | { |
| 185 | return do_krealloc(ptr, size); |
| 186 | } |
| 187 | |
| 188 | void free(void *ptr) |
| 189 | { |
| 190 | return do_kfree(ptr); |
| 191 | } |
| 192 | #endif |
| 193 | |