ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/common/shstr.C
(Generate patch)

Comparing deliantra/server/common/shstr.C (file contents):
Revision 1.22 by root, Thu May 17 21:32:08 2007 UTC vs.
Revision 1.28 by root, Thu Nov 8 19:43:23 2007 UTC

1/* 1/*
2 * CrossFire, A Multiplayer game 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team 4 * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * 5 *
6 * This program is free software; you can redistribute it and/or modify 6 * Deliantra is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by 7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or 8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version. 9 * (at your option) any later version.
10 * 10 *
11 * This program is distributed in the hope that it will be useful, 11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details. 14 * GNU General Public License for more details.
15 * 15 *
16 * You should have received a copy of the GNU General Public License 16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software 17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 * 18 *
20 * The authors can be reached via e-mail at <crossfire@schmorp.de> 19 * The authors can be reached via e-mail to <support@deliantra.net>
21 */ 20 */
22 21
23/* 22/*
24 * shstr.C 23 * shstr.C
25 */ 24 */
29#include <glib.h> 28#include <glib.h>
30#include <tr1/unordered_set> 29#include <tr1/unordered_set>
31 30
32#include "global.h" 31#include "global.h"
33 32
33size_t shstr_alloc;
34
34typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *>, true> HT; 35typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *>, true> HT;
35 36
36static HT ht; 37static HT ht;
37 38
38static const char * 39static const char *
39makevec (const char *s) 40makevec (const char *s)
40{ 41{
41 int len = strlen (s); 42 int len = strlen (s);
42 43
44 shstr_alloc += sizeof (int) * 2 + len + 1;
43 const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (int) * 2 + len + 1)); 45 const char *v = (const char *) (2 + (int *)g_slice_alloc (sizeof (int) * 2 + len + 1));
44 46
45 shstr::length (v) = len; 47 shstr::length (v) = len;
46 shstr::refcnt (v) = 1; 48 shstr::refcnt (v) = 1;
47 49
98 auto (i, curpos ? ht.find (curpos) : ht.begin ()); 100 auto (i, curpos ? ht.find (curpos) : ht.begin ());
99 101
100 if (i == ht.end ()) 102 if (i == ht.end ())
101 i = ht.begin (); 103 i = ht.begin ();
102 104
103 // go through all strings roughly once every 4 minutes
104 int n = ht.size () / 256 + 16; 105 int n = ht.size () / 256 + 16;
105 106
106 for (;;) 107 for (;;)
107 { 108 {
108 if (i == ht.end ()) 109 if (i == ht.end ())
118 const char *s = *o; 119 const char *s = *o;
119 120
120 ht.erase (o); 121 ht.erase (o);
121 122
122 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); 123 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s));
124 shstr_alloc -= sizeof (int) * 2 + length (s) + 1;
123 g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s); 125 g_slice_free1 (sizeof (int) * 2 + length (s) + 1, -2 + (int *) s);
124 } 126 }
125 else 127 else
126 ++i; 128 ++i;
127 } 129 }
128 130
129 curpos = *i; 131 curpos = *i;
130} 132}
131 133
134// declare these here to get correct initialisation order
135#define def(str) const shstr shstr_ ## str (# str);
136# include "shstrinc.h"
137#undef def
138
132shstr skill_names[NUM_SKILLS]; 139shstr skill_names[NUM_SKILLS];
133
134// what weird misoptimisation is this again?
135const shstr undead_name ("undead");
136 140
137//TODO: this should of course not be here 141//TODO: this should of course not be here
138 142
139/* buf_overflow() - we don't want to exceed the buffer size of 143/* buf_overflow() - we don't want to exceed the buffer size of
140 * buf1 by adding on buf2! Returns true if overflow will occur. 144 * buf1 by adding on buf2! Returns true if overflow will occur.
144{ 148{
145 int len1 = 0, len2 = 0; 149 int len1 = 0, len2 = 0;
146 150
147 if (buf1) 151 if (buf1)
148 len1 = strlen (buf1); 152 len1 = strlen (buf1);
153
149 if (buf2) 154 if (buf2)
150 len2 = strlen (buf2); 155 len2 = strlen (buf2);
156
151 if ((len1 + len2) >= bufsize) 157 if ((len1 + len2) >= bufsize)
152 return 1; 158 return 1;
153 159
154 return 0; 160 return 0;
155} 161}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines