1 | /* $Id: strcache.cpp 33540 2010-10-28 09:27:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - String Cache.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009 Oracle Corporation
|
---|
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 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <iprt/strcache.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <iprt/assert.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/mempool.h>
|
---|
38 | #include <iprt/string.h>
|
---|
39 | #include <iprt/once.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | /*******************************************************************************
|
---|
43 | * Structures and Typedefs *
|
---|
44 | *******************************************************************************/
|
---|
45 | /**
|
---|
46 | * String cache entry.
|
---|
47 | *
|
---|
48 | * Each entry is
|
---|
49 | */
|
---|
50 | typedef struct RTSTRCACHEENTRY
|
---|
51 | {
|
---|
52 | /** The number of references. */
|
---|
53 | uint32_t volatile cRefs;
|
---|
54 | /** Offset into the chunk (bytes). */
|
---|
55 | uint32_t offChunk;
|
---|
56 | /** The string length. */
|
---|
57 | uint32_t cch;
|
---|
58 | /** The primary hash value. */
|
---|
59 | uint32_t uHash1;
|
---|
60 | /** The string. */
|
---|
61 | char szString[16];
|
---|
62 | } RTSTRCACHEENTRY;
|
---|
63 | AssertCompileSize(RTSTRCACHEENTRY, 32);
|
---|
64 | /** Pointer to a string cache entry. */
|
---|
65 | typedef RTSTRCACHEENTRY *PRTSTRCACHEENTRY;
|
---|
66 | /** Pointer to a const string cache entry. */
|
---|
67 | typedef RTSTRCACHEENTRY *PCRTSTRCACHEENTRY;
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Allocation chunk.
|
---|
71 | */
|
---|
72 | typedef struct RTSTRCACHECHUNK
|
---|
73 | {
|
---|
74 | /** Pointer to the main string cache structure. */
|
---|
75 | struct RTSTRCACHEINT *pCache;
|
---|
76 | /** Padding to align the entries on a 32-byte boundary. */
|
---|
77 | uint32_t au32Padding[8 - (ARCH_BITS == 64) - 4];
|
---|
78 | /** The index of the first unused entry. */
|
---|
79 | uint32_t iUnused;
|
---|
80 | /** The number of used entries. */
|
---|
81 | uint32_t cUsed;
|
---|
82 | /** The number of entries in this chunk. */
|
---|
83 | uint32_t cEntries;
|
---|
84 | /** The string cache entries, variable size. */
|
---|
85 | RTSTRCACHEENTRY aEntries[1];
|
---|
86 | } RTSTRCACHECHUNK;
|
---|
87 |
|
---|
88 |
|
---|
89 | typedef struct RTSTRCACHEINT
|
---|
90 | {
|
---|
91 | /** The string cache magic (RTSTRCACHE_MAGIC). */
|
---|
92 | uint32_t u32Magic;
|
---|
93 |
|
---|
94 | } RTSTRCACHEINT;
|
---|
95 |
|
---|
96 |
|
---|
97 |
|
---|
98 |
|
---|
99 | /*******************************************************************************
|
---|
100 | * Defined Constants And Macros *
|
---|
101 | *******************************************************************************/
|
---|
102 | /** Validates a string cache handle, translating RTSTRCACHE_DEFAULT when found,
|
---|
103 | * and returns rc if not valid. */
|
---|
104 | #define RTSTRCACHE_VALID_RETURN_RC(pStrCache, rc) \
|
---|
105 | do { \
|
---|
106 | if ((pStrCache) == RTMEMPOOL_DEFAULT) \
|
---|
107 | (pStrCache) = &g_rtMemPoolDefault; \
|
---|
108 | else \
|
---|
109 | { \
|
---|
110 | AssertPtrReturn((pStrCache), (rc)); \
|
---|
111 | AssertReturn((pStrCache)->u32Magic == RTSTRCACHE_MAGIC, (rc)); \
|
---|
112 | } \
|
---|
113 | } while (0)
|
---|
114 |
|
---|
115 | /** Validates a memory pool entry and returns rc if not valid. */
|
---|
116 | #define RTSTRCACHE_VALID_ENTRY_RETURN_RC(pEntry, rc) \
|
---|
117 | do { \
|
---|
118 | AssertPtrReturn(pEntry, (rc)); \
|
---|
119 | AssertPtrNullReturn((pEntry)->pMemPool, (rc)); \
|
---|
120 | Assert((pEntry)->cRefs < UINT32_MAX / 2); \
|
---|
121 | AssertReturn((pEntry)->pMemPool->u32Magic == RTMEMPOOL_MAGIC, (rc)); \
|
---|
122 | } while (0)
|
---|
123 |
|
---|
124 |
|
---|
125 | /*******************************************************************************
|
---|
126 | * Global Variables *
|
---|
127 | *******************************************************************************/
|
---|
128 |
|
---|
129 |
|
---|
130 |
|
---|
131 | RTDECL(int) RTStrCacheCreate(PRTSTRCACHE phStrCache, const char *pszName)
|
---|
132 | {
|
---|
133 | AssertCompile(sizeof(RTSTRCACHE) == sizeof(RTMEMPOOL));
|
---|
134 | AssertCompile(NIL_RTSTRCACHE == (RTSTRCACHE)NIL_RTMEMPOOL);
|
---|
135 | AssertCompile(RTSTRCACHE_DEFAULT == (RTSTRCACHE)RTMEMPOOL_DEFAULT);
|
---|
136 | return RTMemPoolCreate((PRTMEMPOOL)phStrCache, pszName);
|
---|
137 | }
|
---|
138 | RT_EXPORT_SYMBOL(RTStrCacheCreate);
|
---|
139 |
|
---|
140 |
|
---|
141 | RTDECL(int) RTStrCacheDestroy(RTSTRCACHE hStrCache)
|
---|
142 | {
|
---|
143 | if ( hStrCache == NIL_RTSTRCACHE
|
---|
144 | || hStrCache == RTSTRCACHE_DEFAULT)
|
---|
145 | return VINF_SUCCESS;
|
---|
146 | return RTMemPoolDestroy((RTMEMPOOL)hStrCache);
|
---|
147 | }
|
---|
148 | RT_EXPORT_SYMBOL(RTStrCacheDestroy);
|
---|
149 |
|
---|
150 |
|
---|
151 | RTDECL(const char *) RTStrCacheEnterN(RTSTRCACHE hStrCache, const char *pchString, size_t cchString)
|
---|
152 | {
|
---|
153 | AssertPtr(pchString);
|
---|
154 | AssertReturn(cchString < _1G, NULL);
|
---|
155 | Assert(!RTStrEnd(pchString, cchString));
|
---|
156 |
|
---|
157 | return (const char *)RTMemPoolDupEx((RTMEMPOOL)hStrCache, pchString, cchString, 1);
|
---|
158 | }
|
---|
159 | RT_EXPORT_SYMBOL(RTStrCacheEnterN);
|
---|
160 |
|
---|
161 |
|
---|
162 | RTDECL(const char *) RTStrCacheEnter(RTSTRCACHE hStrCache, const char *psz)
|
---|
163 | {
|
---|
164 | return RTStrCacheEnterN(hStrCache, psz, strlen(psz));
|
---|
165 | }
|
---|
166 | RT_EXPORT_SYMBOL(RTStrCacheEnter);
|
---|
167 |
|
---|
168 |
|
---|
169 | RTDECL(uint32_t) RTStrCacheRetain(const char *psz)
|
---|
170 | {
|
---|
171 | AssertPtr(psz);
|
---|
172 | return RTMemPoolRetain((void *)psz);
|
---|
173 | }
|
---|
174 | RT_EXPORT_SYMBOL(RTStrCacheRetain);
|
---|
175 |
|
---|
176 |
|
---|
177 | RTDECL(uint32_t) RTStrCacheRelease(RTSTRCACHE hStrCache, const char *psz)
|
---|
178 | {
|
---|
179 | if (!psz)
|
---|
180 | return 0;
|
---|
181 | return RTMemPoolRelease((RTMEMPOOL)hStrCache, (void *)psz);
|
---|
182 | }
|
---|
183 | RT_EXPORT_SYMBOL(RTStrCacheRelease);
|
---|
184 |
|
---|
185 |
|
---|
186 | RTDECL(size_t) RTStrCacheLength(const char *psz)
|
---|
187 | {
|
---|
188 | if (!psz)
|
---|
189 | return 0;
|
---|
190 | return strlen(psz);
|
---|
191 | }
|
---|
192 | RT_EXPORT_SYMBOL(RTStrCacheLength);
|
---|
193 |
|
---|