root / oslib-win32.c @ ebd669a1
History | View | Annotate | Download (3.1 kB)
1 | c1b0b93b | Jes Sorensen | /*
|
---|---|---|---|
2 | c1b0b93b | Jes Sorensen | * os-win32.c
|
3 | c1b0b93b | Jes Sorensen | *
|
4 | c1b0b93b | Jes Sorensen | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | c1b0b93b | Jes Sorensen | * Copyright (c) 2010 Red Hat, Inc.
|
6 | c1b0b93b | Jes Sorensen | *
|
7 | c1b0b93b | Jes Sorensen | * QEMU library functions for win32 which are shared between QEMU and
|
8 | c1b0b93b | Jes Sorensen | * the QEMU tools.
|
9 | c1b0b93b | Jes Sorensen | *
|
10 | c1b0b93b | Jes Sorensen | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
11 | c1b0b93b | Jes Sorensen | * of this software and associated documentation files (the "Software"), to deal
|
12 | c1b0b93b | Jes Sorensen | * in the Software without restriction, including without limitation the rights
|
13 | c1b0b93b | Jes Sorensen | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
14 | c1b0b93b | Jes Sorensen | * copies of the Software, and to permit persons to whom the Software is
|
15 | c1b0b93b | Jes Sorensen | * furnished to do so, subject to the following conditions:
|
16 | c1b0b93b | Jes Sorensen | *
|
17 | c1b0b93b | Jes Sorensen | * The above copyright notice and this permission notice shall be included in
|
18 | c1b0b93b | Jes Sorensen | * all copies or substantial portions of the Software.
|
19 | c1b0b93b | Jes Sorensen | *
|
20 | c1b0b93b | Jes Sorensen | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
21 | c1b0b93b | Jes Sorensen | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
22 | c1b0b93b | Jes Sorensen | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
23 | c1b0b93b | Jes Sorensen | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
24 | c1b0b93b | Jes Sorensen | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
25 | c1b0b93b | Jes Sorensen | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
26 | c1b0b93b | Jes Sorensen | * THE SOFTWARE.
|
27 | c1b0b93b | Jes Sorensen | */
|
28 | c1b0b93b | Jes Sorensen | #include <windows.h> |
29 | c1b0b93b | Jes Sorensen | #include "config-host.h" |
30 | c1b0b93b | Jes Sorensen | #include "sysemu.h" |
31 | c1b0b93b | Jes Sorensen | #include "trace.h" |
32 | 9549e764 | Jes Sorensen | #include "qemu_socket.h" |
33 | c1b0b93b | Jes Sorensen | |
34 | b152aa84 | Jes Sorensen | void *qemu_oom_check(void *ptr) |
35 | c1b0b93b | Jes Sorensen | { |
36 | c1b0b93b | Jes Sorensen | if (ptr == NULL) { |
37 | c1b0b93b | Jes Sorensen | fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
|
38 | c1b0b93b | Jes Sorensen | abort(); |
39 | c1b0b93b | Jes Sorensen | } |
40 | c1b0b93b | Jes Sorensen | return ptr;
|
41 | c1b0b93b | Jes Sorensen | } |
42 | c1b0b93b | Jes Sorensen | |
43 | c1b0b93b | Jes Sorensen | void *qemu_memalign(size_t alignment, size_t size)
|
44 | c1b0b93b | Jes Sorensen | { |
45 | c1b0b93b | Jes Sorensen | void *ptr;
|
46 | c1b0b93b | Jes Sorensen | |
47 | c1b0b93b | Jes Sorensen | if (!size) {
|
48 | c1b0b93b | Jes Sorensen | abort(); |
49 | c1b0b93b | Jes Sorensen | } |
50 | b152aa84 | Jes Sorensen | ptr = qemu_oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
|
51 | c1b0b93b | Jes Sorensen | trace_qemu_memalign(alignment, size, ptr); |
52 | c1b0b93b | Jes Sorensen | return ptr;
|
53 | c1b0b93b | Jes Sorensen | } |
54 | c1b0b93b | Jes Sorensen | |
55 | c1b0b93b | Jes Sorensen | void *qemu_vmalloc(size_t size)
|
56 | c1b0b93b | Jes Sorensen | { |
57 | c1b0b93b | Jes Sorensen | void *ptr;
|
58 | c1b0b93b | Jes Sorensen | |
59 | c1b0b93b | Jes Sorensen | /* FIXME: this is not exactly optimal solution since VirtualAlloc
|
60 | c1b0b93b | Jes Sorensen | has 64Kb granularity, but at least it guarantees us that the
|
61 | c1b0b93b | Jes Sorensen | memory is page aligned. */
|
62 | c1b0b93b | Jes Sorensen | if (!size) {
|
63 | c1b0b93b | Jes Sorensen | abort(); |
64 | c1b0b93b | Jes Sorensen | } |
65 | b152aa84 | Jes Sorensen | ptr = qemu_oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
|
66 | c1b0b93b | Jes Sorensen | trace_qemu_vmalloc(size, ptr); |
67 | c1b0b93b | Jes Sorensen | return ptr;
|
68 | c1b0b93b | Jes Sorensen | } |
69 | c1b0b93b | Jes Sorensen | |
70 | c1b0b93b | Jes Sorensen | void qemu_vfree(void *ptr) |
71 | c1b0b93b | Jes Sorensen | { |
72 | c1b0b93b | Jes Sorensen | trace_qemu_vfree(ptr); |
73 | c1b0b93b | Jes Sorensen | VirtualFree(ptr, 0, MEM_RELEASE);
|
74 | c1b0b93b | Jes Sorensen | } |
75 | 9549e764 | Jes Sorensen | |
76 | 9549e764 | Jes Sorensen | void socket_set_nonblock(int fd) |
77 | 9549e764 | Jes Sorensen | { |
78 | 9549e764 | Jes Sorensen | unsigned long opt = 1; |
79 | 9549e764 | Jes Sorensen | ioctlsocket(fd, FIONBIO, &opt); |
80 | 9549e764 | Jes Sorensen | } |
81 | 9549e764 | Jes Sorensen | |
82 | 9549e764 | Jes Sorensen | int inet_aton(const char *cp, struct in_addr *ia) |
83 | 9549e764 | Jes Sorensen | { |
84 | 9549e764 | Jes Sorensen | uint32_t addr = inet_addr(cp); |
85 | 9549e764 | Jes Sorensen | if (addr == 0xffffffff) { |
86 | 9549e764 | Jes Sorensen | return 0; |
87 | 9549e764 | Jes Sorensen | } |
88 | 9549e764 | Jes Sorensen | ia->s_addr = addr; |
89 | 9549e764 | Jes Sorensen | return 1; |
90 | 9549e764 | Jes Sorensen | } |
91 | 9549e764 | Jes Sorensen | |
92 | 9549e764 | Jes Sorensen | void qemu_set_cloexec(int fd) |
93 | 9549e764 | Jes Sorensen | { |
94 | 9549e764 | Jes Sorensen | } |
95 | dc786bc9 | Jes Sorensen | |
96 | dc786bc9 | Jes Sorensen | /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
|
97 | dc786bc9 | Jes Sorensen | #define _W32_FT_OFFSET (116444736000000000ULL) |
98 | dc786bc9 | Jes Sorensen | |
99 | dc786bc9 | Jes Sorensen | int qemu_gettimeofday(qemu_timeval *tp)
|
100 | dc786bc9 | Jes Sorensen | { |
101 | dc786bc9 | Jes Sorensen | union {
|
102 | dc786bc9 | Jes Sorensen | unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */ |
103 | dc786bc9 | Jes Sorensen | FILETIME ft; |
104 | dc786bc9 | Jes Sorensen | } _now; |
105 | dc786bc9 | Jes Sorensen | |
106 | dc786bc9 | Jes Sorensen | if(tp) {
|
107 | dc786bc9 | Jes Sorensen | GetSystemTimeAsFileTime (&_now.ft); |
108 | dc786bc9 | Jes Sorensen | tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL ); |
109 | dc786bc9 | Jes Sorensen | tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL); |
110 | dc786bc9 | Jes Sorensen | } |
111 | dc786bc9 | Jes Sorensen | /* Always return 0 as per Open Group Base Specifications Issue 6.
|
112 | dc786bc9 | Jes Sorensen | Do not set errno on error. */
|
113 | dc786bc9 | Jes Sorensen | return 0; |
114 | dc786bc9 | Jes Sorensen | } |