[Assorted-commits] SF.net SVN: assorted: [391] cpp-commons/trunk/src/commons/region.h
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-02-12 17:08:52
|
Revision: 391 http://assorted.svn.sourceforge.net/assorted/?rev=391&view=rev Author: yangzhang Date: 2008-02-12 09:08:58 -0800 (Tue, 12 Feb 2008) Log Message: ----------- added support for arbitrary-sized allocations Modified Paths: -------------- cpp-commons/trunk/src/commons/region.h Modified: cpp-commons/trunk/src/commons/region.h =================================================================== --- cpp-commons/trunk/src/commons/region.h 2008-02-12 16:45:07 UTC (rev 390) +++ cpp-commons/trunk/src/commons/region.h 2008-02-12 17:08:58 UTC (rev 391) @@ -16,45 +16,62 @@ const size_t default_chunk_size = 100000000; /** + * A fixed-size buffer (really just a pair of length and pointer). + */ + struct chunk + { + size_t size; + char* data; + }; + + /** * Simple dynamically growing memory region that does not support * deallocation. */ class mem_region { private: - vector<char*> chunks; + vector<chunk> chunks; + /** + * The default chunk-size when incrementally resizing; we can still handle + * larger allocations. + */ const size_t chunk_size; size_t top; - int m_refcount; + int refcount; public: mem_region() : chunk_size(default_chunk_size), top(0), - m_refcount(0) + refcount(0) { - chunks.push_back(new char[chunk_size]); + chunks.push_back((chunk) {chunk_size, new char[chunk_size]}); } ~mem_region() { + // cout << "deleting" << endl; for (unsigned int i = 0; i < chunks.size(); i++) { - delete [] chunks[i]; + delete [] chunks[i].data; } } - void addref() { ++m_refcount; } - void decref() { if (--m_refcount == 0) { delete this; } } + void addref() { ++refcount; } + void decref() { if (--refcount == 0) { delete this; } } void *alloc_mem(size_t n) { if (top + n > chunk_size) { - chunks.push_back(new char[chunk_size]); + // chunks.push_back(new char[chunk_size]); + size_t sz = max(n, chunk_size); + chunks.push_back((chunk) {sz, new char[sz] }); top = 0; } + // cout << "alloced" << endl; size_t old_top = top; top += n; - return chunks.back() + old_top; + return chunks.back().data + old_top; } }; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |