[Assorted-commits] SF.net SVN: assorted: [396] cpp-commons/trunk/src/commons/region.h
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-02-12 22:13:00
|
Revision: 396 http://assorted.svn.sourceforge.net/assorted/?rev=396&view=rev Author: yangzhang Date: 2008-02-12 14:13:04 -0800 (Tue, 12 Feb 2008) Log Message: ----------- more space-saving; disabled debugs 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 21:58:51 UTC (rev 395) +++ cpp-commons/trunk/src/commons/region.h 2008-02-12 22:13:04 UTC (rev 396) @@ -7,6 +7,7 @@ // XXX: debug #include <set> +#include <vector> #include <pthread.h> namespace commons @@ -17,7 +18,7 @@ /** * Default chunk size is 100MB. */ - const size_t default_chunk_size = 100000000; + const size_t default_chunk_size = 10000000; /** * A fixed-size buffer (really just a pair of length and pointer). @@ -51,13 +52,13 @@ top(0), refcount(0) { - cout << "created " << this << endl; + // cout << "created " << this << endl; chunks.push_back((chunk) {chunk_size, new char[chunk_size]}); } ~mem_region() { - cout << "deleting" << endl; + // cout << "deleting" << endl; for (unsigned int i = 0; i < chunks.size(); i++) { delete [] chunks[i].data; } @@ -68,18 +69,29 @@ void *alloc_mem(size_t n) { - pthread_t t = pthread_self(); - if (threads.insert(t).second) - cout << this << " -- " << t << endl; + // pthread_t t = pthread_self(); + // if (threads.insert(t).second) + // cout << this << " -- " << t << endl; - if (top + n > chunks.back().size) { + chunk* last = &chunks.back(); + if (top + n > last->size) { size_t sz = max(n, chunk_size); - chunks.push_back((chunk) {sz, new char[sz] }); - top = 0; + chunk c = {sz, new char[sz]}; + if (n > chunk_size) { + // cout << "insert " << this << endl; + chunks.insert(chunks.begin(), c); + return c.data; + } else { + // cout << "append " << this << endl; + chunks.push_back(c); + top = 0; + last = &chunks.back(); + } } + // cout << "ALLOC " << this << endl; size_t old_top = top; top += n; - return chunks.back().data + old_top; + return last->data + old_top; } }; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |