From 5f30c790dbc4a3ff469eb350e98817edd38d5de8 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Thu, 26 Aug 2004 02:01:29 -0400 Subject: fixed another memory strangeness (too much destruction) --- value.cc | 1 + value.h | 69 +++++++++++++++++++++++++++++++++++++++++----------------------- walk.h | 6 +++--- 3 files changed, 48 insertions(+), 28 deletions(-) diff --git a/value.cc b/value.cc index 83307dc0..7de21813 100644 --- a/value.cc +++ b/value.cc @@ -26,6 +26,7 @@ value_t& value_t::operator=(const value_t& value) return *this; destroy(); + switch (value.type) { case BOOLEAN: *((bool *) data) = *((bool *) value.data); diff --git a/value.h b/value.h index e2fbda66..b0687d64 100644 --- a/value.h +++ b/value.h @@ -37,7 +37,7 @@ class value_t type = INTEGER; } - value_t(const value_t& value) { + value_t(const value_t& value) : type(INTEGER) { DEBUG_PRINT("ledger.memory.ctors", "ctor value_t"); *this = value; } @@ -51,20 +51,17 @@ class value_t *((unsigned int *) data) = value; type = INTEGER; } - value_t(const amount_t& value) { + value_t(const amount_t& value) : type(INTEGER) { DEBUG_PRINT("ledger.memory.ctors", "ctor value_t"); - new((amount_t *)data) amount_t(value); - type = AMOUNT; + *this = value; } - value_t(const balance_t& value) { + value_t(const balance_t& value) : type(INTEGER) { DEBUG_PRINT("ledger.memory.ctors", "ctor value_t"); - new((balance_t *)data) balance_t(value); - type = BALANCE; + *this = value; } - value_t(const balance_pair_t& value) { + value_t(const balance_pair_t& value) : type(INTEGER) { DEBUG_PRINT("ledger.memory.ctors", "ctor value_t"); - new((balance_pair_t *)data) balance_pair_t(value); - type = BALANCE_PAIR; + *this = value; } ~value_t() { @@ -76,33 +73,55 @@ class value_t value_t& operator=(const value_t& value); value_t& operator=(const bool value) { - destroy(); - *((bool *) data) = value; - type = BOOLEAN; + if ((bool *) data != &value) { + destroy(); + *((bool *) data) = value; + type = BOOLEAN; + } return *this; } value_t& operator=(const unsigned int value) { - destroy(); - *((unsigned int *) data) = value; - type = INTEGER; + if ((unsigned int *) data != &value) { + destroy(); + *((unsigned int *) data) = value; + type = INTEGER; + } return *this; } value_t& operator=(const amount_t& value) { - destroy(); - new((amount_t *)data) amount_t(value); - type = AMOUNT; + if ((amount_t *) data != &value) { + if (! value) { + return *this = 0U; + } else { + destroy(); + new((amount_t *)data) amount_t(value); + type = AMOUNT; + } + } return *this; } value_t& operator=(const balance_t& value) { - destroy(); - new((balance_t *)data) balance_t(value); - type = BALANCE; + if ((balance_t *) data != &value) { + if (value.amounts.size() == 1) { + return *this = (*value.amounts.begin()).second; + } else { + destroy(); + new((balance_t *)data) balance_t(value); + type = BALANCE; + } + } return *this; } value_t& operator=(const balance_pair_t& value) { - destroy(); - new((balance_pair_t *)data) balance_pair_t(value); - type = BALANCE_PAIR; + if ((balance_pair_t *) data != &value) { + if (! value.cost) { + return *this = value.quantity; + } else { + destroy(); + new((balance_pair_t *)data) balance_pair_t(value); + type = BALANCE_PAIR; + } + } return *this; } diff --git a/walk.h b/walk.h index 39bde7f2..93a2a3b0 100644 --- a/walk.h +++ b/walk.h @@ -40,6 +40,9 @@ struct item_handler { template class compare_items { + value_t left_result; + value_t right_result; + const value_expr_t * sort_order; public: @@ -52,9 +55,6 @@ class compare_items { assert(left); assert(right); - value_t left_result; - value_t right_result; - sort_order->compute(left_result, details_t(left)); sort_order->compute(right_result, details_t(right)); -- cgit v1.2.3