Community
cancel
Showing results for 
Search instead for 
Did you mean: 
zoolii
Beginner
158 Views

Checking an address is a valid TBB allocated memory block or not

Hi All,

I am using TBB to allocate memory for C structures and same is exposedto Java using JNI. The pointers becomes Java integers (which actually represents the pointers in native layer).Ihave following doubts.

1. In Java an integer is a32 bit signed value always. So can I restrict the address range of TBB allocation so thatit will never exceed the range 2^31 - excluding the sign bit.

2.From Java,now user can pass a random integer which will be considered as a pointer in native layer.
Does TBB provide a mechanism to check a particularvalueagainst theinternalmemory address range (block) and tellthe passed invalue isa valid pointer or not.

Thank you.
Zooli

0 Kudos
6 Replies
158 Views

Hi Zooli,
You can usescalable_msize(ptr) function. It should return 0 if memory is not recognized.
--Vladimir.
zoolii
Beginner
158 Views

Hi Vladimir,



My requirement is slightly different. The passed in pointer can point to anything within a allocated memory block.

i.e char* pMem = (char*) scalable_malloc(1024);

Then I want something like this :

"isValid(pMem+ 100)" should return true and "isValid (pMem + 1030)" should return false.

This can be achieved by some book keeping inside my application. But I want to know if there is any built in functionality to know this.


Thank you
RafSchietekat
Black Belt
158 Views

Nothing very solid, I'm afraid, but it's not made public anyway, only the function scalable_msize() where you're responsible to pass in a pointer to memory allocated by the scalable allocator (it's documented to return 0 for other pointer values, but its implementationpoints toa more "adventurous" outcome, and I don't just mean the occasional false positive).

On the other hand, if you don't know what you're holding, what would bethe meaning of such a query in a multithreaded application?
zoolii
Beginner
158 Views

Of course , there can be valid reasons. If you read my original question completely, it is about exposing native pointer to Java. Mine is a native library. So a java user can be be mischievous (but often by mistake)to pass a random integer to native layer as valid handle. Without such a mechanism, you cannot trust such a value. This is true for native applications also. But there compiler assists to some extend.

158 Views

well, I did not get whyscalable_msize does not work here. doesn't it? You can use NULL/!=NULL defines instead of true/false.
--Vladimir
RafSchietekat
Black Belt
158 Views

For debugging your own code, you could benefit from it, I agree, but it would never be fully reliable. To get what you want would require more runtime overhead to prevent testing a magic value where no memory is mapped (bus error!), etc. Although, if you want to assert that the memory is valid and are prepared to crash if it's not, that's a different story. You would have to look in src/tbbmalloc/MemoryAllocator.cpp and see if you can do something with the code called from isRecognized(), which I think does now not happily accept a pointer somewhere inside allocated memory instead of at the beginning.
Reply