Memory allocation

Previous topic - Next topic

MrTAToad

Here are a set of routines for allocating (and deallocating) a fixed amount of memory.  PEEK and POKE is now in!  I've updated the program slightly to use new and delete, and changed the memory allocation/freeing functions to something more user-friendly.

Test code :

Code (glbasic) Select

LOCAL mem%

mem%=AllocMem(1024)
IF mem%<>0
FillMemory(mem%,1024,0)
pokeB(mem%,123)
DEBUG "Peek : "+peekB(mem%)+"\n"
pokeS(mem%,1024)
DEBUG "Peek : "+peekS(mem%)+"\n"
pokeL(mem%,65536)
DEBUG "Peek : "+peekL(mem%)+"\n"
pokeString(mem%,"Test")
DEBUG "Peek : "+peekString$(mem%,0)+"\n"
ENDIF
FreeMem(mem%)
DEBUG "Finished"
END


And the main code :

Code (glbasic) Select

// --------------------------------- //
// Project: TestMalloc
// Start: Monday, March 02, 2009
// IDE Version: 6.174

FUNCTION dummy:
ENDFUNCTION

INLINE
}

typedef unsigned int size_t;

extern "C" void *memcpy(void *destination, const void *source, size_t num );
extern "C" void *memset(void *ptr, int value, size_t num);

template <typename type, typename val> void __poke(DWORD Addr, val Value)
{
*((type*)(Addr))=(type)(Value);
}

template <typename type> type __peek(DWORD Addr)
{
return *((type *)(Addr));
}

namespace __GLBASIC__ {
ENDINLINE

FUNCTION AllocMem%:size%
INLINE
return (DGNat) new char[size];
ENDINLINE
ENDFUNCTION

FUNCTION FreeMem%:mem%
INLINE
char *temp;

temp=(char *) mem;
delete[] temp;
ENDINLINE
ENDFUNCTION

FUNCTION ZeroMemory:mem%,size%
INLINE
memset((char *) mem,(char) 0,size);
ENDINLINE
ENDFUNCTION

FUNCTION FillMemory:mem%,size%,val%
INLINE
memset((char *) mem,(char) val,size);
ENDINLINE
ENDFUNCTION

FUNCTION pokeB:mem%,val%
INLINE
__poke<char>((DWORD) mem,char(val));
ENDINLINE
ENDFUNCTION

FUNCTION pokeS:mem%,val%
INLINE
__poke<short>((DWORD) mem,short(val));
ENDINLINE
ENDFUNCTION

FUNCTION pokeL:mem%,val%
INLINE
__poke<int>((DWORD) mem,val);
ENDINLINE
ENDFUNCTION

FUNCTION pokeF:mem%,val
INLINE
__poke<float>((DWORD) mem,(float) val);
ENDINLINE
ENDFUNCTION

FUNCTION pokeDF:mem%,val
INLINE
__poke<double>((DWORD) mem,val);
ENDINLINE
ENDFUNCTION

FUNCTION pokeString:mem%,str$
INLINE
memcpy((char *) mem,str_Str.c_str(),str_Str.Len());
ENDINLINE
ENDFUNCTION

// ------------------------------------------------------------------------------------------------------------------------------------
FUNCTION peekB%:mem%
INLINE
return ((DGNat) (__peek<char>(mem)));
ENDINLINE
ENDFUNCTION

FUNCTION peekS%:mem%
INLINE
return ((DGNat) (__peek<short>(mem)));
ENDINLINE
ENDFUNCTION

FUNCTION peekL%:mem%
INLINE
return ((DGNat) (__peek<int>(mem)));
ENDINLINE
ENDFUNCTION

FUNCTION peekF:mem%
INLINE
return ((DGInt) (__peek<float>(mem)));
ENDINLINE
ENDFUNCTION

FUNCTION peekDF:mem%
INLINE
return ((DGInt) (__peek<double>(mem)));
ENDINLINE
ENDFUNCTION

FUNCTION peekString$:mem%,length%=0
LOCAL temp$
LOCAL l%
LOCAL p%

temp$=""

IF length%>0
FOR l%=0 TO length%-1
temp$=temp$+CHR$(peekB(mem%+l%))
NEXT
ELSE
p%=peekB(mem%)
WHILE p%<>0
temp$=temp$+CHR$(p%)
INC mem%,1
p%=peekB(mem%)
WEND
ENDIF

RETURN temp$
ENDFUNCTION


The use of the template was thought up by Ian Mold years ago in the DBPro forums...

MrTAToad

I think a garbage collection system would be a bit too much, especially the large amount of extra code that would be needed.

The other problem is that there are many headers which call other headers especially stdlib...