]>
git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/d/dmd/root/rmem.c
2 /* Copyright (C) 2000-2020 by The D Language Foundation, All Rights Reserved
3 * http://www.digitalmars.com
4 * Distributed under the Boost Software License, Version 1.0.
5 * (See accompanying file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
6 * https://github.com/D-Programming-Language/dmd/blob/master/src/root/rmem.c
12 /* This implementation of the storage allocator uses the standard C allocation package.
17 char *Mem::xstrdup(const char *s
)
35 void *Mem::xmalloc(size_t size
)
53 void *Mem::xcalloc(size_t size
, size_t n
)
61 p
= ::xcalloc(size
, n
);
71 void *Mem::xrealloc(void *p
, size_t size
)
94 p
= ::xrealloc(psave
, size
);
96 p
= realloc(psave
, size
);
106 void Mem::xfree(void *p
)
112 void *Mem::xmallocdup(void *o
, size_t size
)
134 printf("Error: out of memory\n");
138 /* =================================================== */
140 /* Allocate, but never release
143 // Allocate a little less than 1Mb because the C runtime adds some overhead that
144 // causes the actual memory block to be larger than 1Mb otherwise.
145 #define CHUNK_SIZE (256 * 4096 - 64)
147 static size_t heapleft
= 0;
150 extern "C" void *allocmemory(size_t m_size
)
152 // 16 byte alignment is better (and sometimes needed) for doubles
153 m_size
= (m_size
+ 15) & ~15;
155 // The layout of the code is selected so the most common case is straight through
156 if (m_size
<= heapleft
)
161 heapp
= (void *)((char *)heapp
+ m_size
);
165 if (m_size
> CHUNK_SIZE
)
168 void *p
= xmalloc(m_size
);
170 void *p
= malloc(m_size
);
174 printf("Error: out of memory\n");
179 heapleft
= CHUNK_SIZE
;
181 heapp
= xmalloc(CHUNK_SIZE
);
183 heapp
= malloc(CHUNK_SIZE
);
187 printf("Error: out of memory\n");