📄 memory.zc
字号:
//[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 qd<limit
qd++[] = pd++[]
end
limit += 3
p = pd:bytes
q = qd:bytes
end
while q<>limit
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<limit
if p[]==c
return p
end
++p
end
return nil
end
//[cf]
//[of]:compare (dst, src, size)
//[c]Compare two memory blocks
//[c]
public func compare (dst: mem, src: mem, size: size)
def p = src:bytes
def q = dst:bytes
def limit = q + size
while q < limit
def diff = q[] - p[]
if diff <> 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]
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -