Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit feb9616

Browse files
authored
Merge pull request #2364 from n8sh/issue-19421
Fix Issue 19421: Make pureMalloc, etc. usable in BetterC merged-on-behalf-of: Nathan Sashihara <n8sh@users.noreply.github.com>
2 parents 586a0db + 0e8fe45 commit feb9616

File tree

3 files changed

+57
-15
lines changed

3 files changed

+57
-15
lines changed

src/core/memory.d

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -822,35 +822,35 @@ struct GC
822822
* $(LINK2 https://dlang.org/spec/function.html#pure-functions, D's rules for purity),
823823
* which allow for memory allocation under specific circumstances.
824824
*/
825-
void* pureMalloc(size_t size) @trusted pure @nogc nothrow
825+
void* pureMalloc()(size_t size) @trusted pure @nogc nothrow
826826
{
827-
const errnosave = fakePureErrno();
827+
const errnosave = fakePureErrno;
828828
void* ret = fakePureMalloc(size);
829-
fakePureErrno() = errnosave;
829+
fakePureErrno = errnosave;
830830
return ret;
831831
}
832832
/// ditto
833-
void* pureCalloc(size_t nmemb, size_t size) @trusted pure @nogc nothrow
833+
void* pureCalloc()(size_t nmemb, size_t size) @trusted pure @nogc nothrow
834834
{
835-
const errnosave = fakePureErrno();
835+
const errnosave = fakePureErrno;
836836
void* ret = fakePureCalloc(nmemb, size);
837-
fakePureErrno() = errnosave;
837+
fakePureErrno = errnosave;
838838
return ret;
839839
}
840840
/// ditto
841-
void* pureRealloc(void* ptr, size_t size) @system pure @nogc nothrow
841+
void* pureRealloc()(void* ptr, size_t size) @system pure @nogc nothrow
842842
{
843-
const errnosave = fakePureErrno();
843+
const errnosave = fakePureErrno;
844844
void* ret = fakePureRealloc(ptr, size);
845-
fakePureErrno() = errnosave;
845+
fakePureErrno = errnosave;
846846
return ret;
847847
}
848848
/// ditto
849-
void pureFree(void* ptr) @system pure @nogc nothrow
849+
void pureFree()(void* ptr) @system pure @nogc nothrow
850850
{
851-
const errnosave = fakePureErrno();
851+
const errnosave = fakePureErrno;
852852
fakePureFree(ptr);
853-
fakePureErrno() = errnosave;
853+
fakePureErrno = errnosave;
854854
}
855855

856856
///
@@ -900,6 +900,37 @@ void pureFree(void* ptr) @system pure @nogc nothrow
900900

901901
// locally purified for internal use here only
902902

903+
static import core.stdc.errno;
904+
static if (__traits(getOverloads, core.stdc.errno, "errno").length == 1
905+
&& __traits(getLinkage, core.stdc.errno.errno) == "C")
906+
{
907+
extern(C) pragma(mangle, __traits(identifier, core.stdc.errno.errno))
908+
private ref int fakePureErrno() @nogc nothrow pure @system;
909+
}
910+
else
911+
{
912+
extern(C) private @nogc nothrow pure @system
913+
{
914+
pragma(mangle, __traits(identifier, core.stdc.errno.getErrno))
915+
private int fakePureGetErrno();
916+
917+
pragma(mangle, __traits(identifier, core.stdc.errno.setErrno))
918+
private int fakePureSetErrno(int);
919+
}
920+
921+
private @property int fakePureErrno()() @nogc nothrow pure @system
922+
{
923+
return fakePureGetErrno();
924+
}
925+
926+
private @property void fakePureErrno()(int newValue) @nogc nothrow pure @system
927+
{
928+
fakePureSetErrno(newValue);
929+
}
930+
}
931+
932+
version (D_BetterC) {}
933+
else // TODO: remove this function after Phobos no longer needs it.
903934
extern (C) private @system @nogc nothrow
904935
{
905936
ref int fakePureErrnoImpl()
@@ -911,8 +942,6 @@ extern (C) private @system @nogc nothrow
911942

912943
extern (C) private pure @system @nogc nothrow
913944
{
914-
pragma(mangle, "fakePureErrnoImpl") ref int fakePureErrno();
915-
916945
pragma(mangle, "malloc") void* fakePureMalloc(size_t);
917946
pragma(mangle, "calloc") void* fakePureCalloc(size_t nmemb, size_t size);
918947
pragma(mangle, "realloc") void* fakePureRealloc(void* ptr, size_t size);

test/betterc/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
include ../common.mak
22

3-
TESTS:=test18828 test19416
3+
TESTS:=test18828 test19416 test19421
44

55
.PHONY: all clean
66
all: $(addprefix $(ROOT)/,$(addsuffix ,$(TESTS)))

test/betterc/src/test19421.d

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*******************************************/
2+
// https://issues.dlang.org/show_bug.cgi?id=19421
3+
4+
import core.memory;
5+
6+
extern(C) void main() @nogc nothrow pure
7+
{
8+
auto p = pureMalloc(1);
9+
p = pureRealloc(p, 2);
10+
if (p) pureFree(p);
11+
p = pureCalloc(1, 1);
12+
if (p) pureFree(p);
13+
}

0 commit comments

Comments
 (0)