Remove shallow copies (#782)
This commit is contained in:
parent
7b103e02f2
commit
a086fcba72
|
@ -528,8 +528,7 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] =
|
||||||
|
|
||||||
field = Asn1Field(kind: Asn1Tag.Boolean, klass: aclass,
|
field = Asn1Field(kind: Asn1Tag.Boolean, klass: aclass,
|
||||||
index: ttag, offset: int(ab.offset),
|
index: ttag, offset: int(ab.offset),
|
||||||
length: 1)
|
length: 1, buffer: ab.buffer)
|
||||||
shallowCopy(field.buffer, ab.buffer)
|
|
||||||
field.vbool = (b == 0xFF'u8)
|
field.vbool = (b == 0xFF'u8)
|
||||||
ab.offset += 1
|
ab.offset += 1
|
||||||
return ok(field)
|
return ok(field)
|
||||||
|
@ -554,8 +553,7 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] =
|
||||||
# Negative or Positive integer
|
# Negative or Positive integer
|
||||||
field = Asn1Field(kind: Asn1Tag.Integer, klass: aclass,
|
field = Asn1Field(kind: Asn1Tag.Integer, klass: aclass,
|
||||||
index: ttag, offset: int(ab.offset),
|
index: ttag, offset: int(ab.offset),
|
||||||
length: int(length))
|
length: int(length), buffer: ab.buffer)
|
||||||
shallowCopy(field.buffer, ab.buffer)
|
|
||||||
if (ab.buffer[ab.offset] and 0x80'u8) == 0x80'u8:
|
if (ab.buffer[ab.offset] and 0x80'u8) == 0x80'u8:
|
||||||
# Negative integer
|
# Negative integer
|
||||||
if length <= 8:
|
if length <= 8:
|
||||||
|
@ -579,16 +577,15 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] =
|
||||||
# Zero value integer
|
# Zero value integer
|
||||||
field = Asn1Field(kind: Asn1Tag.Integer, klass: aclass,
|
field = Asn1Field(kind: Asn1Tag.Integer, klass: aclass,
|
||||||
index: ttag, offset: int(ab.offset),
|
index: ttag, offset: int(ab.offset),
|
||||||
length: int(length), vint: 0'u64)
|
length: int(length), vint: 0'u64,
|
||||||
shallowCopy(field.buffer, ab.buffer)
|
buffer: ab.buffer)
|
||||||
ab.offset += int(length)
|
ab.offset += int(length)
|
||||||
return ok(field)
|
return ok(field)
|
||||||
else:
|
else:
|
||||||
# Positive integer with leading zero
|
# Positive integer with leading zero
|
||||||
field = Asn1Field(kind: Asn1Tag.Integer, klass: aclass,
|
field = Asn1Field(kind: Asn1Tag.Integer, klass: aclass,
|
||||||
index: ttag, offset: int(ab.offset) + 1,
|
index: ttag, offset: int(ab.offset) + 1,
|
||||||
length: int(length) - 1)
|
length: int(length) - 1, buffer: ab.buffer)
|
||||||
shallowCopy(field.buffer, ab.buffer)
|
|
||||||
if length <= 9:
|
if length <= 9:
|
||||||
for i in 1 ..< int(length):
|
for i in 1 ..< int(length):
|
||||||
field.vint = (field.vint shl 8) or
|
field.vint = (field.vint shl 8) or
|
||||||
|
@ -610,8 +607,7 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] =
|
||||||
# Zero-length BIT STRING.
|
# Zero-length BIT STRING.
|
||||||
field = Asn1Field(kind: Asn1Tag.BitString, klass: aclass,
|
field = Asn1Field(kind: Asn1Tag.BitString, klass: aclass,
|
||||||
index: ttag, offset: int(ab.offset + 1),
|
index: ttag, offset: int(ab.offset + 1),
|
||||||
length: 0, ubits: 0)
|
length: 0, ubits: 0, buffer: ab.buffer)
|
||||||
shallowCopy(field.buffer, ab.buffer)
|
|
||||||
ab.offset += int(length)
|
ab.offset += int(length)
|
||||||
return ok(field)
|
return ok(field)
|
||||||
|
|
||||||
|
@ -631,8 +627,8 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] =
|
||||||
|
|
||||||
field = Asn1Field(kind: Asn1Tag.BitString, klass: aclass,
|
field = Asn1Field(kind: Asn1Tag.BitString, klass: aclass,
|
||||||
index: ttag, offset: int(ab.offset + 1),
|
index: ttag, offset: int(ab.offset + 1),
|
||||||
length: int(length - 1), ubits: int(unused))
|
length: int(length - 1), ubits: int(unused),
|
||||||
shallowCopy(field.buffer, ab.buffer)
|
buffer: ab.buffer)
|
||||||
ab.offset += int(length)
|
ab.offset += int(length)
|
||||||
return ok(field)
|
return ok(field)
|
||||||
|
|
||||||
|
@ -643,8 +639,7 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] =
|
||||||
|
|
||||||
field = Asn1Field(kind: Asn1Tag.OctetString, klass: aclass,
|
field = Asn1Field(kind: Asn1Tag.OctetString, klass: aclass,
|
||||||
index: ttag, offset: int(ab.offset),
|
index: ttag, offset: int(ab.offset),
|
||||||
length: int(length))
|
length: int(length), buffer: ab.buffer)
|
||||||
shallowCopy(field.buffer, ab.buffer)
|
|
||||||
ab.offset += int(length)
|
ab.offset += int(length)
|
||||||
return ok(field)
|
return ok(field)
|
||||||
|
|
||||||
|
@ -654,8 +649,7 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] =
|
||||||
return err(Asn1Error.Incorrect)
|
return err(Asn1Error.Incorrect)
|
||||||
|
|
||||||
field = Asn1Field(kind: Asn1Tag.Null, klass: aclass, index: ttag,
|
field = Asn1Field(kind: Asn1Tag.Null, klass: aclass, index: ttag,
|
||||||
offset: int(ab.offset), length: 0)
|
offset: int(ab.offset), length: 0, buffer: ab.buffer)
|
||||||
shallowCopy(field.buffer, ab.buffer)
|
|
||||||
ab.offset += int(length)
|
ab.offset += int(length)
|
||||||
return ok(field)
|
return ok(field)
|
||||||
|
|
||||||
|
@ -666,8 +660,7 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] =
|
||||||
|
|
||||||
field = Asn1Field(kind: Asn1Tag.Oid, klass: aclass,
|
field = Asn1Field(kind: Asn1Tag.Oid, klass: aclass,
|
||||||
index: ttag, offset: int(ab.offset),
|
index: ttag, offset: int(ab.offset),
|
||||||
length: int(length))
|
length: int(length), buffer: ab.buffer)
|
||||||
shallowCopy(field.buffer, ab.buffer)
|
|
||||||
ab.offset += int(length)
|
ab.offset += int(length)
|
||||||
return ok(field)
|
return ok(field)
|
||||||
|
|
||||||
|
@ -678,8 +671,7 @@ proc read*(ab: var Asn1Buffer): Asn1Result[Asn1Field] =
|
||||||
|
|
||||||
field = Asn1Field(kind: Asn1Tag.Sequence, klass: aclass,
|
field = Asn1Field(kind: Asn1Tag.Sequence, klass: aclass,
|
||||||
index: ttag, offset: int(ab.offset),
|
index: ttag, offset: int(ab.offset),
|
||||||
length: int(length))
|
length: int(length), buffer: ab.buffer)
|
||||||
shallowCopy(field.buffer, ab.buffer)
|
|
||||||
ab.offset += int(length)
|
ab.offset += int(length)
|
||||||
return ok(field)
|
return ok(field)
|
||||||
|
|
||||||
|
|
|
@ -516,15 +516,10 @@ proc trimRight(s: string, ch: char): string =
|
||||||
break
|
break
|
||||||
result = s[0..(s.high - m)]
|
result = s[0..(s.high - m)]
|
||||||
|
|
||||||
proc shcopy*(m1: var MultiAddress, m2: MultiAddress) =
|
|
||||||
shallowCopy(m1.data.buffer, m2.data.buffer)
|
|
||||||
m1.data.offset = m2.data.offset
|
|
||||||
|
|
||||||
proc protoCode*(ma: MultiAddress): MaResult[MultiCodec] =
|
proc protoCode*(ma: MultiAddress): MaResult[MultiCodec] =
|
||||||
## Returns MultiAddress ``ma`` protocol code.
|
## Returns MultiAddress ``ma`` protocol code.
|
||||||
var header: uint64
|
var header: uint64
|
||||||
var vb: MultiAddress
|
var vb = ma
|
||||||
shcopy(vb, ma)
|
|
||||||
if vb.data.readVarint(header) == -1:
|
if vb.data.readVarint(header) == -1:
|
||||||
err("multiaddress: Malformed binary address!")
|
err("multiaddress: Malformed binary address!")
|
||||||
else:
|
else:
|
||||||
|
@ -537,8 +532,7 @@ proc protoCode*(ma: MultiAddress): MaResult[MultiCodec] =
|
||||||
proc protoName*(ma: MultiAddress): MaResult[string] =
|
proc protoName*(ma: MultiAddress): MaResult[string] =
|
||||||
## Returns MultiAddress ``ma`` protocol name.
|
## Returns MultiAddress ``ma`` protocol name.
|
||||||
var header: uint64
|
var header: uint64
|
||||||
var vb: MultiAddress
|
var vb = ma
|
||||||
shcopy(vb, ma)
|
|
||||||
if vb.data.readVarint(header) == -1:
|
if vb.data.readVarint(header) == -1:
|
||||||
err("multiaddress: Malformed binary address!")
|
err("multiaddress: Malformed binary address!")
|
||||||
else:
|
else:
|
||||||
|
@ -555,9 +549,8 @@ proc protoArgument*(ma: MultiAddress,
|
||||||
## If current MultiAddress do not have argument value, then result will be
|
## If current MultiAddress do not have argument value, then result will be
|
||||||
## ``0``.
|
## ``0``.
|
||||||
var header: uint64
|
var header: uint64
|
||||||
var vb: MultiAddress
|
var vb = ma
|
||||||
var buffer: seq[byte]
|
var buffer: seq[byte]
|
||||||
shcopy(vb, ma)
|
|
||||||
if vb.data.readVarint(header) == -1:
|
if vb.data.readVarint(header) == -1:
|
||||||
err("multiaddress: Malformed binary address!")
|
err("multiaddress: Malformed binary address!")
|
||||||
else:
|
else:
|
||||||
|
@ -792,8 +785,7 @@ proc encode*(mbtype: typedesc[MultiBase], encoding: string,
|
||||||
proc validate*(ma: MultiAddress): bool =
|
proc validate*(ma: MultiAddress): bool =
|
||||||
## Returns ``true`` if MultiAddress ``ma`` is valid.
|
## Returns ``true`` if MultiAddress ``ma`` is valid.
|
||||||
var header: uint64
|
var header: uint64
|
||||||
var vb: MultiAddress
|
var vb = ma
|
||||||
shcopy(vb, ma)
|
|
||||||
while true:
|
while true:
|
||||||
if vb.data.isEmpty():
|
if vb.data.isEmpty():
|
||||||
break
|
break
|
||||||
|
|
|
@ -148,7 +148,7 @@ func init*(pid: var PeerId, data: string): bool =
|
||||||
if Base58.decode(data, p, length) == Base58Status.Success:
|
if Base58.decode(data, p, length) == Base58Status.Success:
|
||||||
p.setLen(length)
|
p.setLen(length)
|
||||||
var opid: PeerId
|
var opid: PeerId
|
||||||
shallowCopy(opid.data, p)
|
opid.data = p
|
||||||
if opid.validate():
|
if opid.validate():
|
||||||
pid = opid
|
pid = opid
|
||||||
result = true
|
result = true
|
||||||
|
|
|
@ -124,7 +124,7 @@ proc vsizeof*(field: ProtoField): int {.inline.} =
|
||||||
proc initProtoBuffer*(data: seq[byte], offset = 0,
|
proc initProtoBuffer*(data: seq[byte], offset = 0,
|
||||||
options: set[ProtoFlags] = {}): ProtoBuffer =
|
options: set[ProtoFlags] = {}): ProtoBuffer =
|
||||||
## Initialize ProtoBuffer with shallow copy of ``data``.
|
## Initialize ProtoBuffer with shallow copy of ``data``.
|
||||||
shallowCopy(result.buffer, data)
|
result.buffer = data
|
||||||
result.offset = offset
|
result.offset = offset
|
||||||
result.options = options
|
result.options = options
|
||||||
|
|
||||||
|
|
|
@ -39,21 +39,9 @@ proc len*(vb: VBuffer): int =
|
||||||
result = len(vb.buffer) - vb.offset
|
result = len(vb.buffer) - vb.offset
|
||||||
doAssert(result >= 0)
|
doAssert(result >= 0)
|
||||||
|
|
||||||
proc isLiteral[T](s: seq[T]): bool {.inline.} =
|
|
||||||
when defined(gcOrc) or defined(gcArc):
|
|
||||||
false
|
|
||||||
else:
|
|
||||||
type
|
|
||||||
SeqHeader = object
|
|
||||||
length, reserved: int
|
|
||||||
(cast[ptr SeqHeader](s).reserved and (1 shl (sizeof(int) * 8 - 2))) != 0
|
|
||||||
|
|
||||||
proc initVBuffer*(data: seq[byte], offset = 0): VBuffer =
|
proc initVBuffer*(data: seq[byte], offset = 0): VBuffer =
|
||||||
## Initialize VBuffer with shallow copy of ``data``.
|
## Initialize VBuffer with shallow copy of ``data``.
|
||||||
if isLiteral(data):
|
result.buffer = data
|
||||||
result.buffer = data
|
|
||||||
else:
|
|
||||||
shallowCopy(result.buffer, data)
|
|
||||||
result.offset = offset
|
result.offset = offset
|
||||||
|
|
||||||
proc initVBuffer*(data: openArray[byte], offset = 0): VBuffer =
|
proc initVBuffer*(data: openArray[byte], offset = 0): VBuffer =
|
||||||
|
|
Loading…
Reference in New Issue