Skip to content

Commit 1738141

Browse files
authored
Merge pull request #10262 from WalterBright/Mem.check
add Mem.check() merged-on-behalf-of: Walter Bright <[email protected]>
2 parents 47d34a4 + 9fd0302 commit 1738141

File tree

1 file changed

+25
-56
lines changed

1 file changed

+25
-56
lines changed

src/dmd/root/rmem.d

Lines changed: 25 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,15 @@ extern (C++) struct Mem
3232
{
3333
static char* xstrdup(const(char)* s) nothrow
3434
{
35-
if (!s)
36-
return null;
37-
3835
version (GC)
3936
if (isGCEnabled)
40-
return s[0 .. strlen(s) + 1].dup.ptr;
37+
return s ? s[0 .. strlen(s) + 1].dup.ptr : null;
4138

42-
auto p = .strdup(s);
43-
if (!p)
44-
error();
45-
return p;
39+
return s ? cast(char*)check(.strdup(s)) : null;
4640
}
4741

4842
static void xfree(void* p) pure nothrow
4943
{
50-
if (!p)
51-
return;
52-
5344
version (GC)
5445
if (isGCEnabled)
5546
return GC.free(p);
@@ -59,33 +50,20 @@ extern (C++) struct Mem
5950

6051
static void* xmalloc(size_t size) pure nothrow
6152
{
62-
if (!size)
63-
return null;
64-
6553
version (GC)
6654
if (isGCEnabled)
67-
return GC.malloc(size);
55+
return size ? GC.malloc(size) : null;
6856

69-
auto p = pureMalloc(size);
70-
if (!p)
71-
error();
72-
return p;
57+
return size ? check(pureMalloc(size)) : null;
7358
}
7459

7560
static void* xcalloc(size_t size, size_t n) pure nothrow
7661
{
77-
const totalSize = size * n;
78-
if (!totalSize)
79-
return null;
80-
8162
version (GC)
8263
if (isGCEnabled)
83-
return GC.calloc(totalSize);
64+
return size * n ? GC.calloc(size * n) : null;
8465

85-
auto p = pureCalloc(size, n);
86-
if (!p)
87-
error();
88-
return p;
66+
return (size && n) ? check(pureCalloc(size, n)) : null;
8967
}
9068

9169
static void* xrealloc(void* p, size_t size) pure nothrow
@@ -96,28 +74,30 @@ extern (C++) struct Mem
9674

9775
if (!size)
9876
{
99-
if (p)
100-
pureFree(p);
77+
pureFree(p);
10178
return null;
10279
}
10380

104-
if (!p)
105-
{
106-
p = pureMalloc(size);
107-
if (!p)
108-
error();
109-
return p;
110-
}
111-
112-
p = pureRealloc(p, size);
113-
if (!p)
114-
error();
115-
return p;
81+
return check(pureRealloc(p, size));
11682
}
11783

118-
static void error() pure nothrow @nogc
84+
static void* error() pure nothrow @nogc
11985
{
12086
onOutOfMemoryError();
87+
assert(0);
88+
}
89+
90+
/**
91+
* Check p for null. If it is, issue out of memory error
92+
* and exit program.
93+
* Params:
94+
* p = pointer to check for null
95+
* Returns:
96+
* p if not null
97+
*/
98+
static void* check(void* p) pure nothrow @nogc
99+
{
100+
return p ? p : error();
121101
}
122102

123103
version (GC)
@@ -174,22 +154,11 @@ extern (C) void* allocmemory(size_t m_size) nothrow @nogc
174154

175155
if (m_size > CHUNK_SIZE)
176156
{
177-
auto p = malloc(m_size);
178-
if (p)
179-
{
180-
return p;
181-
}
182-
printf("Error: out of memory\n");
183-
exit(EXIT_FAILURE);
157+
return Mem.check(malloc(m_size));
184158
}
185159

186160
heapleft = CHUNK_SIZE;
187-
heapp = malloc(CHUNK_SIZE);
188-
if (!heapp)
189-
{
190-
printf("Error: out of memory\n");
191-
exit(EXIT_FAILURE);
192-
}
161+
heapp = Mem.check(malloc(CHUNK_SIZE));
193162
goto L1;
194163
}
195164

0 commit comments

Comments
 (0)