Page 1 of 4

[NXC] Memory Manager

Posted: 02 Mar 2011, 23:16
by muntoo
Links: -----

It may interest you to know I'm working on a "Memory Manager" (yes, that's a terrible name, but what else could I call it?).

Using my library (libraries, actually), you'll be able to:
[Please insert "pseudo-" in front of all the list items.]
  • Create variables (bools, [unsigned] chars, [unsigned] ints, [unsigned] longs, floats, doubles, strings, and arrays of those types, too)
  • Set their values (working on it; support for different ops is being added)
  • Use "hash tables" (working on it)
  • Parsers of [obfuscated] expressions (... guess; yeah, you're right: I need to finish the hash stuff first)
  • "Dynamic" memory management
  • Dynamic typing (if you're insane enough to want this), and duck typing (I just wrote this down for kicks, actually; quack quack)
  • Function overloading
  • Possibly operator overloading
  • And definitely use super advanced kung-fu fighting robot functions
Eventually, I hope to add "pseudo-structs" and "pseudo-classes", but I'm not sure about the implementation. I don't feel like creating an interpreter [do any of us?], but I may have to, if I want such complicated stuff...

If I can get structs down, and function pointers (I figured out part of the implementation for that, but it'll require the user of my library to implement part of it themselves), I may be able to get pseudo-pseudo-recursion, though that's probably the last thing I'll do. (I haven't really thought about this, though, so feel free to criticize me for being an idiot and not seeing that structs and function pointers have nothing to do with pseudo-pseudo-recursion.)
Did I mention you could potentially have some of the most wanted NXC features:
  • pseudo pointers
  • pseudo function pointers (actually, no one really wants this but me)
  • pseudo doubles (64-bit IEEE-754) -- I may need some help with this
By "pseudo", I mean you won't be able to do this:

Code: Select all

int lol = 42;
int *pi = lol;
NumOut(0, 0, *pi, 0);
But we will have something similar to this:

Code: Select all

MM_ID lol = MM_new(MM_ID_TYPE_INT);
// prototype: MM_ID MM_new(MM_ID_TYPE type = MM_ID_TYPE_BYTE, unsigned long len = 1);

MM_mov_int(lol, 42);
// or use MM_set_int(lol, 42);

MM_ID pi = lol;

NumOut(0, 0, MM_get_int(pi), 0);
It'd look a lot nicer if NXC itself supported function overloading. >_> :D Assuming it did, here's the new version:

Code: Select all

MM_ID lol = MM_new(MM_ID_TYPE_INT);
MM_mov(lol, 42);
MM_ID pi = lol;
NumOut(0, 0, MM_get(pi), 0);
No more _int s!

Or, if you take my "parser" into account, something similar to this:

Code: Select all

P_parse("int lol = 42;");
string pi = "lol";
NumOut(0, 0, P_get_int(pi), 0);
-----

It's untested, but when I'm done, I'll need someone to test it. (Unless you want to wait a few more months or a year, since I don't have a NXT to test it with me all the time. :))
Stay tuned for possible updates on my blog. Or the forums, since someone needs to test it...If anyone wants the not even 25% done code, feel free to PM me with your email so I can give it to some spammers/advertisers.

Re: [NXC] Memory Manager

Posted: 04 Mar 2011, 07:29
by timpattinson
Hmmm... Cool...
The pseudo-doubles may be very useful for a project I'm working on, a way to approximate pi on the NXT
Good luck.. :)
-Tim.
ps. even though I don't know what half these things are, I'd be happy to try to test it for you

Re: [NXC] Memory Manager

Posted: 04 Mar 2011, 16:45
by nxtboyiii
I can be the NXT tester!

Re: [NXC] Memory Manager

Posted: 07 Mar 2011, 09:18
by muntoo
Can you tell me what the output of the attached program .rxe is? Maybe a screenshot? If it doesn't work, can you try it with the optimization level set to 1, and then 0?

Thanks guys,
Muntoo

Re: [NXC] Memory Manager

Posted: 07 Mar 2011, 09:42
by timpattinson
muntoo wrote:

Code: Select all

 /* A memory manager, in case you lose your brain. But this is NOT a replacement for brain data backup! Be sure to get RipOff's Brain Backup System for only 100 easy payments of $999.99, with a free bottle of RipOff's Really Expensive Ketchup! */
:lol: :lol: :lol: :lol:
When I run the rxe it says "Deleting memory." after a few seconds and the quits
-Tim\
EDIT: It does this under your rxe, and when I compile it, with optimization levels 0,1,2 (EFW 1.31 compiler 1.2.1r3)

Re: [NXC] Memory Manager

