Why do you even need this code?
It runs fast, but... what does it run fast for? I mean, what does it
DO, basically?
Anyhow, just an idea... but what about turning it into a class?
That way you don't need to worry about passing memoryblocks around,
which in my mind, are quite awkward to deal with. By wrapping it into
a neat class based API, you can make your code much easier to deal with!
As for the crashing, it looks like you are not allocating enough RAM,
to me. All memoryblocks are mutable, btw, so it can't be a mutability
problem, just an "out of memory" problem. Things like that are not
really "RB-like" way to do things! The RB-like way is that you don't
get hard crashes or memory corruptions, only exceptions or just
invalid results returned.
You'd probably want to add a check in your C code to see if the out
memoryblock's length is big enough for the entire output.
In fact... if you were to do this in the class based way, there would
be no memoryblocks. So you'd be in control of the size of the things,
so everything would be fine.
On 7 Nov 2007, at 21:33, Tim Jones wrote:
> Hi Folks,
>
> I've posted this on the forums, but things have dried up there and I
> really need to get this figured out.
>
> I've put out the business fires, so I can get back to the fun side of
> this stuff. I've dug through the new API/SDK info and I'm still a bit
> lost as to the "How's" of RB plugin creation. I was able to build a
> couple of the OS X samples, but they assume you're working with very
> basic operations and already understand the types defined in the
> plugins
> versus the types in a normal C program or library.
>
> I've got some math code that we've made very fast in C, but I can't
> get
> a grip on what RB types apply to what C types.
>
> Here's my main function code that would be turned into a plugin and
> called from within an RB app, Requiring 3 memoryblocks of 64 unsigned
> shorts and returning the results by modifying the 3rd memoryblock's
> contents:
>
> -----------------------------
>
> void modular_exp(unsigned short *base, unsigned short *power,
> unsigned short *result) {
> unsigned short z, sqr[BIGNUM_SHORTS], exp[BIGNUM_SHORTS];
> int i;
>
> for(i=0; i<BIGNUM_SHORTS; i++) {
> result[i] = one[i];
> exp[i] = power[i];
> }
>
> modular_product(base, one, sqr);
>
> for(;;) {
> if (exp[0] & 1)
> montgomery_reduction(sqr, result, result);
>
> z = 0;
> for(i=0; i<BIGNUM_SHORTS-1; i++) {
> exp[i] = (exp[i] >> 1) | ((exp[i+1] & 1) << 15);
> z |= exp[i];
> }
>
> exp[BIGNUM_SHORTS-1] >>= 1;
> z |= exp[BIGNUM_SHORTS-1];
>
> if (z == 0)
> break;
>
> montgomery_reduction(sqr, sqr, sqr);
> }
>
> modular_product(result, rp, result);
> }
>
> REALmethodDefinition ModularExpdefn = {
> (REALproc) modular_exp, nil,
> "ModularExp(base As MemoryBlock, power As MemoryBlock, ByRef result
> As MemoryBlock)",
> REALconsoleSafe
> };
>
>
> void PluginEntry(void)
> {
> REALRegisterMethod(&ModularExpdefn);
> }
>
> -------------------------------------------
>
> Recently, Tim Hare recommended that I change the calling convention to
> use this:
>
> void ModularExp(REALmemoryBlock base, REALmemoryBlock power,
> REALmemoryBlock result)
> {
> short *mbase, *mpower, *mresult;
> mbase = (short *) REALMemoryBlockGetPtr(base);
> mpower = (short *) REALMemoryBlockGetPtr(power);
> mresult = (short *) REALMemoryBlockGetPtr(result);
> modular_exp(mbase, mpower, mresult);
> }
>
> REALmethodDefinition ModularExpdefn = {
> (REALproc) ModularExp, nil,
> "ModularExp(base As MemoryBlock, power As MemoryBlock, ByRef result
> As MemoryBlock)",
> REALconsoleSafe
> };
>
> ---------------------------
>
> However, the result is that the plugin crashes on the attempt to
> copy the contents of an internal array into the mresult array
> (MemoryBlock).
>
> Anyone have guidance on how to get the three arrays of unsigned
> shorts (MemoryBlocks in the RB project) passed into the plugin so
> that the third memoryblock is mutable?
>
> Thanks,
>
> Tim
>
>
> _______________________________________________
> Unsubscribe or switch delivery mode:
> <http://www.realsoftware.com/support/listmanager/>
>
> Search the archives:
> <http://support.realsoftware.com/listarchives/lists.html>
--
http://elfdata.com/plugin/
"String processing, done right"
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>
Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>