[283] | 1 | /** @file
|
---|
[25055] | 2 | * IPRT - Heap Implementations
|
---|
[283] | 3 | */
|
---|
| 4 |
|
---|
| 5 | /*
|
---|
[76553] | 6 | * Copyright (C) 2006-2019 Oracle Corporation
|
---|
[283] | 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
|
---|
[5999] | 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.
|
---|
[283] | 24 | */
|
---|
| 25 |
|
---|
[76557] | 26 | #ifndef IPRT_INCLUDED_heap_h
|
---|
| 27 | #define IPRT_INCLUDED_heap_h
|
---|
[76507] | 28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 29 | # pragma once
|
---|
| 30 | #endif
|
---|
[283] | 31 |
|
---|
| 32 | #include <iprt/cdefs.h>
|
---|
| 33 | #include <iprt/types.h>
|
---|
| 34 |
|
---|
[20374] | 35 | RT_C_DECLS_BEGIN
|
---|
[283] | 36 |
|
---|
[25055] | 37 | /** @defgroup grp_rt_heap RTHeap - Heap Implementations
|
---|
| 38 | * @ingroup grp_rt
|
---|
| 39 | * @{
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 |
|
---|
| 43 | /** @defgroup grp_rt_heap_simple RTHeapSimple - Simple Heap
|
---|
| 44 | * @{
|
---|
| 45 | */
|
---|
| 46 |
|
---|
[283] | 47 | /**
|
---|
| 48 | * Initializes the heap.
|
---|
[288] | 49 | *
|
---|
[22273] | 50 | * @returns IPRT status code.
|
---|
[283] | 51 | * @param pHeap Where to store the heap anchor block on success.
|
---|
| 52 | * @param pvMemory Pointer to the heap memory.
|
---|
| 53 | * @param cbMemory The size of the heap memory.
|
---|
| 54 | */
|
---|
| 55 | RTDECL(int) RTHeapSimpleInit(PRTHEAPSIMPLE pHeap, void *pvMemory, size_t cbMemory);
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * Merge two simple heaps into one.
|
---|
[288] | 59 | *
|
---|
[22273] | 60 | * The requirement is of course that they next two each other memory wise.
|
---|
[288] | 61 | *
|
---|
[22273] | 62 | * @returns IPRT status code.
|
---|
[283] | 63 | * @param pHeap Where to store the handle to the merged heap on success.
|
---|
| 64 | * @param Heap1 Handle to the first heap.
|
---|
| 65 | * @param Heap2 Handle to the second heap.
|
---|
[288] | 66 | * @remark This API isn't implemented yet.
|
---|
[283] | 67 | */
|
---|
| 68 | RTDECL(int) RTHeapSimpleMerge(PRTHEAPSIMPLE pHeap, RTHEAPSIMPLE Heap1, RTHEAPSIMPLE Heap2);
|
---|
| 69 |
|
---|
[25055] | 70 | /**
|
---|
| 71 | * Relocater the heap internal structures after copying it to a new location.
|
---|
| 72 | *
|
---|
| 73 | * This can be used when loading a saved heap.
|
---|
| 74 | *
|
---|
| 75 | * @returns IPRT status code.
|
---|
[22273] | 76 | * @param hHeap Heap handle that has already been adjusted by to the new
|
---|
| 77 | * location. That is to say, when calling
|
---|
| 78 | * RTHeapSimpleInit, the caller must note the offset of the
|
---|
| 79 | * returned heap handle into the heap memory. This offset
|
---|
| 80 | * must be used when calcuating the handle value for the
|
---|
| 81 | * new location. The offset may in some cases not be zero!
|
---|
[25055] | 82 | * @param offDelta The delta between the new and old location, i.e. what
|
---|
[22273] | 83 | * should be added to the internal pointers.
|
---|
| 84 | */
|
---|
| 85 | RTDECL(int) RTHeapSimpleRelocate(RTHEAPSIMPLE hHeap, uintptr_t offDelta);
|
---|
[25055] | 86 |
|
---|
[283] | 87 | /**
|
---|
| 88 | * Allocates memory from the specified simple heap.
|
---|
[288] | 89 | *
|
---|
[283] | 90 | * @returns Pointer to the allocated memory block on success.
|
---|
| 91 | * @returns NULL if the request cannot be satisfied. (A VERR_NO_MEMORY condition.)
|
---|
[288] | 92 | *
|
---|
[283] | 93 | * @param Heap The heap to allocate the memory on.
|
---|
| 94 | * @param cb The requested heap block size.
|
---|
| 95 | * @param cbAlignment The requested heap block alignment. Pass 0 for default alignment.
|
---|
| 96 | * Must be a power of 2.
|
---|
| 97 | */
|
---|
| 98 | RTDECL(void *) RTHeapSimpleAlloc(RTHEAPSIMPLE Heap, size_t cb, size_t cbAlignment);
|
---|
| 99 |
|
---|
| 100 | /**
|
---|
| 101 | * Allocates zeroed memory from the specified simple heap.
|
---|
[288] | 102 | *
|
---|
[283] | 103 | * @returns Pointer to the allocated memory block on success.
|
---|
| 104 | * @returns NULL if the request cannot be satisfied. (A VERR_NO_MEMORY condition.)
|
---|
[288] | 105 | *
|
---|
[283] | 106 | * @param Heap The heap to allocate the memory on.
|
---|
| 107 | * @param cb The requested heap block size.
|
---|
| 108 | * @param cbAlignment The requested heap block alignment. Pass 0 for default alignment.
|
---|
| 109 | * Must be a power of 2.
|
---|
| 110 | */
|
---|
| 111 | RTDECL(void *) RTHeapSimpleAllocZ(RTHEAPSIMPLE Heap, size_t cb, size_t cbAlignment);
|
---|
| 112 |
|
---|
| 113 | /**
|
---|
[288] | 114 | * Reallocates / Allocates / Frees a heap block.
|
---|
| 115 | *
|
---|
| 116 | * @param Heap The heap. This is optional and will only be used for strict assertions.
|
---|
| 117 | * @param pv The heap block returned by RTHeapSimple. If NULL it behaves like RTHeapSimpleAlloc().
|
---|
| 118 | * @param cbNew The new size of the heap block. If NULL it behaves like RTHeapSimpleFree().
|
---|
| 119 | * @param cbAlignment The requested heap block alignment. Pass 0 for default alignment.
|
---|
| 120 | * Must be a power of 2.
|
---|
| 121 | * @remark This API isn't implemented yet.
|
---|
| 122 | */
|
---|
| 123 | RTDECL(void *) RTHeapSimpleRealloc(RTHEAPSIMPLE Heap, void *pv, size_t cbNew, size_t cbAlignment);
|
---|
| 124 |
|
---|
| 125 | /**
|
---|
| 126 | * Reallocates / Allocates / Frees a heap block, zeroing any new bits.
|
---|
| 127 | *
|
---|
| 128 | * @param Heap The heap. This is optional and will only be used for strict assertions.
|
---|
| 129 | * @param pv The heap block returned by RTHeapSimple. If NULL it behaves like RTHeapSimpleAllocZ().
|
---|
| 130 | * @param cbNew The new size of the heap block. If NULL it behaves like RTHeapSimpleFree().
|
---|
| 131 | * @param cbAlignment The requested heap block alignment. Pass 0 for default alignment.
|
---|
| 132 | * Must be a power of 2.
|
---|
| 133 | * @remark This API isn't implemented yet.
|
---|
| 134 | */
|
---|
| 135 | RTDECL(void *) RTHeapSimpleReallocZ(RTHEAPSIMPLE Heap, void *pv, size_t cbNew, size_t cbAlignment);
|
---|
| 136 |
|
---|
| 137 | /**
|
---|
[283] | 138 | * Frees memory allocated from a simple heap.
|
---|
[288] | 139 | *
|
---|
[283] | 140 | * @param Heap The heap. This is optional and will only be used for strict assertions.
|
---|
| 141 | * @param pv The heap block returned by RTHeapSimple
|
---|
| 142 | */
|
---|
| 143 | RTDECL(void) RTHeapSimpleFree(RTHEAPSIMPLE Heap, void *pv);
|
---|
| 144 |
|
---|
| 145 | /**
|
---|
[288] | 146 | * Gets the size of the specified heap block.
|
---|
| 147 | *
|
---|
| 148 | * @returns The actual size of the heap block.
|
---|
| 149 | * @returns 0 if \a pv is NULL or it doesn't point to a valid heap block. An invalid \a pv
|
---|
| 150 | * can also cause traps or trigger assertions.
|
---|
| 151 | * @param Heap The heap. This is optional and will only be used for strict assertions.
|
---|
| 152 | * @param pv The heap block returned by RTHeapSimple
|
---|
| 153 | */
|
---|
| 154 | RTDECL(size_t) RTHeapSimpleSize(RTHEAPSIMPLE Heap, void *pv);
|
---|
| 155 |
|
---|
| 156 | /**
|
---|
| 157 | * Gets the size of the heap.
|
---|
| 158 | *
|
---|
| 159 | * This size includes all the internal heap structures. So, even if the heap is
|
---|
| 160 | * empty the RTHeapSimpleGetFreeSize() will never reach the heap size returned
|
---|
| 161 | * by this function.
|
---|
| 162 | *
|
---|
| 163 | * @returns The heap size.
|
---|
| 164 | * @returns 0 if heap was safely detected as being bad.
|
---|
| 165 | * @param Heap The heap.
|
---|
| 166 | */
|
---|
| 167 | RTDECL(size_t) RTHeapSimpleGetHeapSize(RTHEAPSIMPLE Heap);
|
---|
| 168 |
|
---|
| 169 | /**
|
---|
| 170 | * Returns the sum of all free heap blocks.
|
---|
| 171 | *
|
---|
| 172 | * This is the amount of memory you can theoretically allocate
|
---|
| 173 | * if you do allocations exactly matching the free blocks.
|
---|
| 174 | *
|
---|
| 175 | * @returns The size of the free blocks.
|
---|
| 176 | * @returns 0 if heap was safely detected as being bad.
|
---|
| 177 | * @param Heap The heap.
|
---|
| 178 | */
|
---|
| 179 | RTDECL(size_t) RTHeapSimpleGetFreeSize(RTHEAPSIMPLE Heap);
|
---|
| 180 |
|
---|
| 181 | /**
|
---|
| 182 | * Printf like callbaclk function for RTHeapSimpleDump.
|
---|
| 183 | * @param pszFormat IPRT format string.
|
---|
| 184 | * @param ... Format arguments.
|
---|
| 185 | */
|
---|
[57004] | 186 | typedef DECLCALLBACK(void) FNRTHEAPSIMPLEPRINTF(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
|
---|
[288] | 187 | /** Pointer to a FNRTHEAPSIMPLEPRINTF function. */
|
---|
| 188 | typedef FNRTHEAPSIMPLEPRINTF *PFNRTHEAPSIMPLEPRINTF;
|
---|
| 189 |
|
---|
| 190 | /**
|
---|
| 191 | * Dumps the hypervisor heap.
|
---|
| 192 | *
|
---|
[283] | 193 | * @param Heap The heap handle.
|
---|
[288] | 194 | * @param pfnPrintf Printf like function that groks IPRT formatting.
|
---|
[283] | 195 | */
|
---|
[288] | 196 | RTDECL(void) RTHeapSimpleDump(RTHEAPSIMPLE Heap, PFNRTHEAPSIMPLEPRINTF pfnPrintf);
|
---|
[283] | 197 |
|
---|
[25055] | 198 | /** @} */
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 |
|
---|
| 202 | /** @defgroup grp_rt_heap_offset RTHeapOffset - Offset Based Heap
|
---|
| 203 | *
|
---|
| 204 | * This is a variation on the simple heap that doesn't use pointers internally
|
---|
| 205 | * and therefore can be saved and restored without any extra effort.
|
---|
| 206 | *
|
---|
| 207 | * @{
|
---|
| 208 | */
|
---|
| 209 |
|
---|
| 210 | /**
|
---|
| 211 | * Initializes the heap.
|
---|
| 212 | *
|
---|
| 213 | * @returns IPRT status code.
|
---|
| 214 | * @param phHeap Where to store the heap anchor block on success.
|
---|
| 215 | * @param pvMemory Pointer to the heap memory.
|
---|
| 216 | * @param cbMemory The size of the heap memory.
|
---|
| 217 | */
|
---|
| 218 | RTDECL(int) RTHeapOffsetInit(PRTHEAPOFFSET phHeap, void *pvMemory, size_t cbMemory);
|
---|
| 219 |
|
---|
| 220 | /**
|
---|
| 221 | * Merge two simple heaps into one.
|
---|
| 222 | *
|
---|
| 223 | * The requirement is of course that they next two each other memory wise.
|
---|
| 224 | *
|
---|
| 225 | * @returns IPRT status code.
|
---|
| 226 | * @param phHeap Where to store the handle to the merged heap on success.
|
---|
| 227 | * @param hHeap1 Handle to the first heap.
|
---|
| 228 | * @param hHeap2 Handle to the second heap.
|
---|
| 229 | * @remark This API isn't implemented yet.
|
---|
| 230 | */
|
---|
| 231 | RTDECL(int) RTHeapOffsetMerge(PRTHEAPOFFSET phHeap, RTHEAPOFFSET hHeap1, RTHEAPOFFSET hHeap2);
|
---|
| 232 |
|
---|
| 233 | /**
|
---|
| 234 | * Allocates memory from the specified simple heap.
|
---|
| 235 | *
|
---|
| 236 | * @returns Pointer to the allocated memory block on success.
|
---|
| 237 | * @returns NULL if the request cannot be satisfied. (A VERR_NO_MEMORY condition.)
|
---|
| 238 | *
|
---|
| 239 | * @param hHeap The heap to allocate the memory on.
|
---|
| 240 | * @param cb The requested heap block size.
|
---|
| 241 | * @param cbAlignment The requested heap block alignment. Pass 0 for default alignment.
|
---|
| 242 | * Must be a power of 2.
|
---|
| 243 | */
|
---|
| 244 | RTDECL(void *) RTHeapOffsetAlloc(RTHEAPOFFSET hHeap, size_t cb, size_t cbAlignment);
|
---|
| 245 |
|
---|
| 246 | /**
|
---|
| 247 | * Allocates zeroed memory from the specified simple heap.
|
---|
| 248 | *
|
---|
| 249 | * @returns Pointer to the allocated memory block on success.
|
---|
| 250 | * @returns NULL if the request cannot be satisfied. (A VERR_NO_MEMORY condition.)
|
---|
| 251 | *
|
---|
| 252 | * @param hHeap The heap to allocate the memory on.
|
---|
| 253 | * @param cb The requested heap block size.
|
---|
| 254 | * @param cbAlignment The requested heap block alignment. Pass 0 for default
|
---|
| 255 | * alignment. Must be a power of 2.
|
---|
| 256 | */
|
---|
| 257 | RTDECL(void *) RTHeapOffsetAllocZ(RTHEAPOFFSET hHeap, size_t cb, size_t cbAlignment);
|
---|
| 258 |
|
---|
| 259 | /**
|
---|
| 260 | * Reallocates / Allocates / Frees a heap block.
|
---|
| 261 | *
|
---|
[25059] | 262 | * @param hHeap The heap handle. This is optional and will only be used
|
---|
| 263 | * for strict assertions.
|
---|
[25055] | 264 | * @param pv The heap block returned by RTHeapOffset. If NULL it
|
---|
| 265 | * behaves like RTHeapOffsetAlloc().
|
---|
| 266 | * @param cbNew The new size of the heap block. If NULL it behaves like
|
---|
| 267 | * RTHeapOffsetFree().
|
---|
| 268 | * @param cbAlignment The requested heap block alignment. Pass 0 for default
|
---|
| 269 | * alignment. Must be a power of 2.
|
---|
| 270 | * @remark This API isn't implemented yet.
|
---|
| 271 | */
|
---|
| 272 | RTDECL(void *) RTHeapOffsetRealloc(RTHEAPOFFSET hHeap, void *pv, size_t cbNew, size_t cbAlignment);
|
---|
| 273 |
|
---|
| 274 | /**
|
---|
| 275 | * Reallocates / Allocates / Frees a heap block, zeroing any new bits.
|
---|
| 276 | *
|
---|
[25059] | 277 | * @param hHeap The heap handle. This is optional and will only be used
|
---|
| 278 | * for strict assertions.
|
---|
[25055] | 279 | * @param pv The heap block returned by RTHeapOffset. If NULL it
|
---|
| 280 | * behaves like RTHeapOffsetAllocZ().
|
---|
| 281 | * @param cbNew The new size of the heap block. If NULL it behaves like
|
---|
| 282 | * RTHeapOffsetFree().
|
---|
| 283 | * @param cbAlignment The requested heap block alignment. Pass 0 for default
|
---|
| 284 | * alignment. Must be a power of 2.
|
---|
| 285 | * @remark This API isn't implemented yet.
|
---|
| 286 | */
|
---|
| 287 | RTDECL(void *) RTHeapOffsetReallocZ(RTHEAPOFFSET hHeap, void *pv, size_t cbNew, size_t cbAlignment);
|
---|
| 288 |
|
---|
| 289 | /**
|
---|
| 290 | * Frees memory allocated from a simple heap.
|
---|
| 291 | *
|
---|
[25059] | 292 | * @param hHeap The heap handle. This is optional and will only be used
|
---|
| 293 | * for strict assertions.
|
---|
[25055] | 294 | * @param pv The heap block returned by RTHeapOffset
|
---|
| 295 | */
|
---|
| 296 | RTDECL(void) RTHeapOffsetFree(RTHEAPOFFSET hHeap, void *pv);
|
---|
| 297 |
|
---|
| 298 | /**
|
---|
| 299 | * Gets the size of the specified heap block.
|
---|
| 300 | *
|
---|
| 301 | * @returns The actual size of the heap block.
|
---|
| 302 | * @returns 0 if \a pv is NULL or it doesn't point to a valid heap block. An
|
---|
| 303 | * invalid \a pv can also cause traps or trigger assertions.
|
---|
| 304 | *
|
---|
[25059] | 305 | * @param hHeap The heap handle. This is optional and will only be used
|
---|
| 306 | * for strict assertions.
|
---|
[25055] | 307 | * @param pv The heap block returned by RTHeapOffset
|
---|
| 308 | */
|
---|
| 309 | RTDECL(size_t) RTHeapOffsetSize(RTHEAPOFFSET hHeap, void *pv);
|
---|
| 310 |
|
---|
| 311 | /**
|
---|
| 312 | * Gets the size of the heap.
|
---|
| 313 | *
|
---|
| 314 | * This size includes all the internal heap structures. So, even if the heap is
|
---|
| 315 | * empty the RTHeapOffsetGetFreeSize() will never reach the heap size returned
|
---|
| 316 | * by this function.
|
---|
| 317 | *
|
---|
| 318 | * @returns The heap size.
|
---|
| 319 | * @returns 0 if heap was safely detected as being bad.
|
---|
| 320 | * @param hHeap The heap handle.
|
---|
| 321 | */
|
---|
| 322 | RTDECL(size_t) RTHeapOffsetGetHeapSize(RTHEAPOFFSET hHeap);
|
---|
| 323 |
|
---|
| 324 | /**
|
---|
| 325 | * Returns the sum of all free heap blocks.
|
---|
| 326 | *
|
---|
| 327 | * This is the amount of memory you can theoretically allocate
|
---|
| 328 | * if you do allocations exactly matching the free blocks.
|
---|
| 329 | *
|
---|
| 330 | * @returns The size of the free blocks.
|
---|
| 331 | * @returns 0 if heap was safely detected as being bad.
|
---|
| 332 | * @param hHeap The heap handle.
|
---|
| 333 | */
|
---|
| 334 | RTDECL(size_t) RTHeapOffsetGetFreeSize(RTHEAPOFFSET hHeap);
|
---|
| 335 |
|
---|
| 336 | /**
|
---|
| 337 | * Printf like callbaclk function for RTHeapOffsetDump.
|
---|
| 338 | * @param pszFormat IPRT format string.
|
---|
| 339 | * @param ... Format arguments.
|
---|
| 340 | */
|
---|
[57004] | 341 | typedef DECLCALLBACK(void) FNRTHEAPOFFSETPRINTF(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
|
---|
[25055] | 342 | /** Pointer to a FNRTHEAPOFFSETPRINTF function. */
|
---|
| 343 | typedef FNRTHEAPOFFSETPRINTF *PFNRTHEAPOFFSETPRINTF;
|
---|
| 344 |
|
---|
| 345 | /**
|
---|
| 346 | * Dumps the hypervisor heap.
|
---|
| 347 | *
|
---|
| 348 | * @param hHeap The heap handle.
|
---|
| 349 | * @param pfnPrintf Printf like function that groks IPRT formatting.
|
---|
| 350 | */
|
---|
| 351 | RTDECL(void) RTHeapOffsetDump(RTHEAPOFFSET hHeap, PFNRTHEAPOFFSETPRINTF pfnPrintf);
|
---|
| 352 |
|
---|
| 353 | /** @} */
|
---|
| 354 |
|
---|
| 355 | /** @} */
|
---|
[20374] | 356 | RT_C_DECLS_END
|
---|
[283] | 357 |
|
---|
[76585] | 358 | #endif /* !IPRT_INCLUDED_heap_h */
|
---|
[283] | 359 |
|
---|