1
2
3
4
5
6 namespace System
7 {
8 public inline constexpr long Align(long n, long alignment)
9 {
10 if (alignment <= 0)
11 {
12 ThrowInvalidParameterException();
13 }
14 return (n + alignment - 1) & -alignment;
15 }
16
17 public inline constexpr nothrow long StrLen(const char* s)
18 {
19 long len = 0;
20 if (s != null)
21 {
22 while (*s != '\0')
23 {
24 ++len;
25 ++s;
26 }
27 }
28 return len;
29 }
30
31 public inline constexpr nothrow long StrLen(const wchar* s)
32 {
33 long len = 0;
34 if (s != null)
35 {
36 while (*s != '\0')
37 {
38 ++len;
39 ++s;
40 }
41 }
42 return len;
43 }
44
45 public inline constexpr nothrow long StrLen(const uchar* s)
46 {
47 long len = 0;
48 if (s != null)
49 {
50 while (*s != '\0')
51 {
52 ++len;
53 ++s;
54 }
55 }
56 return len;
57 }
58
59 public inline void StrCopy(char* buf, const char* from)
60 {
61 if (buf == null)
62 {
63 ThrowInvalidParameterException();
64 }
65 if (from != null)
66 {
67 while (*from != '\0')
68 {
69 *buf++ = *from++;
70 }
71 }
72 *buf = '\0';
73 }
74
75 public inline void StrCopy(wchar* buf, const wchar* from)
76 {
77 if (buf == null)
78 {
79 ThrowInvalidParameterException();
80 }
81 if (from != null)
82 {
83 while (*from != '\0')
84 {
85 *buf++ = *from++;
86 }
87 }
88 *buf = '\0';
89 }
90
91 public inline void StrCopy(uchar* buf, const uchar* from)
92 {
93 if (buf == null)
94 {
95 ThrowInvalidParameterException();
96 }
97 if (from != null)
98 {
99 while (*from != '\0')
100 {
101 *buf++ = *from++;
102 }
103 }
104 *buf = '\0';
105 }
106
107 public inline long StrCopy(char* buf, const char* from, long length)
108 {
109 long resultLen = 0;
110 if (buf == null)
111 {
112 ThrowInvalidParameterException();
113 }
114 if (from != null)
115 {
116 while (resultLen < length)
117 {
118 if (*from == '\0')
119 {
120 break;
121 }
122 *buf++ = *from++;
123 ++resultLen;
124 }
125 }
126 *buf = '\0';
127 return resultLen;
128 }
129
130 public inline long StrCopy(wchar* buf, const wchar* from, long length)
131 {
132 long resultLen = 0;
133 if (buf == null)
134 {
135 ThrowInvalidParameterException();
136 }
137 if (from != null)
138 {
139 while (resultLen < length)
140 {
141 if (*from == '\0')
142 {
143 break;
144 }
145 *buf++ = *from++;
146 ++resultLen;
147 }
148 }
149 *buf = '\0';
150 return resultLen;
151 }
152
153 public inline long StrCopy(uchar* buf, const uchar* from, long length)
154 {
155 long resultLen = 0;
156 if (buf == null)
157 {
158 ThrowInvalidParameterException();
159 }
160 if (from != null)
161 {
162 while (resultLen < length)
163 {
164 if (*from == '\0')
165 {
166 break;
167 }
168 *buf++ = *from++;
169 ++resultLen;
170 }
171 }
172 *buf = '\0';
173 return resultLen;
174 }
175
176 public nothrow long MemGrow(long size)
177 {
178 if (size < 8)
179 {
180 return 8;
181 }
182 else if (size < 64)
183 {
184 return 64;
185 }
186 else if (size < 512)
187 {
188 return 512;
189 }
190 else if (size < 4096)
191 {
192 return 4096;
193 }
194 else
195 {
196 return 2 * Align(size, 4096);
197 }
198 }
199
200 public inline nothrow void* MemAlloc(long size)
201 {
202 return RtMemAlloc(size);
203 }
204
205 public inline nothrow void* MemAlloc(long size, const char* info)
206 {
207 return RtMemAllocInfo(size, info);
208 }
209
210 public inline nothrow void MemFree(void* ptr)
211 {
212 #if (DEBUG)
213 RtDispose(ptr);
214 #endif
215 RtMemFree(ptr);
216 }
217 }