shared pages memory allocation
git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1279 c046a42c-6fe2-441c-8c8c-71466251a162
This commit is contained in:
parent
c9ec1fe474
commit
49b470eb96
81
osdep.c
81
osdep.c
|
@ -273,6 +273,8 @@ void *get_mmap_addr(unsigned long size)
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
|
#include <malloc.h>
|
||||||
|
|
||||||
int qemu_write(int fd, const void *buf, size_t n)
|
int qemu_write(int fd, const void *buf, size_t n)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
|
@ -298,6 +300,85 @@ void *qemu_malloc(size_t size)
|
||||||
return malloc(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
|
#endif
|
||||||
|
|
||||||
void *qemu_mallocz(size_t size)
|
void *qemu_mallocz(size_t size)
|
||||||
|
|
3
osdep.h
3
osdep.h
|
@ -12,6 +12,9 @@ void *qemu_mallocz(size_t size);
|
||||||
void qemu_free(void *ptr);
|
void qemu_free(void *ptr);
|
||||||
char *qemu_strdup(const char *str);
|
char *qemu_strdup(const char *str);
|
||||||
|
|
||||||
|
void *qemu_vmalloc(size_t size);
|
||||||
|
void qemu_vfree(void *ptr);
|
||||||
|
|
||||||
void *get_mmap_addr(unsigned long size);
|
void *get_mmap_addr(unsigned long size);
|
||||||
|
|
||||||
/* specific kludges for OS compatibility (should be moved elsewhere) */
|
/* specific kludges for OS compatibility (should be moved elsewhere) */
|
||||||
|
|
Loading…
Reference in a new issue