| 1 |
/** |
| 2 |
* Allocate memory using `malloc` or the GC depending on the configuration. |
| 3 |
* |
| 4 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 5 |
* Authors: Walter Bright, https://www.digitalmars.com |
| 6 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 7 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/root/rmem.d, root/_rmem.d) |
| 8 |
* Documentation: https://dlang.org/phobos/dmd_root_rmem.html |
| 9 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/root/rmem.d |
| 10 |
*/ |
| 11 |
|
| 12 |
module dmd.root.rmem; |
| 13 |
|
| 14 |
import core.exception : onOutOfMemoryError; |
| 15 |
import core.stdc.stdio; |
| 16 |
import core.stdc.stdlib; |
| 17 |
import core.stdc.string; |
| 18 |
|
| 19 |
import core.memory : GC; |
| 20 |
|
| 21 |
extern (C++) struct Mem |
| 22 |
{ |
| 23 |
static char* xstrdup(const(char)* s) nothrow |
| 24 |
{ |
| 25 |
if (isGCEnabled) |
| 26 |
return s ? s[0 .. strlen(s) + 1].dup.ptr : null; |
| 27 |
|
| 28 |
return s ? cast(char*)check(.strdup(s)) : null; |
| 29 |
} |
| 30 |
|
| 31 |
static void xfree(void* p) pure nothrow |
| 32 |
{ |
| 33 |
if (isGCEnabled) |
| 34 |
return GC.free(p); |
| 35 |
|
| 36 |
pureFree(p); |
| 37 |
} |
| 38 |
|
| 39 |
static void* xmalloc(size_t size) pure nothrow |
| 40 |
{ |
| 41 |
if (isGCEnabled) |
| 42 |
return size ? GC.malloc(size) : null; |
| 43 |
|
| 44 |
return size ? check(pureMalloc(size)) : null; |
| 45 |
} |
| 46 |
|
| 47 |
static void* xmalloc_noscan(size_t size) pure nothrow |
| 48 |
{ |
| 49 |
if (isGCEnabled) |
| 50 |
return size ? GC.malloc(size, GC.BlkAttr.NO_SCAN) : null; |
| 51 |
|
| 52 |
return size ? check(pureMalloc(size)) : null; |
| 53 |
} |
| 54 |
|
| 55 |
static void* xcalloc(size_t size, size_t n) pure nothrow |
| 56 |
{ |
| 57 |
if (isGCEnabled) |
| 58 |
return size * n ? GC.calloc(size * n) : null; |
| 59 |
|
| 60 |
return (size && n) ? check(pureCalloc(size, n)) : null; |
| 61 |
} |
| 62 |
|
| 63 |
static void* xcalloc_noscan(size_t size, size_t n) pure nothrow |
| 64 |
{ |
| 65 |
if (isGCEnabled) |
| 66 |
return size * n ? GC.calloc(size * n, GC.BlkAttr.NO_SCAN) : null; |
| 67 |
|
| 68 |
return (size && n) ? check(pureCalloc(size, n)) : null; |
| 69 |
} |
| 70 |
|
| 71 |
static void* xrealloc(void* p, size_t size) pure nothrow |
| 72 |
{ |
| 73 |
if (isGCEnabled) |
| 74 |
return GC.realloc(p, size); |
| 75 |
|
| 76 |
if (!size) |
| 77 |
{ |
| 78 |
pureFree(p); |
| 79 |
return null; |
| 80 |
} |
| 81 |
|
| 82 |
return check(pureRealloc(p, size)); |
| 83 |
} |
| 84 |
|
| 85 |
static void* xrealloc_noscan(void* p, size_t size) pure nothrow |
| 86 |
{ |
| 87 |
if (isGCEnabled) |
| 88 |
return GC.realloc(p, size, GC.BlkAttr.NO_SCAN); |
| 89 |
|
| 90 |
if (!size) |
| 91 |
{ |
| 92 |
pureFree(p); |
| 93 |
return null; |
| 94 |
} |
| 95 |
|
| 96 |
return check(pureRealloc(p, size)); |
| 97 |
} |
| 98 |
|
| 99 |
static void* error() pure nothrow @nogc @safe |
| 100 |
{ |
| 101 |
onOutOfMemoryError(); |
| 102 |
assert(0); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Check p for null. If it is, issue out of memory error |
| 107 |
* and exit program. |
| 108 |
* Params: |
| 109 |
* p = pointer to check for null |
| 110 |
* Returns: |
| 111 |
* p if not null |
| 112 |
*/ |
| 113 |
static void* check(void* p) pure nothrow @nogc @safe |
| 114 |
{ |
| 115 |
return p ? p : error(); |
| 116 |
} |
| 117 |
|
| 118 |
__gshared bool _isGCEnabled = true; |
| 119 |
|
| 120 |
// fake purity by making global variable immutable (_isGCEnabled only modified before startup) |
| 121 |
enum _pIsGCEnabled = cast(immutable bool*) &_isGCEnabled; |
| 122 |
|
| 123 |
static bool isGCEnabled() pure nothrow @nogc @safe |
| 124 |
{ |
| 125 |
return *_pIsGCEnabled; |
| 126 |
} |
| 127 |
|
| 128 |
static void disableGC() nothrow @nogc |
| 129 |
{ |
| 130 |
_isGCEnabled = false; |
| 131 |
} |
| 132 |
|
| 133 |
static void addRange(const(void)* p, size_t size) nothrow @nogc |
| 134 |
{ |
| 135 |
if (isGCEnabled) |
| 136 |
GC.addRange(p, size); |
| 137 |
} |
| 138 |
|
| 139 |
static void removeRange(const(void)* p) nothrow @nogc |
| 140 |
{ |
| 141 |
if (isGCEnabled) |
| 142 |
GC.removeRange(p); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
extern (C++) const __gshared Mem mem; |
| 147 |
|
| 148 |
enum CHUNK_SIZE = (256 * 4096 - 64); |
| 149 |
|
| 150 |
__gshared size_t heapleft = 0; |
| 151 |
__gshared void* heapp; |
| 152 |
__gshared size_t heapTotal = 0; // Total amount of memory allocated using malloc |
| 153 |
|
| 154 |
extern (D) void* allocmemoryNoFree(size_t m_size) nothrow @nogc |
| 155 |
{ |
| 156 |
// 16 byte alignment is better (and sometimes needed) for doubles |
| 157 |
m_size = (m_size + 15) & ~15; |
| 158 |
|
| 159 |
// The layout of the code is selected so the most common case is straight through |
| 160 |
if (m_size <= heapleft) |
| 161 |
{ |
| 162 |
L1: |
| 163 |
heapleft -= m_size; |
| 164 |
auto p = heapp; |
| 165 |
heapp = cast(void*)(cast(char*)heapp + m_size); |
| 166 |
return p; |
| 167 |
} |
| 168 |
|
| 169 |
if (m_size > CHUNK_SIZE) |
| 170 |
{ |
| 171 |
heapTotal += m_size; |
| 172 |
return Mem.check(malloc(m_size)); |
| 173 |
} |
| 174 |
|
| 175 |
heapleft = CHUNK_SIZE; |
| 176 |
heapp = Mem.check(malloc(CHUNK_SIZE)); |
| 177 |
heapTotal += CHUNK_SIZE; |
| 178 |
goto L1; |
| 179 |
} |
| 180 |
|
| 181 |
extern (D) void* allocmemory(size_t m_size) nothrow |
| 182 |
{ |
| 183 |
if (mem.isGCEnabled) |
| 184 |
return GC.malloc(m_size); |
| 185 |
|
| 186 |
return allocmemoryNoFree(m_size); |
| 187 |
} |
| 188 |
|
| 189 |
version (DigitalMars) |
| 190 |
{ |
| 191 |
enum OVERRIDE_MEMALLOC = true; |
| 192 |
} |
| 193 |
else version (LDC) |
| 194 |
{ |
| 195 |
// Memory allocation functions gained weak linkage when the @weak attribute was introduced. |
| 196 |
import ldc.attributes; |
| 197 |
enum OVERRIDE_MEMALLOC = is(typeof(ldc.attributes.weak)); |
| 198 |
} |
| 199 |
else version (GNU) |
| 200 |
{ |
| 201 |
version (IN_GCC) |
| 202 |
enum OVERRIDE_MEMALLOC = false; |
| 203 |
else |
| 204 |
enum OVERRIDE_MEMALLOC = true; |
| 205 |
} |
| 206 |
else |
| 207 |
{ |
| 208 |
enum OVERRIDE_MEMALLOC = false; |
| 209 |
} |
| 210 |
|
| 211 |
static if (OVERRIDE_MEMALLOC) |
| 212 |
{ |
| 213 |
// Override the host druntime allocation functions in order to use the bump- |
| 214 |
// pointer allocation scheme (`allocmemoryNoFree()` above) if the GC is disabled. |
| 215 |
// That scheme is faster and comes with less memory overhead than using a |
| 216 |
// disabled GC alone. |
| 217 |
|
| 218 |
extern (C) void* _d_allocmemory(size_t m_size) nothrow |
| 219 |
{ |
| 220 |
return allocmemory(m_size); |
| 221 |
} |
| 222 |
|
| 223 |
private void* allocClass(const ClassInfo ci) nothrow pure |
| 224 |
{ |
| 225 |
alias BlkAttr = GC.BlkAttr; |
| 226 |
|
| 227 |
assert(!(ci.m_flags & TypeInfo_Class.ClassFlags.isCOMclass)); |
| 228 |
|
| 229 |
BlkAttr attr = BlkAttr.NONE; |
| 230 |
if (ci.m_flags & TypeInfo_Class.ClassFlags.hasDtor |
| 231 |
&& !(ci.m_flags & TypeInfo_Class.ClassFlags.isCPPclass)) |
| 232 |
attr |= BlkAttr.FINALIZE; |
| 233 |
if (ci.m_flags & TypeInfo_Class.ClassFlags.noPointers) |
| 234 |
attr |= BlkAttr.NO_SCAN; |
| 235 |
return GC.malloc(ci.initializer.length, attr, ci); |
| 236 |
} |
| 237 |
|
| 238 |
extern (C) void* _d_newitemU(const TypeInfo ti) nothrow; |
| 239 |
|
| 240 |
extern (C) Object _d_newclass(const ClassInfo ci) nothrow |
| 241 |
{ |
| 242 |
const initializer = ci.initializer; |
| 243 |
|
| 244 |
auto p = mem.isGCEnabled ? allocClass(ci) : allocmemoryNoFree(initializer.length); |
| 245 |
memcpy(p, initializer.ptr, initializer.length); |
| 246 |
return cast(Object) p; |
| 247 |
} |
| 248 |
|
| 249 |
version (LDC) |
| 250 |
{ |
| 251 |
extern (C) Object _d_allocclass(const ClassInfo ci) nothrow |
| 252 |
{ |
| 253 |
if (mem.isGCEnabled) |
| 254 |
return cast(Object) allocClass(ci); |
| 255 |
|
| 256 |
return cast(Object) allocmemoryNoFree(ci.initializer.length); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
extern (C) void* _d_newitemT(TypeInfo ti) nothrow |
| 261 |
{ |
| 262 |
auto p = mem.isGCEnabled ? _d_newitemU(ti) : allocmemoryNoFree(ti.tsize); |
| 263 |
memset(p, 0, ti.tsize); |
| 264 |
return p; |
| 265 |
} |
| 266 |
|
| 267 |
extern (C) void* _d_newitemiT(TypeInfo ti) nothrow |
| 268 |
{ |
| 269 |
auto p = mem.isGCEnabled ? _d_newitemU(ti) : allocmemoryNoFree(ti.tsize); |
| 270 |
const initializer = ti.initializer; |
| 271 |
memcpy(p, initializer.ptr, initializer.length); |
| 272 |
return p; |
| 273 |
} |
| 274 |
|
| 275 |
// TypeInfo.initializer for compilers older than 2.070 |
| 276 |
static if(!__traits(hasMember, TypeInfo, "initializer")) |
| 277 |
private const(void[]) initializer(T : TypeInfo)(const T t) |
| 278 |
nothrow pure @safe @nogc |
| 279 |
{ |
| 280 |
return t.init; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
extern (C) pure @nogc nothrow |
| 285 |
{ |
| 286 |
/** |
| 287 |
* Pure variants of C's memory allocation functions `malloc`, `calloc`, and |
| 288 |
* `realloc` and deallocation function `free`. |
| 289 |
* |
| 290 |
* UNIX 98 requires that errno be set to ENOMEM upon failure. |
| 291 |
* https://linux.die.net/man/3/malloc |
| 292 |
* However, this is irrelevant for DMD's purposes, and best practice |
| 293 |
* protocol for using errno is to treat it as an `out` parameter, and not |
| 294 |
* something with state that can be relied on across function calls. |
| 295 |
* So, we'll ignore it. |
| 296 |
* |
| 297 |
* See_Also: |
| 298 |
* $(LINK2 https://dlang.org/spec/function.html#pure-functions, D's rules for purity), |
| 299 |
* which allow for memory allocation under specific circumstances. |
| 300 |
*/ |
| 301 |
pragma(mangle, "malloc") void* pureMalloc(size_t size) @trusted; |
| 302 |
|
| 303 |
/// ditto |
| 304 |
pragma(mangle, "calloc") void* pureCalloc(size_t nmemb, size_t size) @trusted; |
| 305 |
|
| 306 |
/// ditto |
| 307 |
pragma(mangle, "realloc") void* pureRealloc(void* ptr, size_t size) @system; |
| 308 |
|
| 309 |
/// ditto |
| 310 |
pragma(mangle, "free") void pureFree(void* ptr) @system; |
| 311 |
|
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
Makes a null-terminated copy of the given string on newly allocated memory. |
| 316 |
The null-terminator won't be part of the returned string slice. It will be |
| 317 |
at position `n` where `n` is the length of the input string. |
| 318 |
|
| 319 |
Params: |
| 320 |
s = string to copy |
| 321 |
|
| 322 |
Returns: A null-terminated copy of the input array. |
| 323 |
*/ |
| 324 |
extern (D) char[] xarraydup(scope const(char)[] s) pure nothrow |
| 325 |
{ |
| 326 |
if (!s) |
| 327 |
return null; |
| 328 |
|
| 329 |
auto p = cast(char*)mem.xmalloc_noscan(s.length + 1); |
| 330 |
char[] a = p[0 .. s.length]; |
| 331 |
a[] = s[0 .. s.length]; |
| 332 |
p[s.length] = 0; // preserve 0 terminator semantics |
| 333 |
return a; |
| 334 |
} |
| 335 |
|
| 336 |
/// |
| 337 |
pure nothrow unittest |
| 338 |
{ |
| 339 |
auto s1 = "foo"; |
| 340 |
auto s2 = s1.xarraydup; |
| 341 |
s2[0] = 'b'; |
| 342 |
assert(s1 == "foo"); |
| 343 |
assert(s2 == "boo"); |
| 344 |
assert(*(s2.ptr + s2.length) == '\0'); |
| 345 |
string sEmpty; |
| 346 |
assert(sEmpty.xarraydup is null); |
| 347 |
} |
| 348 |
|
| 349 |
/** |
| 350 |
Makes a copy of the given array on newly allocated memory. |
| 351 |
|
| 352 |
Params: |
| 353 |
s = array to copy |
| 354 |
|
| 355 |
Returns: A copy of the input array. |
| 356 |
*/ |
| 357 |
extern (D) T[] arraydup(T)(const scope T[] s) pure nothrow |
| 358 |
{ |
| 359 |
if (!s) |
| 360 |
return null; |
| 361 |
|
| 362 |
const dim = s.length; |
| 363 |
auto p = (cast(T*)mem.xmalloc(T.sizeof * dim))[0 .. dim]; |
| 364 |
p[] = s; |
| 365 |
return p; |
| 366 |
} |
| 367 |
|
| 368 |
/// |
| 369 |
pure nothrow unittest |
| 370 |
{ |
| 371 |
auto s1 = [0, 1, 2]; |
| 372 |
auto s2 = s1.arraydup; |
| 373 |
s2[0] = 4; |
| 374 |
assert(s1 == [0, 1, 2]); |
| 375 |
assert(s2 == [4, 1, 2]); |
| 376 |
string sEmpty; |
| 377 |
assert(sEmpty.arraydup is null); |
| 378 |
} |