aboutsummaryrefslogtreecommitdiff
path: root/osdep.c
diff options
context:
space:
mode:
authorbellard <bellard@c046a42c-6fe2-441c-8c8c-71466251a162>2005-02-10 21:59:25 +0000
committerbellard <bellard@c046a42c-6fe2-441c-8c8c-71466251a162>2005-02-10 21:59:25 +0000
commit49b470eb96a06cbaf01fa8f16b799d9a44b3a642 (patch)
treeb507f2681bd5f58aecba36b01213aa4777a35f09 /osdep.c
parentc9ec1fe474ee9085b7eaad2326ee1c688f0c2d19 (diff)
shared pages memory allocation
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1279 c046a42c-6fe2-441c-8c8c-71466251a162
Diffstat (limited to 'osdep.c')
-rw-r--r--osdep.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/osdep.c b/osdep.c
index 087a5c2185..bb2c0d1f0d 100644
--- a/osdep.c
+++ b/osdep.c
@@ -273,6 +273,8 @@ void *get_mmap_addr(unsigned long size)
#else
+#include <malloc.h>
+
int qemu_write(int fd, const void *buf, size_t n)
{
int ret;
@@ -298,6 +300,85 @@ void *qemu_malloc(size_t size)
return malloc(size);
}
+#if defined(USE_KQEMU)
+
+#include <sys/mman.h>
+#include <fcntl.h>
+
+void *qemu_vmalloc(size_t size)
+{
+ static int phys_ram_fd = -1;
+ static int phys_ram_size = 0;
+ const char *tmpdir;
+ char phys_ram_file[1024];
+ void *ptr;
+
+ if (phys_ram_fd < 0) {
+ tmpdir = getenv("QEMU_TMPDIR");
+ if (!tmpdir)
+ tmpdir = "/dev/shm";
+ snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX",
+ tmpdir);
+ if (mkstemp(phys_ram_file) < 0) {
+ fprintf(stderr,
+ "warning: could not create temporary file in '%s'.\n"
+ "Use QEMU_TMPDIR to select a directory in a tmpfs filesystem.\n"
+ "Using '/tmp' as fallback.\n",
+ tmpdir);
+ snprintf(phys_ram_file, sizeof(phys_ram_file), "%s/qemuXXXXXX",
+ "/tmp");
+ if (mkstemp(phys_ram_file) < 0) {
+ fprintf(stderr, "Could not create temporary memory file '%s'\n",
+ phys_ram_file);
+ exit(1);
+ }
+ }
+ phys_ram_fd = open(phys_ram_file, O_CREAT | O_TRUNC | O_RDWR, 0600);
+ if (phys_ram_fd < 0) {
+ fprintf(stderr, "Could not open temporary memory file '%s'\n",
+ phys_ram_file);
+ exit(1);
+ }
+ unlink(phys_ram_file);
+ }
+ size = (size + 4095) & ~4095;
+ ftruncate(phys_ram_fd, phys_ram_size + size);
+ ptr = mmap(NULL,
+ size,
+ PROT_WRITE | PROT_READ, MAP_SHARED,
+ phys_ram_fd, phys_ram_size);
+ if (ptr == MAP_FAILED) {
+ fprintf(stderr, "Could not map physical memory\n");
+ exit(1);
+ }
+ phys_ram_size += size;
+ return ptr;
+}
+
+void qemu_vfree(void *ptr)
+{
+ /* may be useful some day, but currently we do not need to free */
+}
+
+#else
+
+/* alloc shared memory pages */
+void *qemu_vmalloc(size_t size)
+{
+#ifdef _BSD
+ return valloc(size);
+#else
+ return memalign(4096, size);
+#endif
+}
+
+void qemu_vfree(void *ptr)
+{
+ free(ptr);
+}
+
+#endif
+
#endif
void *qemu_mallocz(size_t size)