parent
f34ae81971
commit
c0d240b8cd
4 changed files with 70 additions and 14 deletions
|
|
@ -19,6 +19,11 @@ var
|
||||||
envComputed {.threadvar.}: bool
|
envComputed {.threadvar.}: bool
|
||||||
environment {.threadvar.}: seq[string]
|
environment {.threadvar.}: seq[string]
|
||||||
|
|
||||||
|
when defined(nimV2):
|
||||||
|
proc unpairedEnvAllocs*(): int =
|
||||||
|
result = environment.len
|
||||||
|
if result > 0: inc result
|
||||||
|
|
||||||
when defined(windows) and not defined(nimscript):
|
when defined(windows) and not defined(nimscript):
|
||||||
# because we support Windows GUI applications, things get really
|
# because we support Windows GUI applications, things get really
|
||||||
# messy here...
|
# messy here...
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,52 @@
|
||||||
|
|
||||||
type
|
type
|
||||||
Utf16Char* = distinct int16
|
Utf16Char* = distinct int16
|
||||||
WideCString* = ref UncheckedArray[Utf16Char]
|
|
||||||
|
when defined(nimv2):
|
||||||
|
|
||||||
|
import core / allocators
|
||||||
|
|
||||||
|
type
|
||||||
|
WideCString* = ptr UncheckedArray[Utf16Char]
|
||||||
|
|
||||||
|
WideCStringObj* = object
|
||||||
|
bytes: int
|
||||||
|
data: WideCString
|
||||||
|
|
||||||
|
proc `=destroy`(a: var WideCStringObj) =
|
||||||
|
if a.data != nil:
|
||||||
|
let alor = getLocalAllocator()
|
||||||
|
alor.dealloc(alor, a.data, a.bytes)
|
||||||
|
a.data = nil
|
||||||
|
|
||||||
|
proc `=`(a: var WideCStringObj; b: WideCStringObj) {.error.}
|
||||||
|
|
||||||
|
proc `=sink`(a: var WideCStringObj; b: WideCStringObj) =
|
||||||
|
a.bytes = b.bytes
|
||||||
|
a.data = b.data
|
||||||
|
|
||||||
|
proc createWide(a: var WideCStringObj; bytes: int) =
|
||||||
|
a.bytes = bytes
|
||||||
|
let alor = getLocalAllocator()
|
||||||
|
a.data = cast[typeof(a.data)](alor.alloc(alor, bytes))
|
||||||
|
|
||||||
|
template `[]`(a: WideCStringObj; idx: int): Utf16Char = a.data[idx]
|
||||||
|
template `[]=`(a: WideCStringObj; idx: int; val: Utf16Char) = a.data[idx] = val
|
||||||
|
|
||||||
|
template nullWide(): untyped = WideCStringObj(bytes: 0, data: nil)
|
||||||
|
|
||||||
|
converter toWideCString*(x: WideCStringObj): WideCString {.inline.} =
|
||||||
|
result = x.data
|
||||||
|
|
||||||
|
else:
|
||||||
|
template nullWide(): untyped = nil
|
||||||
|
|
||||||
|
type
|
||||||
|
WideCString* = ref UncheckedArray[Utf16Char]
|
||||||
|
WideCStringObj* = WideCString
|
||||||
|
|
||||||
|
template createWide(a; L) =
|
||||||
|
unsafeNew(a, L * 4 + 2)
|
||||||
|
|
||||||
proc ord(arg: Utf16Char): int = int(cast[uint16](arg))
|
proc ord(arg: Utf16Char): int = int(cast[uint16](arg))
|
||||||
|
|
||||||
|
|
@ -95,8 +140,8 @@ iterator runes(s: cstring, L: int): int =
|
||||||
fastRuneAt(s, i, L, result, true)
|
fastRuneAt(s, i, L, result, true)
|
||||||
yield result
|
yield result
|
||||||
|
|
||||||
proc newWideCString*(source: cstring, L: int): WideCString =
|
proc newWideCString*(source: cstring, L: int): WideCStringObj =
|
||||||
unsafeNew(result, L * 4 + 2)
|
createWide(result, L * 4 + 2)
|
||||||
#result = cast[wideCString](alloc(L * 4 + 2))
|
#result = cast[wideCString](alloc(L * 4 + 2))
|
||||||
var d = 0
|
var d = 0
|
||||||
for ch in runes(source, L):
|
for ch in runes(source, L):
|
||||||
|
|
@ -116,12 +161,12 @@ proc newWideCString*(source: cstring, L: int): WideCString =
|
||||||
inc d
|
inc d
|
||||||
result[d] = Utf16Char(0)
|
result[d] = Utf16Char(0)
|
||||||
|
|
||||||
proc newWideCString*(s: cstring): WideCString =
|
proc newWideCString*(s: cstring): WideCStringObj =
|
||||||
if s.isNil: return nil
|
if s.isNil: return nullWide
|
||||||
|
|
||||||
result = newWideCString(s, s.len)
|
result = newWideCString(s, s.len)
|
||||||
|
|
||||||
proc newWideCString*(s: string): WideCString =
|
proc newWideCString*(s: string): WideCStringObj =
|
||||||
result = newWideCString(s, s.len)
|
result = newWideCString(s, s.len)
|
||||||
|
|
||||||
proc `$`*(w: WideCString, estimate: int, replacement: int = 0xFFFD): string =
|
proc `$`*(w: WideCString, estimate: int, replacement: int = 0xFFFD): string =
|
||||||
|
|
|
||||||
|
|
@ -13,21 +13,21 @@ proc add[T](x: var seq[T]; y: openArray[T])
|
||||||
first type mismatch at position: 1
|
first type mismatch at position: 1
|
||||||
required type for x: var seq[T]
|
required type for x: var seq[T]
|
||||||
but expression 'k' is of type: Alias
|
but expression 'k' is of type: Alias
|
||||||
|
proc add[T](x: var seq[T]; y: T)
|
||||||
|
first type mismatch at position: 1
|
||||||
|
required type for x: var seq[T]
|
||||||
|
but expression 'k' is of type: Alias
|
||||||
proc add(result: var string; x: float)
|
proc add(result: var string; x: float)
|
||||||
first type mismatch at position: 1
|
first type mismatch at position: 1
|
||||||
required type for result: var string
|
required type for result: var string
|
||||||
but expression 'k' is of type: Alias
|
but expression 'k' is of type: Alias
|
||||||
proc add(x: var string; y: string)
|
|
||||||
first type mismatch at position: 1
|
|
||||||
required type for x: var string
|
|
||||||
but expression 'k' is of type: Alias
|
|
||||||
proc add(x: var string; y: cstring)
|
proc add(x: var string; y: cstring)
|
||||||
first type mismatch at position: 1
|
first type mismatch at position: 1
|
||||||
required type for x: var string
|
required type for x: var string
|
||||||
but expression 'k' is of type: Alias
|
but expression 'k' is of type: Alias
|
||||||
proc add[T](x: var seq[T]; y: T)
|
proc add(x: var string; y: string)
|
||||||
first type mismatch at position: 1
|
first type mismatch at position: 1
|
||||||
required type for x: var seq[T]
|
required type for x: var string
|
||||||
but expression 'k' is of type: Alias
|
but expression 'k' is of type: Alias
|
||||||
proc add(result: var string; x: int64)
|
proc add(result: var string; x: int64)
|
||||||
first type mismatch at position: 1
|
first type mismatch at position: 1
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,8 @@
|
||||||
discard """
|
discard """
|
||||||
cmd: '''nim cpp --newruntime $file'''
|
cmd: '''nim cpp --newruntime $file'''
|
||||||
output: '''(field: "value")
|
output: '''(field: "value")
|
||||||
3 3 new: 0'''
|
Indeed
|
||||||
|
0 new: 0'''
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import core / allocators
|
import core / allocators
|
||||||
|
|
@ -13,10 +14,15 @@ type
|
||||||
Node = ref object
|
Node = ref object
|
||||||
field: string
|
field: string
|
||||||
|
|
||||||
|
# bug #11807
|
||||||
|
import os
|
||||||
|
putEnv("HEAPTRASHING", "Indeed")
|
||||||
|
|
||||||
proc main =
|
proc main =
|
||||||
var w = newTable[string, owned Node]()
|
var w = newTable[string, owned Node]()
|
||||||
w["key"] = Node(field: "value")
|
w["key"] = Node(field: "value")
|
||||||
echo w["key"][]
|
echo w["key"][]
|
||||||
|
echo getEnv("HEAPTRASHING")
|
||||||
|
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
@ -41,4 +47,4 @@ type
|
||||||
var t: MyType
|
var t: MyType
|
||||||
|
|
||||||
let (a, d) = allocCounters()
|
let (a, d) = allocCounters()
|
||||||
discard cprintf("%ld %ld new: %ld\n", a, d, allocs)
|
discard cprintf("%ld new: %ld\n", a - unpairedEnvAllocs() - d, allocs)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue