113820Sgabeblack@google.com/*
213820Sgabeblack@google.com * Copyright (c) 2015, University of Kaiserslautern
313820Sgabeblack@google.com * All rights reserved.
413820Sgabeblack@google.com *
513820Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without
613820Sgabeblack@google.com * modification, are permitted provided that the following conditions are
713820Sgabeblack@google.com * met:
813820Sgabeblack@google.com *
913820Sgabeblack@google.com * 1. Redistributions of source code must retain the above copyright notice,
1013820Sgabeblack@google.com *    this list of conditions and the following disclaimer.
1113820Sgabeblack@google.com *
1213820Sgabeblack@google.com * 2. Redistributions in binary form must reproduce the above copyright
1313820Sgabeblack@google.com *    notice, this list of conditions and the following disclaimer in the
1413820Sgabeblack@google.com *    documentation and/or other materials provided with the distribution.
1513820Sgabeblack@google.com *
1613820Sgabeblack@google.com * 3. Neither the name of the copyright holder nor the names of its
1713820Sgabeblack@google.com *    contributors may be used to endorse or promote products derived from
1813820Sgabeblack@google.com *    this software without specific prior written permission.
1913820Sgabeblack@google.com *
2013820Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2113820Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2213820Sgabeblack@google.com * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2313820Sgabeblack@google.com * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
2413820Sgabeblack@google.com * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2513820Sgabeblack@google.com * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2613820Sgabeblack@google.com * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2713820Sgabeblack@google.com * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2813820Sgabeblack@google.com * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2913820Sgabeblack@google.com * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3013820Sgabeblack@google.com * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3113820Sgabeblack@google.com *
3213820Sgabeblack@google.com * Authors:
3313820Sgabeblack@google.com *    Robert Gernhardt
3413820Sgabeblack@google.com *    Matthias Jung
3513820Sgabeblack@google.com */
3613820Sgabeblack@google.com
3713820Sgabeblack@google.com#include "systemc/tlm_bridge/sc_mm.hh"
3813820Sgabeblack@google.com
3913820Sgabeblack@google.comnamespace Gem5SystemC
4013820Sgabeblack@google.com{
4113820Sgabeblack@google.com
4213820Sgabeblack@google.comMemoryManager::MemoryManager() : numberOfAllocations(0), numberOfFrees(0) {}
4313820Sgabeblack@google.com
4413820Sgabeblack@google.comMemoryManager::~MemoryManager()
4513820Sgabeblack@google.com{
4613820Sgabeblack@google.com    for (gp *payload: freePayloads) {
4713820Sgabeblack@google.com        delete payload;
4813820Sgabeblack@google.com        numberOfFrees++;
4913820Sgabeblack@google.com    }
5013820Sgabeblack@google.com}
5113820Sgabeblack@google.com
5213820Sgabeblack@google.comgp *
5313820Sgabeblack@google.comMemoryManager::allocate()
5413820Sgabeblack@google.com{
5513820Sgabeblack@google.com    if (freePayloads.empty()) {
5613820Sgabeblack@google.com        numberOfAllocations++;
5713820Sgabeblack@google.com        return new gp(this);
5813820Sgabeblack@google.com    } else {
5913820Sgabeblack@google.com        gp *result = freePayloads.back();
6013820Sgabeblack@google.com        freePayloads.pop_back();
6113820Sgabeblack@google.com        return result;
6213820Sgabeblack@google.com    }
6313820Sgabeblack@google.com}
6413820Sgabeblack@google.com
6513820Sgabeblack@google.comvoid
6613820Sgabeblack@google.comMemoryManager::free(gp *payload)
6713820Sgabeblack@google.com{
6813820Sgabeblack@google.com    payload->reset(); // clears all extensions
6913820Sgabeblack@google.com    freePayloads.push_back(payload);
7013820Sgabeblack@google.com}
7113820Sgabeblack@google.com
7213820Sgabeblack@google.com} // namespace Gem5SystemC
73