… | |
… | |
28 | #include <loader.h> |
28 | #include <loader.h> |
29 | |
29 | |
30 | #include <tr1/functional> |
30 | #include <tr1/functional> |
31 | #include <tr1/unordered_map> |
31 | #include <tr1/unordered_map> |
32 | |
32 | |
33 | archetype *loading_arch; // ugly flag to object laoder etc. to suppress/request special processing |
33 | archetype *loading_arch; // ugly flag to object loader etc. to suppress/request special processing |
34 | archetype *archetype::empty; |
34 | arch_ptr archetype::empty; |
35 | |
35 | |
36 | // the hashtable |
36 | // the hashtable |
37 | typedef std::tr1::unordered_map |
37 | typedef std::tr1::unordered_map |
38 | < |
38 | < |
39 | const char *, |
39 | const char *, |
… | |
… | |
41 | str_hash, |
41 | str_hash, |
42 | str_equal, |
42 | str_equal, |
43 | slice_allocator< std::pair<const char *const, arch_ptr> > |
43 | slice_allocator< std::pair<const char *const, arch_ptr> > |
44 | > HT; |
44 | > HT; |
45 | |
45 | |
46 | static HT ht (5000); |
46 | static HT ht (10000); |
47 | archvec archetypes; |
47 | archvec archetypes; |
|
|
48 | static unordered_vector<archetype *> allarch; |
|
|
49 | static int dirtycnt; |
48 | |
50 | |
49 | // the vector of other_arch references to be resolved |
51 | // the vector of other_arch references to be resolved |
50 | static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref; |
52 | static std::vector< std::pair<arch_ptr *, shstr> > postponed_arch_ref; |
51 | // the vector of loaded but not yet committed archetypes |
53 | // the vector of loaded but not yet committed archetypes |
52 | static std::vector<archetype *> postponed_arch; |
54 | static std::vector<archetype *> postponed_arch; |
… | |
… | |
286 | } |
288 | } |
287 | |
289 | |
288 | void |
290 | void |
289 | archetype::link () |
291 | archetype::link () |
290 | { |
292 | { |
291 | ht.insert (std::make_pair (archname, this)); |
|
|
292 | |
|
|
293 | if (!archetypes.contains (this)) |
293 | if (!archetypes.contains (this)) |
|
|
294 | { |
294 | archetypes.insert (this); |
295 | archetypes.insert (this); |
|
|
296 | ht.insert (std::make_pair (archname, this)); |
|
|
297 | } |
295 | } |
298 | } |
296 | |
299 | |
297 | void |
300 | void |
298 | archetype::unlink () |
301 | archetype::unlink () |
299 | { |
302 | { |
300 | ht.erase (archname); |
|
|
301 | |
|
|
302 | if (archetypes.contains (this)) |
303 | if (archetypes.contains (this)) |
|
|
304 | { |
303 | archetypes.erase (this); |
305 | archetypes.erase (this); |
|
|
306 | ht.erase (archname); |
|
|
307 | } |
304 | } |
308 | } |
305 | |
309 | |
306 | /* |
310 | /* |
307 | * Finds, using the hashtable, which archetype matches the given name. |
311 | * Finds, using the hashtable, which archetype matches the given name. |
308 | * returns a pointer to the found archetype, otherwise NULL. |
312 | * returns a pointer to the found archetype, otherwise NULL. |
… | |
… | |
412 | archetype *less = new_head; |
416 | archetype *less = new_head; |
413 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
417 | for (auto (p, parts.begin () + 1); p != parts.end (); ++p) |
414 | { |
418 | { |
415 | archetype *at = *p; |
419 | archetype *at = *p; |
416 | |
420 | |
417 | // some flags get inherited formt he head (probably a lot more) |
421 | // some flags get inherited from the head (probably a lot more) |
418 | // doing it here doesn't feel too cozy, but it allows code |
422 | // doing it here doesn't feel too cozy, but it allows code |
419 | // to ignore head checks for these flags, which saves time |
423 | // to ignore head checks for these flags, which saves time |
420 | at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; |
424 | at->flag [FLAG_ALIVE] = new_head->flag [FLAG_ALIVE]; |
421 | at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; |
425 | at->flag [FLAG_NO_PICK] = new_head->flag [FLAG_NO_PICK]; |
422 | at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; |
426 | at->flag [FLAG_MONSTER] = new_head->flag [FLAG_MONSTER]; |
423 | at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; |
427 | at->flag [FLAG_IS_FLOOR] = new_head->flag [FLAG_IS_FLOOR]; |
424 | |
428 | |
425 | if (at->x < new_head->min_x) new_head->min_x = at->x; |
429 | 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; |
430 | 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; |
431 | 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; |
432 | new_head->max_y = max (new_head->max_y, at->y); |
429 | |
433 | |
430 | at->head = new_head; |
434 | at->head = new_head; |
431 | less->more = at; |
435 | less->more = at; |
432 | less = at; |
436 | less = at; |
433 | } |
437 | } |
… | |
… | |
460 | archetype *at = *p; |
464 | archetype *at = *p; |
461 | |
465 | |
462 | if (archetype *old = find (at->archname)) |
466 | if (archetype *old = find (at->archname)) |
463 | old->unlink (); |
467 | old->unlink (); |
464 | |
468 | |
|
|
469 | allarch.push_back (at); |
|
|
470 | |
465 | at->link (); |
471 | at->link (); |
|
|
472 | ++dirtycnt; |
466 | } |
473 | } |
467 | |
474 | |
468 | postponed_arch.clear (); |
475 | postponed_arch.clear (); |
469 | |
476 | |
470 | // now resolve arch references |
477 | // now resolve arch references |
… | |
… | |
480 | } |
487 | } |
481 | |
488 | |
482 | postponed_arch_ref.clear (); |
489 | postponed_arch_ref.clear (); |
483 | |
490 | |
484 | empty = find (shstr_empty_archetype); |
491 | empty = find (shstr_empty_archetype); |
|
|
492 | } |
|
|
493 | |
|
|
494 | void |
|
|
495 | archetype::gc () |
|
|
496 | { |
|
|
497 | int cnt = max (1, min (allarch.size () / 128, dirtycnt)); |
|
|
498 | dirtycnt = max (0, dirtycnt - cnt); |
|
|
499 | |
|
|
500 | do |
|
|
501 | { |
|
|
502 | static int idx; |
|
|
503 | |
|
|
504 | if (idx >= allarch.size ()) |
|
|
505 | if (idx) |
|
|
506 | idx = 0; |
|
|
507 | else |
|
|
508 | return; |
|
|
509 | |
|
|
510 | archetype *at = allarch [idx]; |
|
|
511 | |
|
|
512 | if (at->refcnt_cnt () > 1) // all arches have ONE refcount from their object |
|
|
513 | ++idx; |
|
|
514 | else |
|
|
515 | { |
|
|
516 | LOG (llevDebug, "garbage collect arch %s", at->debug_desc ()); |
|
|
517 | assert (at->arch == at); // verify that refcnt == 1 is truely valid |
|
|
518 | allarch.erase (idx); |
|
|
519 | at->arch = 0; |
|
|
520 | at->more = 0; |
|
|
521 | at->destroy (); |
|
|
522 | } |
|
|
523 | } |
|
|
524 | while (--cnt); |
485 | } |
525 | } |
486 | |
526 | |
487 | /* |
527 | /* |
488 | * Creates and returns a new object which is a copy of the given archetype. |
528 | * Creates and returns a new object which is a copy of the given archetype. |
489 | * This function returns NULL on failure. |
529 | * This function returns NULL on failure. |