ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/server/freezethaw.C
Revision: 1.32
Committed: Tue Apr 1 19:50:40 2008 UTC (16 years, 2 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-2_80, rel-2_6, rel-2_7, rel-2_5, rel-2_72, rel-2_73, rel-2_71, rel-2_76, rel-2_77, rel-2_74, rel-2_75, rel-2_54, rel-2_55, rel-2_56, rel-2_79, rel-2_52, rel-2_53, rel-2_78, rel-2_61, rel-2_43
Changes since 1.31: +2 -2 lines
Log Message:
tuning, glibc-bugworkaround

File Contents

# User Rev Content
1 root 1.1 /*
2 root 1.30 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 root 1.21 *
4 root 1.30 * Copyright (©) 2006,2007 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
5 root 1.21 *
6 root 1.30 * Deliantra is free software: you can redistribute it and/or modify
7 root 1.22 * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation, either version 3 of the License, or
9     * (at your option) any later version.
10 root 1.21 *
11 root 1.22 * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15 root 1.21 *
16 root 1.22 * You should have received a copy of the GNU General Public License
17     * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 root 1.21 *
19 root 1.30 * The authors can be reached via e-mail to <support@deliantra.net>
20 root 1.1 */
21    
22 root 1.4 #include "global.h" // bug in cfperl.h, doesn't include interface_class stuff
23 root 1.1 #include "logger.h"
24     #include "cfperl.h"
25     #include "kw_hash.h"
26    
27     object_freezer::object_freezer ()
28 root 1.17 : dynbuf_text (128 * 1024, 64 * 1024)
29 root 1.1 {
30     av = newAV ();
31     }
32    
33     object_freezer::~object_freezer ()
34     {
35     SvREFCNT_dec (av);
36     }
37    
38 root 1.4 void
39     object_freezer::put (attachable *ext)
40 root 1.1 {
41     ext->optimise ();
42    
43     if (ext->self)
44     {
45     int idx = AvFILLp ((AV *)av) + 1;
46     av_store (av, idx, newRV_inc ((SV *)ext->self));
47    
48     add ((void *)"oid ", 4);
49     add ((sint32)idx);
50     add ('\n');
51     }
52     }
53    
54 root 1.4 bool
55     object_freezer::save (const char *path)
56 root 1.1 {
57     CALL_BEGIN (3);
58 root 1.4 CALL_ARG_SV (newSVpv (path, 0));
59 root 1.1 CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ())));
60     CALL_ARG_SV (newRV_inc ((SV *)av));
61     CALL_CALL ("cf::object_freezer_save", G_VOID | G_DISCARD);
62     CALL_END;
63 root 1.13
64     return 1;
65 root 1.1 }
66    
67 root 1.4 char *
68     object_freezer::as_string ()
69 root 1.1 {
70     CALL_BEGIN (2);
71     CALL_ARG_SV (newRV_noinc (newSVpvn ((char *)linearise (), size ())));
72     CALL_ARG_SV (newRV_inc ((SV *)av));
73     CALL_CALL ("cf::object_freezer_as_string", G_SCALAR);
74    
75     char *res = count > 0
76 root 1.3 ? strdup (SvPVX (POPs))
77 root 1.1 : strdup ("[fatal error]");
78    
79     CALL_END;
80    
81     return res;
82     }
83    
84 root 1.20 #if 0
85 root 1.13 void
86 root 1.4 fprintf (object_freezer &freezer, const char *format, ...)
87 root 1.1 {
88     va_list ap;
89    
90     va_start (ap, format);
91    
92     int len = vsnprintf ((char *)freezer.force (1024), 1024, format, ap);
93    
94     if (len >= 0)
95     freezer.alloc (len);
96    
97     va_end (ap);
98     }
99    
100 pippijn 1.12 // XXX: function not returning an int
101 root 1.13 void
102 root 1.4 fputs (const char *s, object_freezer &freezer)
103 root 1.1 {
104     freezer.add (s);
105     }
106 root 1.20 #endif
107 root 1.1
108     static const char thawer_eof[] = "\n\n\n\0\0\0";
109    
110 root 1.4 object_thawer::object_thawer (const char *path)
111     : name (strdup (path))
112 root 1.1 {
113     static const char eof[] = "\n\n\n\0\0\0";
114    
115 root 1.10 av = 0;
116     text = 0;
117     line = 0;
118 root 1.8 linenum = 0;
119 root 1.1
120 root 1.10 kw = KW_ERROR;
121     kw_str = 0;
122     value = 0;
123    
124 root 1.4 if (path)
125 root 1.1 {
126     CALL_BEGIN (1);
127 root 1.4 CALL_ARG_SV (newSVpv (path, 0));
128 root 1.1 CALL_CALL ("cf::object_thawer_load", G_ARRAY);
129    
130     if (count == 2)
131     {
132     // second value - perl objects
133     {
134     SV *sv = POPs;
135     if (SvROK (sv))
136     av = (AV *)SvREFCNT_inc (SvRV (sv));
137     }
138    
139     // first value - text part, pad with 3 zeroes
140     {
141     SV *sv = POPs;
142     STRLEN len;
143     char *sv_ = SvPVbyte (sv, len);
144     text = newSV (len + sizeof (eof));
145 root 1.32 SvCUR_set (text, len + sizeof (eof));
146 root 1.1 memcpy (SvPVX (text), sv_, len);
147 root 1.32 memcpy (SvPVX (text) + len, eof, sizeof (eof));
148 root 1.1
149     line = SvPVX (text);
150 root 1.25 next ();
151 root 1.1 }
152     }
153    
154     CALL_END;
155     }
156     }
157    
158     object_thawer::object_thawer (const char *data, AV *perlav)
159 root 1.23 : name (strdup ("(memory stream)"))
160 root 1.1 {
161     av = perlav;
162     text = newSVpv (data, 0);
163     sv_catpv (text, thawer_eof);
164     line = SvPVbyte_nolen (text);
165 root 1.25 next ();
166 root 1.1 }
167    
168 root 1.4 void
169     object_thawer::get (attachable *obj, int oid)
170 root 1.1 {
171     if (!av || oid < 0) // this is actually an error of sorts
172     return;
173    
174     SV **svp = av_fetch ((AV *)av, oid, 0);
175    
176     if (!svp || !SvROK (*svp))
177     {
178 root 1.15 LOG (llevError, "trying to thaw duplicate or never-issued oid %d, ignoring.\n", oid);
179 root 1.1 return;
180     }
181    
182     if (!SvROK (*svp))
183     {
184 root 1.15 LOG (llevError, "deserialised perl object is not an RV\n");
185 root 1.1 return;
186     }
187    
188     HV *hv = (HV *)SvRV (*svp);
189    
190     if (SvTYPE (hv) != SVt_PVHV)
191     {
192 root 1.15 LOG (llevError, "deserialised perl object is not a PVHV\n");
193 root 1.1 return;
194     }
195    
196     if (obj->self)
197     {
198     // the hard way(?)
199    
200 root 1.6 CALL_BEGIN (2);
201     CALL_ARG_SV (newRV_inc ((SV *)obj->self));
202     CALL_ARG_SV (newRV_inc ((SV *)hv));
203     PUTBACK;
204     call_method ("thawer_merge", G_DISCARD | G_EVAL);
205     SPAGAIN;
206     CALL_END;
207 root 1.1 }
208     else
209     {
210     // the easy way(?)
211    
212     obj->self = hv;
213     SvRV_set (*svp, &PL_sv_undef);
214    
215     sv_magicext ((SV *)obj->self, 0, PERL_MAGIC_ext, &attachable::vtbl, (char *)obj, 0);
216     }
217    
218     obj->reattach ();
219     }
220    
221     object_thawer::~object_thawer ()
222     {
223     if (text) SvREFCNT_dec (text);
224     if (av) SvREFCNT_dec (av);
225 root 1.4
226 root 1.25 resolve_delayed_derefs (false);
227    
228 root 1.4 free ((void *)name);
229 root 1.1 }
230    
231 root 1.8 //TODO: remove
232 root 1.4 char *
233     fgets (char *s, int n, object_thawer &thawer)
234 root 1.1 {
235     char *p = thawer.line;
236     char *q = s;
237    
238     if (!p)
239     return 0;
240    
241     while (--n)
242     {
243     if (!*p)
244     break;
245    
246     *q++ = *p;
247    
248     if (*p++ == '\n')
249 root 1.8 {
250     ++thawer.linenum;
251     break;
252     }
253 root 1.1 }
254    
255     *q = 0;
256     thawer.line = p;
257    
258     return s == q ? 0 : s;
259     }
260    
261 root 1.24 void
262     object_thawer::parse_warn (const char *msg)
263     {
264     LOG (llevWarn, "%s:%d, \"%s %s\": %s\n",
265     this->name, linenum,
266     kw_str ? kw_str : "<null>",
267     value ? value : "<null>",
268     msg);
269     }
270    
271 root 1.8 bool
272 root 1.10 object_thawer::parse_error (const char *type, const char *name, bool skip)
273 root 1.8 {
274     if (!type) type = "file section";
275     if (!name) name = "generic";
276    
277     switch (kw)
278     {
279     case KW_EOF:
280     LOG (llevError, "%s:%d end of file while reading %s '%s', aborting load.\n",
281     this->name, linenum, type, name);
282     return false;
283    
284     case KW_ERROR:
285 root 1.10 LOG (llevError, "%s:%d error while reading %s '%s', at '%s', aborting load.\n",
286 root 1.8 this->name, linenum,
287 root 1.10 type, name,
288     kw_str ? kw_str : "<file load>");
289 root 1.8 return false;
290    
291     default:
292 root 1.9 LOG (llevError, "%s:%d unexpected line (%s %s) while reading %s '%s', %s.\n",
293 root 1.8 this->name, linenum,
294 root 1.10 kw_str ? kw_str : "<null>",
295 root 1.24 value ? value : "<null>",
296 root 1.8 type, name,
297     skip ? "skipping line" : "aborting load");
298     return skip;
299     }
300     }
301    
302 root 1.10 void
303 root 1.11 object_thawer::next ()
304 root 1.1 {
305     if (!line)
306 root 1.10 {
307     kw = KW_ERROR;
308     return;
309     }
310 root 1.1
311     for (;;)
312     {
313     char *p = line;
314    
315 root 1.16 if (*p <= ' ')
316     {
317     // skip whitespace (only some files need this)
318     while (*p == ' ' || *p == '\t')
319     p++;
320    
321     line = p;
322     }
323    
324 root 1.1 if (!*p)
325 root 1.10 {
326     kw = KW_EOF;
327     break;
328     }
329 root 1.1
330     // parse keyword
331     while (*p > ' ')
332     p++;
333    
334     int klen = p - line;
335    
336 root 1.19 value_nn = "";
337 root 1.16 value = 0;
338    
339 root 1.1 if (*p++ != '\n')
340     {
341     // parse value
342 root 1.16 while (*(unsigned char *)p <= ' ' && *p != '\n')
343 root 1.1 ++p;
344    
345 root 1.19 value_nn = value = p;
346 root 1.1
347     while (*p != '\n')
348     p++;
349    
350     *p++ = 0;
351     }
352    
353 root 1.8 ++linenum;
354 root 1.1 line [klen] = 0;
355 root 1.10 keyword_idx *kw_idx = kw_lex::match (line, klen);
356 root 1.1
357 root 1.10 kw_str = line;
358 root 1.1 line = p;
359    
360 root 1.10 if (kw_idx)
361     {
362     kw = kw_idx->index;
363     break;
364     }
365     else if (!*kw_str || *kw_str == '#')
366 root 1.1 ; // empty/comment line
367     else
368 root 1.10 {
369     kw = KW_ERROR;
370     break;
371     }
372 root 1.1 }
373     }
374    
375 root 1.4 void
376 root 1.11 object_thawer::skip ()
377 root 1.1 {
378     shstr ml;
379    
380     switch (kw)
381     {
382     case KW_msg: get_ml (KW_endmsg , ml); break;
383     case KW_lore: get_ml (KW_endlore , ml); break;
384     case KW_maplore: get_ml (KW_endmaplore, ml); break;
385 pippijn 1.14 default: break;
386 root 1.1 }
387 root 1.10
388 root 1.11 next ();
389 root 1.1 }
390    
391 root 1.4 void
392 root 1.25 object_thawer::skip_block ()
393     {
394 root 1.27 // must not stop at KW_ERROR, as those signify custom keys
395 root 1.26 while (kw != KW_EOF)
396 root 1.25 {
397     keyword w = kw;
398 root 1.29 skip ();
399 root 1.28
400 root 1.29 if (0 && (w == KW_map || w == KW_arch || w == KW_object || w == KW_region))
401 root 1.28 skip_block ();
402 root 1.29 else if (w == KW_end)
403     break;
404 root 1.25 }
405     }
406    
407     void
408 root 1.4 object_thawer::get_ml (keyword kend, shstr &sh)
409 root 1.1 {
410     char kw[128];
411    
412     int klen = keyword_len [kend];
413    
414     kw [0] = '\n';
415     memcpy (kw + 1, keyword_str [kend], klen);
416     kw [klen + 1] = '\n';
417     kw [klen + 2] = 0;
418    
419 root 1.8 ++linenum;
420    
421 root 1.1 // first test for completely empty msg... "endXXX\n"
422     if (!strncmp (line, kw + 1, klen + 1))
423     {
424     sh = 0;
425    
426     line += klen + 1;
427    
428     return;
429     }
430     else
431     {
432     // multi-line strings are delimited by "\nendXXX\n" or "endXXX\n" (NULL)
433    
434     char *end = strstr (line, kw);
435    
436     if (!end)
437     {
438     sh = 0;
439     return;
440     }
441    
442     *end = 0;
443     sh = line;
444    
445 root 1.8 // count line numbers
446     while (line < end)
447     linenum += *line++ == '\n';
448    
449     line += keyword_len [kend];
450 root 1.1
451     while (*line++ != '\n')
452     ;
453 root 1.8
454     ++linenum;
455 root 1.1 }
456     }
457    
458 root 1.4 sint32
459     object_thawer::get_sint32 () const
460 root 1.1 {
461 root 1.31 const char *p = value_nn;
462 root 1.1
463     sint32 val = 0;
464     bool negate;
465    
466     if (*p == '-')
467     {
468     negate = true;
469     ++p;
470     }
471     else
472     negate = false;
473    
474     do
475     {
476     val *= 10;
477     val += *p++ - '0';
478     }
479     while (*p);
480    
481     return negate ? -val : val;
482     }
483    
484 root 1.25 void
485     object_thawer::delayed_deref (attachable *op, object_ptr &ptr, const char *ref)
486     {
487     op->refcnt_inc ();
488     delayed_ref r = { op, &ptr, ref ? strdup (ref) : 0 };
489     delrefs.push_back (r);
490     ptr = 0;
491     }
492    
493     void
494     object_thawer::resolve_delayed_derefs (bool deref)
495     {
496     while (!delrefs.empty ())
497     {
498     delayed_ref r = delrefs.back ();
499     delrefs.pop_back ();
500    
501     if (deref)
502     *r.ptr = object::deref (r.ref);
503    
504     free ((void *)r.ref);
505     r.op->refcnt_dec ();
506     }
507     }