comparison lphobos/gc/win32.d @ 473:373489eeaf90

Applied downs' lphobos update
author Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
date Mon, 04 Aug 2008 19:28:49 +0200
parents
children
comparison
equal deleted inserted replaced
472:15c804b6ce77 473:373489eeaf90
1
2 // Copyright (C) 2001-2002 by Digital Mars
3 // All Rights Reserved
4 // www.digitalmars.com
5 // Written by Walter Bright
6
7 import std.c.windows.windows;
8
9 alias int pthread_t;
10
11 /***********************************
12 * Map memory.
13 */
14
15 void *os_mem_map(uint nbytes)
16 {
17 return VirtualAlloc(null, nbytes, MEM_RESERVE, PAGE_READWRITE);
18 }
19
20 /***********************************
21 * Commit memory.
22 * Returns:
23 * 0 success
24 * !=0 failure
25 */
26
27 int os_mem_commit(void *base, uint offset, uint nbytes)
28 {
29 void *p;
30
31 p = VirtualAlloc(base + offset, nbytes, MEM_COMMIT, PAGE_READWRITE);
32 return cast(int)(p == null);
33 }
34
35
36 /***********************************
37 * Decommit memory.
38 * Returns:
39 * 0 success
40 * !=0 failure
41 */
42
43 int os_mem_decommit(void *base, uint offset, uint nbytes)
44 {
45 return cast(int)VirtualFree(base + offset, nbytes, MEM_DECOMMIT) == 0;
46 }
47
48 /***********************************
49 * Unmap memory allocated with os_mem_map().
50 * Memory must have already been decommitted.
51 * Returns:
52 * 0 success
53 * !=0 failure
54 */
55
56 int os_mem_unmap(void *base, uint nbytes)
57 {
58 return cast(int)VirtualFree(base, 0, MEM_RELEASE) == 0;
59 }
60
61
62 /********************************************
63 */
64
65 pthread_t pthread_self()
66 {
67 //printf("pthread_self() = %x\n", GetCurrentThreadId());
68 return cast(pthread_t) GetCurrentThreadId();
69 }
70
71 /**********************************************
72 * Determine "bottom" of stack (actually the top on Win32 systems).
73 */
74
75 void *os_query_stackBottom()
76 {
77 asm
78 {
79 naked ;
80 mov EAX,FS:4 ;
81 ret ;
82 }
83 }
84
85 /**********************************************
86 * Determine base address and size of static data segment.
87 */
88
89 version (GNU)
90 {
91 // This is MinGW specific
92 extern (C)
93 {
94 // TODO: skip the .rdata between .data and .bss?
95 extern int _data_start__;
96 extern int _bss_end__;
97 }
98
99 void os_query_staticdataseg(void **base, uint *nbytes)
100 {
101 *base = cast(void *)&_data_start__;
102 *nbytes = cast(uint)(cast(char *)&_bss_end__ - cast(char *)&_data_start__);
103 }
104
105 }
106 else
107 {
108
109 extern (C)
110 {
111 extern int _xi_a; // &_xi_a just happens to be start of data segment
112 extern int _edata; // &_edata is start of BSS segment
113 extern int _end; // &_end is past end of BSS
114 }
115
116 void os_query_staticdataseg(void **base, uint *nbytes)
117 {
118 *base = cast(void *)&_xi_a;
119 *nbytes = cast(uint)(cast(char *)&_end - cast(char *)&_xi_a);
120 }
121
122 }
123 /++++
124
125 void os_query_staticdataseg(void **base, uint *nbytes)
126 {
127 static char dummy = 6;
128 SYSTEM_INFO si;
129 MEMORY_BASIC_INFORMATION mbi;
130 char *p;
131 void *bottom = null;
132 uint size = 0;
133
134 // Tests show the following does not work reliably.
135 // The reason is that the data segment is arbitrarily divided
136 // up into PAGE_READWRITE and PAGE_WRITECOPY.
137 // This means there are multiple regions to query, and
138 // can even wind up including the code segment.
139 assert(0); // fix implementation
140
141 GetSystemInfo(&si);
142 p = (char *)((uint)(&dummy) & ~(si.dwPageSize - 1));
143 while (VirtualQuery(p, &mbi, sizeof(mbi)) == sizeof(mbi) &&
144 mbi.Protect & (PAGE_READWRITE | PAGE_WRITECOPY) &&
145 !(mbi.Protect & PAGE_GUARD) &&
146 mbi.AllocationBase != 0)
147 {
148 bottom = (void *)mbi.BaseAddress;
149 size = (uint)mbi.RegionSize;
150
151 printf("dwPageSize = x%x\n", si.dwPageSize);
152 printf("&dummy = %p\n", &dummy);
153 printf("BaseAddress = %p\n", mbi.BaseAddress);
154 printf("AllocationBase = %p\n", mbi.AllocationBase);
155 printf("AllocationProtect = x%x\n", mbi.AllocationProtect);
156 printf("RegionSize = x%x\n", mbi.RegionSize);
157 printf("State = x%x\n", mbi.State);
158 printf("Protect = x%x\n", mbi.Protect);
159 printf("Type = x%x\n\n", mbi.Type);
160
161 p -= si.dwPageSize;
162 }
163
164 *base = bottom;
165 *nbytes = size;
166 }
167
168 ++++/