News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

best technique for inserting data into Memory ?

Started by Rainstorm, May 14, 2008, 05:18:56 PM

Previous topic - Next topic

Rainstorm

hi, what's the best most efficient technique for inserting data into a buffer
this is what i usually do....
- I copy the data till the insertion point into another buffer
- copy the data i want to insert,..into the 2nd buffer
- then copy the rest of the data from the 1st buffer into the 2nd
- (in certain cases) then copy the 2nd buffer into the first buffer again,if I have to use just the 1st one

would appreciate any feedback if there is a faster way...

thanks.

Jimg

It depends upon whether the original buffer is already large enough.  If it is, I copy backwards from the end of the original data in the buffer, moving the latter part upward, then insert the new data in the space just created.

Rainstorm

QuoteIt depends upon whether the original buffer is already large enough.  If it is, I copy backwards from the end of the original data in the buffer, moving the latter part upward, then insert the new data in the space just created.

jimq , thanks for the feedback,.. i could check if more memory is needed & reallocate/extend the buffer the buffer

Nordwind64

Hi.

If data is a string, you easiliy can use the API lstrcat().
Greetings, Nordwind.
Windows 7 (64Bit), JWASM/PoLink

ecube


hutch--

If you are repeatedly adding data in something like an array, the method Jim suggested used in conjunction with a memory realloc is probably the way to go.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Rainstorm

hutch, yes that's what i did in the end.

Nordwind64, wouldn't Jim's method be faster?.. & i'd be doing it all in my code itself without breaking the flow.(assuming i'm doing it all in the same buffer itself)

KeepingRealBusy

If you are going to be doing many inserts into memory, use a doubly linked list tacking all new data to the end of the buffer (no moving and just update two links) and then when you are all done, just follow the linked list and copy the data to the new buffer one time (or just write the data to a file chunk by chunk with no moves at all). All depends on the data. Another dodge is to allocate a big buffer and put the first piece into the middle (track the first byte offset and the total size). Then if you need to insert into the middle of the data (I won't say never, but almost always it will be closer to one end than the other), just move the smallest piece up or down to make a hole, then insert, and adjust the start or maybe the size depending on which way moved. If not enough room to move, move the entire buffer to center it and then proceed again.

Dave.

Nordwind64

QuoteNordwind64, wouldn't Jim's method be faster?..

Sure, it's faster. But lstrcat is very simple to use and if you only want to add some short strings together, its an easy way to do it.  :P
Look in kernel32 and shlwapi to find some really interesting string functions! My favorite function ist StrDup, very helpful to build arrays of strings :U
Greetings, Nordwind.
Windows 7 (64Bit), JWASM/PoLink

ecube

to elaborate on my other post

this moves the contents on mystring in mybuff starting at the 5th byte in mybuff
lea edi,mybuff
add edi,5
invoke RtlMoveMemory,edi,addr mystring,5

dedndave


        mov     edi,offset mybuff+5
        invoke  RtlMoveMemory,edi,addr mystring,5

if mybuff is a local variable

        lea     edi,mybuff+5
        invoke  RtlMoveMemory,edi,addr mystring,5