diff --git a/src/Aircraft/FlightHistory.cxx b/src/Aircraft/FlightHistory.cxx index 410d4e582..82304fdd4 100644 --- a/src/Aircraft/FlightHistory.cxx +++ b/src/Aircraft/FlightHistory.cxx @@ -51,6 +51,7 @@ FGFlightHistory::~FGFlightHistory() void FGFlightHistory::init() { + m_enabled = fgGetNode("/sim/history/enabled", true); m_sampleInterval = fgGetDouble("/sim/history/sample-interval-sec", 1.0); if (m_sampleInterval <= 0.0) { // would be bad SG_LOG(SG_FLIGHT, SG_INFO, "invalid flight-history sample interval:" << m_sampleInterval @@ -58,6 +59,8 @@ void FGFlightHistory::init() m_sampleInterval = 1.0; } + // cap memory use at 4MB + m_maxMemoryUseBytes = fgGetInt("/sim/history/max-memory-use-bytes", 1024 * 1024 * 4); m_weightOnWheels = NULL; // reset the history when we detect a take-off if (fgGetBool("/sim/history/clear-on-takeoff", true)) { @@ -82,7 +85,9 @@ void FGFlightHistory::reinit() void FGFlightHistory::update(double dt) { - SG_UNUSED(dt); // we care about sim-time, not frame-time + if ((dt == 0.0) || !m_enabled->getBoolValue()) { + return; // paused or disabled + } if (m_weightOnWheels) { @@ -100,7 +105,14 @@ void FGFlightHistory::update(double dt) void FGFlightHistory::allocateNewBucket() { - SampleBucket* bucket = new SampleBucket; + SampleBucket* bucket = NULL; + if (!m_buckets.empty() && (currentMemoryUseBytes() > m_maxMemoryUseBytes)) { + bucket = m_buckets.front(); + m_buckets.erase(m_buckets.begin()); + } else { + bucket = new SampleBucket; + } + m_buckets.push_back(bucket); m_validSampleCount = 0; } @@ -164,3 +176,8 @@ void FGFlightHistory::clear() m_validSampleCount = SAMPLE_BUCKET_WIDTH; } +size_t FGFlightHistory::currentMemoryUseBytes() const +{ + return sizeof(SampleBucket) * m_buckets.size(); +} + diff --git a/src/Aircraft/FlightHistory.hxx b/src/Aircraft/FlightHistory.hxx index 29b0fb122..38e3d44f2 100644 --- a/src/Aircraft/FlightHistory.hxx +++ b/src/Aircraft/FlightHistory.hxx @@ -94,12 +94,17 @@ private: unsigned int m_validSampleCount; SGPropertyNode_ptr m_weightOnWheels; + SGPropertyNode_ptr m_enabled; + bool m_lastWoW; - + size_t m_maxMemoryUseBytes; + void allocateNewBucket(); void clear(); void capture(); + + size_t currentMemoryUseBytes() const; }; #endif \ No newline at end of file