473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Static Region Allocator...

This region allocator is not dynamic and can be fed with a buffer residing
on the stack of the calling thread. Therefore, you can use this in an
environment which does not have a heap; its basically analogous to `alloca'.
However, it does not align each buffer on a boundary sufficient for _any_
type. Instead, it dynamically calculates the types alignment requirements
and mutates the offset accordingly. This makes it more efficient wrt
conserving space. For instance, it does not need to align a char pointer on
a max-boundary. Here is the code (pre-alpha sketch) which should compile
fine:
__________________________________________________ ______________________
#include <cstdio>
#include <cstddef>
#include <cassert>
#include <new>
#if ! defined(NDEBUG)
# include <typeinfo>
# define DBG_PRINTF(mp_exp) std::printf mp_exp
#else
# define DBG_PRINTF(mp_exp)
#endif


#define ALIGN_POW2(mp_this, mp_type) ((mp_type)( \
(((std::ptrdiff_t const)(mp_this)) + 1) & (-2) \
))
#define ALIGN(mp_this, mp_type, mp_align) ((mp_type)( \
(((std::ptrdiff_t const)(mp_this)) + \
ALIGN_POW2(mp_align, std::ptrdiff_t const) - 1) \
& (-ALIGN_POW2(mp_align, std::ptrdiff_t const)) \
))


class stack_region_allocator {
unsigned char* const m_buf;
std::size_t const m_size;
std::size_t m_offset;
unsigned m_count;
public:
stack_region_allocator(
unsigned char* const buf,
std::size_t const size
) throw(std::bad_alloc)
: m_buf(buf),
m_size(size),
m_offset(0),
m_count(0) {
if (! m_buf || ! m_size) {
assert(m_buf);
assert(m_size);
throw std::bad_alloc();
}
}

~stack_region_allocator() throw() {
assert(! m_offset);
assert(! m_count);
}
public:
template<typename T>
T* allocate(
std::size_t count = 1
) throw(std::bad_alloc) {

struct offset_calc {
char pad;
T object;
};

if (! count) {
count = 1;
}

DBG_PRINTF(("typename: %s\n", typeid(T).name()));
DBG_PRINTF(("object count: %lu\n",
(unsigned long)count));

DBG_PRINTF(("object size: %lu\n",
(unsigned long)sizeof(T)));

std::size_t const alignment = offsetof(offset_calc, object);
DBG_PRINTF(("object alignment: %lu\n",
(unsigned long)alignment));

std::size_t const osize = count * sizeof(T);
DBG_PRINTF(("allocation size: %lu\n",
(unsigned long)osize));

unsigned char* const origin = m_buf + m_offset;
DBG_PRINTF(("origin buffer: %p\n", (void*)origin));

unsigned char* const align = (alignment != 1) ?
ALIGN(origin, unsigned char*, alignment) : origin;
DBG_PRINTF(("align buffer: %p\n", (void*)align));

std::ptrdiff_t const diff = align - origin;
DBG_PRINTF(("difference size: %d\n", diff));

std::size_t const offset = m_offset + diff;
DBG_PRINTF(("offset pre-size: %lu\n",
(unsigned long)offset));

if (offset + osize m_size) {
throw std::bad_alloc();
}

m_offset = offset + osize;
DBG_PRINTF(("offset post-size: %lu\n\
--------------------------------------\n",
(unsigned long)(offset + osize)));

++m_count;

return reinterpret_cast<T*>(align);
}

void deallocate(void*) throw() {
if (! (--m_count)) {
m_offset = 0;
}
}

void reset() throw() {
m_count = 0;
m_offset = 0;
}
};


struct foo {
char c[5];
short s[2];
int i[3];
double d[3];
long double ld[2];
};
int main() {
{
unsigned char buf[1024 * 8] = { '\0' };
stack_region_allocator region(buf, sizeof(buf));
long double* ld1 = region.allocate<long double>();
char* c = region.allocate<char>(125);
long double* ld2 = region.allocate<long double>(4);
double* d = region.allocate<double>(2);
short* s = region.allocate<short>(7);
float* f = region.allocate<float>(5);
foo* _fo = region.allocate<foo>(0);
foo* _foa = region.allocate<foo>(12);
char* c2 = region.allocate<char>(13);
char* c3 = region.allocate<char>(17);
short* s1 = region.allocate<short>(3);
foo* _foa1 = region.allocate<foo>(3);
region.reset();
}
std::puts("\n\n\n_________________________________ ___________\
_________________\npress <ENTERto exit...");
std::getchar();
return 0;
}

__________________________________________________ ______________________


Here is a link to the same code just in case it gets mangled by the
newsreader:

http://pastebin.com/f7cfdef4d


Any thoughts? Is it CRAP!?

;^o

--
Chris M. Thomasson
http://appcore.home.comcast.net

Jun 27 '08 #1
1 2031
"Chris Thomasson" <cr*****@comcast.netwrote in message
news:Do******************************@comcast.com. ..
This region allocator is not dynamic and can be fed with a buffer residing
on the stack of the calling thread. Therefore, you can use this in an
environment which does not have a heap; its basically analogous to
`alloca'. However, it does not align each buffer on a boundary sufficient
for _any_ type. Instead, it dynamically calculates the types alignment
requirements and mutates the offset accordingly. This makes it more
efficient wrt conserving space. For instance, it does not need to align a
char pointer on a max-boundary. Here is the code (pre-alpha sketch) which
should compile fine:
[...]
Any thoughts? Is it CRAP!?
Here is how to use it with a non-POD type:
__________________________________________________ ____________
#include <string>
#include <cstdio>
#include <new>
#define PRINTF_THIS(mp_type, mp_func) \
std::printf("(%p)->" mp_type "::" mp_func "\n", (void*)this)
struct foo_ctor {
std::string const m_name;

foo_ctor(std::string const& name) : m_name(name) {
PRINTF_THIS("foo_ctor", "foo_ctor()");
}

~foo_ctor() {
PRINTF_THIS("foo_ctor", "~foo_ctor()");
}

void display_name() {
PRINTF_THIS("foo_ctor", "display_name()");
std::printf("%s\n", m_name.c_str());
}
};
int main() {
{
unsigned char buf[1024 * 8] = { '\0' };
stack_region_allocator region(buf, sizeof(buf));
foo_ctor* fctor = new (region.allocate<foo_ctor>()) foo_ctor("Chris");
fctor->display_name();
fctor->~foo_ctor();
region.reset();
}
std::puts("\n\n\n_________________________________ ___________\
_________________\npress <ENTERto exit...");
std::getchar();
return 0;
}

