A mem copy function for allocated memory and datatypes

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
SamL
Posts: 63
Joined: Nov 23, 2019 17:30
Location: Minnesota

A mem copy function for allocated memory and datatypes

Post by SamL »

I was testing different ways to copy data types and bench marking them. I came up with a new way to copy memory faster then byte by byte chunks.
I have included a byte by byte mem copy along with the faster copy function.
of course you can't copy strings unless they have a defined size.

I use this to copy UDT's into allocated memory(the data header) along with allocated memory to allocated memory ( the data ) for writing to disk, in my database program.

the function mem_all_copy() is showing extremely faster copy times then the function mem_byte_copy() this is noticeable when copying larger chunks of memory.

I thought id share it and if any one sees any issues or a better way.

Code: Select all

declare sub mem_all_copy(byval tooPTR as any ptr, byval fromPTR as any ptr, byval size_of_data as ulongint)
declare sub mem_byte_copy(byval tooPTR as any ptr, byval fromPTR as any ptr, byval size_of_data as ulongint)
declare sub mem_all_copy_test(byval tooPTR as any ptr, byval fromPTR as any ptr, byval size_of_data as ulongint)

type udt_dat
    as string*3 a
    as string*2 b
    as string*1 c
end type

print "sizeof(udt_dat) ="; sizeof(udt_dat)

dim as udt_dat too
dim as udt_dat from

from.a = "aaa"
from.b = "bb"
from.c = "c"
print "--------"

'mem_all_copy(@too, @from, sizeof(udt_dat))
'mem_byte_copy(@too, @from, sizeof(udt_dat))
mem_all_copy_test(@too, @from, sizeof(udt_dat))

print "--------"
print
print "|" & too.a & "|"
print "|" & too.b & "|"
print "|" & too.c & "|"

sleep


sub mem_all_copy(byval tooPTR as any ptr, byval fromPTR as any ptr, byval size_of_data as ulongint)
    do    
        select case size_of_data
            case 0
                exit do
            case is >= 8 'copy longint sized chuncks
                *cast(ulongint ptr, tooPTR) = *cast(ulongint ptr, fromPTR)
                fromPTR += 8
                tooPTR += 8
                size_of_data -= 8
            case is >= 4 'copy long sized chuncks
                *cast(ulong ptr, tooPTR) = *cast(ulong ptr, fromPTR)
                fromPTR += 4
                tooPTR += 4
                size_of_data -= 4
            case is >= 2 'copy short sized chuncks
                *cast(ushort ptr, tooPTR) = *cast(ushort ptr, fromPTR)
                fromPTR += 2
                tooPTR += 2
                size_of_data -= 2
            case else 'copy byte sized chuncks
                *cast(ubyte ptr, tooPTR) = *cast(ubyte ptr, fromPTR)
                fromPTR += 1
                tooPTR += 1
                size_of_data -= 1
        end select
    loop
end sub

sub mem_byte_copy(byval tooPTR as any ptr, byval fromPTR as any ptr, byval size_of_data as ulongint)
    while size_of_data > 0
        (*cast(ubyte ptr, tooPTR)) = *cast(ubyte ptr, fromPTR)
        size_of_data-=1
        fromPTR +=1
        tooPTR +=1
    wend   
end sub

sub mem_all_copy_test(byval tooPTR as any ptr, byval fromPTR as any ptr, byval size_of_data as ulongint)
    do    
        if size_of_data <> 0 then print size_of_data, chr(*cast(ulongint ptr, fromPTR))
        select case size_of_data
            case 0
                exit do
'            case is >= 8 'copy longint sized chuncks
'                (*cast(ulongint ptr, tooPTR)) = *cast(ulongint ptr, fromPTR)
'                fromPTR += 8
'                tooPTR += 8
'                size_of_data -= 8
'            case is >= 4 'copy long sized chuncks
'                (*cast(ulong ptr, tooPTR)) = *cast(ulong ptr, fromPTR)
'                fromPTR += 4
'                tooPTR += 4
'                size_of_data -= 4
'            case is >= 2 'copy short sized chuncks
'                (*cast(ushort ptr, tooPTR)) = *cast(ushort ptr, fromPTR)
'                fromPTR += 2
'                tooPTR += 2
'                size_of_data -= 2
            case else 'copy byte sized chuncks
                (*cast(ubyte ptr, tooPTR)) = *cast(ubyte ptr, fromPTR)
                fromPTR += 1
                tooPTR += 1
                size_of_data -= 1
        end select
    loop
end sub
Post Reply