1 | /* $Id: strcache.cpp 23038 2009-09-15 20:01:09Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - String Cache.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <iprt/strcache.h>
|
---|
36 | #include "internal/iprt.h"
|
---|
37 |
|
---|
38 | #include <iprt/asm.h>
|
---|
39 | #include <iprt/assert.h>
|
---|
40 | #include <iprt/err.h>
|
---|
41 | #include <iprt/mempool.h>
|
---|
42 | #include <iprt/string.h>
|
---|
43 | #include <iprt/once.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | /*******************************************************************************
|
---|
47 | * Structures and Typedefs *
|
---|
48 | *******************************************************************************/
|
---|
49 | /**
|
---|
50 | * String cache entry.
|
---|
51 | *
|
---|
52 | * Each entry is
|
---|
53 | */
|
---|
54 | typedef struct RTSTRCACHEENTRY
|
---|
55 | {
|
---|
56 | /** The number of references. */
|
---|
57 | uint32_t volatile cRefs;
|
---|
58 | /** Offset into the chunk (bytes). */
|
---|
59 | uint32_t offChunk;
|
---|
60 | /** The string length. */
|
---|
61 | uint32_t cch;
|
---|
62 | /** The primary hash value. */
|
---|
63 | uint32_t uHash1;
|
---|
64 | /** The string. */
|
---|
65 | char szString[16];
|
---|
66 | } RTSTRCACHEENTRY;
|
---|
67 | AssertCompileSize(RTSTRCACHEENTRY, 32);
|
---|
68 | /** Pointer to a string cache entry. */
|
---|
69 | typedef RTSTRCACHEENTRY *PRTSTRCACHEENTRY;
|
---|
70 | /** Pointer to a const string cache entry. */
|
---|
71 | typedef RTSTRCACHEENTRY *PCRTSTRCACHEENTRY;
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Allocation chunk.
|
---|
75 | */
|
---|
76 | typedef struct RTSTRCACHECHUNK
|
---|
77 | {
|
---|
78 | /** Pointer to the main string cache structure. */
|
---|
79 | struct RTSTRCACHEINT *pCache;
|
---|
80 | /** Padding to align the entries on a 32-byte boundrary. */
|
---|
81 | uint32_t au32Padding[8 - (ARCH_BITS == 64) - 4];
|
---|
82 | /** The index of the first unused entry. */
|
---|
83 | uint32_t iUnused;
|
---|
84 | /** The number of used entries. */
|
---|
85 | uint32_t cUsed;
|
---|
86 | /** The number of entries in this chunk. */
|
---|
87 | uint32_t cEntries;
|
---|
88 | /** The string cache entries, variable size. */
|
---|
89 | RTSTRCACHEENTRY aEntries[1];
|
---|
90 | } RTSTRCACHECHUNK;
|
---|
91 |
|
---|
92 |
|
---|
93 | typedef struct RTSTRCACHEINT
|
---|
94 | {
|
---|
95 | /** The string cache magic (RTSTRCACHE_MAGIC). */
|
---|
96 | uint32_t u32Magic;
|
---|
97 |
|
---|
98 | } RTSTRCACHEINT;
|
---|
99 |
|
---|
100 |
|
---|
101 |
|
---|
102 |
|
---|
103 | /*******************************************************************************
|
---|
104 | * Defined Constants And Macros *
|
---|
105 | *******************************************************************************/
|
---|
106 | /** Validates a string cache handle, translating RTSTRCACHE_DEFAULT when found,
|
---|
107 | * and returns rc if not valid. */
|
---|
108 | #define RTSTRCACHE_VALID_RETURN_RC(pStrCache, rc) \
|
---|
109 | do { \
|
---|
110 | if ((pStrCache) == RTMEMPOOL_DEFAULT) \
|
---|
111 | (pStrCache) = &g_rtMemPoolDefault; \
|
---|
112 | else \
|
---|
113 | { \
|
---|
114 | AssertPtrReturn((pStrCache), (rc)); \
|
---|
115 | AssertReturn((pStrCache)->u32Magic == RTSTRCACHE_MAGIC, (rc)); \
|
---|
116 | } \
|
---|
117 | } while (0)
|
---|
118 |
|
---|
119 | /** Validates a memory pool entry and returns rc if not valid. */
|
---|
120 | #define RTSTRCACHE_VALID_ENTRY_RETURN_RC(pEntry, rc) \
|
---|
121 | do { \
|
---|
122 | AssertPtrReturn(pEntry, (rc)); \
|
---|
123 | AssertPtrNullReturn((pEntry)->pMemPool, (rc)); \
|
---|
124 | Assert((pEntry)->cRefs < UINT32_MAX / 2); \
|
---|
125 | AssertReturn((pEntry)->pMemPool->u32Magic == RTMEMPOOL_MAGIC, (rc)); \
|
---|
126 | } while (0)
|
---|
127 |
|
---|
128 |
|
---|
129 | /*******************************************************************************
|
---|
130 | * Global Variables *
|
---|
131 | *******************************************************************************/
|
---|
132 |
|
---|
133 |
|
---|
134 |
|
---|
135 | RTDECL(int) RTStrCacheCreate(PRTSTRCACHE phStrCache, const char *pszName)
|
---|
136 | {
|
---|
137 | AssertCompile(sizeof(RTSTRCACHE) == sizeof(RTMEMPOOL));
|
---|
138 | AssertCompile(NIL_RTSTRCACHE == (RTSTRCACHE)NIL_RTMEMPOOL);
|
---|
139 | AssertCompile(RTSTRCACHE_DEFAULT == (RTSTRCACHE)RTMEMPOOL_DEFAULT);
|
---|
140 | return RTMemPoolCreate((PRTMEMPOOL)phStrCache, pszName);
|
---|
141 | }
|
---|
142 | RT_EXPORT_SYMBOL(RTStrCacheCreate);
|
---|
143 |
|
---|
144 |
|
---|
145 | RTDECL(int) RTStrCacheDestroy(RTSTRCACHE hStrCache)
|
---|
146 | {
|
---|
147 | if ( hStrCache == NIL_RTSTRCACHE
|
---|
148 | || hStrCache == RTSTRCACHE_DEFAULT)
|
---|
149 | return VINF_SUCCESS;
|
---|
150 | return RTMemPoolDestroy((RTMEMPOOL)hStrCache);
|
---|
151 | }
|
---|
152 | RT_EXPORT_SYMBOL(RTStrCacheDestroy);
|
---|
153 |
|
---|
154 |
|
---|
155 | RTDECL(const char *) RTStrCacheEnterN(RTSTRCACHE hStrCache, const char *pchString, size_t cchString)
|
---|
156 | {
|
---|
157 | AssertPtr(pchString);
|
---|
158 | AssertReturn(cchString < _1G, NULL);
|
---|
159 | Assert(!memchr(pchString, '\0', cchString));
|
---|
160 |
|
---|
161 | return (const char *)RTMemPoolDupEx((RTMEMPOOL)hStrCache, pchString, cchString, 1);
|
---|
162 | }
|
---|
163 | RT_EXPORT_SYMBOL(RTStrCacheEnterN);
|
---|
164 |
|
---|
165 |
|
---|
166 | RTDECL(const char *) RTStrCacheEnter(RTSTRCACHE hStrCache, const char *psz)
|
---|
167 | {
|
---|
168 | return RTStrCacheEnterN(hStrCache, psz, strlen(psz));
|
---|
169 | }
|
---|
170 | RT_EXPORT_SYMBOL(RTStrCacheEnter);
|
---|
171 |
|
---|
172 |
|
---|
173 | RTDECL(uint32_t) RTStrCacheRetain(const char *psz)
|
---|
174 | {
|
---|
175 | AssertPtr(psz);
|
---|
176 | return RTMemPoolRetain((void *)psz);
|
---|
177 | }
|
---|
178 | RT_EXPORT_SYMBOL(RTStrCacheRetain);
|
---|
179 |
|
---|
180 |
|
---|
181 | RTDECL(uint32_t) RTStrCacheRelease(RTSTRCACHE hStrCache, const char *psz)
|
---|
182 | {
|
---|
183 | if (!psz)
|
---|
184 | return 0;
|
---|
185 | return RTMemPoolRelease((RTMEMPOOL)hStrCache, (void *)psz);
|
---|
186 | }
|
---|
187 | RT_EXPORT_SYMBOL(RTStrCacheRelease);
|
---|
188 |
|
---|
189 |
|
---|
190 | RTDECL(size_t) RTStrCacheLength(const char *psz)
|
---|
191 | {
|
---|
192 | if (!psz)
|
---|
193 | return 0;
|
---|
194 | return strlen(psz);
|
---|
195 | }
|
---|
196 | RT_EXPORT_SYMBOL(RTStrCacheLength);
|
---|
197 |
|
---|
198 |
|
---|