Revision c1b0b93b

b/Makefile
129 129
qemu-img.o: qemu-img-cmds.h
130 130
qemu-img.o qemu-tool.o qemu-nbd.o qemu-io.o: $(GENERATED_HEADERS)
131 131

  
132
qemu-img$(EXESUF): qemu-img.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o
132
qemu-img$(EXESUF): qemu-img.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o
133 133

  
134
qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o
134
qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o
135 135

  
136
qemu-io$(EXESUF): qemu-io.o cmd.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o
136
qemu-io$(EXESUF): qemu-io.o cmd.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y) qemu-timer-common.o
137 137

  
138 138
qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
139 139
	$(call quiet-command,sh $(SRC_PATH)/hxtool -h < $< > $@,"  GEN   $@")
b/Makefile.objs
5 5
qobject-obj-y += qerror.o
6 6

  
7 7
#######################################################################
8
# oslib-obj-y is code depending on the OS (win32 vs posix)
9
oslib-obj-y = osdep.o
10
oslib-obj-$(CONFIG_WIN32) += oslib-win32.o
11
oslib-obj-$(CONFIG_POSIX) += oslib-posix.o
12

  
13
#######################################################################
8 14
# block-obj-y is code used by both qemu system emulation and qemu-img
9 15

  
10 16
block-obj-y = cutils.o cache-utils.o qemu-malloc.o qemu-option.o module.o
11
block-obj-y += nbd.o block.o aio.o aes.o osdep.o qemu-config.o
17
block-obj-y += nbd.o block.o aio.o aes.o qemu-config.o
12 18
block-obj-$(CONFIG_POSIX) += posix-aio-compat.o
13 19
block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
14 20

  
......
50 56
common-obj-y += $(qobject-obj-y)
51 57
common-obj-$(CONFIG_LINUX) += $(fsdev-obj-$(CONFIG_LINUX))
52 58
common-obj-y += readline.o console.o cursor.o async.o qemu-error.o
59
common-obj-y += $(oslib-obj-y)
53 60
common-obj-$(CONFIG_WIN32) += os-win32.o
54 61
common-obj-$(CONFIG_POSIX) += os-posix.o
55 62

  
b/osdep.c
61 61
#include "sysemu.h"
62 62
#include "qemu_socket.h"
63 63

  
64
#if !defined(_POSIX_C_SOURCE) || defined(_WIN32) || defined(__sun__)
65
static void *oom_check(void *ptr)
66
{
67
    if (ptr == NULL) {
68
#if defined(_WIN32)
69
        fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
70
#else
71
        fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
72
#endif
73
        abort();
74
    }
75
    return ptr;
76
}
77
#endif
78

  
79
#if defined(_WIN32)
80
void *qemu_memalign(size_t alignment, size_t size)
81
{
82
    void *ptr;
83

  
84
    if (!size) {
85
        abort();
86
    }
87
    ptr = oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
88
    trace_qemu_memalign(alignment, size, ptr);
89
    return ptr;
90
}
91

  
92
void *qemu_vmalloc(size_t size)
93
{
94
    void *ptr;
95

  
96
    /* FIXME: this is not exactly optimal solution since VirtualAlloc
97
       has 64Kb granularity, but at least it guarantees us that the
98
       memory is page aligned. */
99
    if (!size) {
100
        abort();
101
    }
102
    ptr = oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
103
    trace_qemu_vmalloc(size, ptr);
104
    return ptr;
105
}
106

  
107
void qemu_vfree(void *ptr)
108
{
109
    trace_qemu_vfree(ptr);
110
    VirtualFree(ptr, 0, MEM_RELEASE);
111
}
112

  
113
#else
114

  
115
void *qemu_memalign(size_t alignment, size_t size)
116
{
117
    void *ptr;
118
#if defined(_POSIX_C_SOURCE) && !defined(__sun__)
119
    int ret;
120
    ret = posix_memalign(&ptr, alignment, size);
121
    if (ret != 0) {
122
        fprintf(stderr, "Failed to allocate %zu B: %s\n",
123
                size, strerror(ret));
124
        abort();
125
    }
126
#elif defined(CONFIG_BSD)
127
    ptr = oom_check(valloc(size));
128
#else
129
    ptr = oom_check(memalign(alignment, size));
130
#endif
131
    trace_qemu_memalign(alignment, size, ptr);
132
    return ptr;
133
}
134

  
135
/* alloc shared memory pages */
136
void *qemu_vmalloc(size_t size)
137
{
138
    return qemu_memalign(getpagesize(), size);
139
}
140

  
141
void qemu_vfree(void *ptr)
142
{
143
    trace_qemu_vfree(ptr);
144
    free(ptr);
145
}
146

  
147
#endif
148

  
149 64
int qemu_madvise(void *addr, size_t len, int advice)
150 65
{
151 66
    if (advice == QEMU_MADV_INVALID) {
b/oslib-posix.c
1
/*
2
 * os-posix-lib.c
3
 *
4
 * Copyright (c) 2003-2008 Fabrice Bellard
5
 * Copyright (c) 2010 Red Hat, Inc.
6
 *
7
 * QEMU library functions on POSIX which are shared between QEMU and
8
 * the QEMU tools.
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining a copy
11
 * of this software and associated documentation files (the "Software"), to deal
12
 * in the Software without restriction, including without limitation the rights
13
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
 * copies of the Software, and to permit persons to whom the Software is
15
 * furnished to do so, subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included in
18
 * all copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
 * THE SOFTWARE.
27
 */
28

  
29
#include "config-host.h"
30
#include "sysemu.h"
31
#include "trace.h"
32

  
33
#if !defined(_POSIX_C_SOURCE) || defined(__sun__)
34
static void *oom_check(void *ptr)
35
{
36
    if (ptr == NULL) {
37
        fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
38
        abort();
39
    }
40
    return ptr;
41
}
42
#endif
43

  
44
void *qemu_memalign(size_t alignment, size_t size)
45
{
46
    void *ptr;
47
#if defined(_POSIX_C_SOURCE) && !defined(__sun__)
48
    int ret;
49
    ret = posix_memalign(&ptr, alignment, size);
50
    if (ret != 0) {
51
        fprintf(stderr, "Failed to allocate %zu B: %s\n",
52
                size, strerror(ret));
53
        abort();
54
    }
55
#elif defined(CONFIG_BSD)
56
    ptr = oom_check(valloc(size));
57
#else
58
    ptr = oom_check(memalign(alignment, size));
59
#endif
60
    trace_qemu_memalign(alignment, size, ptr);
61
    return ptr;
62
}
63

  
64
/* alloc shared memory pages */
65
void *qemu_vmalloc(size_t size)
66
{
67
    return qemu_memalign(getpagesize(), size);
68
}
69

  
70
void qemu_vfree(void *ptr)
71
{
72
    trace_qemu_vfree(ptr);
73
    free(ptr);
74
}
b/oslib-win32.c
1
/*
2
 * os-win32.c
3
 *
4
 * Copyright (c) 2003-2008 Fabrice Bellard
5
 * Copyright (c) 2010 Red Hat, Inc.
6
 *
7
 * QEMU library functions for win32 which are shared between QEMU and
8
 * the QEMU tools.
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining a copy
11
 * of this software and associated documentation files (the "Software"), to deal
12
 * in the Software without restriction, including without limitation the rights
13
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
 * copies of the Software, and to permit persons to whom the Software is
15
 * furnished to do so, subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included in
18
 * all copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
 * THE SOFTWARE.
27
 */
28
#include <windows.h>
29
#include "config-host.h"
30
#include "sysemu.h"
31
#include "trace.h"
32

  
33
static void *oom_check(void *ptr)
34
{
35
    if (ptr == NULL) {
36
        fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
37
        abort();
38
    }
39
    return ptr;
40
}
41

  
42
void *qemu_memalign(size_t alignment, size_t size)
43
{
44
    void *ptr;
45

  
46
    if (!size) {
47
        abort();
48
    }
49
    ptr = oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
50
    trace_qemu_memalign(alignment, size, ptr);
51
    return ptr;
52
}
53

  
54
void *qemu_vmalloc(size_t size)
55
{
56
    void *ptr;
57

  
58
    /* FIXME: this is not exactly optimal solution since VirtualAlloc
59
       has 64Kb granularity, but at least it guarantees us that the
60
       memory is page aligned. */
61
    if (!size) {
62
        abort();
63
    }
64
    ptr = oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
65
    trace_qemu_vmalloc(size, ptr);
66
    return ptr;
67
}
68

  
69
void qemu_vfree(void *ptr)
70
{
71
    trace_qemu_vfree(ptr);
72
    VirtualFree(ptr, 0, MEM_RELEASE);
73
}

Also available in: Unified diff