__________________________________________________ ____________
No need to call delete; just the dtor.

;^o
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bernhard Kick | last post by:
Hi all, I saw this code in the book "Accelerated C++" (chapt 11, iirc): template <class T> class Vec { ... std::allocator<T> alloc; // object to handle memory allocation // ??would static...
3
by: Diebels | last post by:
Hi, I have some problems using static variables which results in a core dump. I have attached code and coredump to the end of my message. I am trying to implement a kind of factory design. I...
15
by: Bit byte | last post by:
I am writing a small parser object. I need to store keywords etc in lsts. Because this data is to be shared by all instances of my parser class, I have declared the variable as class variables...
3
by: salunkerahul | last post by:
Hello All, I have some static libraries generated on MSVC 2005 express edition and I have to use them along with static libraries created with MSVC 2003 and create an application on MSVC2003. ...
1
by: toefraz | last post by:
Hey, guys. I have an assignment to work with static data members with numbers. I've done all the programming needed, but I keep getting this error when I try to compile: 1>Number.obj : error...
3
by: Chris Thomasson | last post by:
Here is the VERY crude initial code: http://pastebin.com/m2dacea67 The allocator overloads the global new/delete operators when 'TEST_USE_REGION_ALLOCATOR' is defined. This makes it easier...
11
by: Chris Thomasson | last post by:
Here is the code: http://pastebin.com/m4a405e67 One advantage to using a region allocator is that you can usually skip most calls to free. Instead you can merge multiple free calls into a...
5
by: Bob Doe | last post by:
I have a const static object. What is the right syntax to get a reference to the std::vector within the std::map variable?: class MyObj { public: ... std::map<std::string,...
8
by: Chris M. Thomasson | last post by:
Here is the initial crude implmentation which compiles under Comeau with no warnings: ___________________________________________________________________ #include <cassert> #include <cstdlib>...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.