Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 164 lines (139 sloc) 2.964 kB
9840a79 @braddr dmd 0.82
braddr authored
1
f62f211 @WalterBright switch root to Boost license
WalterBright authored
2 /* Copyright (c) 2000-2014 by Digital Mars
3 * All Rights Reserved, written by Walter Bright
4 * http://www.digitalmars.com
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
7 * https://github.com/D-Programming-Language/dmd/blob/master/src/root/rmem.c
8 */
9840a79 @braddr dmd 0.82
braddr authored
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13
00337ef @braddr dmd 2.026
braddr authored
14 #include "rmem.h"
9840a79 @braddr dmd 0.82
braddr authored
15
16 /* This implementation of the storage allocator uses the standard C allocation package.
17 */
18
19 Mem mem;
20
973b7f8 @ibuclaw Prefix Mem methods with 'x' to differ from stdlib memory functions
ibuclaw authored
21 char *Mem::xstrdup(const char *s)
9840a79 @braddr dmd 0.82
braddr authored
22 {
23 char *p;
24
25 if (s)
26 {
973b7f8 @ibuclaw Prefix Mem methods with 'x' to differ from stdlib memory functions
ibuclaw authored
27 p = strdup(s);
3addcfb remove tabs, any trailing spaces
Walter Bright authored
28 if (p)
29 return p;
30 error();
9840a79 @braddr dmd 0.82
braddr authored
31 }
32 return NULL;
33 }
34
973b7f8 @ibuclaw Prefix Mem methods with 'x' to differ from stdlib memory functions
ibuclaw authored
35 void *Mem::xmalloc(size_t size)
9840a79 @braddr dmd 0.82
braddr authored
36 { void *p;
37
38 if (!size)
3addcfb remove tabs, any trailing spaces
Walter Bright authored
39 p = NULL;
9840a79 @braddr dmd 0.82
braddr authored
40 else
41 {
973b7f8 @ibuclaw Prefix Mem methods with 'x' to differ from stdlib memory functions
ibuclaw authored
42 p = malloc(size);
3addcfb remove tabs, any trailing spaces
Walter Bright authored
43 if (!p)
44 error();
9840a79 @braddr dmd 0.82
braddr authored
45 }
46 return p;
47 }
48
973b7f8 @ibuclaw Prefix Mem methods with 'x' to differ from stdlib memory functions
ibuclaw authored
49 void *Mem::xcalloc(size_t size, size_t n)
9840a79 @braddr dmd 0.82
braddr authored
50 { void *p;
51
52 if (!size || !n)
3addcfb remove tabs, any trailing spaces
Walter Bright authored
53 p = NULL;
9840a79 @braddr dmd 0.82
braddr authored
54 else
55 {
973b7f8 @ibuclaw Prefix Mem methods with 'x' to differ from stdlib memory functions
ibuclaw authored
56 p = calloc(size, n);
3addcfb remove tabs, any trailing spaces
Walter Bright authored
57 if (!p)
58 error();
9840a79 @braddr dmd 0.82
braddr authored
59 }
60 return p;
61 }
62
973b7f8 @ibuclaw Prefix Mem methods with 'x' to differ from stdlib memory functions
ibuclaw authored
63 void *Mem::xrealloc(void *p, size_t size)
9840a79 @braddr dmd 0.82
braddr authored
64 {
65 if (!size)
3addcfb remove tabs, any trailing spaces
Walter Bright authored
66 { if (p)
973b7f8 @ibuclaw Prefix Mem methods with 'x' to differ from stdlib memory functions
ibuclaw authored
67 {
68 free(p);
3addcfb remove tabs, any trailing spaces
Walter Bright authored
69 p = NULL;
70 }
9840a79 @braddr dmd 0.82
braddr authored
71 }
72 else if (!p)
73 {
973b7f8 @ibuclaw Prefix Mem methods with 'x' to differ from stdlib memory functions
ibuclaw authored
74 p = malloc(size);
3addcfb remove tabs, any trailing spaces
Walter Bright authored
75 if (!p)
76 error();
9840a79 @braddr dmd 0.82
braddr authored
77 }
78 else
79 {
8fcaeae @WalterBright various analyzer issues
WalterBright authored
80 void *psave = p;
973b7f8 @ibuclaw Prefix Mem methods with 'x' to differ from stdlib memory functions
ibuclaw authored
81 p = realloc(psave, size);
3addcfb remove tabs, any trailing spaces
Walter Bright authored
82 if (!p)
973b7f8 @ibuclaw Prefix Mem methods with 'x' to differ from stdlib memory functions
ibuclaw authored
83 { xfree(psave);
3addcfb remove tabs, any trailing spaces
Walter Bright authored
84 error();
8fcaeae @WalterBright various analyzer issues
WalterBright authored
85 }
9840a79 @braddr dmd 0.82
braddr authored
86 }
87 return p;
88 }
89
973b7f8 @ibuclaw Prefix Mem methods with 'x' to differ from stdlib memory functions
ibuclaw authored
90 void Mem::xfree(void *p)
9840a79 @braddr dmd 0.82
braddr authored
91 {
92 if (p)
973b7f8 @ibuclaw Prefix Mem methods with 'x' to differ from stdlib memory functions
ibuclaw authored
93 free(p);
9840a79 @braddr dmd 0.82
braddr authored
94 }
95
973b7f8 @ibuclaw Prefix Mem methods with 'x' to differ from stdlib memory functions
ibuclaw authored
96 void *Mem::xmallocdup(void *o, size_t size)
9840a79 @braddr dmd 0.82
braddr authored
97 { void *p;
98
99 if (!size)
3addcfb remove tabs, any trailing spaces
Walter Bright authored
100 p = NULL;
9840a79 @braddr dmd 0.82
braddr authored
101 else
102 {
973b7f8 @ibuclaw Prefix Mem methods with 'x' to differ from stdlib memory functions
ibuclaw authored
103 p = malloc(size);
3addcfb remove tabs, any trailing spaces
Walter Bright authored
104 if (!p)
105 error();
106 else
107 memcpy(p,o,size);
9840a79 @braddr dmd 0.82
braddr authored
108 }
109 return p;
110 }
111
112 void Mem::error()
113 {
114 printf("Error: out of memory\n");
115 exit(EXIT_FAILURE);
116 }
117
118 /* =================================================== */
119
2eeefa3 @WalterBright new, but never delete
WalterBright authored
120 /* Allocate, but never release
121 */
122
a1d2fef @WalterBright reboot hpohl's #2470
WalterBright authored
123 // Allocate a little less than 1Mb because the C runtime adds some overhead that
124 // causes the actual memory block to be larger than 1Mb otherwise.
125 #define CHUNK_SIZE (256 * 4096 - 64)
2eeefa3 @WalterBright new, but never delete
WalterBright authored
126
127 static size_t heapleft = 0;
128 static void *heapp;
129
8a4fb01 @yebblies Split C++ operator new declaration into a new file
yebblies authored
130 void *allocmemory(size_t m_size)
2eeefa3 @WalterBright new, but never delete
WalterBright authored
131 {
132 // 16 byte alignment is better (and sometimes needed) for doubles
133 m_size = (m_size + 15) & ~15;
134
135 // The layout of the code is selected so the most common case is straight through
136 if (m_size <= heapleft)
137 {
138 L1:
139 heapleft -= m_size;
140 void *p = heapp;
141 heapp = (void *)((char *)heapp + m_size);
142 return p;
143 }
144
145 if (m_size > CHUNK_SIZE)
146 {
147 void *p = malloc(m_size);
148 if (p)
149 return p;
150 printf("Error: out of memory\n");
151 exit(EXIT_FAILURE);
152 return p;
153 }
154
155 heapleft = CHUNK_SIZE;
156 heapp = malloc(CHUNK_SIZE);
157 if (!heapp)
158 {
159 printf("Error: out of memory\n");
160 exit(EXIT_FAILURE);
161 }
162 goto L1;
163 }
Something went wrong with that request. Please try again.