xref: /qemu/tests/qtest/libqos/libqos-malloc.h (revision fa02e6084f727191e15fc6b2d1328c4fae874741)
1 /*
2  * libqos malloc support
3  *
4  * Copyright IBM, Corp. 2012-2013
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  */
12 
13 #ifndef LIBQOS_MALLOC_H
14 #define LIBQOS_MALLOC_H
15 
16 #include <stdint.h>
17 #include <sys/types.h>
18 #include "qemu/queue.h"
19 
20 #define MLIST_ENTNAME entries
21 
22 typedef enum {
23     ALLOC_NO_FLAGS    = 0x00,
24     ALLOC_LEAK_WARN   = 0x01,
25     ALLOC_LEAK_ASSERT = 0x02,
26     ALLOC_PARANOID    = 0x04
27 } QAllocOpts;
28 
29 typedef QTAILQ_HEAD(MemList, MemBlock) MemList;
30 typedef struct MemBlock {
31     QTAILQ_ENTRY(MemBlock) MLIST_ENTNAME;
32     uint64_t size;
33     uint64_t addr;
34 } MemBlock;
35 
36 typedef struct QGuestAllocator {
37     QAllocOpts opts;
38     uint64_t start;
39     uint64_t end;
40     uint32_t page_size;
41 
42     MemList used;
43     MemList free;
44 } QGuestAllocator;
45 
46 MemBlock *mlist_new(uint64_t addr, uint64_t size);
47 void alloc_uninit(QGuestAllocator *allocator);
48 
49 /* Always returns page aligned values */
50 uint64_t guest_alloc(QGuestAllocator *allocator, size_t size);
51 void guest_free(QGuestAllocator *allocator, uint64_t addr);
52 
53 QGuestAllocator *alloc_init(uint64_t start, uint64_t end);
54 QGuestAllocator *alloc_init_flags(QAllocOpts flags,
55                                   uint64_t start, uint64_t end);
56 #endif
57