April 23, 2024, 10:48:52 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


New, Delete, Renew functions

Started by Parker, November 16, 2005, 04:34:55 PM

Previous topic - Next topic

0 Members and 3 Guests are viewing this topic.

Parker

I wrote these short routines to help while new and delete aren't included yet. You can also renew memory, something I missed but decided to find out how to do.

Here's the functions:
// declarations for the _new, _renew, and _delete functions
declare import, GlobalAlloc(flags as unsigned int, size as unsigned int),unsigned int;
declare import, GlobalReAlloc(hMem as unsigned int, nBytes as unsigned int, uFlags as unsigned int),unsigned int;
declare import, GlobalFree(memory as unsigned int),unsigned int;

// helper, since there's no NEW and DELETE

sub _new(size as unsigned int),unsigned int
{
return GlobalAlloc(0x40, size);
}

sub _renew(mem as unsigned int, size as unsigned int, opt zeromem=0 as int),unsigned int
{
return GlobalReAlloc(mem, size, (zeromem * 0x40));
}

sub _delete(ptr as unsigned int),unsigned int
{
return GlobalFree(ptr);
}


Here's some constants you can use when defining built in types:
// the size of various types
#define SIZE_BYTE 1
#define SIZE_WORD 2
#define SIZE_SHORT 2
#define SIZE_INT 4
#define SIZE_INT64 8
#define SIZE_FLOAT 4
#define SIZE_DOUBLE 8


And here's a test program that demonstrates using _new, _renew and _delete.
// declarations for the _new, _renew, and _delete functions
declare import, GlobalAlloc(flags as unsigned int, size as unsigned int),unsigned int;
declare import, GlobalReAlloc(hMem as unsigned int, nBytes as unsigned int, uFlags as unsigned int),unsigned int;
declare import, GlobalFree(memory as unsigned int),unsigned int;

// helper, since there's no NEW and DELETE

sub _new(size as unsigned int),unsigned int
{
return GlobalAlloc(0x40, size);
}

sub _renew(mem as unsigned int, size as unsigned int, opt zeromem=0 as int),unsigned int
{
return GlobalReAlloc(mem, size, (zeromem * 0x40));
}

sub _delete(ptr as unsigned int),unsigned int
{
return GlobalFree(ptr);
}

// the size of various types
#define SIZE_BYTE 1
#define SIZE_WORD 2
#define SIZE_SHORT 2
#define SIZE_INT 4
#define SIZE_INT64 8
#define SIZE_FLOAT 4
#define SIZE_DOUBLE 8

global sub main()
{
int *test;
test = _new( SIZE_INT );
*test = 13;
writeln(str$(*test,0) + "\n");
test = _renew( test, SIZE_INT*2 );
*test[1] = 26;
writeln(str$(*test[0],0) + " " + str$(*test[1],0) + "\n");
_delete(test);
while (GetKey() = "");
return 0;
}


I named them with a prefix of '_' to avoid conflicts with the future NEW and DELETE operators.