MOS Source Code
Loading...
Searching...
No Matches
mos_stdlib.cpp
Go to the documentation of this file.
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
13unsigned char tolower(unsigned char c)
14{
15 if (c >= 'A' && c <= 'Z')
16 return c - 'A' + 'a';
17 return c;
18}
19
20static int isspace(int _c)
21{
22 return ((_c > 8 && _c < 14) || (_c == 32));
23}
24
26{
27 return (x < 0) ? -x : x;
28}
29
30long labs(long x)
31{
32 return (x < 0) ? -x : x;
33}
34
36{
37 return (x < 0) ? -x : x;
38}
39
40s32 atoi(const char *nptr)
41{
42 s32 c;
43 s32 neg = 0;
44 s32 val = 0;
45
46 while (isspace(*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
67unsigned long strtoul(const char *__restrict nptr, char **__restrict endptr, int base)
68{
69 return strtoll(nptr, endptr, base);
70}
71
72s64 strtoll(const char *str, char **endptr, int base)
73{
74 return strntoll(str, endptr, base, strlen(str));
75}
76
77s64 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
107void 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(buf, buf_size, "%llu %s", size, units[i]);
121 }
122 else
123 {
124 snprintf(buf, buf_size, "%llu %s + %zu %s", size, units[i], diff, units[i - 1]);
125 }
126}
127
128char *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(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__
153void *do_kmalloc(size_t size)
154{
155 return slab_alloc(size);
156}
157
158void *do_kcalloc(size_t nmemb, size_t size)
159{
160 return slab_calloc(nmemb, size);
161}
162
163void *do_krealloc(void *ptr, size_t size)
164{
165 return slab_realloc(ptr, size);
166}
167
168void do_kfree(const void *ptr)
169{
170 slab_free(ptr);
171}
172
173void *malloc(size_t size)
174{
175 return do_kmalloc(size);
176}
177
178void *calloc(size_t nmemb, size_t size)
179{
180 return do_kcalloc(nmemb, size);
181}
182
183void *realloc(void *ptr, size_t size)
184{
185 return do_krealloc(ptr, size);
186}
187
188void free(void *ptr)
189{
190 return do_kfree(ptr);
191}
192#endif
MOSAPI void * do_kmalloc(size_t size)
MOSAPI void * do_krealloc(void *ptr, size_t size)
MOSAPI void * do_kcalloc(size_t nmemb, size_t size)
MOSAPI void do_kfree(const void *ptr)
s64 strtoll(const char *str, char **endptr, int base)
unsigned char tolower(unsigned char c)
s64 llabs(s64 x)
s32 abs(s32 x)
void format_size(char *buf, size_t buf_size, u64 size)
s64 strntoll(const char *str, char **endptr, int base, size_t n)
long labs(long x)
char * string_trim(char *in)
s32 atoi(const char *nptr)
#define MOS_ARRAY_SIZE(x)
Definition mos_global.h:84
#define unlikely(x)
Definition mos_global.h:40
int snprintf(char *__restrict str, size_t size, const char *__restrict format,...)
Definition mos_stdio.cpp:16
unsigned long strtoul(const char *__restrict nptr, char **__restrict endptr, int base)
static int isspace(int _c)
#define NULL
Definition pb_syshdr.h:46
static size_t strlen(const char *s)
Definition pb_syshdr.h:80
mos::shared_ptr< T > ptr
size_t size
Definition slab.cpp:34
void * slab_realloc(void *addr, size_t size)
Reallocate a block of memory from the slab allocator.
Definition slab.cpp:157
void * slab_alloc(size_t size)
Allocate a block of memory from the slab allocator.
Definition slab.cpp:129
void slab_free(const void *addr)
Free a block of memory from the slab allocator.
Definition slab.cpp:199
void * slab_calloc(size_t nmemb, size_t size)
Allocate a block of memory from the slab allocator and zero it.
Definition slab.cpp:147
signed int s32
Definition types.h:11
signed long long int s64
Definition types.h:13
unsigned long long u64
Definition types.h:19