@@ -32,24 +32,15 @@ extern (C++) struct Mem
32
32
{
33
33
static char * xstrdup (const (char )* s) nothrow
34
34
{
35
- if (! s)
36
- return null ;
37
-
38
35
version (GC)
39
36
if (isGCEnabled)
40
- return s[0 .. strlen(s) + 1 ].dup .ptr;
37
+ return s ? s [0 .. strlen(s) + 1 ].dup .ptr : null ;
41
38
42
- auto p = .strdup(s);
43
- if (! p)
44
- error();
45
- return p;
39
+ return s ? cast (char * )check(.strdup(s)) : null ;
46
40
}
47
41
48
42
static void xfree (void * p) pure nothrow
49
43
{
50
- if (! p)
51
- return ;
52
-
53
44
version (GC)
54
45
if (isGCEnabled)
55
46
return GC .free(p);
@@ -59,33 +50,20 @@ extern (C++) struct Mem
59
50
60
51
static void * xmalloc (size_t size) pure nothrow
61
52
{
62
- if (! size)
63
- return null ;
64
-
65
53
version (GC)
66
54
if (isGCEnabled)
67
- return GC .malloc(size);
55
+ return size ? GC .malloc(size) : null ;
68
56
69
- auto p = pureMalloc(size);
70
- if (! p)
71
- error();
72
- return p;
57
+ return size ? check(pureMalloc(size)) : null ;
73
58
}
74
59
75
60
static void * xcalloc (size_t size, size_t n) pure nothrow
76
61
{
77
- const totalSize = size * n;
78
- if (! totalSize)
79
- return null ;
80
-
81
62
version (GC)
82
63
if (isGCEnabled)
83
- return GC .calloc(totalSize) ;
64
+ return size * n ? GC .calloc(size * n) : null ;
84
65
85
- auto p = pureCalloc(size, n);
86
- if (! p)
87
- error();
88
- return p;
66
+ return (size && n) ? check(pureCalloc(size, n)) : null ;
89
67
}
90
68
91
69
static void * xrealloc (void * p, size_t size) pure nothrow
@@ -96,28 +74,30 @@ extern (C++) struct Mem
96
74
97
75
if (! size)
98
76
{
99
- if (p)
100
- pureFree(p);
77
+ pureFree(p);
101
78
return null ;
102
79
}
103
80
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));
116
82
}
117
83
118
- static void error () pure nothrow @nogc
84
+ static void * error () pure nothrow @nogc
119
85
{
120
86
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();
121
101
}
122
102
123
103
version (GC)
@@ -174,22 +154,11 @@ extern (C) void* allocmemory(size_t m_size) nothrow @nogc
174
154
175
155
if (m_size > CHUNK_SIZE )
176
156
{
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));
184
158
}
185
159
186
160
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 ));
193
162
goto L1 ;
194
163
}
195
164
0 commit comments