Operating System - OpenVMS
1748202 Members
3071 Online
108759 Solutions
New Discussion юеВ

Re: Size of memory block pointed by a pointer?

 
dschwarz
Frequent Advisor

Size of memory block pointed by a pointer?

Is there a way to find out the size of memory allocated to a pointer on OpenVMS ?

On Linux we can use malloc_usable_size.
On Windows we can use _msize.

6 REPLIES 6
Joseph Huber_1
Honored Contributor

Re: Size of memory block pointed by a pointer?

Maybe read the discussion in the HP-UX forum:
http://forums13.itrc.hp.com/service/forums/questionanswer.do?admit=109447627+1289218785071+28353475&threadId=1380264

As far I know there is no such call in VMS/DECC
(searched all decc rtl and starlet includes).

Could You try to explain, why You need it ?
Trying to tweak some few bytes out of an allocated memory structure ?
Maybe a better design of the software would be preferable, if it works without non-standard extensions ?
http://www.mpp.mpg.de/~huber
Joseph Huber_1
Honored Contributor

Re: Size of memory block pointed by a pointer?

And BTW, using malloc_usable_size() defeats use of valgrind and similar tools, they do not work together friendly.
(not from my own experience, but seen in changelogs of e.g. CVS).
http://www.mpp.mpg.de/~huber
dschwarz
Frequent Advisor

Re: Size of memory block pointed by a pointer?

Joseph,

I have read the UX discussion before opening this thread.

I have also searched the header files.

We have a little piece of software written by one of our linux guys that shall be ported to OpenVMS. It uses this function.
Hein van den Heuvel
Honored Contributor

Re: Size of memory block pointed by a pointer?

First google hit on that function tell me is is 'not portable' (see below)

You may want to dig a little bit deeper to see why they use that function. Too lazy/frugal to store their own size or do they want to actually use any 'excess' (alignment) space?

You can probably roll your own if need be.
The malloced size is stored in the addresses before the pointer. A few trials in a debugger session with allocations 1 .. 32 will soon enough show what is going on.

Hein


*Portability*
`malloc', `realloc', and `free' are specified by the ANSI C standard,
but other conforming implementations of `malloc' may behave differently
when NBYTES is zero.
`memalign' is part of SVR4.
`malloc_usable_size' is not portable.
WW304289
Frequent Advisor

Re: Size of memory block pointed by a pointer?

> Is there a way to find out the size of memory allocated to a pointer on OpenVMS ?

As was already pointed out, there is no portable way of doing it.

On VMS, you can intercept calls to malloc() by compiling with /pref=except=malloc and providing your own malloc, as shown below. If you write in C++ and do allocations using new expression, you can override global operator new.

-Boris

$ cc/pref=except=malloc x.c
$ link x.obj
$ run x.exe
1? s = 0
2? s = 123456789
$

x.c
---
#include
#include

size_t s = 0;

#ifdef __cplusplus
extern "C" {
#endif

void* decc$malloc(size_t size);
void* malloc(size_t size) { s = size; return decc$malloc(size); }

#ifdef __cplusplus
}
#endif

int main() {
printf("1? s = %d\n", s);
malloc(123456789);
printf("2? s = %d\n", s);
}
John Gillings
Honored Contributor

Re: Size of memory block pointed by a pointer?

Not in a documented, supported or portable manner.

Get a copy of the sources for the LIB$*VM routines, which underpin the CRTL heap management routines. As Hein has found by experiment, LIB$GET_VM keeps some information about the allocation in the quadword immediately below the allocated block. Rather than make assumptions, use the source to determine exactly what it is, and how you might use it.

Note there is are two LIBRTL "hidden" routines which are exposed in the symbol vector, LIB$$GET_VM_POINTERS and LIB$$GET_VM_POINTERS_64. (note the double $). There's a chance they're used to decode the meta data in an allocated block. As they say "Use the source Luke".
A crucible of informative mistakes