C++ improvements
[deliverable/binutils-gdb.git] / gdb / bcache.h
1 /* Include file cached obstack implementation.
2 Written by Fred Fish <fnf@cygnus.com>
3 Rewritten by Jim Blandy <jimb@cygnus.com>
4 Copyright 1999 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 #ifndef BCACHE_H
24 #define BCACHE_H 1
25
26 /* A bcache is a data structure for factoring out duplication in
27 read-only structures. You give the bcache some string of bytes S.
28 If the bcache already contains a copy of S, it hands you back a
29 pointer to its copy. Otherwise, it makes a fresh copy of S, and
30 hands you back a pointer to that. In either case, you can throw
31 away your copy of S, and use the bcache's.
32
33 The "strings" in question are arbitrary strings of bytes --- they
34 can contain zero bytes. You pass in the length explicitly when you
35 call the bcache function.
36
37 This means that you can put ordinary C objects in a bcache.
38 However, if you do this, remember that structs can contain `holes'
39 between members, added for alignment. These bytes usually contain
40 garbage. If you try to bcache two objects which are identical from
41 your code's point of view, but have different garbage values in the
42 structure's holes, then the bcache will treat them as separate
43 strings, and you won't get the nice elimination of duplicates you
44 were hoping for. So, remember to memset your structures full of
45 zeros before bcaching them!
46
47 You shouldn't modify the strings you get from a bcache, because:
48
49 - You don't necessarily know who you're sharing space with. If I
50 stick eight bytes of text in a bcache, and then stick an
51 eight-byte structure in the same bcache, there's no guarantee
52 those two objects don't actually comprise the same sequence of
53 bytes. If they happen to, the bcache will use a single byte
54 string for both of them. Then, modifying the structure will
55 change the string. In bizarre ways.
56
57 - Even if you know for some other reason that all that's okay,
58 there's another problem. A bcache stores all its strings in a
59 hash table. If you modify a string's contents, you will probably
60 change its hash value. This means that the modified string is
61 now in the wrong place in the hash table, and future bcache
62 probes will never find it. So by mutating a string, you give up
63 any chance of sharing its space with future duplicates. */
64
65
66 /* The type used to hold a single bcache string. The user data is
67 stored in d.data. Since it can be any type, it needs to have the
68 same alignment as the most strict alignment of any type on the host
69 machine. I don't know of any really correct way to do this in
70 stock ANSI C, so just do it the same way obstack.h does.
71
72 It would be nicer to have this stuff hidden away in bcache.c, but
73 struct objstack contains a struct bcache directly --- not a pointer
74 to one --- and then the memory-mapped stuff makes this a real pain.
75 We don't strictly need to expose struct bstring, but it's better to
76 have it all in one place. */
77
78 struct bstring {
79 struct bstring *next;
80 size_t length;
81
82 union
83 {
84 char data[1];
85 double dummy;
86 }
87 d;
88 };
89
90
91 /* The structure for a bcache itself.
92 To initialize a bcache, just fill it with zeros. */
93 struct bcache {
94 /* All the bstrings are allocated here. */
95 struct obstack cache;
96
97 /* How many hash buckets we're using. */
98 unsigned int num_buckets;
99
100 /* Hash buckets. This table is allocated using malloc, so when we
101 grow the table we can return the old table to the system. */
102 struct bstring **bucket;
103
104 /* Statistics. */
105 unsigned long unique_count; /* number of unique strings */
106 long total_count; /* total number of strings cached, including dups */
107 long unique_size; /* size of unique strings, in bytes */
108 long total_size; /* total number of bytes cached, including dups */
109 long structure_size; /* total size of bcache, including infrastructure */
110 };
111
112
113 /* Find a copy of the LENGTH bytes at ADDR in BCACHE. If BCACHE has
114 never seen those bytes before, add a copy of them to BCACHE. In
115 either case, return a pointer to BCACHE's copy of that string. */
116 extern void *bcache (void *addr, int length, struct bcache *bcache);
117
118 /* Free all the storage that BCACHE refers to. The result is a valid,
119 but empty, bcache. This does not free BCACHE itself, since that
120 might be part of some larger object. */
121 extern void free_bcache (struct bcache *bcache);
122
123 /* Print statistics on BCACHE's memory usage and efficacity at
124 eliminating duplication. TYPE should be a string describing the
125 kind of data BCACHE holds. Statistics are printed using
126 `printf_filtered' and its ilk. */
127 extern void print_bcache_statistics (struct bcache *bcache, char *type);
128 /* The hash function */
129 extern unsigned long hash(void *addr, int length);
130 #endif /* BCACHE_H */
This page took 0.033904 seconds and 5 git commands to generate.