2 * winmisc.c: miscellaneous Windows-specific things.
10 void platform_get_x11_auth(char *display
, int *proto
,
11 unsigned char *data
, int *datalen
)
13 /* We don't support this at all under Windows. */
16 Filename
filename_from_str(const char *str
)
19 strncpy(ret
.path
, str
, sizeof(ret
.path
));
20 ret
.path
[sizeof(ret
.path
)-1] = '\0';
24 const char *filename_to_str(const Filename
*fn
)
29 int filename_equal(Filename f1
, Filename f2
)
31 return !strcmp(f1
.path
, f2
.path
);
34 int filename_is_null(Filename fn
)
40 static FILE *debug_fp
= NULL
;
41 static HANDLE debug_hdl
= INVALID_HANDLE_VALUE
;
42 static int debug_got_console
= 0;
48 if (!debug_got_console
) {
50 debug_got_console
= 1;
51 debug_hdl
= GetStdHandle(STD_OUTPUT_HANDLE
);
55 debug_fp
= fopen("debug.log", "w");
58 if (debug_hdl
!= INVALID_HANDLE_VALUE
) {
59 WriteFile(debug_hdl
, buf
, strlen(buf
), &dw
, NULL
);
68 * Minefield - a Windows equivalent for Electric Fence
76 * We start by reserving as much virtual address space as Windows
77 * will sensibly (or not sensibly) let us have. We flag it all as
80 * Any allocation attempt is satisfied by committing one or more
81 * pages, with an uncommitted page on either side. The returned
82 * memory region is jammed up against the _end_ of the pages.
84 * Freeing anything causes instantaneous decommitment of the pages
85 * involved, so stale pointers are caught as soon as possible.
88 static int minefield_initialised
= 0;
89 static void *minefield_region
= NULL
;
90 static long minefield_size
= 0;
91 static long minefield_npages
= 0;
92 static long minefield_curpos
= 0;
93 static unsigned short *minefield_admin
= NULL
;
94 static void *minefield_pages
= NULL
;
96 static void minefield_admin_hide(int hide
)
98 int access
= hide ? PAGE_NOACCESS
: PAGE_READWRITE
;
99 VirtualProtect(minefield_admin
, minefield_npages
* 2, access
, NULL
);
102 static void minefield_init(void)
108 for (size
= 0x40000000; size
> 0; size
= ((size
>> 3) * 7) & ~0xFFF) {
109 minefield_region
= VirtualAlloc(NULL
, size
,
110 MEM_RESERVE
, PAGE_NOACCESS
);
111 if (minefield_region
)
114 minefield_size
= size
;
117 * Firstly, allocate a section of that to be the admin block.
118 * We'll need a two-byte field for each page.
120 minefield_admin
= minefield_region
;
121 minefield_npages
= minefield_size
/ PAGESIZE
;
122 admin_size
= (minefield_npages
* 2 + PAGESIZE
- 1) & ~(PAGESIZE
- 1);
123 minefield_npages
= (minefield_size
- admin_size
) / PAGESIZE
;
124 minefield_pages
= (char *) minefield_region
+ admin_size
;
127 * Commit the admin region.
129 VirtualAlloc(minefield_admin
, minefield_npages
* 2,
130 MEM_COMMIT
, PAGE_READWRITE
);
133 * Mark all pages as unused (0xFFFF).
135 for (i
= 0; i
< minefield_npages
; i
++)
136 minefield_admin
[i
] = 0xFFFF;
139 * Hide the admin region.
141 minefield_admin_hide(1);
143 minefield_initialised
= 1;
146 static void minefield_bomb(void)
148 div(1, *(int *) minefield_pages
);
151 static void *minefield_alloc(int size
)
154 int pos
, lim
, region_end
, region_start
;
158 npages
= (size
+ PAGESIZE
- 1) / PAGESIZE
;
160 minefield_admin_hide(0);
163 * Search from current position until we find a contiguous
164 * bunch of npages+2 unused pages.
166 pos
= minefield_curpos
;
167 lim
= minefield_npages
;
169 /* Skip over used pages. */
170 while (pos
< lim
&& minefield_admin
[pos
] != 0xFFFF)
172 /* Count unused pages. */
174 while (pos
< lim
&& pos
- start
< npages
+ 2 &&
175 minefield_admin
[pos
] == 0xFFFF)
177 if (pos
- start
== npages
+ 2)
179 /* If we've reached the limit, reset the limit or stop. */
181 if (lim
== minefield_npages
) {
182 /* go round and start again at zero */
183 lim
= minefield_curpos
;
186 minefield_admin_hide(1);
192 minefield_curpos
= pos
- 1;
195 * We have npages+2 unused pages starting at start. We leave
196 * the first and last of these alone and use the rest.
198 region_end
= (start
+ npages
+ 1) * PAGESIZE
;
199 region_start
= region_end
- size
;
200 /* FIXME: could align here if we wanted */
203 * Update the admin region.
205 for (i
= start
+ 2; i
< start
+ npages
+ 1; i
++)
206 minefield_admin
[i
] = 0xFFFE; /* used but no region starts here */
207 minefield_admin
[start
+ 1] = region_start
% PAGESIZE
;
209 minefield_admin_hide(1);
211 VirtualAlloc((char *) minefield_pages
+ region_start
, size
,
212 MEM_COMMIT
, PAGE_READWRITE
);
213 return (char *) minefield_pages
+ region_start
;
216 static void minefield_free(void *ptr
)
218 int region_start
, i
, j
;
220 minefield_admin_hide(0);
222 region_start
= (char *) ptr
- (char *) minefield_pages
;
223 i
= region_start
/ PAGESIZE
;
224 if (i
< 0 || i
>= minefield_npages
||
225 minefield_admin
[i
] != region_start
% PAGESIZE
)
227 for (j
= i
; j
< minefield_npages
&& minefield_admin
[j
] != 0xFFFF; j
++) {
228 minefield_admin
[j
] = 0xFFFF;
231 VirtualFree(ptr
, j
* PAGESIZE
- region_start
, MEM_DECOMMIT
);
233 minefield_admin_hide(1);
236 static int minefield_get_size(void *ptr
)
238 int region_start
, i
, j
;
240 minefield_admin_hide(0);
242 region_start
= (char *) ptr
- (char *) minefield_pages
;
243 i
= region_start
/ PAGESIZE
;
244 if (i
< 0 || i
>= minefield_npages
||
245 minefield_admin
[i
] != region_start
% PAGESIZE
)
247 for (j
= i
; j
< minefield_npages
&& minefield_admin
[j
] != 0xFFFF; j
++);
249 minefield_admin_hide(1);
251 return j
* PAGESIZE
- region_start
;
254 void *minefield_c_malloc(size_t size
)
256 if (!minefield_initialised
)
258 return minefield_alloc(size
);
261 void minefield_c_free(void *p
)
263 if (!minefield_initialised
)
269 * realloc _always_ moves the chunk, for rapid detection of code
270 * that assumes it won't.
272 void *minefield_c_realloc(void *p
, size_t size
)
276 if (!minefield_initialised
)
278 q
= minefield_alloc(size
);
279 oldsize
= minefield_get_size(p
);
280 memcpy(q
, p
, (oldsize
< size ? oldsize
: size
));
285 #endif /* MINEFIELD */