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

Comparing deliantra/server/common/arch.C (file contents):
Revision 1.73 by root, Sun Apr 20 00:44:12 2008 UTC vs.
Revision 1.79 by root, Sun May 4 11:12:40 2008 UTC

22 */ 22 */
23 23
24#include <cassert> 24#include <cassert>
25 25
26#include <global.h> 26#include <global.h>
27#include <funcpoint.h>
28#include <loader.h> 27#include <loader.h>
29 28
30#include <tr1/functional> 29#include <tr1/functional>
31#include <tr1/unordered_map> 30#include <tr1/unordered_map>
32 31
33archetype *loading_arch; // ugly flag to object laoder etc. to suppress/request special processing 32archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing
34archetype *archetype::empty; 33arch_ptr archetype::empty;
35 34
36// the hashtable 35// the hashtable
37typedef std::tr1::unordered_map 36typedef std::tr1::unordered_map
38 < 37 <
39 const char *, 38 const char *,
41 str_hash, 40 str_hash,
42 str_equal, 41 str_equal,
43 slice_allocator< std::pair<const char *const, arch_ptr> > 42 slice_allocator< std::pair<const char *const, arch_ptr> >
44 > HT; 43 > HT;
45 44
46static HT ht (5000); 45static HT ht (10000);
47archvec archetypes; 46archvec archetypes;
47static unordered_vector<archetype *> allarch;
48static int dirtycnt;
48 49
49// the vector of other_arch references to be resolved 50// the vector of other_arch references to be resolved
50static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref; 51static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref;
51// the vector of loaded but not yet committed archetypes 52// the vector of loaded but not yet committed archetypes
52static std::vector<archetype *> postponed_arch; 53static std::vector<archetype *> postponed_arch;
132 * - name: The name we're searching for (ex: "writing pen"); 133 * - name: The name we're searching for (ex: "writing pen");
133 * Return value: 134 * Return value:
134 * - a corresponding object if found; a singularity object if not found. 135 * - a corresponding object if found; a singularity object if not found.
135 * Note by MSW - it appears that it takes the full name and keeps 136 * Note by MSW - it appears that it takes the full name and keeps
136 * shortening it until it finds a match. I re-wrote this so that it 137 * shortening it until it finds a match. I re-wrote this so that it
137 * doesn't malloc it each time - not that this function is used much, 138 * doesn't allocate it each time - not that this function is used much,
138 * but it otherwise had a big memory leak. 139 * but it otherwise had a big memory leak.
139 */ 140 */
140object * 141object *
141get_archetype_by_object_name (const char *name) 142get_archetype_by_object_name (const char *name)
142{ 143{
286} 287}
287 288
288void 289void
289archetype::link () 290archetype::link ()
290{ 291{
291 ht.insert (std::make_pair (archname, this));
292
293 if (!archetypes.contains (this)) 292 if (!archetypes.contains (this))
293 {
294 archetypes.insert (this); 294 archetypes.insert (this);
295 ht.insert (std::make_pair (archname, this));
296 }
295} 297}
296 298
297void 299void
298archetype::unlink () 300archetype::unlink ()
299{ 301{
300 ht.erase (archname);
301
302 if (archetypes.contains (this)) 302 if (archetypes.contains (this))
303 {
303 archetypes.erase (this); 304 archetypes.erase (this);
305 ht.erase (archname);
306 }
304} 307}
305 308
306/* 309/*
307 * Finds, using the hashtable, which archetype matches the given name. 310 * Finds, using the hashtable, which archetype matches the given name.
308 * returns a pointer to the found archetype, otherwise NULL. 311 * returns a pointer to the found archetype, otherwise NULL.
412 archetype *less = new_head; 415 archetype *less = new_head;
413 for (auto (p, parts.begin () + 1); p != parts.end (); ++p) 416 for (auto (p, parts.begin () + 1); p != parts.end (); ++p)
414 { 417 {
415 archetype *at = *p; 418 archetype *at = *p;
416 419
417 // some flags get inherited formt he head (probably a lot more) 420 // some flags get inherited from the head (probably a lot more)
418 // doing it here doesn't feel too cozy, but it allows code 421 // doing it here doesn't feel too cozy, but it allows code
419 // to ignore head checks for these flags, which saves time 422 // to ignore head checks for these flags, which saves time
420 at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; 423 at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE];
421 at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; 424 at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK];
422 at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; 425 at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER];
423 at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; 426 at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR];
424 427
425 if (at->x < new_head->min_x) new_head->min_x = at->x; 428 new_head->min_x = min (new_head->min_x, at->x);
426 if (at->y < new_head->min_y) new_head->min_y = at->y; 429 new_head->min_y = min (new_head->min_y, at->y);
427 if (at->x > new_head->max_x) new_head->max_x = at->x; 430 new_head->max_x = max (new_head->max_x, at->x);
428 if (at->y > new_head->max_y) new_head->max_y = at->y; 431 new_head->max_y = max (new_head->max_y, at->y);
429 432
430 at->head = new_head; 433 at->head = new_head;
431 less->more = at; 434 less->more = at;
432 less = at; 435 less = at;
433 } 436 }
460 archetype *at = *p; 463 archetype *at = *p;
461 464
462 if (archetype *old = find (at->archname)) 465 if (archetype *old = find (at->archname))
463 old->unlink (); 466 old->unlink ();
464 467
468 allarch.push_back (at);
469
465 at->link (); 470 at->link ();
471 ++dirtycnt;
466 } 472 }
467 473
468 postponed_arch.clear (); 474 postponed_arch.clear ();
469 475
470 // now resolve arch references 476 // now resolve arch references
480 } 486 }
481 487
482 postponed_arch_ref.clear (); 488 postponed_arch_ref.clear ();
483 489
484 empty = find (shstr_empty_archetype); 490 empty = find (shstr_empty_archetype);
491}
492
493void
494archetype::gc ()
495{
496 int cnt = max (1, min (allarch.size () / 128, dirtycnt));
497 dirtycnt = max (0, dirtycnt - cnt);
498
499 do
500 {
501 static int idx;
502
503 if (idx >= allarch.size ())
504 if (idx)
505 idx = 0;
506 else
507 return;
508
509 archetype *at = allarch [idx];
510
511 if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object
512 ++idx;
513 else
514 {
515 LOG (llevDebug, "garbage collect arch %s", at->debug_desc ());
516 assert (at->arch == at); // verify that refcnt == 1 is truly valid
517 allarch.erase (idx);
518
519 // break chain
520 for (object *op = at->head_ (); op; )
521 {
522 object *next = op->more;
523 op->head = 0;
524 op->more = 0;
525 op = next;
526 }
527
528 at->destroy ();
529 at->arch = 0;
530 }
531 }
532 while (--cnt);
485} 533}
486 534
487/* 535/*
488 * Creates and returns a new object which is a copy of the given archetype. 536 * Creates and returns a new object which is a copy of the given archetype.
489 * This function returns NULL on failure. 537 * This function returns NULL on failure.
546 return create_singularity (name); 594 return create_singularity (name);
547 595
548 return arch_to_object (at); 596 return arch_to_object (at);
549} 597}
550 598
599object *
600archetype::get (const char *name)
601{
602 return get_archetype (name);
603}
604
551/* 605/*
552 * Returns the first archetype using the given type. 606 * Returns the first archetype using the given type.
553 * Used in treasure-generation. 607 * Used in treasure-generation.
554 */ 608 */
555archetype * 609archetype *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines