1 | /** @file
|
---|
2 | * IPRT - Memory Allocation Pool.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2009-2022 Oracle Corporation
|
---|
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 |
|
---|
26 | #ifndef IPRT_INCLUDED_mempool_h
|
---|
27 | #define IPRT_INCLUDED_mempool_h
|
---|
28 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
29 | # pragma once
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #include <iprt/types.h>
|
---|
33 |
|
---|
34 | RT_C_DECLS_BEGIN
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Creates a new memory pool.
|
---|
38 | *
|
---|
39 | * @returns IPRT status code.
|
---|
40 | *
|
---|
41 | * @param phMemPool Where to return the handle to the new memory
|
---|
42 | * pool.
|
---|
43 | * @param pszName The name of the pool (for debug purposes).
|
---|
44 | */
|
---|
45 | RTDECL(int) RTMemPoolCreate(PRTMEMPOOL phMemPool, const char *pszName);
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Destroys the specified pool, freeing all the memory it contains.
|
---|
49 | *
|
---|
50 | * @returns IPRT status code.
|
---|
51 | *
|
---|
52 | * @param hMemPool The handle to the pool. The nil handle and
|
---|
53 | * RTMEMPOOL_DEFAULT are quietly ignored (retval
|
---|
54 | * VINF_SUCCESS).
|
---|
55 | */
|
---|
56 | RTDECL(int) RTMemPoolDestroy(RTMEMPOOL hMemPool);
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Allocates memory.
|
---|
60 | *
|
---|
61 | * @returns Pointer to the allocated memory.
|
---|
62 | * @returns NULL on failure.
|
---|
63 | *
|
---|
64 | * @param hMemPool Handle to the pool to allocate the memory from.
|
---|
65 | * @param cb Size in bytes of the memory block to allocated.
|
---|
66 | */
|
---|
67 | RTDECL(void *) RTMemPoolAlloc(RTMEMPOOL hMemPool, size_t cb) RT_NO_THROW_PROTO;
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Allocates zero'd memory.
|
---|
71 | *
|
---|
72 | * Instead of memset(pv, 0, sizeof()) use this when you want zero'd
|
---|
73 | * memory. This keeps the code smaller and the heap can skip the memset
|
---|
74 | * in about 0.42% of calls :-).
|
---|
75 | *
|
---|
76 | * @returns Pointer to the allocated memory.
|
---|
77 | * @returns NULL on failure.
|
---|
78 | *
|
---|
79 | * @param hMemPool Handle to the pool to allocate the memory from.
|
---|
80 | * @param cb Size in bytes of the memory block to allocated.
|
---|
81 | */
|
---|
82 | RTDECL(void *) RTMemPoolAllocZ(RTMEMPOOL hMemPool, size_t cb) RT_NO_THROW_PROTO;
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Duplicates a chunk of memory into a new heap block.
|
---|
86 | *
|
---|
87 | * @returns New heap block with the duplicate data.
|
---|
88 | * @returns NULL if we're out of memory.
|
---|
89 | *
|
---|
90 | * @param hMemPool Handle to the pool to allocate the memory from.
|
---|
91 | * @param pvSrc The memory to duplicate.
|
---|
92 | * @param cb The amount of memory to duplicate.
|
---|
93 | */
|
---|
94 | RTDECL(void *) RTMemPoolDup(RTMEMPOOL hMemPool, const void *pvSrc, size_t cb) RT_NO_THROW_PROTO;
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Duplicates a chunk of memory into a new heap block with some
|
---|
98 | * additional zeroed memory.
|
---|
99 | *
|
---|
100 | * @returns New heap block with the duplicate data.
|
---|
101 | * @returns NULL if we're out of memory.
|
---|
102 | *
|
---|
103 | * @param hMemPool Handle to the pool to allocate the memory from.
|
---|
104 | * @param pvSrc The memory to duplicate.
|
---|
105 | * @param cbSrc The amount of memory to duplicate.
|
---|
106 | * @param cbExtra The amount of extra memory to allocate and zero.
|
---|
107 | */
|
---|
108 | RTDECL(void *) RTMemPoolDupEx(RTMEMPOOL hMemPool, const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW_PROTO;
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Reallocates memory.
|
---|
112 | *
|
---|
113 | * @returns Pointer to the allocated memory.
|
---|
114 | * @returns NULL on failure.
|
---|
115 | *
|
---|
116 | * @param hMemPool Handle to the pool containing the old memory.
|
---|
117 | * @param pvOld The memory block to reallocate.
|
---|
118 | * @param cbNew The new block size (in bytes).
|
---|
119 | */
|
---|
120 | RTDECL(void *) RTMemPoolRealloc(RTMEMPOOL hMemPool, void *pvOld, size_t cbNew) RT_NO_THROW_PROTO;
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Frees memory allocated from a pool.
|
---|
124 | *
|
---|
125 | * @param hMemPool Handle to the pool containing the memory. Passing
|
---|
126 | * NIL here is fine, but it may come at a slight
|
---|
127 | * performance cost.
|
---|
128 | * @param pv Pointer to memory block.
|
---|
129 | *
|
---|
130 | * @remarks This is the same a RTMemPoolRelease but included here as a separate
|
---|
131 | * function to simplify code migration.
|
---|
132 | */
|
---|
133 | RTDECL(void) RTMemPoolFree(RTMEMPOOL hMemPool, void *pv) RT_NO_THROW_PROTO;
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Retains a reference to a memory block in a pool.
|
---|
137 | *
|
---|
138 | * @returns New reference count, UINT32_MAX on error (asserted).
|
---|
139 | *
|
---|
140 | * @param pv Pointer to memory block.
|
---|
141 | */
|
---|
142 | RTDECL(uint32_t) RTMemPoolRetain(void *pv) RT_NO_THROW_PROTO;
|
---|
143 |
|
---|
144 | /**
|
---|
145 | * Releases a reference to a memory block in a pool.
|
---|
146 | *
|
---|
147 | * @returns New reference count, UINT32_MAX on error (asserted).
|
---|
148 | *
|
---|
149 | * @param hMemPool Handle to the pool containing the memory. Passing
|
---|
150 | * NIL here is fine, but it may come at a slight
|
---|
151 | * performance cost.
|
---|
152 | * @param pv Pointer to memory block.
|
---|
153 | */
|
---|
154 | RTDECL(uint32_t) RTMemPoolRelease(RTMEMPOOL hMemPool, void *pv) RT_NO_THROW_PROTO;
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Get the current reference count.
|
---|
158 | *
|
---|
159 | * @returns The reference count, UINT32_MAX on error (asserted).
|
---|
160 | * @param pv Pointer to memory block.
|
---|
161 | */
|
---|
162 | RTDECL(uint32_t) RTMemPoolRefCount(void *pv) RT_NO_THROW_PROTO;
|
---|
163 |
|
---|
164 |
|
---|
165 | RT_C_DECLS_END
|
---|
166 |
|
---|
167 | #endif /* !IPRT_INCLUDED_mempool_h */
|
---|
168 |
|
---|