Better way to deal with memory
Blitz3D Forums/Blitz3D Programming/Better way to deal with memory| 
 | ||
| Hi. I coded a dll to use my own function to peek/poke into memory ( using VC++ 2008 ) but I'm really not sure it's the best way to do this... So If anyone has a better experience on this kind of things, he's welcome ^^ It works fine, and allows to : Peek/Poke int, short, byte at memory address ( cf : MDT_Peek/Poke ) return Integer address of Type pointers ( cf : GetPointer(t.MyType) ) return pointer from string/Int/Float variables ( cf : GetInt/String/Float Pointer ) return String from memory address ( cf : GetPtrString$( pointer ) ) here is my stuff obsolete ! Cf : code in post below (updated) +> The Compiled Dll <+ +> The Decls <+ | 
| 
 | ||
| Forgive my ignorance, but why wouldn't you just use a bank? | 
| 
 | ||
| I made a simple test between my dll and the MemoryLib ( available on the code archive ), it seems my function is just a bit faster ( lucky me, but I don't know if the MemoryLib.dll that I use is not an old version... ) As I want to build a Script library, I would like to store pointers to array and types to manage "Class" support etc... and of course, I need it to be fast enough to run IA scripts, and else. 
Graphics3D 800,600,0,2
SetBuffer BackBuffer()
D3DDev7=SystemProperty("Direct3DDevice7")
CountTest=100000000
t0=MilliSecs()
For i = 1 To CountTest
	Ptr1=PeekMemInt(D3DDev7)
Next
t1=MilliSecs()
For i = 1 To CountTest
	Ptr2=MDT_PeekInt(D3DDev7)
Next
t2=MilliSecs()
Print "MemLib : Res = "+ptr1+" time = "+(t1-t0)
Print "MdtLib : Res = "+ptr2+" time = "+(t2-t1)
WaitKey
End
I have a Problem dealing with float value... I hope I'll find a solution | 
| 
 | ||
| Forgive my ignorance, but why wouldn't you just use a bank? 1/ create a temorary bank (buffer) 2/ pass the Bank to the dll 3/ return the bank and compute results ... It is too long. And, for the purpose, I need Type support for my script library. So bank won't help. | 
| 
 | ||
| Ok, now I can Get Floats And , I don't know if it's a good way, but I get it 2 or 3 times faster than the MemoryLib.dll from the code archive... maybe I have some unsafe code, so I can't really expect it to be stable. anyway, here is the c++ source code : 
//GetMemAndPointer.dll
#define MDTEXP extern "C" _declspec(dllexport)
#define MDTCALL _stdcall
#define PEEK(addr)      (*(unsigned char *)(addr))
#define POKE(addr, b)   (*(unsigned char *)(addr) = (b))
MDTEXP int MDTCALL GetPointer( void* p ){return reinterpret_cast<int>(p);};
MDTEXP void MDTCALL GetInstance( int p ,void* res){res= (void*)p;};
MDTEXP float MDTCALL PeekMemF( int addr )
{	float val; unsigned char *ptr = (unsigned char *)&val;
	ptr[0]=PEEK(addr); ptr[1]=PEEK(addr+1); ptr[2]=PEEK(addr+2); ptr[3]=PEEK(addr+3);
	return val;
};
MDTEXP int MDTCALL PeekMemB( int addr ){	return PEEK(addr);};
MDTEXP int MDTCALL PeekMemS( int addr ){return (PEEK(addr)+(PEEK(addr+1)<<8));};
MDTEXP int MDTCALL PeekMemI( int addr ){return (PEEK(addr)+(PEEK(addr+1)<<8)+(PEEK(addr+2)<<16)+(PEEK(addr+3)<<24));};
MDTEXP void MDTCALL PokeMemB( unsigned char *addr , int b){	POKE(addr,b);};
MDTEXP void MDTCALL PokeMemS( unsigned char *addr , int s){POKE(addr,s & 0xff);POKE(addr+1,(s >> 8) & 0xff);};
MDTEXP void MDTCALL PokeMemI( unsigned char *addr , int i)
{POKE(addr,i & 0xff);POKE(addr+1,(i >> 8) & 0xff);POKE(addr+2,(i >> 16) & 0xff);POKE(addr+3,(i >> 24) & 0xff);};
MDTEXP const char* MDTCALL GetString( const char*addr ){ return (addr);};
MDTEXP const char* MDTCALL GetTypeString( int addr ,int FieldId)
{	// Get The Pointer of the field
	addr=PeekMemI(addr+FieldId*4);
	// Get The Pointer of the string and read the string at the address( PokeMemI(addr+8) = StringLen )
	return GetString((const char*)PeekMemI(addr+4));
};
MDTEXP int MDTCALL GetTypeInt( int addr ,int FieldId)
{	// Return the Integer value of the field
	return PeekMemI(addr+FieldId*4);
};
MDTEXP float MDTCALL GetTypeFloat( int addr ,int FieldId)
{	// Return the float value of the field
	addr=addr+FieldId*4;
	float val; unsigned char *ptr = (unsigned char *)&val;
	ptr[0]=PEEK(addr); ptr[1]=PEEK(addr+1); ptr[2]=PEEK(addr+2); ptr[3]=PEEK(addr+3);
	return val;
};
And Here are the compiled dll and the decls +> The Compiled Dll <+ +> The Decls <+ And I 'm still looking for a way to get Int and Float pointers... Here, I can only extract the fields of a type, using the GetPointer() function, but I can't grab pointers from blitz3d vars. | 
| 
 | ||
| [French] Des peeks et des pokes. Hummm!. Merci Boby ;) [/French] Peeks and pokes. Love that. thx JP |