Posted: 07 Mar 2011, 10:06
by muntoo
timpattinson wrote:
muntoo wrote:

Code: Select all

 /* A memory manager, in case you lose your brain. But this is NOT a replacement for brain data backup! Be sure to get RipOff's Brain Backup System for only 100 easy payments of $999.99, with a free bottle of RipOff's Really Expensive Ketchup! */
:lol: :lol: :lol: :lol:
When I run the rxe it says "Deleting memory." after a few seconds and the quits
-Tim\
EDIT: It does this under your rxe, and when I compile it, with optimization levels 0,1,2 (EFW 1.31 compiler 1.2.1r3)
I'm assuming the "deleting memory" bit comes after 2 seconds of program start? Are you sure nothing else comes up?

EDIT: Oh woops, try this:

Code: Select all

/*  Program Name: "MM_v0_3.nxc"
**  This program was created by "muntoo" on March 4, 2011
**  Created using BricxCC (by John Hansen) and written in NXC (by John Hansen)
**  ( http://bricxcc.sourceforge.net/ )

Description:
A demo program for a memory manager, in case you lose your brain. But this is NOT a replacement for brain data backup! Be sure to get RipOff's Brain Backup System for only 100 easy payments of $999.99, with a free bottle of RipOff's Really Expensive Ketchup!
*/


#include "MM.nxc"



/*

*/
// <ATTENTION> <UNTESTED>
byte MM_get_BYTE(MM_ID /* & is not necessary; pointers! */ id)
{
	// <ATTENTION> Possible bug. Do you think using returns like this is *safe*?
	return(MM_mem[id.idx]);
}


/*

*/
// <ATTENTION> <UNTESTED>
MM_ID MM_set_BYTE(MM_ID /* & is not necessary; pointers! */ id, byte val)
{
	MM_mem[id.idx] = val;

	return(id); // return id for no reason
}


MM_ID MM_ID_CPY(MM_ID id)
{
	MM_ID retVal;

	retVal.type = id.type;
	retVal.len = id.len;
	retVal.idx = id.idx;

	return(retVal);
}


task main()
{
	MM_init(); // Initialize Memory Manager; Always call at beginning of program

	MM_ID myvar = MM_new(MM_ID_TYPE_BYTE, 5); // Create pointer to an array of 5 elements
	
	MM_ID ptr;
	ptr = MM_ID_CPY(myvar);
	
	// <ATTENTION> Possible bug! Does ptr.idx++ really work?
	// <ATTENTION> Possible bug! MM_ID ptr = MM_ID_CPY(myvar);
	// Set values to random numbers
// Report NXC Bug. Can't use "MM_ID ptr = myvar;"
	for(; ptr.idx < (myvar.idx + MM_sizeOf(myvar.type)); ptr.idx++)
	{
		// MM_set_BYTE(ptr, Random(pow(0x100, MM_sizeOf(myvar.type)) - 1));
		MM_set_BYTE(ptr, (ptr.idx - myvar.idx));
	}

	// <ATTENTION> Possible bug! Does ptr.idx++ really work?
	// Display values
	for(; ptr.idx < (myvar.idx + MM_sizeOf(myvar.type)); ptr.idx++)
	{
		NumOut(0, 8 * (myvar.idx + MM_sizeOf(myvar.type) - 1 - ptr.idx), MM_get_BYTE(ptr));
	}

	Wait(2000);

	ClearScreen();
	TextOut(0, 0, "Deleting memory...", 0);

	MM_delete(myvar); // Mark memory free for this variable

	MM_init(); // Reset Memory Manager. Frees all Memory Manager memory.

	Wait(5000);
}

Re: [NXC] Memory Manager

Posted: 07 Mar 2011, 10:07
by muntoo
BTW, remind me to write a tutorial/guide/documentation for all this, once I'm done.

Re: [NXC] Memory Manager

Posted: 08 Mar 2011, 00:56
by nxtboyiii
There is an error saying "duplicate case labels are not allowed" on line 177.

Here's a pic:

Re: [NXC] Memory Manager

Posted: 08 Mar 2011, 01:02
by mattallen37
OT, but how do you get those tabs? I always just cascade my windows when I have multiple programs open in BCC. I would like to have tabs instead (like you).

Re: [NXC] Memory Manager

Posted: 08 Mar 2011, 01:40
by muntoo
mattallen37 wrote:OT, but how do you get those tabs? I always just cascade my windows when I have multiple programs open in BCC. I would like to have tabs instead (like you).
Go to Edit->Prefrences->General->Uncheck "Use MDI mode" and restart BricxCC.

-----

@nxtboyiii Thanks for that bug fix.
The fixed code is attached.