=== modified file 'src/MemBlob.cc' --- src/MemBlob.cc 2010-12-03 01:56:04 +0000 +++ src/MemBlob.cc 2011-04-05 15:51:29 +0000 @@ -15,50 +15,48 @@ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. */ #include "config.h" #include "base/TextException.h" #include "Debug.h" +#include "Mem.h" #include "MemBlob.h" +#include "protos.h" + #if HAVE_IOSTREAM #include #endif -#define MEMBLOB_USES_MEM_POOLS 0 - -#if MEMBLOB_USES_MEM_POOLS -#include "protos.h" -#endif MemBlobStats MemBlob::Stats; InstanceIdDefinitions(MemBlob, "blob"); /* MemBlobStats */ MemBlobStats::MemBlobStats(): alloc(0), live(0), append(0) {} std::ostream& MemBlobStats::dump(std::ostream &os) const { os << "MemBlob created: " << alloc << "\nMemBlob alive: " << live << "\nMemBlob append calls: " << append << "\nMemBlob currently allocated size: " << liveBytes << "\nlive MemBlob mean current allocation size: " << (static_cast(liveBytes)/(live?live:1)) << std::endl; @@ -73,95 +71,71 @@ { debugs(MEMBLOB_DEBUGSECTION,9, HERE << "constructed, this=" << static_cast(this) << " id=" << id << " reserveSize=" << reserveSize); memAlloc(reserveSize); } MemBlob::MemBlob(const char *buffer, const MemBlob::size_type bufSize) : mem(NULL), capacity(0), size(0) // will be set by memAlloc { debugs(MEMBLOB_DEBUGSECTION,9, HERE << "constructed, this=" << static_cast(this) << " id=" << id << " buffer=" << static_cast(buffer) << " bufSize=" << bufSize); memAlloc(bufSize); append(buffer, bufSize); } MemBlob::~MemBlob() { -#if MEMBLOB_USES_MEM_POOLS - //no mempools for now - // \todo reinstate mempools use memFreeString(capacity,mem); -#else - xfree(mem); -#endif Stats.liveBytes -= capacity; --Stats.live; debugs(MEMBLOB_DEBUGSECTION,9, HERE << "destructed, this=" << static_cast(this) << " id=" << id << " capacity=" << capacity << " size=" << size); } -/** - * Given the requested minimum size, return a rounded allocation size +/** Given the requested minimum size, return a rounded allocation size * for the backing store. - * This is a stopgap call, this job is eventually expected to be handled - * by MemPools via memAllocString. */ -MemBlob::size_type +// TODO: exploit better integration with MemPools, moving much of this there. +// TODO: have MemPools do system page size magic. They don't really belong here +inline MemBlob::size_type MemBlob::calcAllocSize(const size_type sz) const { - if (sz <= 36) return 36; - if (sz <= 128) return 128; - if (sz <= 512) return 512; - if (sz <= 4096) return RoundTo(sz, 512); - // XXX: recover squidSystemPageSize functionality. It's easy for - // the main squid, harder for tests -#if 0 - return RoundTo(sz, squidSystemPageSize); -#else - return RoundTo(sz, 4096); -#endif + return sz; } /** Allocate an available space area of at least minSize bytes in size. * Must be called by constructors and only by constructors. */ void MemBlob::memAlloc(const size_type minSize) { size_t actualAlloc = calcAllocSize(minSize); Must(!mem); -#if MEMBLOB_USES_MEM_POOLS - // XXX: for now, do without mempools. In order to do it, MemPools - // need to be singletons so that initialization order can be enforced - mem = static_cast(memAllocString(minSize, &actualAlloc)); -#else - // \todo reinstate mempools use - mem = static_cast(xmalloc(actualAlloc)); -#endif + mem = static_cast(memAllocString(actualAlloc, &actualAlloc)); Must(mem); capacity = actualAlloc; size = 0; debugs(MEMBLOB_DEBUGSECTION, 8, id << " memAlloc: requested=" << minSize << ", received=" << capacity); ++Stats.live; ++Stats.alloc; Stats.liveBytes += capacity; } void MemBlob::append(const char *source, const size_type n) { if (n > 0) { // appending zero bytes is allowed but only affects the stats Must(willFit(n)); Must(source); /// \note memcpy() is safe because we copy to an unused area memcpy(mem + size, source, n);