How to allocate and free memory dynamically?

This is where you talk about the NXJ software itself, installation issues, and programming talk.

Moderators: 99jonathan, roger, imaqine

How to allocate and free memory dynamically?

Postby lejosfan » Wed Feb 01, 2012 7:59 am

Hello experts.

I am writing a native function for the Lejos VM and the function needs to allocate some memory dynamically for a multi-precision number computation (memory allocated could be around 2K), which would be released immediately after the computation. I saw some functions in memory.c (java_allocate(), system_allocate()), but not sure which is the correct one to use as none is working.

Advice much appreciated.

Thanks.
lejosfan
New User
 
Posts: 19
Joined: Sat Jan 24, 2009 6:45 pm

Re: How to allocate and free memory dynamically?

Postby gloomyandy » Wed Feb 01, 2012 8:56 am

My first question would be why do you need to write a native function to do this? What does the native function do? Is there any reason why you can't simply allocate the memory from Java and then i you really need a native unction pass the memory to that when it is needed. Memory handling in any JVM is typically complex and even more so in native code, and especially so in a system like leJOS, you really have to understand what it is you are doing, if not it is very easy to create problems which result in memory access errors. My advice would be to everything you can to avoid writing native code, and if you have to write native code do the absolute minimum in that native code and do as much as possible in Java...

Andy
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3646
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: How to allocate and free memory dynamically?

Postby lejosfan » Wed Feb 01, 2012 1:58 pm

Hi Andy.

You have a good point. The problem, though, is that there is no Java library that does multi-precision computation, such as what BigInteger does in the desktop, with acceptable performance. To give you an idea, running native code, a message (basically a 255 byte array) decryption is taking 30 seconds. Even if you write Java to do some of that work, you would still have to write some functionality in native for speed. Which goes back to the original question: What would be the best way to handle this? Any existing function in the VM code that I could call?

I'd love to use Lejos for my encryption/decryption schemme.

Thanks.
lejosfan
New User
 
Posts: 19
Joined: Sat Jan 24, 2009 6:45 pm

Re: How to allocate and free memory dynamically?

Postby gloomyandy » Wed Feb 01, 2012 2:23 pm

As I said before I would try and allocate any memory that you require in Java and pass a reference to that data into your native code. I'd still be tempted to use Java to begin with and to simply move the hot parts of that code into native methods...
User avatar
gloomyandy
leJOS Team Member
 
Posts: 3646
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: How to allocate and free memory dynamically?

Postby lejosfan » Thu Feb 02, 2012 3:51 am

Andy, I see your points.

Thanks.
Lejosfan
lejosfan
New User
 
Posts: 19
Joined: Sat Jan 24, 2009 6:45 pm


Return to NXJ Software

Who is online

Users browsing this forum: No registered users and 1 guest

more stuff