//[of]:description //[c]Utility functions to copy, compare memory blocks //[cf] //[of]:imports //[c] import "base/types" import "c-types" //[cf] //[of]:types //[c] typedef bytes = [] byte typedef dwords = [] dword typedef chars = [] char //[cf] //[c] //[of]:copy (dst, src, size) //[c]Copies a memory block //[c] public func copy (dst: mem, src: mem, size: size) def p = src:bytes def q = dst:bytes def limit = q + size // Optimize for big memory blocks if size>64 // Align destination on a long word while is not aligned (q, 3, 0) q++[] = p++[] end // Copy long words limit -= 3 def pd = p:dwords def qd = q:dwords while qdlimit q++[] = p++[] end end //[cf] //[of]:move (dst, src, size) //[c]Moves a memory block //[c] public func move (dst: mem, src: mem, size: size) equ s = src:bytes equ d = dst:bytes if d == s return end // incremental copy if dst is after src or // if dst is after the end of src if d < s || s+size <= d copy (d, s, size) else def q = d + size def p = s + size while q<>d (--q)[] = (--p)[] end end end //[cf] //[of]:first occurrence (buf, char, size) //[c]Returns the first occurrence of a char //[c] public func first occurrence (buf: mem, c: char, size: size) def p = buf:chars def limit = p + size while p 0:byte // extend with sign return diff:octet:int end ++p ++q end return 0 end //[cf] //[c] //[of]:is equal (dst, src, size) //[c] public equ is equal (dst: mem, src: mem, size: size) = compare (dst, src, size) == 0 //[cf] //[of]:not equal (dst, src, size) //[c] public equ not equal (dst: mem, src: mem, size: size) = compare (dst, src, size) <> 0 //[cf]