controling allocated resources

  • Thread starter =?ISO-8859-2?Q?Rafa=B3?=
  • Start date
?

=?ISO-8859-2?Q?Rafa=B3?=

Hi!

I would like to make my program know how much memmory it consumes at
runtime. At now I thought to do something like that:

/* test.cpp */
#include <cstdlib>
#include <stdio.h>

unsigned long long memory = 0;
bool makeout = false;

void* operator new[](size_t sz) {
void* m = malloc(sz);

if(m) {
memory += sz;
if (makeout) printf("Allocated by new[]: %i\n", sz);
}

return m;
}

void operator delete[](void* m) {
if (!m) return;

memory -= sizeof(m);
if (makeout) printf("Deallocated by delete[]: %i\n", sizeof(m));

free(m);
}

int main() {
memory = 0;
makeout = true;

int* p = new int[47];
delete [] p;

makeout = false;
}
/* End of file */

But of course it won't work fine becouse of "sizeof" used in delete[]...
:(

The workaround I found is to make things like this:

void* operator new[](size_t sz) {
void* m = malloc(sz+4);

if(m) {
memory += sz+4;
*(unsigned int*)m = sz+4;
m = (char*)m+4;
if (makeout) printf("Allocated by new[]: %i\n", sz+4);
}

return m;
}

void operator delete[](void* m) {
if (!m) return;

m = (char*)m-4;
memory -= *(unsigned int*)m;
if (makeout) printf("Deallocated by delete[]: %i\n", *(unsigned int*)m);

free(m);
}

But this one makes a coast of 4 bytes on every allocation, which is not what
can satisfy me :(

Any suggestion welcome.
Thanks in advance,
Rafal
 
?

=?ISO-8859-2?Q?Rafa=B3?=


Its describes some interesting technics but not solving my problem.

The best I could expect (but I think is not possible :( ), is to know how to
get the size of object passed to delete operator...

I'm not looking for very advanced way to get trace over all my program
allocations / deallocations, but simple, efficient, with small additional
coast way to get know, that my program after a week/month/year of
continuous work takes still "same" amount of memory...

Rafal
 
D

davidrubin

[snip]
The workaround I found is to make things like this:

void* operator new[](size_t sz) {
void* m = malloc(sz+4);

if(m) {
memory += sz+4;
*(unsigned int*)m = sz+4;
m = (char*)m+4;
if (makeout) printf("Allocated by new[]: %i\n", sz+4);
}

return m;
}

void operator delete[](void* m) {
if (!m) return;

m = (char*)m-4;
memory -= *(unsigned int*)m;
if (makeout) printf("Deallocated by delete[]: %i\n", *(unsigned int*)m);

free(m);
}

But this one makes a coast of 4 bytes on every allocation, which is not what
can satisfy me :(

How about

*(unsigned int *)m = sz;

instead of

*(unsigned int *)m = sz + 4;

in your overloaded 'new'? /david
 
?

=?ISO-8859-2?Q?Rafa=B3?=

How about

*(unsigned int *)m = sz;

instead of

*(unsigned int *)m = sz + 4;

in your overloaded 'new'? /david

When I'm allocationg 4 bytes extra to store the size of allocated memory:
void* m = malloc(sz+4); I count them as allocated:
*(unsigned int*)m = sz+4; Couse I want to know how much memory is deallocated when I use delete:
void operator delete[](void* m) {
if (!m) return;

m = (char*)m-4;
memory -= *(unsigned int*)m; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
if (makeout) printf("Deallocated by delete[]: %i\n", *(unsigned int*)m);

free(m);
}
 
D

davidrubin

Rafal said:
How about

*(unsigned int *)m = sz;

instead of

*(unsigned int *)m = sz + 4;

in your overloaded 'new'? /david

When I'm allocationg 4 bytes extra to store the size of allocated memory:
void* m = malloc(sz+4); I count them as allocated:
*(unsigned int*)m = sz+4; Couse I want to know how much memory is deallocated when I use delete:
void operator delete[](void* m) {
if (!m) return;

m = (char*)m-4;
memory -= *(unsigned int*)m;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I thought the intent was to monitor how much memory your process uses
under normal curcumstances, without this "test" allocator. In that
case, you don't want to count the test allocator overhead. Otherwise,
you actually *are* using an extra four bytes, and you need to count
them, as you do.

/david
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top