PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Fastest way to clear a block of memory

Hey guys... general question here... if I allocate a block of memory with...

unsigned short int* memBlock = (unsigned short int*)malloc(mbSize);

What would be the FASTEST way to clear said block of memory? Not deallocate it, but write all 0's to it (or some arbitrary value, but 0 is what I'm thinking).

I'm assuming that...

for (int i = 0; i < mbSize; i++) {
*(memBlock + i) = 0;
}

..would NOT be the fastest way, right?

Thanks all!
BurningSheep · 25 Jul 2002, 21:03 UTC
I think memset is the fastest way to clear the memory

memset(memblock, 0, mbSize);
If you mean you'd like the memory cleared for you when it's given to you, simply use calloc instead of malloc.

If you want to clean it yourself, then BurningSheep's suggestion is the one I'd go for.

... unless it's a severe problem and you have lots of time and patience and you want to start playing with DMA (which would actually be a slower way of clearing the memory, but could run contiguously with other processing).

Cheers,

Refractor
No, I did mean clearing it after it's allocated. So, memset looks like the answer. I wasn't sure if that was the fastest way, but it looks like that's the consensus. Thanks all!
Conan · 30 Jul 2002, 11:22 UTC — clearing a block of memory
I was looking for the fastest way to clear a block of 16 bit integers to a set value not zero. I ended up making a long containing the int twice by shifting & adding and running round a loop but it's not much faster than copying integers. If memcpy is filling 'n' bytes then it may be slower than copying longs. has anyone disassebled what memcpy does on the PocketPC?
"Copying integers" or "storing integers"? A straight store of 32-bit integers should be faster than copying a value from memory.

Memset should do the same kind of things as memcpy(), though - i.e. store up to a cache line, repeatedly store 4 registers (aligned with the write-buffer and cache-line), and then store "the rest".

This is one of those situations where embedded assembler could walk all over C/C++. In C, you (probably) have to store one register at a time... in ARM code you can store many more than that in one instruction (though for a large block, you're still limited by the write-buffer and the memory write speed).
Conan, you can use memcpy to copy shorts, but only shorts with specific values (the hi byte and low byte must be equal) ie 0x0000, 0x0101, 0x0202, 0xfefe, etc.

What does your source look like? Something like this comes to mind:
Code:

void memsetshort(short *dest, short value, int count) {
unsigned int valuelong=value;
valuelong<<=16;
valuelong+=value;

unsigned int *pTar=(unsigned int *)dest;
unsigned int *pStop=pTar+(count/2);

while (pTar<pStop)
*pTar++=valuelong;

//Handle remaining odd element
if (count & 1)
*(short *)pTar=value;
}


Dan East
Hi Dan, my current loop is:-
Code:
for(int y = 0; y < 24000; y++)//copy longs containing [far_clip|far_clip]
{
// set offset to beginning of the next line to draw
ZBuff[offs] = CLIP;
offs++;
}
This is wasteful compared to your approach as I'm incrementing a loop counter y and the offset offs. CLIP is a long containing the data set up just like your variable:-
Code:
CLIP = (FAR_CLIP<<16);// make a long containing [far_clip<<16+far_clip] to make copy faster
CLIP += FAR_CLIP;// add on the 2nd far_clip


My performance is fine for the purpose but anything which speeds up the display is a help. I will implement your solution & see what the timings are like. Basically I've merged PocketGL & PocketFrog so I can use Frog's 2D functions so I gain functionality plus device independance but the 3D takes a big speed hit which in the case of Space Treker does not matter but for my next game it will. ( I'm converting Space Treker to C++ from PocketC so that I know enough to write another Space game which I have already started to design )

ps: I'm copying 240 x 200 only
>has anyone disassebled what memcpy does on the PocketPC?

Yes, I posted it here. Shall I repost?

It uses stmia and is the quickest possible (quicker than C code), so must have been hand coded in Asm.