F
Francesco S. Carta
Hi there,
after having been taught that nifty new thing about overriding
"operator new" and "operator delete", I've started messing with them
and with custom allocations.
Now I have some kind of memory pool which allocates a big chunk of
memory via "new char[pool_size]" and some functions which return sub-
chunks of it via void*.
For the moment I prefer to skip about the details - I will post the
implementation later in order to straighten out something more, with
your help - if I'll feel brave enough to actually post it.
So then, I'm testing it in this way:
-------
char* pchar = static_cast<char*>(Arena::allocate(sizeof(char)*3));
short* pshort = static_cast<short*>(Arena::allocate(sizeof(short)
*3));
int* pint = static_cast<int*>(Arena::allocate(sizeof(int)*3));
for(int i = 0; i < 3; ++i) {
pchar = i+1;
pshort = i+1;
pint = i+1;
}
-------
A straight hex-dump of the pool gives me this:
-------
010203010002000300010000000200000003000000adbeefdeadbeef...
-------
Where the "deadbeef" pattern highlights the unused part of the pool.
Well that made me wonder. I'd have expected to hit some memory
alignment problem, instead I am getting a nicely packed and (so far)
working pool... how's that?
My implementation aligns types to their size boundaries when laying
out classes, for example:
-------
struct foo {
bool b;
int i;
char c;
long long ll;
foo() : b(1), i(2), c(3), ll(4) {}
};
-------
Dumping the memory of a foo instance I get the following:
-------
01adbeef0200000003adbeefdeadbeef0400000000000000
-------
Where the "deadbeef" pattern highlights the padding.
Why these two different behaviors?
Am I simply lucky getting my pool so nicely packed?
Honestly, I somehow hoped for problems to show up... but they didn't
yet.
I'll continue experimenting.
after having been taught that nifty new thing about overriding
"operator new" and "operator delete", I've started messing with them
and with custom allocations.
Now I have some kind of memory pool which allocates a big chunk of
memory via "new char[pool_size]" and some functions which return sub-
chunks of it via void*.
For the moment I prefer to skip about the details - I will post the
implementation later in order to straighten out something more, with
your help - if I'll feel brave enough to actually post it.
So then, I'm testing it in this way:
-------
char* pchar = static_cast<char*>(Arena::allocate(sizeof(char)*3));
short* pshort = static_cast<short*>(Arena::allocate(sizeof(short)
*3));
int* pint = static_cast<int*>(Arena::allocate(sizeof(int)*3));
for(int i = 0; i < 3; ++i) {
pchar = i+1;
pshort = i+1;
pint = i+1;
}
-------
A straight hex-dump of the pool gives me this:
-------
010203010002000300010000000200000003000000adbeefdeadbeef...
-------
Where the "deadbeef" pattern highlights the unused part of the pool.
Well that made me wonder. I'd have expected to hit some memory
alignment problem, instead I am getting a nicely packed and (so far)
working pool... how's that?
My implementation aligns types to their size boundaries when laying
out classes, for example:
-------
struct foo {
bool b;
int i;
char c;
long long ll;
foo() : b(1), i(2), c(3), ll(4) {}
};
-------
Dumping the memory of a foo instance I get the following:
-------
01adbeef0200000003adbeefdeadbeef0400000000000000
-------
Where the "deadbeef" pattern highlights the padding.
Why these two different behaviors?
Am I simply lucky getting my pool so nicely packed?
Honestly, I somehow hoped for problems to show up... but they didn't
yet.
I'll continue experimenting.