VirtualBox

source: vbox/trunk/include/iprt/memcache.h@ 76507

Last change on this file since 76507 was 76507, checked in by vboxsync, 6 years ago

/include: scm --fix-header-guards. bugref:9344

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 KB
Line 
1/** @file
2 * IPRT - Memory Object Allocation Cache.
3 */
4
5/*
6 * Copyright (C) 2006-2017 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_memcache_h
27#define ___iprt_memcache_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32
33#include <iprt/cdefs.h>
34#include <iprt/types.h>
35
36RT_C_DECLS_BEGIN
37
38
39/** @defgroup grp_rt_memcache RTMemCache - Memory Object Allocation Cache
40 * @ingroup grp_rt
41 *
42 * Optimized allocation, initialization, freeing and destruction of memory
43 * objects of the same kind and size. Objects are constructed once, then
44 * allocated and freed one or more times, until finally destructed together with
45 * the cache (RTMemCacheDestroy). It's expected behavior, even when pfnCtor is
46 * NULL, that the user will be store information that should be persistent
47 * across RTMemCacheFree calls.
48 *
49 * The objects are zeroed prior to calling pfnCtor. For obvious reasons, the
50 * objects are not touched by the cache after that, so that RTMemCacheAlloc will
51 * return the object in the same state as when it as handed to RTMemCacheFree.
52 *
53 * @todo A callback for the reuse (at alloc time) might be of interest.
54 *
55 * @{
56 */
57
58/** A memory cache handle. */
59typedef R3R0PTRTYPE(struct RTMEMCACHEINT *) RTMEMCACHE;
60/** Pointer to a memory cache handle. */
61typedef RTMEMCACHE *PRTMEMCACHE;
62/** Nil memory cache handle. */
63#define NIL_RTMEMCACHE ((RTMEMCACHE)0)
64
65
66/**
67 * Object constructor.
68 *
69 * This is called for when an element is allocated for the first time.
70 *
71 * @returns IPRT status code.
72 * @param hMemCache The cache handle.
73 * @param pvObj The memory object that should be initialized.
74 * @param pvUser The user argument.
75 *
76 * @remarks No serialization is performed.
77 */
78typedef DECLCALLBACK(int) FNMEMCACHECTOR(RTMEMCACHE hMemCache, void *pvObj, void *pvUser);
79/** Pointer to an object constructor for the memory cache. */
80typedef FNMEMCACHECTOR *PFNMEMCACHECTOR;
81
82/**
83 * Object destructor.
84 *
85 * This is called when we're shrinking or destroying the cache.
86 *
87 * @param hMemCache The cache handle.
88 * @param pvObj The memory object that should be initialized.
89 * @param pvUser The user argument.
90 *
91 * @remarks No serialization is performed.
92 */
93typedef DECLCALLBACK(void) FNMEMCACHEDTOR(RTMEMCACHE hMemCache, void *pvObj, void *pvUser);
94/** Pointer to an object destructor for the memory cache. */
95typedef FNMEMCACHEDTOR *PFNMEMCACHEDTOR;
96
97
98/**
99 * Create an allocation cache for fixed size memory objects.
100 *
101 * @returns IPRT status code.
102 * @param phMemCache Where to return the cache handle.
103 * @param cbObject The size of one memory object.
104 * @param cbAlignment The object alignment. This must be a power of
105 * two. The higest alignment is 64. If set to 0,
106 * a sensible alignment value will be derived from
107 * the object size.
108 * @param cMaxObjects The maximum cache size. Pass UINT32_MAX if unsure.
109 * @param pfnCtor Object constructor callback. Optional.
110 * @param pfnDtor Object destructor callback. Optional.
111 * @param pvUser User argument for the two callbacks.
112 * @param fFlags Flags reserved for future use. Must be zero.
113 */
114RTDECL(int) RTMemCacheCreate(PRTMEMCACHE phMemCache, size_t cbObject, size_t cbAlignment, uint32_t cMaxObjects,
115 PFNMEMCACHECTOR pfnCtor, PFNMEMCACHEDTOR pfnDtor, void *pvUser, uint32_t fFlags);
116
117/**
118 * Destroy a cache destroying and freeing allocated memory.
119 *
120 * @returns IPRT status code.
121 * @param hMemCache The cache handle. NIL is quietly (VINF_SUCCESS)
122 * ignored.
123 */
124RTDECL(int) RTMemCacheDestroy(RTMEMCACHE hMemCache);
125
126/**
127 * Allocate an object.
128 *
129 * @returns Pointer to the allocated cache object.
130 * @param hMemCache The cache handle.
131 */
132RTDECL(void *) RTMemCacheAlloc(RTMEMCACHE hMemCache);
133
134/**
135 * Allocate an object and return a proper status code.
136 *
137 * @returns IPRT status code.
138 * @retval VERR_MEM_CACHE_MAX_SIZE if we've reached maximum size (see
139 * RTMemCacheCreate).
140 * @retval VERR_NO_MEMORY if we failed to allocate more memory for the cache.
141 *
142 * @param hMemCache The cache handle.
143 * @param ppvObj Where to return the object.
144 */
145RTDECL(int) RTMemCacheAllocEx(RTMEMCACHE hMemCache, void **ppvObj);
146
147/**
148 * Free an object previously returned by RTMemCacheAlloc or RTMemCacheAllocEx.
149 *
150 * @param hMemCache The cache handle.
151 * @param pvObj The object to free. NULL is fine.
152 */
153RTDECL(void) RTMemCacheFree(RTMEMCACHE hMemCache, void *pvObj);
154
155/** @} */
156
157RT_C_DECLS_END
158
159#endif
160
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette