Loading

Paste #p2vpdxstj

  1. Index: src/saveload/engine_sl.cpp
  2. ===================================================================
  3. --- src/saveload/engine_sl.cpp  (revision 27242)
  4. +++ src/saveload/engine_sl.cpp  (working copy)
  5. @@ -48,21 +48,39 @@
  6.     SLE_END()
  7.  };
  8.  
  9. -static std::vector<Engine> _temp_engine;
  10. +static std::vector<Engine*> _temp_engine;
  11.  
  12. +/**
  13. + * Allocate an Engine structure, but not using the pools.
  14. + * The allocated Engine must be freed using FreeEngine;
  15. + * @return Allocated engine.
  16. + */
  17. +static Engine* CallocEngine()
  18. +{
  19. +   uint8 *zero = CallocT<uint8>(sizeof(Engine));
  20. +   Engine *engine = new (zero) Engine();
  21. +   return engine;
  22. +}
  23. +
  24. +/**
  25. + * Deallocate an Engine constructed by CallocEngine.
  26. + * @param e Engine to free.
  27. + */
  28. +static void FreeEngine(Engine *e)
  29. +{
  30. +   if (e != NULL) {
  31. +       e->~Engine();
  32. +       free(e);
  33. +   }
  34. +}
  35. +
  36.  Engine *GetTempDataEngine(EngineID index)
  37.  {
  38.     if (index < _temp_engine.size()) {
  39. -       return &_temp_engine[index];
  40. +       return _temp_engine[index];
  41.     } else if (index == _temp_engine.size()) {
  42. -       uint8 zero[sizeof(Engine)];
  43. -       memset(zero, 0, sizeof(zero));
  44. -       Engine *engine = new (zero) Engine();
  45. -
  46. -       /* Adding 'engine' to the vector makes a shallow copy, so we do not want to destruct 'engine' */
  47. -       _temp_engine.push_back(*engine);
  48. -
  49. -       return &_temp_engine[index];
  50. +       _temp_engine.push_back(CallocEngine());
  51. +       return _temp_engine[index];
  52.     } else {
  53.         NOT_REACHED();
  54.     }
  55. @@ -127,6 +145,9 @@
  56.     }
  57.  
  58.     /* Get rid of temporary data */
  59. +   for (std::vector<Engine*>::iterator it = _temp_engine.begin(); it != _temp_engine.end(); ++it) {
  60. +       FreeEngine(*it);
  61. +   }
  62.     _temp_engine.clear();
  63.  }
  64.  
  65.  

Comments