www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/1999/05/20/17:24:07

From: Alain Magloire <alainm AT rcsm DOT ece DOT mcgill DOT ca>
Message-Id: <199905202123.RAA11152@mccoy2.ECE.McGill.CA>
Subject: Re: Heap-Memory Function
To: djgpp-workers AT delorie DOT com
Date: Thu, 20 May 1999 17:23:52 -0400 (EDT)
In-Reply-To: <71df8489.2475a865@aol.com> from "Kbwms@aol.com" at May 20, 99 02:03:17 pm
X-Mailer: ELM [version 2.4 PL25]
MIME-Version: 1.0
Reply-To: djgpp-workers AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

Bonjour

> Years ago when I worked with Microsoft C/C++, I used a function
> called _msize().  Its purpose was to return the size of a block
> of memory allocated on the heap.  I found this very handy when
> tracking memory allocations and, especially, leaks and pointer
> misuse.

8-)
Sorry I could not help smiling.  I just finish implementing an allocator
for an RTOS, and usually when people ask me for ways to get back the
size, it is for all the wrong reasons (I'm not saying that you
don't have some valid ones)

Most of the time the information block is hold in an offset, -1 of the pointer.
So p = malloc(40), you should get the size by moving back 1 and casting
to the appropriate struct.  Ususally the size will be the
last field in the structure so you don't have to guess the offset.
So something like this .. might work.

size = *(int *)(char *)p-1;
or 
size = *(int *)(char *)p-2;
size = *(int *)(char *)p-3;
size = *(int *)(char *)p-4;
size = *(int *)(char *)p-5;
size = *(int *)(char *)p-6;
...

I don't have DJGPP src here to test the theorie.

And even if the theorie is correct must allocator will round the size
to a modulo so if you ask for 13, the allocator may actually give you 16.

But the simplest way would be to get DJGPP core src and compile
malloc into your own library that you could modify to your heart
content and link with it :

gcc -o prog -lmalloc

> 
> Do we have a similar function available in DJGPP?

I hope not !
Kidding 8-), I don't know.

Good luck, memory bugs are the reasons why I'm getting balder 8-)

-- 
au revoir, alain
----
Aussi haut que l'on soit assis, on est toujours assis que sur son cul !!!

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019