1 | /** @file
|
---|
2 | * IPRT - Memory Allocation Pool.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2009-2024 Oracle and/or its affiliates.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox base platform packages, as
|
---|
9 | * available from https://www.virtualbox.org.
|
---|
10 | *
|
---|
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 | *
|
---|
24 | * The contents of this file may alternatively be used under the terms
|
---|
25 | * of the Common Development and Distribution License Version 1.0
|
---|
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
|
---|
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.
|
---|
32 | *
|
---|
33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef IPRT_INCLUDED_mempool_h
|
---|
37 | #define IPRT_INCLUDED_mempool_h
|
---|
38 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
39 | # pragma once
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #include <iprt/types.h>
|
---|
43 |
|
---|
44 | RT_C_DECLS_BEGIN
|
---|
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
|
---|
63 | * RTMEMPOOL_DEFAULT are quietly ignored (retval
|
---|
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 | */
|
---|
77 | RTDECL(void *) RTMemPoolAlloc(RTMEMPOOL hMemPool, size_t cb) RT_NO_THROW_PROTO;
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Allocates zero'd memory.
|
---|
81 | *
|
---|
82 | * Instead of memset(pv, 0, sizeof()) use this when you want zero'd
|
---|
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 | */
|
---|
92 | RTDECL(void *) RTMemPoolAllocZ(RTMEMPOOL hMemPool, size_t cb) RT_NO_THROW_PROTO;
|
---|
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 | */
|
---|
104 | RTDECL(void *) RTMemPoolDup(RTMEMPOOL hMemPool, const void *pvSrc, size_t cb) RT_NO_THROW_PROTO;
|
---|
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 | */
|
---|
118 | RTDECL(void *) RTMemPoolDupEx(RTMEMPOOL hMemPool, const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW_PROTO;
|
---|
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 | */
|
---|
130 | RTDECL(void *) RTMemPoolRealloc(RTMEMPOOL hMemPool, void *pvOld, size_t cbNew) RT_NO_THROW_PROTO;
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Frees memory allocated from a pool.
|
---|
134 | *
|
---|
135 | * @param hMemPool Handle to the pool containing the memory. Passing
|
---|
136 | * NIL here is fine, but it may come at a slight
|
---|
137 | * performance cost.
|
---|
138 | * @param pv Pointer to memory block.
|
---|
139 | *
|
---|
140 | * @remarks This is the same a RTMemPoolRelease but included here as a separate
|
---|
141 | * function to simplify code migration.
|
---|
142 | */
|
---|
143 | RTDECL(void) RTMemPoolFree(RTMEMPOOL hMemPool, void *pv) RT_NO_THROW_PROTO;
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Retains a reference to a memory block in a pool.
|
---|
147 | *
|
---|
148 | * @returns New reference count, UINT32_MAX on error (asserted).
|
---|
149 | *
|
---|
150 | * @param pv Pointer to memory block.
|
---|
151 | */
|
---|
152 | RTDECL(uint32_t) RTMemPoolRetain(void *pv) RT_NO_THROW_PROTO;
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Releases a reference to a memory block in a pool.
|
---|
156 | *
|
---|
157 | * @returns New reference count, UINT32_MAX on error (asserted).
|
---|
158 | *
|
---|
159 | * @param hMemPool Handle to the pool containing the memory. Passing
|
---|
160 | * NIL here is fine, but it may come at a slight
|
---|
161 | * performance cost.
|
---|
162 | * @param pv Pointer to memory block.
|
---|
163 | */
|
---|
164 | RTDECL(uint32_t) RTMemPoolRelease(RTMEMPOOL hMemPool, void *pv) RT_NO_THROW_PROTO;
|
---|
165 |
|
---|
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 | */
|
---|
172 | RTDECL(uint32_t) RTMemPoolRefCount(void *pv) RT_NO_THROW_PROTO;
|
---|
173 |
|
---|
174 |
|
---|
175 | RT_C_DECLS_END
|
---|
176 |
|
---|
177 | #endif /* !IPRT_INCLUDED_mempool_h */
|
---|
178 |
|
---|