This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
/
memory.d
273 lines (257 loc) · 5.76 KB
/
memory.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* This module exposes functionality for inspecting and manipulating memory.
*
* Copyright: Copyright Digital Mars 2000 - 2010.
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
* Authors: Walter Bright, Sean Kelly
*/
/* Copyright Digital Mars 2000 - 2010.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
module rt.memory;
private
{
version( GNU )
{
import gcc.builtins;
}
version( linux )
{
version = SimpleLibcStackEnd;
version( SimpleLibcStackEnd )
{
extern (C) extern __gshared void* __libc_stack_end;
}
}
version( FreeBSD )
{
extern (C) int sysctlbyname( const(char)*, void*, size_t*, void*, size_t );
}
version( Solaris )
{
version = SimpleLibcStackEnd;
version( SimpleLibcStackEnd )
{
extern (C) extern __gshared void* __libc_stack_end;
}
}
version( OSX )
{
import core.sys.osx.pthread;
}
extern (C) void gc_addRange( void* p, size_t sz );
extern (C) void gc_removeRange( void* p );
}
/**
*
*/
extern (C) void* rt_stackBottom()
{
version( Windows )
{
version( D_InlineAsm_X86 )
{
asm
{
naked;
mov EAX,FS:4;
ret;
}
}
else version( D_InlineAsm_X86_64 )
{
static assert( false, "is this right?" );
asm
{
naked;
mov RAX,FS:8;
ret;
}
}
else
{
static assert( false, "Platform not supported." );
}
}
else version( linux )
{
version( SimpleLibcStackEnd )
{
return __libc_stack_end;
}
else
{
// See discussion: http://autopackage.org/forums/viewtopic.php?t=22
static void** libc_stack_end;
if( libc_stack_end == libc_stack_end.init )
{
void* handle = dlopen( null, RTLD_NOW );
libc_stack_end = cast(void**) dlsym( handle, "__libc_stack_end" );
dlclose( handle );
}
return *libc_stack_end;
}
}
else version( OSX )
{
return pthread_get_stackaddr_np(pthread_self());
}
else version( FreeBSD )
{
static void* kern_usrstack;
if( kern_usrstack == kern_usrstack.init )
{
size_t len = kern_usrstack.sizeof;
sysctlbyname( "kern.usrstack", &kern_usrstack, &len, null, 0 );
}
return kern_usrstack;
}
else version( Solaris )
{
return __libc_stack_end;
}
else
{
static assert( false, "Operating system not supported." );
}
}
/**
*
*/
extern (C) void* rt_stackTop()
{
version( D_InlineAsm_X86 )
{
asm
{
naked;
mov EAX, ESP;
ret;
}
}
else version( D_InlineAsm_X86_64 )
{
asm
{
naked;
mov RAX, RSP;
ret;
}
}
else version( GNU )
{
return __builtin_frame_address(0);
}
else
{
static assert( false, "Architecture not supported." );
}
}
private
{
version( Windows )
{
extern (C)
{
extern __gshared
{
int _xi_a; // &_xi_a just happens to be start of data segment
int _edata; // &_edata is start of BSS segment
int _end; // &_end is past end of BSS
}
}
}
else version( linux )
{
extern (C)
{
extern __gshared
{
int _data;
int __data_start;
int _end;
int _data_start__;
int _data_end__;
int _bss_start__;
int _bss_end__;
int __fini_array_end;
}
}
alias __data_start Data_Start;
alias _end Data_End;
}
else version( OSX )
{
extern (C) void _d_osx_image_init();
}
else version( FreeBSD )
{
extern (C)
{
extern __gshared
{
size_t etext;
size_t _end;
}
}
version (X86_64)
{
extern (C)
{
extern __gshared
{
size_t _deh_end;
size_t __progname;
}
}
}
}
else version( Solaris )
{
extern (C)
{
extern __gshared
{
int etext;
int _end;
}
}
}
}
void initStaticDataGC()
{
version( Windows )
{
gc_addRange( &_xi_a, cast(size_t) &_end - cast(size_t) &_xi_a );
}
else version( linux )
{
gc_addRange( &__data_start, cast(size_t) &_end - cast(size_t) &__data_start );
}
else version( OSX )
{
_d_osx_image_init();
}
else version( FreeBSD )
{
version (X86_64)
{
gc_addRange( &etext, cast(size_t) &_deh_end - cast(size_t) &etext );
gc_addRange( &__progname, cast(size_t) &_end - cast(size_t) &__progname );
}
else
{
gc_addRange( &etext, cast(size_t) &_end - cast(size_t) &etext );
}
}
else version( Solaris )
{
gc_addRange( &etext, cast(size_t) &_end - cast(size_t) &etext );
}
else
{
static assert( false, "Operating system not supported." );
}
}