VirtualBox

source: vbox/trunk/include/iprt/cache.h@ 19944

Last change on this file since 19944 was 10848, checked in by vboxsync, 16 years ago

Export to OSE

  • 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 - Object cache
3 */
4
5/*
6 * Copyright (C) 2006-2007 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
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34
35#ifndef ___iprt_cache_h
36#define ___iprt_cache_h
37
38#include <iprt/cdefs.h>
39#include <iprt/types.h>
40#include <iprt/critsect.h>
41
42__BEGIN_DECLS
43
44/** Protect the object requester against concurrent access from different threads. */
45#define RTOBJCACHE_PROTECT_REQUEST RT_BIT(0)
46/** Protect the object insert function against concurrent access from different threads. */
47#define RTOBJCACHE_PROTECT_INSERT RT_BIT(1)
48/** All valid protection flags. */
49#define RTOBJCACHE_PROTECT_VALID (RTOBJCACHE_PROTECT_REQUEST | RTOBJCACHE_PROTECT_INSERT)
50
51/**
52 * Object cache header for unlimited sized caches.
53 */
54typedef struct RTOBJCACHEHDR
55{
56 /** Magic value for identifying .*/
57 uint32_t uMagic;
58 /** Next element in the list. */
59 volatile struct RTOBJCACHEHDR *pNext;
60} RTOBJCACHEHDR, *PRTOBJCACHEHDR;
61
62/**
63 * Object cache
64 */
65typedef struct RTOBJCACHE
66{
67 /** Size of the objects to cache. */
68 size_t cbObj;
69 /** Spinlock protecting the object request function if requested. */
70 RTSPINLOCK SpinlockRequest;
71 /** Spinlock protecting the object insert function if requested. */
72 RTSPINLOCK SpinlockInsert;
73 /** Size of the cache - 0 if unlimited size. */
74 uint32_t cElements;
75 /** Cache type dependent data. */
76 union
77 {
78 /** Structure for unlimited cache. */
79 struct
80 {
81 /** Pointer to the first element in the list. */
82 volatile RTOBJCACHEHDR *pFirst;
83 /** Pointer to the last element in the list. */
84 volatile RTOBJCACHEHDR *pLast;
85 } unlimited;
86 /** Structure for cache with defined size. */
87 struct
88 {
89 /** Current fill rate. */
90 volatile uint32_t cElementsInCache;
91 /** Next free element to get. */
92 volatile uint32_t cNextObjRead;
93 /** Next free slot to write to. */
94 volatile uint32_t cNextFreeSlotWrite;
95 /** Array of cached objects. - real size allocated on cache creation. */
96 volatile void *apObjCached[1];
97 } defined;
98 } u;
99} RTOBJCACHE, *PRTOBJCACHE;
100
101
102/**
103 * Create a cache for objects.
104 *
105 * @returns iprt status code.
106 * @param ppObjCache Where to store the pointer to the created cache.
107 * @param cElements Number of elements the cache can hold.
108 * 0 if unlimited size.
109 * @param cbElement Size of one element in bytes
110 * @param fProt Protection flags for protecting cache against concurrent access
111 * from different threads.
112 * RTOBJCACHE_PROTECT_REQUEST to protect the request function.
113 * RTOBJCACHE_PROTECT_INSERT to protect the insert function.
114 */
115RTDECL(int) RTCacheCreate(PRTOBJCACHE *ppCache, uint32_t cElements, size_t cbElement, uint32_t fProt);
116
117/**
118 * Destroy a cache freeing allocated memory.
119 *
120 * @returns iprt status code.
121 * @param pCache Pointer to the cache.
122 */
123RTDECL(int) RTCacheDestroy(PRTOBJCACHE pCache);
124
125/**
126 * Request an object from the cache.
127 *
128 * @returns iprt status
129 * VERR_CACHE_EMPTY if cache is not unlimited and there is no object left in cache.
130 * @param pCache Pointer to the cache to get an object from.
131 * @param ppObj Where to store the pointer to the object.
132 */
133RTDECL(int) RTCacheRequest(PRTOBJCACHE pCache, void **ppObj);
134
135/**
136 * Insert an object into the cache.
137 *
138 * @returns iprt status code.
139 * VERR_CACHE_FULL if cache is not unlimited and there is no free entry left in cache.
140 * @param pCache Pointer to the cache.
141 * @param pObj Pointer to the object to insert.
142 */
143RTDECL(int) RTCacheInsert(PRTOBJCACHE pCache, void *pObj);
144
145__END_DECLS
146
147#endif /* __iprt_cache_h */
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