[20360] | 1 | /** @file
|
---|
| 2 | * IPRT - Memory Allocation Pool.
|
---|
| 3 | */
|
---|
| 4 |
|
---|
| 5 | /*
|
---|
[98103] | 6 | * Copyright (C) 2009-2023 Oracle and/or its affiliates.
|
---|
[20360] | 7 | *
|
---|
[96407] | 8 | * This file is part of VirtualBox base platform packages, as
|
---|
| 9 | * available from https://www.virtualbox.org.
|
---|
[20360] | 10 | *
|
---|
[96407] | 11 | * This program is free software; you can redistribute it and/or
|
---|
| 12 | * modify it under the terms of the GNU General Public License
|
---|
| 13 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 14 | * License.
|
---|
| 15 | *
|
---|
| 16 | * This program is distributed in the hope that it will be useful, but
|
---|
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 19 | * General Public License for more details.
|
---|
| 20 | *
|
---|
| 21 | * You should have received a copy of the GNU General Public License
|
---|
| 22 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 23 | *
|
---|
[20360] | 24 | * The contents of this file may alternatively be used under the terms
|
---|
| 25 | * of the Common Development and Distribution License Version 1.0
|
---|
[96407] | 26 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
| 27 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
[20360] | 28 | * CDDL are applicable instead of those of the GPL.
|
---|
| 29 | *
|
---|
| 30 | * You may elect to license modified versions of this file under the
|
---|
| 31 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
[96407] | 32 | *
|
---|
| 33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
[20360] | 34 | */
|
---|
| 35 |
|
---|
[76557] | 36 | #ifndef IPRT_INCLUDED_mempool_h
|
---|
| 37 | #define IPRT_INCLUDED_mempool_h
|
---|
[76507] | 38 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 39 | # pragma once
|
---|
| 40 | #endif
|
---|
[20360] | 41 |
|
---|
| 42 | #include <iprt/types.h>
|
---|
| 43 |
|
---|
[20374] | 44 | RT_C_DECLS_BEGIN
|
---|
[20360] | 45 |
|
---|
| 46 | /**
|
---|
| 47 | * Creates a new memory pool.
|
---|
| 48 | *
|
---|
| 49 | * @returns IPRT status code.
|
---|
| 50 | *
|
---|
| 51 | * @param phMemPool Where to return the handle to the new memory
|
---|
| 52 | * pool.
|
---|
| 53 | * @param pszName The name of the pool (for debug purposes).
|
---|
| 54 | */
|
---|
| 55 | RTDECL(int) RTMemPoolCreate(PRTMEMPOOL phMemPool, const char *pszName);
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * Destroys the specified pool, freeing all the memory it contains.
|
---|
| 59 | *
|
---|
| 60 | * @returns IPRT status code.
|
---|
| 61 | *
|
---|
| 62 | * @param hMemPool The handle to the pool. The nil handle and
|
---|
[33540] | 63 | * RTMEMPOOL_DEFAULT are quietly ignored (retval
|
---|
[20360] | 64 | * VINF_SUCCESS).
|
---|
| 65 | */
|
---|
| 66 | RTDECL(int) RTMemPoolDestroy(RTMEMPOOL hMemPool);
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * Allocates memory.
|
---|
| 70 | *
|
---|
| 71 | * @returns Pointer to the allocated memory.
|
---|
| 72 | * @returns NULL on failure.
|
---|
| 73 | *
|
---|
| 74 | * @param hMemPool Handle to the pool to allocate the memory from.
|
---|
| 75 | * @param cb Size in bytes of the memory block to allocated.
|
---|
| 76 | */
|
---|
[57432] | 77 | RTDECL(void *) RTMemPoolAlloc(RTMEMPOOL hMemPool, size_t cb) RT_NO_THROW_PROTO;
|
---|
[20360] | 78 |
|
---|
| 79 | /**
|
---|
[33540] | 80 | * Allocates zero'd memory.
|
---|
[20360] | 81 | *
|
---|
[33540] | 82 | * Instead of memset(pv, 0, sizeof()) use this when you want zero'd
|
---|
[20360] | 83 | * memory. This keeps the code smaller and the heap can skip the memset
|
---|
| 84 | * in about 0.42% of calls :-).
|
---|
| 85 | *
|
---|
| 86 | * @returns Pointer to the allocated memory.
|
---|
| 87 | * @returns NULL on failure.
|
---|
| 88 | *
|
---|
| 89 | * @param hMemPool Handle to the pool to allocate the memory from.
|
---|
| 90 | * @param cb Size in bytes of the memory block to allocated.
|
---|
| 91 | */
|
---|
[57432] | 92 | RTDECL(void *) RTMemPoolAllocZ(RTMEMPOOL hMemPool, size_t cb) RT_NO_THROW_PROTO;
|
---|
[20360] | 93 |
|
---|
| 94 | /**
|
---|
| 95 | * Duplicates a chunk of memory into a new heap block.
|
---|
| 96 | *
|
---|
| 97 | * @returns New heap block with the duplicate data.
|
---|
| 98 | * @returns NULL if we're out of memory.
|
---|
| 99 | *
|
---|
| 100 | * @param hMemPool Handle to the pool to allocate the memory from.
|
---|
| 101 | * @param pvSrc The memory to duplicate.
|
---|
| 102 | * @param cb The amount of memory to duplicate.
|
---|
| 103 | */
|
---|
[57432] | 104 | RTDECL(void *) RTMemPoolDup(RTMEMPOOL hMemPool, const void *pvSrc, size_t cb) RT_NO_THROW_PROTO;
|
---|
[20360] | 105 |
|
---|
| 106 | /**
|
---|
| 107 | * Duplicates a chunk of memory into a new heap block with some
|
---|
| 108 | * additional zeroed memory.
|
---|
| 109 | *
|
---|
| 110 | * @returns New heap block with the duplicate data.
|
---|
| 111 | * @returns NULL if we're out of memory.
|
---|
| 112 | *
|
---|
| 113 | * @param hMemPool Handle to the pool to allocate the memory from.
|
---|
| 114 | * @param pvSrc The memory to duplicate.
|
---|
| 115 | * @param cbSrc The amount of memory to duplicate.
|
---|
| 116 | * @param cbExtra The amount of extra memory to allocate and zero.
|
---|
| 117 | */
|
---|
[57432] | 118 | RTDECL(void *) RTMemPoolDupEx(RTMEMPOOL hMemPool, const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW_PROTO;
|
---|
[20360] | 119 |
|
---|
| 120 | /**
|
---|
| 121 | * Reallocates memory.
|
---|
| 122 | *
|
---|
| 123 | * @returns Pointer to the allocated memory.
|
---|
| 124 | * @returns NULL on failure.
|
---|
| 125 | *
|
---|
| 126 | * @param hMemPool Handle to the pool containing the old memory.
|
---|
| 127 | * @param pvOld The memory block to reallocate.
|
---|
| 128 | * @param cbNew The new block size (in bytes).
|
---|
| 129 | */
|
---|
[57432] | 130 | RTDECL(void *) RTMemPoolRealloc(RTMEMPOOL hMemPool, void *pvOld, size_t cbNew) RT_NO_THROW_PROTO;
|
---|
[20360] | 131 |
|
---|
| 132 | /**
|
---|
| 133 | * Frees memory allocated from a pool.
|
---|
| 134 | *
|
---|
[27787] | 135 | * @param hMemPool Handle to the pool containing the memory. Passing
|
---|
[20360] | 136 | * NIL here is fine, but it may come at a slight
|
---|
| 137 | * performance cost.
|
---|
| 138 | * @param pv Pointer to memory block.
|
---|
| 139 | *
|
---|
[33540] | 140 | * @remarks This is the same a RTMemPoolRelease but included here as a separate
|
---|
[20360] | 141 | * function to simplify code migration.
|
---|
| 142 | */
|
---|
[57432] | 143 | RTDECL(void) RTMemPoolFree(RTMEMPOOL hMemPool, void *pv) RT_NO_THROW_PROTO;
|
---|
[20360] | 144 |
|
---|
| 145 | /**
|
---|
[33540] | 146 | * Retains a reference to a memory block in a pool.
|
---|
[20360] | 147 | *
|
---|
| 148 | * @returns New reference count, UINT32_MAX on error (asserted).
|
---|
| 149 | *
|
---|
| 150 | * @param pv Pointer to memory block.
|
---|
| 151 | */
|
---|
[57432] | 152 | RTDECL(uint32_t) RTMemPoolRetain(void *pv) RT_NO_THROW_PROTO;
|
---|
[20360] | 153 |
|
---|
| 154 | /**
|
---|
| 155 | * Releases a reference to a memory block in a pool.
|
---|
| 156 | *
|
---|
[27787] | 157 | * @returns New reference count, UINT32_MAX on error (asserted).
|
---|
| 158 | *
|
---|
| 159 | * @param hMemPool Handle to the pool containing the memory. Passing
|
---|
[20360] | 160 | * NIL here is fine, but it may come at a slight
|
---|
| 161 | * performance cost.
|
---|
| 162 | * @param pv Pointer to memory block.
|
---|
| 163 | */
|
---|
[57432] | 164 | RTDECL(uint32_t) RTMemPoolRelease(RTMEMPOOL hMemPool, void *pv) RT_NO_THROW_PROTO;
|
---|
[20360] | 165 |
|
---|
[27787] | 166 | /**
|
---|
| 167 | * Get the current reference count.
|
---|
| 168 | *
|
---|
| 169 | * @returns The reference count, UINT32_MAX on error (asserted).
|
---|
| 170 | * @param pv Pointer to memory block.
|
---|
| 171 | */
|
---|
[57432] | 172 | RTDECL(uint32_t) RTMemPoolRefCount(void *pv) RT_NO_THROW_PROTO;
|
---|
[20360] | 173 |
|
---|
[27787] | 174 |
|
---|
[20374] | 175 | RT_C_DECLS_END
|
---|
[20360] | 176 |
|
---|
[76585] | 177 | #endif /* !IPRT_INCLUDED_mempool_h */
|
---|
[20360] | 178 |
|
---|