VirtualBox

source: kBuild/trunk/src/kmk/strcache2.h@ 1869

Last change on this file since 1869 was 1869, checked in by bird, 16 years ago

kmk: untested version of strcache2, lacking rehash. This is a string allocation cache similar to the one found in strcache, except that all the hashing and hashtable code is inlined and tuned. It further allows the user to have as many instances as he wishes as well as associating a value (pointer) with each entry.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.1 KB
Line 
1/* $Id: strcache2.h 1869 2008-10-16 05:05:04Z bird $ */
2/** @file
3 * strcache - New string cache.
4 */
5
6/*
7 * Copyright (c) 2006-2008 knut st. osmundsen <[email protected]>
8 *
9 * This file is part of kBuild.
10 *
11 * kBuild is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * kBuild is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with kBuild; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 *
25 */
26
27#ifndef ___strcache2_h
28#define ___strcache2_h
29
30/* string cache memory segment. */
31struct strcache2_seg
32{
33 struct strcache2_seg *next; /* The next cache segment. */
34 char *start; /* The first byte in the segment. */
35 size_t size; /* The size of the segment. */
36 size_t avail; /* The number of available bytes. */
37 char *cursor; /* Allocation cursor. */
38};
39
40/* string cache hash table entry. */
41struct strcache2_entry
42{
43 void *user;
44 unsigned int hash1;
45 unsigned int hash2;
46 unsigned int length;
47};
48
49struct strcache2
50{
51 struct strcache2_entry **hash_tab; /* The hash table. */
52 int case_insensitive; /* case insensitive or not. */
53 unsigned int hash_size; /* The hash table size. */
54 unsigned int count; /* Number entries in the cache. */
55 unsigned int rehash_count; /* When to rehash the table. */
56 unsigned long collision_count; /* The number of collisions. */
57 unsigned long lookup_count; /* The number of lookups. */
58 void *lock; /* The lock handle. */
59 struct strcache2_seg *seg_head; /* The memory segment list. */
60 struct strcache2 *next; /* The next string cache. */
61 const char *name; /* Cache name. */
62};
63
64
65void strcache2_init (struct strcache2 *cache, const char *name, int case_insensitive, int thread_safe);
66void strcache2_term (struct strcache2 *cache);
67void strcache2_print_stats (struct strcache2 *cache, const char *prefix);
68void strcache2_print_stats_all (const char *prefix);
69const char *strcache2_add (struct strcache2 *cache, const char *str, unsigned int length);
70int strcache2_verify_entry (struct strcache2 *cache, const char *str);
71unsigned int strcache2_get_hash2_fallback (struct strcache2 *cache, const char *str);
72
73/* Get the hash table entry pointer. */
74MY_INLINE struct strcache2_entry const *
75strcache2_get_entry (struct strcache2 *cache, const char *str)
76{
77#ifndef NDEBUG
78 strcache2_verify_entry (cache, str);
79#endif
80 return (struct strcache2_entry const *)str - 1;
81}
82
83/* Get the string length. */
84MY_INLINE unsigned int
85strcache2_get_len (struct strcache2 *cache, const char *str)
86{
87 return strcache2_get_entry (cache, str)->length;
88}
89
90/* Get the first hash value for the string. */
91MY_INLINE unsigned int
92strcache2_get_hash1 (struct strcache2 *cache, const char *str)
93{
94 return strcache2_get_entry (cache, str)->hash1;
95}
96
97/* Get the second hash value for the string. */
98MY_INLINE unsigned int
99strcache2_get_hash2 (struct strcache2 *cache, const char *str)
100{
101 unsigned int hash2 = strcache2_get_entry (cache, str)->hash2;
102 if (hash2)
103 hash2 = strcache2_get_hash2_fallback (cache, str);
104 return hash2;
105}
106
107/* Get the user value for the string. */
108MY_INLINE void *
109strcache2_get_user_val (struct strcache2 *cache, const char *str)
110{
111 return strcache2_get_entry (cache, str)->user;
112}
113
114/* Get the user value for the string. */
115MY_INLINE void
116strcache2_set_user_val (struct strcache2 *cache, const char *str, void *value)
117{
118 struct strcache2_entry *entry = (struct strcache2_entry *)str - 1;
119#ifndef NDEBUG
120 strcache2_verify_entry (cache, str);
121#endif
122 entry->user = value;
123}
124
125#endif
126
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