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.39 by root, Mon Oct 12 14:00:57 2009 UTC vs.
Revision 1.44 by root, Thu Apr 15 00:36:51 2010 UTC

1/* 1/*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG. 2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 * 3 *
4 * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team 4 * Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 * 5 *
6 * Deliantra is free software: you can redistribute it and/or modify it under 6 * Deliantra is free software: you can redistribute it and/or modify it under
7 * the terms of the Affero GNU General Public License as published by the 7 * the terms of the Affero GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your 8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version. 9 * option) any later version.
36typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *> > HT; 36typedef std::tr1::unordered_set <const char *, str_hash, str_equal, slice_allocator<const char *> > HT;
37 37
38static HT ht; 38static HT ht;
39static int next_gc; 39static int next_gc;
40 40
41#define NUM_INT 3
42
41static const char * 43static const char *
42makevec (const char *s) 44makevec (const char *s)
43{ 45{
44 int len = strlen (s); 46 int len = strlen (s);
45 int alloc = sizeof (uint32_t) * 2 + len + 1; 47 int alloc = sizeof (uint32_t) * NUM_INT + len + 1;
46 48
47 shstr_alloc += alloc; 49 shstr_alloc += alloc;
48 char *v = (char *)g_slice_alloc (alloc); 50 char *v = (char *)g_slice_alloc (alloc);
49 v += sizeof (uint32_t) * 2; 51 v += sizeof (uint32_t) * NUM_INT;
50 52
53 shstr::hash (v) = strhsh (s);
51 shstr::length (v) = len; 54 shstr::length (v) = len;
52 shstr::refcnt (v) = 1; 55 shstr::refcnt (v) = 1;
53 56
54 memcpy (v, s, len + 1); 57 memcpy (v, s, len + 1);
55 58
56 return v; 59 return v;
57} 60}
58 61
59shstr_vec<sizeof "(null)"> shstr_tmp::nullvec = { 0, 0xffffffff, "(null)" }; 62shstr_vec<sizeof "(null)"> shstr_tmp::nullvec = { STRHSH_NULL, 0, 0xffffffff, "(null)" };
60 63
61const char * 64const char *
62shstr::find (const char *s) 65shstr::find (const char *s)
63{ 66{
64 if (!s) 67 if (!s)
120 const char *s = *o; 123 const char *s = *o;
121 124
122 ht.erase (o); 125 ht.erase (o);
123 126
124 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s)); 127 //printf ("GC %4d %3d %d >%s<%d\n", (int)ht.size (), n, shstr::refcnt (s), s, shstr::length (s));
125 int alloc = sizeof (uint32_t) * 2 + length (s) + 1; 128 int alloc = sizeof (uint32_t) * NUM_INT + length (s) + 1;
126 shstr_alloc -= alloc; 129 shstr_alloc -= alloc;
127 g_slice_free1 (alloc, (void *)(s - sizeof (uint32_t) * 2)); 130 g_slice_free1 (alloc, (void *)(s - sizeof (uint32_t) * NUM_INT));
128 } 131 }
129 else 132 else
130 ++i; 133 ++i;
131 } 134 }
132 135
138#define def(id) def2(shstr_ ## id, # id) 141#define def(id) def2(shstr_ ## id, # id)
139# include "shstrinc.h" 142# include "shstrinc.h"
140#undef def 143#undef def
141#undef def2 144#undef def2
142 145
143shstr skill_names[NUM_SKILLS]; 146materialtype_t material_null;
144 147
145//TODO: this should of course not be here
146
147/* buf_overflow() - we don't want to exceed the buffer size of
148 * buf1 by adding on buf2! Returns true if overflow will occur.
149 */
150int
151buf_overflow (const char *buf1, const char *buf2, int bufsize)
152{
153 int len1 = 0, len2 = 0;
154
155 if (buf1)
156 len1 = strlen (buf1);
157
158 if (buf2)
159 len2 = strlen (buf2);
160
161 if ((len1 + len2) >= bufsize)
162 return 1;
163
164 return 0;
165}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines