Changeset 25055 in vbox
- Timestamp:
- Nov 27, 2009 3:45:49 PM (15 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
-
include/iprt/heap.h (modified) (5 diffs)
-
include/iprt/types.h (modified) (1 diff)
-
src/VBox/Runtime/common/alloc/heapsimple.cpp (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/heap.h
r22273 r25055 1 1 /** @file 2 * IPRT - A Simple Heap.2 * IPRT - Heap Implementations 3 3 */ 4 4 5 5 /* 6 * Copyright (C) 2006-200 7Sun Microsystems, Inc.6 * Copyright (C) 2006-2009 Sun Microsystems, Inc. 7 7 * 8 8 * This file is part of VirtualBox Open Source Edition (OSE), as … … 36 36 RT_C_DECLS_BEGIN 37 37 38 /** @defgroup grp_rt_heap RTHeap - Heap Implementations 39 * @ingroup grp_rt 40 * @{ 41 */ 42 43 44 /** @defgroup grp_rt_heap_simple RTHeapSimple - Simple Heap 45 * @{ 46 */ 47 38 48 /** 39 49 * Initializes the heap. … … 59 69 RTDECL(int) RTHeapSimpleMerge(PRTHEAPSIMPLE pHeap, RTHEAPSIMPLE Heap1, RTHEAPSIMPLE Heap2); 60 70 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. 71 /** 72 * Relocater the heap internal structures after copying it to a new location. 73 * 74 * This can be used when loading a saved heap. 75 * 76 * @returns IPRT status code. 67 77 * @param hHeap Heap handle that has already been adjusted by to the new 68 78 * location. That is to say, when calling … … 71 81 * must be used when calcuating the handle value for the 72 82 * 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 83 * @param offDelta The delta between the new and old location, i.e. what 74 84 * should be added to the internal pointers. 75 85 */ 76 86 RTDECL(int) RTHeapSimpleRelocate(RTHEAPSIMPLE hHeap, uintptr_t offDelta); 77 87 78 88 /** 79 89 * Allocates memory from the specified simple heap. … … 187 197 RTDECL(void) RTHeapSimpleDump(RTHEAPSIMPLE Heap, PFNRTHEAPSIMPLEPRINTF pfnPrintf); 188 198 199 /** @} */ 200 201 202 203 /** @defgroup grp_rt_heap_offset RTHeapOffset - Offset Based Heap 204 * 205 * This is a variation on the simple heap that doesn't use pointers internally 206 * and therefore can be saved and restored without any extra effort. 207 * 208 * @{ 209 */ 210 211 /** 212 * Initializes the heap. 213 * 214 * @returns IPRT status code. 215 * @param phHeap Where to store the heap anchor block on success. 216 * @param pvMemory Pointer to the heap memory. 217 * @param cbMemory The size of the heap memory. 218 */ 219 RTDECL(int) RTHeapOffsetInit(PRTHEAPOFFSET phHeap, void *pvMemory, size_t cbMemory); 220 221 /** 222 * Merge two simple heaps into one. 223 * 224 * The requirement is of course that they next two each other memory wise. 225 * 226 * @returns IPRT status code. 227 * @param phHeap Where to store the handle to the merged heap on success. 228 * @param hHeap1 Handle to the first heap. 229 * @param hHeap2 Handle to the second heap. 230 * @remark This API isn't implemented yet. 231 */ 232 RTDECL(int) RTHeapOffsetMerge(PRTHEAPOFFSET phHeap, RTHEAPOFFSET hHeap1, RTHEAPOFFSET hHeap2); 233 234 /** 235 * Allocates memory from the specified simple heap. 236 * 237 * @returns Pointer to the allocated memory block on success. 238 * @returns NULL if the request cannot be satisfied. (A VERR_NO_MEMORY condition.) 239 * 240 * @param hHeap The heap to allocate the memory on. 241 * @param cb The requested heap block size. 242 * @param cbAlignment The requested heap block alignment. Pass 0 for default alignment. 243 * Must be a power of 2. 244 */ 245 RTDECL(void *) RTHeapOffsetAlloc(RTHEAPOFFSET hHeap, size_t cb, size_t cbAlignment); 246 247 /** 248 * Allocates zeroed memory from the specified simple heap. 249 * 250 * @returns Pointer to the allocated memory block on success. 251 * @returns NULL if the request cannot be satisfied. (A VERR_NO_MEMORY condition.) 252 * 253 * @param hHeap The heap to allocate the memory on. 254 * @param cb The requested heap block size. 255 * @param cbAlignment The requested heap block alignment. Pass 0 for default 256 * alignment. Must be a power of 2. 257 */ 258 RTDECL(void *) RTHeapOffsetAllocZ(RTHEAPOFFSET hHeap, size_t cb, size_t cbAlignment); 259 260 /** 261 * Reallocates / Allocates / Frees a heap block. 262 * 263 * @param hHeap The heap, mandatory. 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 * 277 * @param hHeap The heap, mandatory. 278 * @param pv The heap block returned by RTHeapOffset. If NULL it 279 * behaves like RTHeapOffsetAllocZ(). 280 * @param cbNew The new size of the heap block. If NULL it behaves like 281 * RTHeapOffsetFree(). 282 * @param cbAlignment The requested heap block alignment. Pass 0 for default 283 * alignment. Must be a power of 2. 284 * @remark This API isn't implemented yet. 285 */ 286 RTDECL(void *) RTHeapOffsetReallocZ(RTHEAPOFFSET hHeap, void *pv, size_t cbNew, size_t cbAlignment); 287 288 /** 289 * Frees memory allocated from a simple heap. 290 * 291 * @param hHeap The heap handle, mandatory. 292 * @param pv The heap block returned by RTHeapOffset 293 */ 294 RTDECL(void) RTHeapOffsetFree(RTHEAPOFFSET hHeap, void *pv); 295 296 /** 297 * Gets the size of the specified heap block. 298 * 299 * @returns The actual size of the heap block. 300 * @returns 0 if \a pv is NULL or it doesn't point to a valid heap block. An 301 * invalid \a pv can also cause traps or trigger assertions. 302 * 303 * @param hHeap The heap handle, mandatory. 304 * @param pv The heap block returned by RTHeapOffset 305 */ 306 RTDECL(size_t) RTHeapOffsetSize(RTHEAPOFFSET hHeap, void *pv); 307 308 /** 309 * Gets the size of the heap. 310 * 311 * This size includes all the internal heap structures. So, even if the heap is 312 * empty the RTHeapOffsetGetFreeSize() will never reach the heap size returned 313 * by this function. 314 * 315 * @returns The heap size. 316 * @returns 0 if heap was safely detected as being bad. 317 * @param hHeap The heap handle. 318 */ 319 RTDECL(size_t) RTHeapOffsetGetHeapSize(RTHEAPOFFSET hHeap); 320 321 /** 322 * Returns the sum of all free heap blocks. 323 * 324 * This is the amount of memory you can theoretically allocate 325 * if you do allocations exactly matching the free blocks. 326 * 327 * @returns The size of the free blocks. 328 * @returns 0 if heap was safely detected as being bad. 329 * @param hHeap The heap handle. 330 */ 331 RTDECL(size_t) RTHeapOffsetGetFreeSize(RTHEAPOFFSET hHeap); 332 333 /** 334 * Printf like callbaclk function for RTHeapOffsetDump. 335 * @param pszFormat IPRT format string. 336 * @param ... Format arguments. 337 */ 338 typedef DECLCALLBACK(void) FNRTHEAPOFFSETPRINTF(const char *pszFormat, ...); 339 /** Pointer to a FNRTHEAPOFFSETPRINTF function. */ 340 typedef FNRTHEAPOFFSETPRINTF *PFNRTHEAPOFFSETPRINTF; 341 342 /** 343 * Dumps the hypervisor heap. 344 * 345 * @param hHeap The heap handle. 346 * @param pfnPrintf Printf like function that groks IPRT formatting. 347 */ 348 RTDECL(void) RTHeapOffsetDump(RTHEAPOFFSET hHeap, PFNRTHEAPOFFSETPRINTF pfnPrintf); 349 350 /** @} */ 351 352 /** @} */ 189 353 RT_C_DECLS_END 190 354 -
trunk/include/iprt/types.h
r23525 r25055 1237 1237 #define NIL_RTHEAPSIMPLE ((RTHEAPSIMPLE)0) 1238 1238 1239 /** Handle to a offset based heap. */ 1240 typedef R3R0PTRTYPE(struct RTHEAPOFFSETINTERNAL *) RTHEAPOFFSET; 1241 /** Pointer to a handle to a offset based heap. */ 1242 typedef RTHEAPOFFSET *PRTHEAPOFFSET; 1243 /** NIL offset based heap handle. */ 1244 #define NIL_RTHEAPOFFSET ((RTHEAPOFFSET)0) 1245 1239 1246 /** Handle to an environment block. */ 1240 1247 typedef R3PTRTYPE(struct RTENVINTERNAL *) RTENV; -
trunk/src/VBox/Runtime/common/alloc/heapsimple.cpp
r22281 r25055 282 282 283 283 284 /** 285 * Initializes the heap. 286 * 287 * @returns IPRT status code. 288 * @param pHeap Where to store the heap anchor block on success. 289 * @param pvMemory Pointer to the heap memory. 290 * @param cbMemory The size of the heap memory. 291 */ 292 RTDECL(int) RTHeapSimpleInit(PRTHEAPSIMPLE pHeap, void *pvMemory, size_t cbMemory) 284 RTDECL(int) RTHeapSimpleInit(PRTHEAPSIMPLE phHeap, void *pvMemory, size_t cbMemory) 293 285 { 294 286 PRTHEAPSIMPLEINTERNAL pHeapInt; … … 338 330 pFree->cb = pHeapInt->cbFree; 339 331 340 *p Heap = pHeapInt;332 *phHeap = pHeapInt; 341 333 342 334 #ifdef RTHEAPSIMPLE_STRICT … … 348 340 349 341 350 /**351 * Relocater the heap internal structures after copying it to a new location.352 *353 * This can be used when loading a saved heap.354 *355 * @returns IPRT status code.356 * @param hHeap Heap handle that has already been adjusted by to the new357 * location. That is to say, when calling358 * RTHeapSimpleInit, the caller must note the offset of the359 * returned heap handle into the heap memory. This offset360 * must be used when calcuating the handle value for the361 * new location. The offset may in some cases not be zero!362 * @param offDelta The delta between the new and old location, i.e. what363 * should be added to the internal pointers.364 */365 342 RTDECL(int) RTHeapSimpleRelocate(RTHEAPSIMPLE hHeap, uintptr_t offDelta) 366 343 { … … 373 350 AssertPtrReturn(pHeapInt, VERR_INVALID_HANDLE); 374 351 AssertReturn(pHeapInt->uMagic == RTHEAPSIMPLE_MAGIC, VERR_INVALID_HANDLE); 375 AssertMsgReturn((uintptr_t)pHeapInt - (uintptr_t)pHeapInt->pvEnd + pHeapInt->cbHeap == offDelta, 376 ("offDelta=%p, expected=%p\n", offDelta, (uintptr_t)pHeapInt->pvEnd - pHeapInt->cbHeap - (uintptr_t)pHeapInt), 352 AssertMsgReturn((uintptr_t)pHeapInt - (uintptr_t)pHeapInt->pvEnd + pHeapInt->cbHeap == offDelta, 353 ("offDelta=%p, expected=%p\n", offDelta, (uintptr_t)pHeapInt->pvEnd - pHeapInt->cbHeap - (uintptr_t)pHeapInt), 377 354 VERR_INVALID_PARAMETER); 378 355 … … 388 365 * Walk the heap blocks. 389 366 */ 390 for (pCur = (PRTHEAPSIMPLEFREE)(pHeapInt + 1); 391 pCur && (uintptr_t)pCur < (uintptr_t)pHeapInt->pvEnd; 367 for (pCur = (PRTHEAPSIMPLEFREE)(pHeapInt + 1); 368 pCur && (uintptr_t)pCur < (uintptr_t)pHeapInt->pvEnd; 392 369 pCur = (PRTHEAPSIMPLEFREE)pCur->Core.pNext) 393 370 { … … 409 386 rtHeapSimpleAssertAll(pHeapInt); 410 387 #endif 411 return VINF_SUCCESS; 388 return VINF_SUCCESS; 412 389 } 413 390 RT_EXPORT_SYMBOL(RTHeapSimpleRelocate); 414 391 415 392 416 417 /** 418 * Allocates memory from the specified simple heap. 419 * 420 * @returns Pointer to the allocated memory block on success. 421 * @returns NULL if the request cannot be satisfied. (A VERR_NO_MEMORY condition.) 422 * 423 * @param Heap The heap to allocate the memory on. 424 * @param cb The requested heap block size. 425 * @param cbAlignment The requested heap block alignment. Pass 0 for default alignment. 426 * Must be a power of 2. 427 */ 428 RTDECL(void *) RTHeapSimpleAlloc(RTHEAPSIMPLE Heap, size_t cb, size_t cbAlignment) 429 { 430 PRTHEAPSIMPLEINTERNAL pHeapInt = Heap; 393 RTDECL(void *) RTHeapSimpleAlloc(RTHEAPSIMPLE hHeap, size_t cb, size_t cbAlignment) 394 { 395 PRTHEAPSIMPLEINTERNAL pHeapInt = hHeap; 431 396 PRTHEAPSIMPLEBLOCK pBlock; 432 397 … … 463 428 464 429 465 /** 466 * Allocates zeroed memory from the specified simple heap. 467 * 468 * @returns Pointer to the allocated memory block on success. 469 * @returns NULL if the request cannot be satisfied. (A VERR_NO_MEMORY condition.) 470 * 471 * @param Heap The heap to allocate the memory on. 472 * @param cb The requested heap block size. 473 * @param cbAlignment The requested heap block alignment. Pass 0 for default alignment. 474 * Must be a power of 2. 475 */ 476 RTDECL(void *) RTHeapSimpleAllocZ(RTHEAPSIMPLE Heap, size_t cb, size_t cbAlignment) 477 { 478 PRTHEAPSIMPLEINTERNAL pHeapInt = Heap; 430 RTDECL(void *) RTHeapSimpleAllocZ(RTHEAPSIMPLE hHeap, size_t cb, size_t cbAlignment) 431 { 432 PRTHEAPSIMPLEINTERNAL pHeapInt = hHeap; 479 433 PRTHEAPSIMPLEBLOCK pBlock; 480 434 … … 519 473 * @returns Pointer to the allocated block. 520 474 * @returns NULL on failure. 475 * 521 476 * @param pHeapInt The heap. 522 477 * @param cb Size of the memory block to allocate. … … 686 641 687 642 688 689 690 /** 691 * Frees memory allocated from a simple heap. 692 * 693 * @param Heap The heap. This is optional and will only be used for strict assertions. 694 * @param pv The heap block returned by RTHeapSimple 695 */ 696 RTDECL(void) RTHeapSimpleFree(RTHEAPSIMPLE Heap, void *pv) 643 RTDECL(void) RTHeapSimpleFree(RTHEAPSIMPLE hHeap, void *pv) 697 644 { 698 645 PRTHEAPSIMPLEINTERNAL pHeapInt; … … 714 661 ASSERT_BLOCK_USED(pHeapInt, pBlock); 715 662 ASSERT_ANCHOR(pHeapInt); 716 Assert(pHeapInt == (PRTHEAPSIMPLEINTERNAL) Heap || !Heap);663 Assert(pHeapInt == (PRTHEAPSIMPLEINTERNAL)hHeap || !hHeap); 717 664 718 665 #ifdef RTHEAPSIMPLE_FREE_POISON … … 887 834 888 835 889 /** 890 * Gets the size of the specified heap block. 891 * 892 * @returns The actual size of the heap block. 893 * @returns 0 if \a pv is NULL or it doesn't point to a valid heap block. An invalid \a pv 894 * can also cause traps or trigger assertions. 895 * @param Heap The heap. This is optional and will only be used for strict assertions. 896 * @param pv The heap block returned by RTHeapSimple 897 */ 898 RTDECL(size_t) RTHeapSimpleSize(RTHEAPSIMPLE Heap, void *pv) 836 RTDECL(size_t) RTHeapSimpleSize(RTHEAPSIMPLE hHeap, void *pv) 899 837 { 900 838 PRTHEAPSIMPLEINTERNAL pHeapInt; … … 917 855 ASSERT_BLOCK_USED(pHeapInt, pBlock); 918 856 ASSERT_ANCHOR(pHeapInt); 919 Assert(pHeapInt == (PRTHEAPSIMPLEINTERNAL) Heap || !Heap);857 Assert(pHeapInt == (PRTHEAPSIMPLEINTERNAL)hHeap || !hHeap); 920 858 921 859 /* … … 929 867 930 868 931 /** 932 * Gets the size of the heap. 933 * 934 * This size includes all the internal heap structures. So, even if the heap is 935 * empty the RTHeapSimpleGetFreeSize() will never reach the heap size returned 936 * by this function. 937 * 938 * @returns The heap size. 939 * @returns 0 if heap was safely detected as being bad. 940 * @param Heap The heap. 941 */ 942 RTDECL(size_t) RTHeapSimpleGetHeapSize(RTHEAPSIMPLE Heap) 869 RTDECL(size_t) RTHeapSimpleGetHeapSize(RTHEAPSIMPLE hHeap) 943 870 { 944 871 PRTHEAPSIMPLEINTERNAL pHeapInt; 945 872 946 if ( Heap == NIL_RTHEAPSIMPLE)873 if (hHeap == NIL_RTHEAPSIMPLE) 947 874 return 0; 948 875 949 pHeapInt = Heap;876 pHeapInt = hHeap; 950 877 AssertPtrReturn(pHeapInt, 0); 951 878 ASSERT_ANCHOR(pHeapInt); … … 955 882 956 883 957 /** 958 * Returns the sum of all free heap blocks. 959 * 960 * This is the amount of memory you can theoretically allocate 961 * if you do allocations exactly matching the free blocks. 962 * 963 * @returns The size of the free blocks. 964 * @returns 0 if heap was safely detected as being bad. 965 * @param Heap The heap. 966 */ 967 RTDECL(size_t) RTHeapSimpleGetFreeSize(RTHEAPSIMPLE Heap) 884 RTDECL(size_t) RTHeapSimpleGetFreeSize(RTHEAPSIMPLE hHeap) 968 885 { 969 886 PRTHEAPSIMPLEINTERNAL pHeapInt; 970 887 971 if ( Heap == NIL_RTHEAPSIMPLE)888 if (hHeap == NIL_RTHEAPSIMPLE) 972 889 return 0; 973 890 974 pHeapInt = Heap;891 pHeapInt = hHeap; 975 892 AssertPtrReturn(pHeapInt, 0); 976 893 ASSERT_ANCHOR(pHeapInt); … … 980 897 981 898 982 /** 983 * Dumps the hypervisor heap. 984 * 985 * @param Heap The heap handle. 986 * @param pfnPrintf Printf like function that groks IPRT formatting. 987 */ 988 RTDECL(void) RTHeapSimpleDump(RTHEAPSIMPLE Heap, PFNRTHEAPSIMPLEPRINTF pfnPrintf) 989 { 990 PRTHEAPSIMPLEINTERNAL pHeapInt = (PRTHEAPSIMPLEINTERNAL)Heap; 899 RTDECL(void) RTHeapSimpleDump(RTHEAPSIMPLE hHeap, PFNRTHEAPSIMPLEPRINTF pfnPrintf) 900 { 901 PRTHEAPSIMPLEINTERNAL pHeapInt = (PRTHEAPSIMPLEINTERNAL)hHeap; 991 902 PRTHEAPSIMPLEFREE pBlock; 992 903 993 904 pfnPrintf("**** Dumping Heap %p - cbHeap=%zx cbFree=%zx ****\n", 994 Heap, pHeapInt->cbHeap, pHeapInt->cbFree);905 hHeap, pHeapInt->cbHeap, pHeapInt->cbFree); 995 906 996 907 for (pBlock = (PRTHEAPSIMPLEFREE)(pHeapInt + 1); … … 1008 919 pBlock, (uintptr_t)pBlock - (uintptr_t)(pHeapInt + 1), pBlock->Core.pNext, pBlock->Core.pPrev, pBlock->Core.fFlags, cb); 1009 920 } 1010 pfnPrintf("**** Done dumping Heap %p ****\n", Heap);921 pfnPrintf("**** Done dumping Heap %p ****\n", hHeap); 1011 922 } 1012 923 RT_EXPORT_SYMBOL(RTHeapSimpleDump);
Note:
See TracChangeset
for help on using the changeset viewer.

