-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd.h
170 lines (134 loc) · 3.68 KB
/
std.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* std.h - standard library interface aka stdlib.h bindings aka api
This file is part of yalloc, yet another memory allocator providing affordable safety in a compact package.
SPDX-FileCopyrightText: © 2024 Joris van der Geer
SPDX-License-Identifier: GPL-3.0-or-later
*/
#define Logfile Fstd
void *malloc(size_t len)
{
void *p;
#if Yal_prep_TLS
if (unlikely(yal_tls_inited == 0)) {
p = bootalloc(Fln,0,Lnone,(ub4)len);
minidiag(Fln,Lalloc,Debug,0,"TLS init %zu = %zx",len,(size_t)p);
return p;
}
#endif
p = ymalloc(len,Fln);
return p;
}
void free(void *p)
{
#if Yal_prep_TLS
if (unlikely(yal_tls_inited == 0)) return;
#endif
yfree(p,0,Fln);
}
void *calloc (size_t count, size_t size)
{
void *p;
size_t len;
// syscall wrappers call calloc() ...
#ifdef __FreeBSD__
static ub4 bootallocs;
if (unlikely(bootallocs++ < 3)) return bootalloc(Fln,0,Lnone,(ub4)(count * size));
#endif
#if Yal_prep_TLS
if (unlikely(yal_tls_inited == 0)) {
p = bootalloc(Fln,0,Lnone,(ub4)(count * size));
minidiag(Fln,Lcalloc,Debug,0,"TLS init %zu * %zu = %zx",count,size,(size_t)p);
return p;
}
#endif
if (sizeof(size_t ) < sizeof(long long)) { // e.g. 32-bit system
unsigned long long nn = (unsigned long long)count * size;
if (unlikely( nn > Size_max)) return oom(nil,Fln,Lcalloc,count,size);
len = (size_t)nn;
} else { // common for 64-bit systems
bool rv = sat_mul(count,size,&len);
if (unlikely(rv != 0)) return oom(nil,Fln,Lcalloc,count,size);
}
#if Yal_enable_stats && Yal_trigger_stats
if (unlikely(len == 0)) { // calloc(0,Yal_magic) prints stats
yal_trigger_stats(size);
}
#endif
p = yalloc(len,Lcalloc,Fln);
return p;
}
void *realloc(void *p,size_t newlen)
{
void *q;
#if Yal_prep_TLS
if (unlikely(yal_tls_inited == 0)) {
if (p == nil) p = bootalloc(Fln,0,Lnone,(ub4)newlen);
else p = nil; // should not occur, cannot locate
minidiag(Fln,Lalloc,Debug,0,"TLS init %zu = %zx",newlen,(size_t)p);
return nil;
}
#endif
q = yrealloc(p,Nolen,newlen,Fln);
return q;
}
void *aligned_alloc(size_t align, size_t size)
{
void *p;
#if Yal_prep_TLS
if (unlikely(yal_tls_inited == 0)) {
p = osmmap(size); // assuming align > Page not done
minidiag(Fln,Lalloc,Debug,0,"TLS init %zu = %zx",size,(size_t)p);
return p;
}
#endif
p = yalloc_align(align,size,Fln);
return p;
}
#if Yal_psx_memalign && __musl_libc__ == 0
#include <errno.h>
int posix_memalign(void **memptr, size_t align, size_t size)
{
void *p;
// if (align < sizeof(void *)) return EINVAL;
p = aligned_alloc(align,size);
*memptr = p;
if (p == nil) return ENOMEM;
return 0;
}
void *memalign(size_t a,size_t n)
{
return aligned_alloc(a,n);
}
#if Yal_psx_memalign > 1
void *valloc(size_t n) // deprecated
{
return aligned_alloc(Pagesize,n);
}
void *pvalloc(size_t n) // deprecated
{
return aligned_alloc(Pagesize,doalign8(n,Pagesize));
}
#endif
#endif // psx_memalign
#if Yal_reallocarray && __haiku_libroot__ == 0 && __musl_libc__ == 0
void *reallocarray(void *p,size_t nelem, size_t elsize)
{
size_t len;
bool rv = sat_mul(nelem,elsize,&len);
if (unlikely(rv != 0)) return oom(nil,Fln,Lreal,nelem,elsize);
return yrealloc(p,Nolen,len,Fln);
}
#endif
#if Yal_enable_c23
// https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2699.htm
// In yalloc, the size arg is used for checking only. If zero, it is equivalent to free(p)
void free_sized(void *ptr,size_t size)
{
yfree(ptr,size,Fln);
}
// in contrast with the C23 standard, the pointer passed may have been obtained from any of the allocation functions
void free_aligned_sized(void *ptr, size_t Unused alignment, size_t size)
{
yfree(ptr,size,Fln);
}
#endif // c23
#undef Logfile