Revision c1b0b93b osdep.c

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) {

Also available in: Unified diff