aboutsummaryrefslogtreecommitdiff
path: root/tests/libqos
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2018-11-29 12:37:04 +0100
committerPaolo Bonzini <pbonzini@redhat.com>2019-03-07 17:28:07 +0100
commiteb5937bad691ed18a401079a0604aa11fea0ecdd (patch)
treea26310ca140c15230c51cceeebc538f7f55b7888 /tests/libqos
parent143e6db6fa4ecd2a85de740cc3754aeb86d1e802 (diff)
tests/libqos: embed allocators instead of malloc-ing them separately
qgraph will embed these objects instead of allocating them in a separate object. Expose a new API "generic_alloc_init" and "generic_alloc_destroy" for that, and rename the existing API with s/init/new/ and s/uninit/free/. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Diffstat (limited to 'tests/libqos')
-rw-r--r--tests/libqos/libqos-pc.c3
-rw-r--r--tests/libqos/libqos-spapr.c3
-rw-r--r--tests/libqos/libqos.c13
-rw-r--r--tests/libqos/libqos.h11
-rw-r--r--tests/libqos/malloc-generic.c25
-rw-r--r--tests/libqos/malloc-generic.h7
-rw-r--r--tests/libqos/malloc-pc.c22
-rw-r--r--tests/libqos/malloc-pc.h4
-rw-r--r--tests/libqos/malloc-spapr.c19
-rw-r--r--tests/libqos/malloc-spapr.h4
-rw-r--r--tests/libqos/malloc.c42
-rw-r--r--tests/libqos/malloc.h21
12 files changed, 44 insertions, 130 deletions
diff --git a/tests/libqos/libqos-pc.c b/tests/libqos/libqos-pc.c
index 293f9b6fe6..d04abc548b 100644
--- a/tests/libqos/libqos-pc.c
+++ b/tests/libqos/libqos-pc.c
@@ -4,8 +4,7 @@
#include "libqos/pci-pc.h"
static QOSOps qos_ops = {
- .init_allocator = pc_alloc_init_flags,
- .uninit_allocator = pc_alloc_uninit,
+ .alloc_init = pc_alloc_init,
.qpci_new = qpci_new_pc,
.qpci_free = qpci_free_pc,
.shutdown = qtest_pc_shutdown,
diff --git a/tests/libqos/libqos-spapr.c b/tests/libqos/libqos-spapr.c
index 64addfe577..8766d543ce 100644
--- a/tests/libqos/libqos-spapr.c
+++ b/tests/libqos/libqos-spapr.c
@@ -4,8 +4,7 @@
#include "libqos/pci-spapr.h"
static QOSOps qos_ops = {
- .init_allocator = spapr_alloc_init_flags,
- .uninit_allocator = spapr_alloc_uninit,
+ .alloc_init = spapr_alloc_init,
.qpci_new = qpci_new_spapr,
.qpci_free = qpci_free_spapr,
.shutdown = qtest_spapr_shutdown,
diff --git a/tests/libqos/libqos.c b/tests/libqos/libqos.c
index 6c9137165d..636a111a6f 100644
--- a/tests/libqos/libqos.c
+++ b/tests/libqos/libqos.c
@@ -24,8 +24,8 @@ QOSState *qtest_vboot(QOSOps *ops, const char *cmdline_fmt, va_list ap)
qs->qts = qtest_init(cmdline);
qs->ops = ops;
if (ops) {
- qs->alloc = ops->init_allocator(qs->qts, ALLOC_NO_FLAGS);
- qs->pcibus = ops->qpci_new(qs->qts, qs->alloc);
+ ops->alloc_init(&qs->alloc, qs->qts, ALLOC_NO_FLAGS);
+ qs->pcibus = ops->qpci_new(qs->qts, &qs->alloc);
}
g_free(cmdline);
@@ -58,11 +58,8 @@ void qtest_common_shutdown(QOSState *qs)
qs->ops->qpci_free(qs->pcibus);
qs->pcibus = NULL;
}
- if (qs->alloc && qs->ops->uninit_allocator) {
- qs->ops->uninit_allocator(qs->alloc);
- qs->alloc = NULL;
- }
}
+ alloc_destroy(&qs->alloc);
qtest_quit(qs->qts);
g_free(qs);
}
@@ -116,7 +113,7 @@ void migrate(QOSState *from, QOSState *to, const char *uri)
/* If we were running, we can wait for an event. */
if (running) {
- migrate_allocator(from->alloc, to->alloc);
+ migrate_allocator(&from->alloc, &to->alloc);
set_context(to);
qtest_qmp_eventwait(to->qts, "RESUME");
return;
@@ -146,7 +143,7 @@ void migrate(QOSState *from, QOSState *to, const char *uri)
g_assert_not_reached();
}
- migrate_allocator(from->alloc, to->alloc);
+ migrate_allocator(&from->alloc, &to->alloc);
set_context(to);
}
diff --git a/tests/libqos/libqos.h b/tests/libqos/libqos.h
index 1af6035db4..149b0be8bc 100644
--- a/tests/libqos/libqos.h
+++ b/tests/libqos/libqos.h
@@ -3,13 +3,12 @@
#include "libqtest.h"
#include "libqos/pci.h"
-#include "libqos/malloc-pc.h"
+#include "libqos/malloc.h"
typedef struct QOSState QOSState;
typedef struct QOSOps {
- QGuestAllocator *(*init_allocator)(QTestState *qts, QAllocOpts);
- void (*uninit_allocator)(QGuestAllocator *);
+ void (*alloc_init)(QGuestAllocator *, QTestState *, QAllocOpts);
QPCIBus *(*qpci_new)(QTestState *qts, QGuestAllocator *alloc);
void (*qpci_free)(QPCIBus *bus);
void (*shutdown)(QOSState *);
@@ -17,7 +16,7 @@ typedef struct QOSOps {
struct QOSState {
QTestState *qts;
- QGuestAllocator *alloc;
+ QGuestAllocator alloc;
QPCIBus *pcibus;
QOSOps *ops;
};
@@ -36,12 +35,12 @@ void generate_pattern(void *buffer, size_t len, size_t cycle_len);
static inline uint64_t qmalloc(QOSState *q, size_t bytes)
{
- return guest_alloc(q->alloc, bytes);
+ return guest_alloc(&q->alloc, bytes);
}
static inline void qfree(QOSState *q, uint64_t addr)
{
- guest_free(q->alloc, addr);
+ guest_free(&q->alloc, addr);
}
#endif
diff --git a/tests/libqos/malloc-generic.c b/tests/libqos/malloc-generic.c
index 33ce90b925..94290daffc 100644
--- a/tests/libqos/malloc-generic.c
+++ b/tests/libqos/malloc-generic.c
@@ -11,29 +11,10 @@
#include "libqos/malloc-generic.h"
#include "libqos/malloc.h"
-/*
- * Mostly for valgrind happiness, but it does offer
- * a chokepoint for debugging guest memory leaks, too.
- */
-void generic_alloc_uninit(QGuestAllocator *allocator)
-{
- alloc_uninit(allocator);
-}
-
-QGuestAllocator *generic_alloc_init_flags(uint64_t base_addr, uint64_t size,
- uint32_t page_size, QAllocOpts flags)
+void generic_alloc_init(QGuestAllocator *s, uint64_t base_addr,
+ uint64_t size, uint32_t page_size)
{
- QGuestAllocator *s;
uint64_t start = base_addr + (1 << 20); /* Start at 1MB */
- s = alloc_init_flags(flags, start, start + size);
- alloc_set_page_size(s, page_size);
-
- return s;
-}
-
-inline QGuestAllocator *generic_alloc_init(uint64_t base_addr, uint64_t size,
- uint32_t page_size)
-{
- return generic_alloc_init_flags(base_addr, size, page_size, ALLOC_NO_FLAGS);
+ alloc_init(s, 0, start, start + size, page_size);
}
diff --git a/tests/libqos/malloc-generic.h b/tests/libqos/malloc-generic.h
index 90104ecec9..40ea058b2d 100644
--- a/tests/libqos/malloc-generic.h
+++ b/tests/libqos/malloc-generic.h
@@ -12,10 +12,7 @@
#include "libqos/malloc.h"
-QGuestAllocator *generic_alloc_init(uint64_t base_addr, uint64_t size,
- uint32_t page_size);
-QGuestAllocator *generic_alloc_init_flags(uint64_t base_addr, uint64_t size,
- uint32_t page_size, QAllocOpts flags);
-void generic_alloc_uninit(QGuestAllocator *allocator);
+void generic_alloc_init(QGuestAllocator *s, uint64_t base_addr, uint64_t size,
+ uint32_t page_size);
#endif
diff --git a/tests/libqos/malloc-pc.c b/tests/libqos/malloc-pc.c
index b83cb8f0af..949a99361d 100644
--- a/tests/libqos/malloc-pc.c
+++ b/tests/libqos/malloc-pc.c
@@ -20,32 +20,14 @@
#define PAGE_SIZE (4096)
-/*
- * Mostly for valgrind happiness, but it does offer
- * a chokepoint for debugging guest memory leaks, too.
- */
-void pc_alloc_uninit(QGuestAllocator *allocator)
-{
- alloc_uninit(allocator);
-}
-
-QGuestAllocator *pc_alloc_init_flags(QTestState *qts, QAllocOpts flags)
+void pc_alloc_init(QGuestAllocator *s, QTestState *qts, QAllocOpts flags)
{
- QGuestAllocator *s;
uint64_t ram_size;
QFWCFG *fw_cfg = pc_fw_cfg_init(qts);
ram_size = qfw_cfg_get_u64(fw_cfg, FW_CFG_RAM_SIZE);
- s = alloc_init_flags(flags, 1 << 20, MIN(ram_size, 0xE0000000));
- alloc_set_page_size(s, PAGE_SIZE);
+ alloc_init(s, flags, 1 << 20, MIN(ram_size, 0xE0000000), PAGE_SIZE);
/* clean-up */
g_free(fw_cfg);
-
- return s;
-}
-
-inline QGuestAllocator *pc_alloc_init(QTestState *qts)
-{
- return pc_alloc_init_flags(qts, ALLOC_NO_FLAGS);
}
diff --git a/tests/libqos/malloc-pc.h b/tests/libqos/malloc-pc.h
index 10f3da6cf2..21e75ae004 100644
--- a/tests/libqos/malloc-pc.h
+++ b/tests/libqos/malloc-pc.h
@@ -15,8 +15,6 @@
#include "libqos/malloc.h"
-QGuestAllocator *pc_alloc_init(QTestState *qts);
-QGuestAllocator *pc_alloc_init_flags(QTestState *qts, QAllocOpts flags);
-void pc_alloc_uninit(QGuestAllocator *allocator);
+void pc_alloc_init(QGuestAllocator *s, QTestState *qts, QAllocOpts flags);
#endif
diff --git a/tests/libqos/malloc-spapr.c b/tests/libqos/malloc-spapr.c
index 1c359cea6c..2a6b7e3776 100644
--- a/tests/libqos/malloc-spapr.c
+++ b/tests/libqos/malloc-spapr.c
@@ -17,22 +17,7 @@
*/
#define SPAPR_MIN_SIZE 0x10000000
-void spapr_alloc_uninit(QGuestAllocator *allocator)
+void spapr_alloc_init(QGuestAllocator *s, QTestState *qts, QAllocOpts flags)
{
- alloc_uninit(allocator);
-}
-
-QGuestAllocator *spapr_alloc_init_flags(QTestState *qts, QAllocOpts flags)
-{
- QGuestAllocator *s;
-
- s = alloc_init_flags(flags, 1 << 20, SPAPR_MIN_SIZE);
- alloc_set_page_size(s, PAGE_SIZE);
-
- return s;
-}
-
-QGuestAllocator *spapr_alloc_init(void)
-{
- return spapr_alloc_init_flags(NULL, ALLOC_NO_FLAGS);
+ alloc_init(s, flags, 1 << 20, SPAPR_MIN_SIZE, PAGE_SIZE);
}
diff --git a/tests/libqos/malloc-spapr.h b/tests/libqos/malloc-spapr.h
index 52a9346a26..e5fe9bfc4b 100644
--- a/tests/libqos/malloc-spapr.h
+++ b/tests/libqos/malloc-spapr.h
@@ -10,8 +10,6 @@
#include "libqos/malloc.h"
-QGuestAllocator *spapr_alloc_init(void);
-QGuestAllocator *spapr_alloc_init_flags(QTestState *qts, QAllocOpts flags);
-void spapr_alloc_uninit(QGuestAllocator *allocator);
+void spapr_alloc_init(QGuestAllocator *s, QTestState *qts, QAllocOpts flags);
#endif
diff --git a/tests/libqos/malloc.c b/tests/libqos/malloc.c
index f7bae47a08..615422a5c4 100644
--- a/tests/libqos/malloc.c
+++ b/tests/libqos/malloc.c
@@ -15,24 +15,12 @@
#include "qemu-common.h"
#include "qemu/host-utils.h"
-typedef QTAILQ_HEAD(MemList, MemBlock) MemList;
-
typedef struct MemBlock {
QTAILQ_ENTRY(MemBlock) MLIST_ENTNAME;
uint64_t size;
uint64_t addr;
} MemBlock;
-struct QGuestAllocator {
- QAllocOpts opts;
- uint64_t start;
- uint64_t end;
- uint32_t page_size;
-
- MemList *used;
- MemList *free;
-};
-
#define DEFAULT_PAGE_SIZE 4096
static void mlist_delete(MemList *list, MemBlock *node)
@@ -225,7 +213,7 @@ static void mlist_free(QGuestAllocator *s, uint64_t addr)
* Mostly for valgrind happiness, but it does offer
* a chokepoint for debugging guest memory leaks, too.
*/
-void alloc_uninit(QGuestAllocator *allocator)
+void alloc_destroy(QGuestAllocator *allocator)
{
MemBlock *node;
MemBlock *tmp;
@@ -261,7 +249,6 @@ void alloc_uninit(QGuestAllocator *allocator)
g_free(allocator->used);
g_free(allocator->free);
- g_free(allocator);
}
uint64_t guest_alloc(QGuestAllocator *allocator, size_t size)
@@ -297,11 +284,13 @@ void guest_free(QGuestAllocator *allocator, uint64_t addr)
}
}
-QGuestAllocator *alloc_init(uint64_t start, uint64_t end)
+void alloc_init(QGuestAllocator *s, QAllocOpts opts,
+ uint64_t start, uint64_t end,
+ size_t page_size)
{
- QGuestAllocator *s = g_malloc0(sizeof(*s));
MemBlock *node;
+ s->opts = opts;
s->start = start;
s->end = end;
@@ -313,26 +302,7 @@ QGuestAllocator *alloc_init(uint64_t start, uint64_t end)
node = mlist_new(s->start, s->end - s->start);
QTAILQ_INSERT_HEAD(s->free, node, MLIST_ENTNAME);
- s->page_size = DEFAULT_PAGE_SIZE;
-
- return s;
-}
-
-QGuestAllocator *alloc_init_flags(QAllocOpts opts,
- uint64_t start, uint64_t end)
-{
- QGuestAllocator *s = alloc_init(start, end);
- s->opts = opts;
- return s;
-}
-
-void alloc_set_page_size(QGuestAllocator *allocator, size_t page_size)
-{
- /* Can't alter the page_size for an allocator in-use */
- g_assert(QTAILQ_EMPTY(allocator->used));
-
- g_assert(is_power_of_2(page_size));
- allocator->page_size = page_size;
+ s->page_size = page_size;
}
void alloc_set_flags(QGuestAllocator *allocator, QAllocOpts opts)
diff --git a/tests/libqos/malloc.h b/tests/libqos/malloc.h
index 828fddabdb..4d1a2e2bef 100644
--- a/tests/libqos/malloc.h
+++ b/tests/libqos/malloc.h
@@ -23,19 +23,28 @@ typedef enum {
ALLOC_PARANOID = 0x04
} QAllocOpts;
-typedef struct QGuestAllocator QGuestAllocator;
+typedef QTAILQ_HEAD(MemList, MemBlock) MemList;
-void alloc_uninit(QGuestAllocator *allocator);
+typedef struct QGuestAllocator {
+ QAllocOpts opts;
+ uint64_t start;
+ uint64_t end;
+ uint32_t page_size;
+
+ MemList *used;
+ MemList *free;
+} QGuestAllocator;
/* Always returns page aligned values */
uint64_t guest_alloc(QGuestAllocator *allocator, size_t size);
void guest_free(QGuestAllocator *allocator, uint64_t addr);
void migrate_allocator(QGuestAllocator *src, QGuestAllocator *dst);
-QGuestAllocator *alloc_init(uint64_t start, uint64_t end);
-QGuestAllocator *alloc_init_flags(QAllocOpts flags,
- uint64_t start, uint64_t end);
-void alloc_set_page_size(QGuestAllocator *allocator, size_t page_size);
void alloc_set_flags(QGuestAllocator *allocator, QAllocOpts opts);
+void alloc_init(QGuestAllocator *alloc, QAllocOpts flags,
+ uint64_t start, uint64_t end,
+ size_t page_size);
+void alloc_destroy(QGuestAllocator *allocator);
+
#endif