VirtualBox

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

Last change on this file since 26419 was 26419, checked in by vboxsync, 15 years ago

tstRTMemCache: Testcase + bug fixes. Works pretty well, but should try tune it a bit.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.1 KB
Line 
1/** @file
2 * IPRT - Memory Object Allocation Cache.
3 */
4
5/*
6 * Copyright (C) 2006-2010 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
26 * Clara, CA 95054 USA or visit http://www.sun.com if you need
27 * additional information or have any questions.
28 */
29
30#ifndef ___iprt_memcache_h
31#define ___iprt_memcache_h
32
33
34#include <iprt/cdefs.h>
35#include <iprt/types.h>
36
37RT_C_DECLS_BEGIN
38
39
40/** @defgroup grp_rt_memcache RTMemCache - Memory Object Allocation Cache
41 * @ingroup grp_rt
42 *
43 * Optimized allocation, initialization, freeing and destruction of memory
44 * objects of the same kind and size.
45 *
46 * @{
47 */
48
49/** A memory cache handle. */
50typedef R3R0PTRTYPE(struct RTMEMCACHEINT *) RTMEMCACHE;
51/** Pointer to a memory cache handle. */
52typedef RTMEMCACHE *PRTMEMCACHE;
53/** Nil memory cache handle. */
54#define NIL_RTMEMCACHE ((RTMEMCACHE)0)
55
56
57/**
58 * Object constructor.
59 *
60 * This is called for when an element is allocated for the first time.
61 *
62 * @returns IPRT status code.
63 * @param hMemCache The cache handle.
64 * @param pvObj The memory object that should be initialized.
65 * @param pvUser The user argument.
66 *
67 * @remarks No serialization is performed.
68 */
69typedef DECLCALLBACK(int) FNMEMCACHECTOR(RTMEMCACHE hMemCache, void *pvObj, void *pvUser);
70/** Pointer to an object constructor for the memory cache. */
71typedef FNMEMCACHECTOR *PFNMEMCACHECTOR;
72
73/**
74 * Object destructor.
75 *
76 * This is called when we're shrinking or destroying the cache.
77 *
78 * @param hMemCache The cache handle.
79 * @param pvObj The memory object that should be initialized.
80 * @param pvUser The user argument.
81 *
82 * @remarks No serialization is performed.
83 */
84typedef DECLCALLBACK(void) FNMEMCACHEDTOR(RTMEMCACHE hMemCache, void *pvObj, void *pvUser);
85/** Pointer to an object destructor for the memory cache. */
86typedef FNMEMCACHEDTOR *PFNMEMCACHEDTOR;
87
88
89/**
90 * Create an allocation cache for fixed size memory objects.
91 *
92 * @returns IPRT status code.
93 * @param phMemCache Where to return the cache handle.
94 * @param cbObject The size of one memory object.
95 * @param cbAlignment The object alignment. This must be a power of
96 * two. The higest alignment is 64. If set to 0,
97 * a sensible alignment value will be derived from
98 * the object size.
99 * @param cMaxObjects The maximum cache size. Pass UINT32_MAX if unsure.
100 * @param pfnCtor Object constructor callback. Optional.
101 * @param pfnDtor Object destructor callback. Optional.
102 * @param pvUser User argument for the two callbacks.
103 */
104RTDECL(int) RTMemCacheCreate(PRTMEMCACHE phMemCache, size_t cbObject, size_t cbAlignment, uint32_t cMaxObjects,
105 PFNMEMCACHECTOR pfnCtor, PFNMEMCACHEDTOR pfnDtor, void *pvUser);
106
107/**
108 * Destroy a cache destroying and freeing allocated memory.
109 *
110 * @returns IPRT status code.
111 * @param hMemCache The cache handle. NIL is quietly (VINF_SUCCESS)
112 * ignored.
113 */
114RTDECL(int) RTMemCacheDestroy(RTMEMCACHE hMemCache);
115
116/**
117 * Allocate an object.
118 *
119 * @returns Pointer to the allocated cache object.
120 * @param hMemCache The cache handle.
121 */
122RTDECL(void *) RTMemCacheAlloc(RTMEMCACHE hMemCache);
123
124/**
125 * Allocate an object and return a proper status code.
126 *
127 * @returns IPRT status code.
128 * @retval VERR_MEM_CACHE_MAX_SIZE if we've reached maximum size (see
129 * RTMemCacheCreate).
130 * @retval VERR_NO_MEMORY if we failed to allocate more memory for the cache.
131 *
132 * @param hMemCache The cache handle.
133 * @param ppvObj Where to return the object.
134 */
135RTDECL(int) RTMemCacheAllocEx(RTMEMCACHE hMemCache, void **ppvObj);
136
137/**
138 * Free an object previously returned by RTMemCacheAlloc or RTMemCacheAllocEx.
139 *
140 * @param hMemCache The cache handle.
141 * @param pvObj The object to free. NULL is fine.
142 */
143RTDECL(void) RTMemCacheFree(RTMEMCACHE hMemCache, void *pvObj);
144
145/** @} */
146
147RT_C_DECLS_END
148
149#endif
150
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