1 | /** @file
|
---|
2 | * IPRT - A Simple Heap.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | *
|
---|
16 | * The contents of this file may alternatively be used under the terms
|
---|
17 | * of the Common Development and Distribution License Version 1.0
|
---|
18 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
19 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
20 | * CDDL are applicable instead of those of the GPL.
|
---|
21 | *
|
---|
22 | * You may elect to license modified versions of this file under the
|
---|
23 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
24 | *
|
---|
25 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
26 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
27 | * additional information or have any questions.
|
---|
28 | */
|
---|
29 |
|
---|
30 | #ifndef ___iprt_heap_h
|
---|
31 | #define ___iprt_heap_h
|
---|
32 |
|
---|
33 | #include <iprt/cdefs.h>
|
---|
34 | #include <iprt/types.h>
|
---|
35 |
|
---|
36 | RT_C_DECLS_BEGIN
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Initializes the heap.
|
---|
40 | *
|
---|
41 | * @returns IPRT status code.
|
---|
42 | * @param pHeap Where to store the heap anchor block on success.
|
---|
43 | * @param pvMemory Pointer to the heap memory.
|
---|
44 | * @param cbMemory The size of the heap memory.
|
---|
45 | */
|
---|
46 | RTDECL(int) RTHeapSimpleInit(PRTHEAPSIMPLE pHeap, void *pvMemory, size_t cbMemory);
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Merge two simple heaps into one.
|
---|
50 | *
|
---|
51 | * The requirement is of course that they next two each other memory wise.
|
---|
52 | *
|
---|
53 | * @returns IPRT status code.
|
---|
54 | * @param pHeap Where to store the handle to the merged heap on success.
|
---|
55 | * @param Heap1 Handle to the first heap.
|
---|
56 | * @param Heap2 Handle to the second heap.
|
---|
57 | * @remark This API isn't implemented yet.
|
---|
58 | */
|
---|
59 | RTDECL(int) RTHeapSimpleMerge(PRTHEAPSIMPLE pHeap, RTHEAPSIMPLE Heap1, RTHEAPSIMPLE Heap2);
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * Relocater the heap internal structures after copying it to a new location.
|
---|
63 | *
|
---|
64 | * This can be used when loading a saved heap.
|
---|
65 | *
|
---|
66 | * @returns IPRT status code.
|
---|
67 | * @param hHeap Heap handle that has already been adjusted by to the new
|
---|
68 | * location. That is to say, when calling
|
---|
69 | * RTHeapSimpleInit, the caller must note the offset of the
|
---|
70 | * returned heap handle into the heap memory. This offset
|
---|
71 | * must be used when calcuating the handle value for the
|
---|
72 | * new location. The offset may in some cases not be zero!
|
---|
73 | * @param offDelta The delta between the new and old location, i.e. what
|
---|
74 | * should be added to the internal pointers.
|
---|
75 | */
|
---|
76 | RTDECL(int) RTHeapSimpleRelocate(RTHEAPSIMPLE hHeap, uintptr_t offDelta);
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Allocates memory from the specified simple heap.
|
---|
80 | *
|
---|
81 | * @returns Pointer to the allocated memory block on success.
|
---|
82 | * @returns NULL if the request cannot be satisfied. (A VERR_NO_MEMORY condition.)
|
---|
83 | *
|
---|
84 | * @param Heap The heap to allocate the memory on.
|
---|
85 | * @param cb The requested heap block size.
|
---|
86 | * @param cbAlignment The requested heap block alignment. Pass 0 for default alignment.
|
---|
87 | * Must be a power of 2.
|
---|
88 | */
|
---|
89 | RTDECL(void *) RTHeapSimpleAlloc(RTHEAPSIMPLE Heap, size_t cb, size_t cbAlignment);
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * Allocates zeroed memory from the specified simple heap.
|
---|
93 | *
|
---|
94 | * @returns Pointer to the allocated memory block on success.
|
---|
95 | * @returns NULL if the request cannot be satisfied. (A VERR_NO_MEMORY condition.)
|
---|
96 | *
|
---|
97 | * @param Heap The heap to allocate the memory on.
|
---|
98 | * @param cb The requested heap block size.
|
---|
99 | * @param cbAlignment The requested heap block alignment. Pass 0 for default alignment.
|
---|
100 | * Must be a power of 2.
|
---|
101 | */
|
---|
102 | RTDECL(void *) RTHeapSimpleAllocZ(RTHEAPSIMPLE Heap, size_t cb, size_t cbAlignment);
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Reallocates / Allocates / Frees a heap block.
|
---|
106 | *
|
---|
107 | * @param Heap The heap. This is optional and will only be used for strict assertions.
|
---|
108 | * @param pv The heap block returned by RTHeapSimple. If NULL it behaves like RTHeapSimpleAlloc().
|
---|
109 | * @param cbNew The new size of the heap block. If NULL it behaves like RTHeapSimpleFree().
|
---|
110 | * @param cbAlignment The requested heap block alignment. Pass 0 for default alignment.
|
---|
111 | * Must be a power of 2.
|
---|
112 | * @remark This API isn't implemented yet.
|
---|
113 | */
|
---|
114 | RTDECL(void *) RTHeapSimpleRealloc(RTHEAPSIMPLE Heap, void *pv, size_t cbNew, size_t cbAlignment);
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Reallocates / Allocates / Frees a heap block, zeroing any new bits.
|
---|
118 | *
|
---|
119 | * @param Heap The heap. This is optional and will only be used for strict assertions.
|
---|
120 | * @param pv The heap block returned by RTHeapSimple. If NULL it behaves like RTHeapSimpleAllocZ().
|
---|
121 | * @param cbNew The new size of the heap block. If NULL it behaves like RTHeapSimpleFree().
|
---|
122 | * @param cbAlignment The requested heap block alignment. Pass 0 for default alignment.
|
---|
123 | * Must be a power of 2.
|
---|
124 | * @remark This API isn't implemented yet.
|
---|
125 | */
|
---|
126 | RTDECL(void *) RTHeapSimpleReallocZ(RTHEAPSIMPLE Heap, void *pv, size_t cbNew, size_t cbAlignment);
|
---|
127 |
|
---|
128 | /**
|
---|
129 | * Frees memory allocated from a simple heap.
|
---|
130 | *
|
---|
131 | * @param Heap The heap. This is optional and will only be used for strict assertions.
|
---|
132 | * @param pv The heap block returned by RTHeapSimple
|
---|
133 | */
|
---|
134 | RTDECL(void) RTHeapSimpleFree(RTHEAPSIMPLE Heap, void *pv);
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Gets the size of the specified heap block.
|
---|
138 | *
|
---|
139 | * @returns The actual size of the heap block.
|
---|
140 | * @returns 0 if \a pv is NULL or it doesn't point to a valid heap block. An invalid \a pv
|
---|
141 | * can also cause traps or trigger assertions.
|
---|
142 | * @param Heap The heap. This is optional and will only be used for strict assertions.
|
---|
143 | * @param pv The heap block returned by RTHeapSimple
|
---|
144 | */
|
---|
145 | RTDECL(size_t) RTHeapSimpleSize(RTHEAPSIMPLE Heap, void *pv);
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Gets the size of the heap.
|
---|
149 | *
|
---|
150 | * This size includes all the internal heap structures. So, even if the heap is
|
---|
151 | * empty the RTHeapSimpleGetFreeSize() will never reach the heap size returned
|
---|
152 | * by this function.
|
---|
153 | *
|
---|
154 | * @returns The heap size.
|
---|
155 | * @returns 0 if heap was safely detected as being bad.
|
---|
156 | * @param Heap The heap.
|
---|
157 | */
|
---|
158 | RTDECL(size_t) RTHeapSimpleGetHeapSize(RTHEAPSIMPLE Heap);
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Returns the sum of all free heap blocks.
|
---|
162 | *
|
---|
163 | * This is the amount of memory you can theoretically allocate
|
---|
164 | * if you do allocations exactly matching the free blocks.
|
---|
165 | *
|
---|
166 | * @returns The size of the free blocks.
|
---|
167 | * @returns 0 if heap was safely detected as being bad.
|
---|
168 | * @param Heap The heap.
|
---|
169 | */
|
---|
170 | RTDECL(size_t) RTHeapSimpleGetFreeSize(RTHEAPSIMPLE Heap);
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Printf like callbaclk function for RTHeapSimpleDump.
|
---|
174 | * @param pszFormat IPRT format string.
|
---|
175 | * @param ... Format arguments.
|
---|
176 | */
|
---|
177 | typedef DECLCALLBACK(void) FNRTHEAPSIMPLEPRINTF(const char *pszFormat, ...);
|
---|
178 | /** Pointer to a FNRTHEAPSIMPLEPRINTF function. */
|
---|
179 | typedef FNRTHEAPSIMPLEPRINTF *PFNRTHEAPSIMPLEPRINTF;
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Dumps the hypervisor heap.
|
---|
183 | *
|
---|
184 | * @param Heap The heap handle.
|
---|
185 | * @param pfnPrintf Printf like function that groks IPRT formatting.
|
---|
186 | */
|
---|
187 | RTDECL(void) RTHeapSimpleDump(RTHEAPSIMPLE Heap, PFNRTHEAPSIMPLEPRINTF pfnPrintf);
|
---|
188 |
|
---|
189 | RT_C_DECLS_END
|
---|
190 |
|
---|
191 | #endif
|
---|
192 |
|
---|