Connecting Tech Pros Worldwide Help | Site Map

controling allocated resources

Rafał
Guest
 
Posts: n/a
#1: Jul 23 '05
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

Rapscallion
Guest
 
Posts: n/a
#2: Jul 23 '05

re: controling allocated resources


Rafal wrote:[color=blue]
> I would like to make my program know how much memmory it consumes at
> runtime. At now I thought to do something like that:[/color]

This might interest you:
http://www.relisoft.com/book/tech/9new.html

Rafał
Guest
 
Posts: n/a
#3: Jul 23 '05

re: controling allocated resources


[color=blue][color=green]
>> I would like to make my program know how much memmory it consumes at
>> runtime. At now I thought to do something like that:[/color]
>
> This might interest you:
> http://www.relisoft.com/book/tech/9new.html[/color]

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

Nagarajan Makala
Guest
 
Posts: n/a
#4: Jul 23 '05

re: controling allocated resources


Hi Rafal,
It looks like you are already doing best. Here is some discussion
about it.

1. associative array technique
http://www.parashift.com/c++-faq-lit....html#faq-38.7

2. over allocate ( I think you are already doing it)
http://www.parashift.com/c++-faq-lit....html#faq-38.8

Nagarajan Makala


Rafal wrote:[color=blue][color=green][color=darkred]
> >> I would like to make my program know how much memmory it consumes at
> >> runtime. At now I thought to do something like that:[/color]
> >
> > This might interest you:
> > http://www.relisoft.com/book/tech/9new.html[/color]
>
> 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[/color]

davidrubin@warpmail.net
Guest
 
Posts: n/a
#5: Jul 23 '05

re: controling allocated resources


[snip][color=blue]
> 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 :([/color]

How about

*(unsigned int *)m = sz;

instead of

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

in your overloaded 'new'? /david

Rafał
Guest
 
Posts: n/a
#6: Jul 23 '05

re: controling allocated resources


davidrubin@warpmail.net wrote:
[color=blue]
>
> How about
>
> *(unsigned int *)m = sz;
>
> instead of
>
> *(unsigned int *)m = sz + 4;
>
> in your overloaded 'new'? /david[/color]

When I'm allocationg 4 bytes extra to store the size of allocated memory:[color=blue][color=green]
>> void* m = malloc(sz+4);[/color][/color]
I count them as allocated:[color=blue][color=green]
>> *(unsigned int*)m = sz+4;[/color][/color]
Couse I want to know how much memory is deallocated when I use delete:[color=blue][color=green]
>> void operator delete[](void* m) {
>> if (!m) return;
>>
>> m = (char*)m-4;
>> memory -= *(unsigned int*)m;[/color][/color]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^[color=blue][color=green]
>> if (makeout) printf("Deallocated by delete[]: %i\n", *(unsigned[/color][/color]
int*)m);[color=blue][color=green]
>>
>> free(m);
>> }[/color][/color]

davidrubin@warpmail.net
Guest
 
Posts: n/a
#7: Jul 23 '05

re: controling allocated resources




Rafal wrote:[color=blue]
> davidrubin@warpmail.net wrote:
>[color=green]
> >
> > How about
> >
> > *(unsigned int *)m = sz;
> >
> > instead of
> >
> > *(unsigned int *)m = sz + 4;
> >
> > in your overloaded 'new'? /david[/color]
>
> When I'm allocationg 4 bytes extra to store the size of allocated memory:[color=green][color=darkred]
> >> void* m = malloc(sz+4);[/color][/color]
> I count them as allocated:[color=green][color=darkred]
> >> *(unsigned int*)m = sz+4;[/color][/color]
> Couse I want to know how much memory is deallocated when I use delete:[color=green][color=darkred]
> >> void operator delete[](void* m) {
> >> if (!m) return;
> >>
> >> m = (char*)m-4;
> >> memory -= *(unsigned int*)m;[/color][/color]
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^[/color]

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

Closed Thread