Message ID | 1467104499-27517-6-git-send-email-pl@kamp.de (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
On Tue, Jun 28, 2016 at 11:01:29AM +0200, Peter Lieven wrote: > Signed-off-by: Peter Lieven <pl@kamp.de> > --- > include/qemu/mmap-alloc.h | 6 ++++++ > util/mmap-alloc.c | 17 +++++++++++++++++ > 2 files changed, 23 insertions(+) > > diff --git a/include/qemu/mmap-alloc.h b/include/qemu/mmap-alloc.h > index 0899b2f..a457721 100644 > --- a/include/qemu/mmap-alloc.h > +++ b/include/qemu/mmap-alloc.h > @@ -9,4 +9,10 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared); > > void qemu_ram_munmap(void *ptr, size_t size); > > +/* qemu_anon_ram_mmap maps private anonymous memory using mmap and > + * aborts if the allocation fails. its meant to act as an replacement > + * for g_malloc0 and friends. */ This needs better docs. When should one use g_malloc0 and when qemu_anon_ram_munmap? > +void *qemu_anon_ram_mmap(size_t size); > +void qemu_anon_ram_munmap(void *ptr, size_t size); > + The names are confusing - this isn't guest RAM, this is just internal QEMU memory, isn't it? Just rename it qemu_malloc0 then ... > #endif > diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c > index 629d97a..c099858 100644 > --- a/util/mmap-alloc.c > +++ b/util/mmap-alloc.c > @@ -107,3 +107,20 @@ void qemu_ram_munmap(void *ptr, size_t size) > munmap(ptr, size + getpagesize()); > } > } > + > +void *qemu_anon_ram_mmap(size_t size) > +{ > + void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, > + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); > + if (ptr == MAP_FAILED) { > + abort(); > + } > + return ptr; > +} > + > +void qemu_anon_ram_munmap(void *ptr, size_t size) > +{ > + if (ptr) { > + munmap(ptr, size); > + } > +} > -- > 1.9.1
Michael S. Tsirkin <mst@redhat.com> writes: > On Tue, Jun 28, 2016 at 11:01:29AM +0200, Peter Lieven wrote: >> Signed-off-by: Peter Lieven <pl@kamp.de> >> --- >> include/qemu/mmap-alloc.h | 6 ++++++ >> util/mmap-alloc.c | 17 +++++++++++++++++ >> 2 files changed, 23 insertions(+) >> >> diff --git a/include/qemu/mmap-alloc.h b/include/qemu/mmap-alloc.h >> index 0899b2f..a457721 100644 >> --- a/include/qemu/mmap-alloc.h >> +++ b/include/qemu/mmap-alloc.h >> @@ -9,4 +9,10 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared); >> >> void qemu_ram_munmap(void *ptr, size_t size); >> >> +/* qemu_anon_ram_mmap maps private anonymous memory using mmap and >> + * aborts if the allocation fails. its meant to act as an replacement >> + * for g_malloc0 and friends. */ > > This needs better docs. When should one use g_malloc0 and when > qemu_anon_ram_munmap? My concern is does this break memory sanitizers when we could just tweak libc's allocation strategy to use mmap (which it should do for blocks over a certain threshold). > > > >> +void *qemu_anon_ram_mmap(size_t size); >> +void qemu_anon_ram_munmap(void *ptr, size_t size); >> + > > The names are confusing - this isn't guest RAM, this is > just internal QEMU memory, isn't it? > > Just rename it qemu_malloc0 then ... > >> #endif >> diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c >> index 629d97a..c099858 100644 >> --- a/util/mmap-alloc.c >> +++ b/util/mmap-alloc.c >> @@ -107,3 +107,20 @@ void qemu_ram_munmap(void *ptr, size_t size) >> munmap(ptr, size + getpagesize()); >> } >> } >> + >> +void *qemu_anon_ram_mmap(size_t size) >> +{ >> + void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, >> + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); >> + if (ptr == MAP_FAILED) { >> + abort(); >> + } >> + return ptr; >> +} >> + >> +void qemu_anon_ram_munmap(void *ptr, size_t size) >> +{ >> + if (ptr) { >> + munmap(ptr, size); >> + } >> +} >> -- >> 1.9.1 -- Alex Bennée
diff --git a/include/qemu/mmap-alloc.h b/include/qemu/mmap-alloc.h index 0899b2f..a457721 100644 --- a/include/qemu/mmap-alloc.h +++ b/include/qemu/mmap-alloc.h @@ -9,4 +9,10 @@ void *qemu_ram_mmap(int fd, size_t size, size_t align, bool shared); void qemu_ram_munmap(void *ptr, size_t size); +/* qemu_anon_ram_mmap maps private anonymous memory using mmap and + * aborts if the allocation fails. its meant to act as an replacement + * for g_malloc0 and friends. */ +void *qemu_anon_ram_mmap(size_t size); +void qemu_anon_ram_munmap(void *ptr, size_t size); + #endif diff --git a/util/mmap-alloc.c b/util/mmap-alloc.c index 629d97a..c099858 100644 --- a/util/mmap-alloc.c +++ b/util/mmap-alloc.c @@ -107,3 +107,20 @@ void qemu_ram_munmap(void *ptr, size_t size) munmap(ptr, size + getpagesize()); } } + +void *qemu_anon_ram_mmap(size_t size) +{ + void *ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (ptr == MAP_FAILED) { + abort(); + } + return ptr; +} + +void qemu_anon_ram_munmap(void *ptr, size_t size) +{ + if (ptr) { + munmap(ptr, size); + } +}
Signed-off-by: Peter Lieven <pl@kamp.de> --- include/qemu/mmap-alloc.h | 6 ++++++ util/mmap-alloc.c | 17 +++++++++++++++++ 2 files changed, 23 insertions(+)