Commit 49b470eb96a06cbaf01fa8f16b799d9a44b3a642
1 parent
c9ec1fe4
shared pages memory allocation
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1279 c046a42c-6fe2-441c-8c8c-71466251a162
Showing
2 changed files
with
84 additions
and
0 deletions
osdep.c
| ... | ... | @@ -273,6 +273,8 @@ void *get_mmap_addr(unsigned long size) |
| 273 | 273 | |
| 274 | 274 | #else |
| 275 | 275 | |
| 276 | +#include <malloc.h> | |
| 277 | + | |
| 276 | 278 | int qemu_write(int fd, const void *buf, size_t n) |
| 277 | 279 | { |
| 278 | 280 | int ret; |
| ... | ... | @@ -298,6 +300,85 @@ void *qemu_malloc(size_t size) |
| 298 | 300 | return malloc(size); |
| 299 | 301 | } |
| 300 | 302 | |
| 303 | +#if defined(USE_KQEMU) | |
| 304 | + | |
| 305 | +#include <sys/mman.h> | |
| 306 | +#include <fcntl.h> | |
| 307 | + | |
| 308 | +void *qemu_vmalloc(size_t size) | |
| 309 | +{ | |
| 310 | + static int phys_ram_fd = -1; | |
| 311 | + static int phys_ram_size = 0; | |
| 312 | + const char *tmpdir; | |
| 313 | + char phys_ram_file[1024]; | |
| 314 | + void *ptr; | |
| 315 | + | |
| 316 | + if (phys_ram_fd < 0) { | |
| 317 | + tmpdir = getenv("QEMU_TMPDIR"); | |
| 318 | + if (!tmpdir) | |
| 319 | + tmpdir = "/dev/shm"; | |
| 320 | + snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX", | |
| 321 | + tmpdir); | |
| 322 | + if (mkstemp(phys_ram_file) < 0) { | |
| 323 | + fprintf(stderr, | |
| 324 | + "warning: could not create temporary file in '%s'.\n" | |
| 325 | + "Use QEMU_TMPDIR to select a directory in a tmpfs filesystem.\n" | |
| 326 | + "Using '/tmp' as fallback.\n", | |
| 327 | + tmpdir); | |
| 328 | + snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX", | |
| 329 | + "/tmp"); | |
| 330 | + if (mkstemp(phys_ram_file) < 0) { | |
| 331 | + fprintf(stderr, "Could not create temporary memory file '%s'\n", | |
| 332 | + phys_ram_file); | |
| 333 | + exit(1); | |
| 334 | + } | |
| 335 | + } | |
| 336 | + phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600); | |
| 337 | + if (phys_ram_fd < 0) { | |
| 338 | + fprintf(stderr, "Could not open temporary memory file '%s'\n", | |
| 339 | + phys_ram_file); | |
| 340 | + exit(1); | |
| 341 | + } | |
| 342 | + unlink(phys_ram_file); | |
| 343 | + } | |
| 344 | + size = (size + 4095) & ~4095; | |
| 345 | + ftruncate(phys_ram_fd, phys_ram_size + size); | |
| 346 | + ptr = mmap(NULL, | |
| 347 | + size, | |
| 348 | + PROT_WRITE | PROT_READ, MAP_SHARED, | |
| 349 | + phys_ram_fd, phys_ram_size); | |
| 350 | + if (ptr == MAP_FAILED) { | |
| 351 | + fprintf(stderr, "Could not map physical memory\n"); | |
| 352 | + exit(1); | |
| 353 | + } | |
| 354 | + phys_ram_size += size; | |
| 355 | + return ptr; | |
| 356 | +} | |
| 357 | + | |
| 358 | +void qemu_vfree(void *ptr) | |
| 359 | +{ | |
| 360 | + /* may be useful some day, but currently we do not need to free */ | |
| 361 | +} | |
| 362 | + | |
| 363 | +#else | |
| 364 | + | |
| 365 | +/* alloc shared memory pages */ | |
| 366 | +void *qemu_vmalloc(size_t size) | |
| 367 | +{ | |
| 368 | +#ifdef _BSD | |
| 369 | + return valloc(size); | |
| 370 | +#else | |
| 371 | + return memalign(4096, size); | |
| 372 | +#endif | |
| 373 | +} | |
| 374 | + | |
| 375 | +void qemu_vfree(void *ptr) | |
| 376 | +{ | |
| 377 | + free(ptr); | |
| 378 | +} | |
| 379 | + | |
| 380 | +#endif | |
| 381 | + | |
| 301 | 382 | #endif |
| 302 | 383 | |
| 303 | 384 | void *qemu_mallocz(size_t size) | ... | ... |
osdep.h
| ... | ... | @@ -12,6 +12,9 @@ void *qemu_mallocz(size_t size); |
| 12 | 12 | void qemu_free(void *ptr); |
| 13 | 13 | char *qemu_strdup(const char *str); |
| 14 | 14 | |
| 15 | +void *qemu_vmalloc(size_t size); | |
| 16 | +void qemu_vfree(void *ptr); | |
| 17 | + | |
| 15 | 18 | void *get_mmap_addr(unsigned long size); |
| 16 | 19 | |
| 17 | 20 | /* specific kludges for OS compatibility (should be moved elsewhere) */ | ... | ... |