Skip to content

Commit 5242beb

Browse files
dkorpeldlang-bot
authored andcommitted
add return scope to realloc
1 parent 4d6254f commit 5242beb

3 files changed

Lines changed: 36 additions & 30 deletions

File tree

std/internal/cstring.d

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ if (isSomeChar!To && (isInputRange!From || isSomeString!From) &&
145145
{
146146
if (i + 1 == p.length)
147147
{
148-
heapBuffer = trustedRealloc(p, strLength, heapBuffer is null);
148+
if (heapBuffer is null)
149+
heapBuffer = trustedReallocStack(p, strLength);
150+
else
151+
heapBuffer = trustedRealloc(heapBuffer);
149152
p = heapBuffer;
150153
}
151154
p[i++] = c;
@@ -269,38 +272,41 @@ private:
269272
static TempCStringBuffer trustedVoidInit() { TempCStringBuffer res = void; return res; }
270273
}
271274

272-
private To[] trustedRealloc(To)(scope To[] buf, size_t strLength, bool bufIsOnStack)
275+
private To[] trustedRealloc(To)(return scope To[] buf)
273276
@trusted @nogc pure nothrow
274277
{
275278
pragma(inline, false); // because it's rarely called
279+
import std.internal.memory : enforceRealloc;
276280

277-
import std.internal.memory : enforceMalloc, enforceRealloc;
278-
279-
size_t newlen = buf.length * 3 / 2;
280-
281-
if (bufIsOnStack)
282-
{
283-
if (newlen <= strLength)
284-
newlen = strLength + 1; // +1 for terminating 0
285-
auto ptr = cast(To*) enforceMalloc(newlen * To.sizeof);
286-
ptr[0 .. buf.length] = buf[];
287-
return ptr[0 .. newlen];
288-
}
289-
else
281+
const size_t newlen = buf.length * 3 / 2;
282+
if (buf.length >= size_t.max / (2 * To.sizeof))
290283
{
291-
if (buf.length >= size_t.max / (2 * To.sizeof))
284+
version (D_Exceptions)
292285
{
293-
version (D_Exceptions)
294-
{
295-
import core.exception : onOutOfMemoryError;
296-
onOutOfMemoryError();
297-
}
298-
else
299-
{
300-
assert(0, "Memory allocation failed");
301-
}
286+
import core.exception : onOutOfMemoryError;
287+
onOutOfMemoryError();
288+
}
289+
else
290+
{
291+
assert(0, "Memory allocation failed");
302292
}
303-
auto ptr = cast(To*) enforceRealloc(buf.ptr, newlen * To.sizeof);
304-
return ptr[0 .. newlen];
305293
}
294+
auto ptr = cast(To*) enforceRealloc(buf.ptr, newlen * To.sizeof);
295+
return ptr[0 .. newlen];
296+
297+
}
298+
299+
private To[] trustedReallocStack(To)(scope To[] buf, size_t strLength)
300+
@trusted @nogc pure nothrow
301+
{
302+
pragma(inline, false); // because it's rarely called
303+
304+
import std.internal.memory : enforceMalloc;
305+
306+
size_t newlen = buf.length * 3 / 2;
307+
if (newlen <= strLength)
308+
newlen = strLength + 1; // +1 for terminating 0
309+
auto ptr = cast(To*) enforceMalloc(newlen * To.sizeof);
310+
ptr[0 .. buf.length] = buf[];
311+
return ptr[0 .. newlen];
306312
}

std/internal/memory.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ void* enforceCalloc()(size_t nmemb, size_t size) @nogc nothrow pure @safe
4242
}
4343

4444
// ditto
45-
void* enforceRealloc()(void* ptr, size_t size) @nogc nothrow pure @system
45+
void* enforceRealloc()(return scope void* ptr, size_t size) @nogc nothrow pure @system
4646
{
4747
auto result = fakePureRealloc(ptr, size);
4848
if (!result) mixin(allocationFailed);
@@ -54,5 +54,5 @@ extern (C) @nogc nothrow pure private
5454
{
5555
pragma(mangle, "malloc") void* fakePureMalloc(size_t) @safe;
5656
pragma(mangle, "calloc") void* fakePureCalloc(size_t nmemb, size_t size) @safe;
57-
pragma(mangle, "realloc") void* fakePureRealloc(void* ptr, size_t size) @system;
57+
pragma(mangle, "realloc") void* fakePureRealloc(return scope void* ptr, size_t size) @system;
5858
}

std/uni/package.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1819,7 +1819,7 @@ alias sharSwitchLowerBound = sharMethod!switchUniformLowerBound;
18191819
return ptr[0 .. size];
18201820
}
18211821

1822-
static T[] realloc(T)(scope T[] arr, size_t size) @trusted
1822+
static T[] realloc(T)(return scope T[] arr, size_t size) @trusted
18231823
{
18241824
import std.internal.memory : enforceRealloc;
18251825
if (!size)

0 commit comments

Comments
 (0)