Merge pull request #2472 from def-/natural

Use more Natural and Positive numbers in proc parameters
This commit is contained in:
Andreas Rumpf 2015-04-07 13:37:08 +02:00
commit d170a51f54
10 changed files with 1196 additions and 1196 deletions

View file

@ -24,7 +24,7 @@ proc `*`*(x: int, order: SortOrder): int {.inline.} =
var y = order.ord - 1 var y = order.ord - 1
result = (x xor y) - y result = (x xor y) - y
proc reverse*[T](a: var openArray[T], first, last: int) = proc reverse*[T](a: var openArray[T], first, last: Natural) =
## reverses the array ``a[first..last]``. ## reverses the array ``a[first..last]``.
var x = first var x = first
var y = last var y = last
@ -37,7 +37,7 @@ proc reverse*[T](a: var openArray[T]) =
## reverses the array `a`. ## reverses the array `a`.
reverse(a, 0, a.high) reverse(a, 0, a.high)
proc reversed*[T](a: openArray[T], first, last: int): seq[T] = proc reversed*[T](a: openArray[T], first, last: Natural): seq[T] =
## returns the reverse of the array `a[first..last]`. ## returns the reverse of the array `a[first..last]`.
result = newSeq[T](last - first + 1) result = newSeq[T](last - first + 1)
var x = first var x = first

View file

@ -114,7 +114,7 @@ proc mustRehash(length, counter: int): bool {.inline.} =
assert(length > counter) assert(length > counter)
result = (length * 2 < counter * 3) or (length - counter < 4) result = (length * 2 < counter * 3) or (length - counter < 4)
proc rightSize*(count: int): int {.inline.} = proc rightSize*(count: Natural): int {.inline.} =
## Return the value of `initialSize` to support `count` items. ## Return the value of `initialSize` to support `count` items.
## ##
## If more items are expected to be added, simply add that ## If more items are expected to be added, simply add that

View file

@ -128,7 +128,7 @@ proc mustRehash(length, counter: int): bool {.inline.} =
assert(length > counter) assert(length > counter)
result = (length * 2 < counter * 3) or (length - counter < 4) result = (length * 2 < counter * 3) or (length - counter < 4)
proc rightSize*(count: int): int {.inline.} = proc rightSize*(count: Natural): int {.inline.} =
## Return the value of `initialSize` to support `count` items. ## Return the value of `initialSize` to support `count` items.
## ##
## If more items are expected to be added, simply add that ## If more items are expected to be added, simply add that

View file

@ -386,7 +386,7 @@ proc split*(s: string, sep: string): seq[string] {.noSideEffect,
## `split iterator <#split.i,string,string>`_. ## `split iterator <#split.i,string,string>`_.
accumulateResult(split(s, sep)) accumulateResult(split(s, sep))
proc toHex*(x: BiggestInt, len: int): string {.noSideEffect, proc toHex*(x: BiggestInt, len: Positive): string {.noSideEffect,
rtl, extern: "nsuToHex".} = rtl, extern: "nsuToHex".} =
## Converts `x` to its hexadecimal representation. ## Converts `x` to its hexadecimal representation.
## ##
@ -403,7 +403,7 @@ proc toHex*(x: BiggestInt, len: int): string {.noSideEffect,
# handle negative overflow # handle negative overflow
if n == 0 and x < 0: n = -1 if n == 0 and x < 0: n = -1
proc intToStr*(x: int, minchars: int = 1): string {.noSideEffect, proc intToStr*(x: int, minchars: Positive = 1): string {.noSideEffect,
rtl, extern: "nsuIntToStr".} = rtl, extern: "nsuIntToStr".} =
## Converts `x` to its decimal representation. ## Converts `x` to its decimal representation.
## ##
@ -499,7 +499,7 @@ proc parseEnum*[T: enum](s: string, default: T): T =
return e return e
result = default result = default
proc repeat*(c: char, count: int): string {.noSideEffect, proc repeat*(c: char, count: Natural): string {.noSideEffect,
rtl, extern: "nsuRepeatChar".} = rtl, extern: "nsuRepeatChar".} =
## Returns a string of length `count` consisting only of ## Returns a string of length `count` consisting only of
## the character `c`. You can use this proc to left align strings. Example: ## the character `c`. You can use this proc to left align strings. Example:
@ -514,7 +514,7 @@ proc repeat*(c: char, count: int): string {.noSideEffect,
result = newString(count) result = newString(count)
for i in 0..count-1: result[i] = c for i in 0..count-1: result[i] = c
proc repeat*(s: string, n: int): string {.noSideEffect, proc repeat*(s: string, n: Natural): string {.noSideEffect,
rtl, extern: "nsuRepeatStr".} = rtl, extern: "nsuRepeatStr".} =
## Returns String `s` concatenated `n` times. Example: ## Returns String `s` concatenated `n` times. Example:
## ##
@ -523,7 +523,7 @@ proc repeat*(s: string, n: int): string {.noSideEffect,
result = newStringOfCap(n * s.len) result = newStringOfCap(n * s.len)
for i in 1..n: result.add(s) for i in 1..n: result.add(s)
template spaces*(n: int): string = repeat(' ',n) template spaces*(n: Natural): string = repeat(' ',n)
## Returns a String with `n` space characters. You can use this proc ## Returns a String with `n` space characters. You can use this proc
## to left align strings. Example: ## to left align strings. Example:
## ##
@ -535,15 +535,15 @@ template spaces*(n: int): string = repeat(' ',n)
## echo text1 & spaces(max(0, width - text1.len)) & "|" ## echo text1 & spaces(max(0, width - text1.len)) & "|"
## echo text2 & spaces(max(0, width - text2.len)) & "|" ## echo text2 & spaces(max(0, width - text2.len)) & "|"
proc repeatChar*(count: int, c: char = ' '): string {.deprecated.} = repeat(c, count) proc repeatChar*(count: Natural, c: char = ' '): string {.deprecated.} = repeat(c, count)
## deprecated: use repeat() or spaces() ## deprecated: use repeat() or spaces()
proc repeatStr*(count: int, s: string): string {.deprecated.} = repeat(s, count) proc repeatStr*(count: Natural, s: string): string {.deprecated.} = repeat(s, count)
## deprecated: use repeat(string, count) or string.repeat(count) ## deprecated: use repeat(string, count) or string.repeat(count)
proc align*(s: string, count: int, padding = ' '): string {. proc align*(s: string, count: Natural, padding = ' '): string {.
noSideEffect, rtl, extern: "nsuAlignString".} = noSideEffect, rtl, extern: "nsuAlignString".} =
## Aligns a string `s` with `padding`, so that is of length `count`. ## Aligns a string `s` with `padding`, so that it is of length `count`.
## ##
## `padding` characters (by default spaces) are added before `s` resulting in ## `padding` characters (by default spaces) are added before `s` resulting in
## right alignment. If ``s.len >= count``, no spaces are added and `s` is ## right alignment. If ``s.len >= count``, no spaces are added and `s` is
@ -682,7 +682,7 @@ proc endsWith*(s, suffix: string): bool {.noSideEffect,
inc(i) inc(i)
if suffix[i] == '\0': return true if suffix[i] == '\0': return true
proc continuesWith*(s, substr: string, start: int): bool {.noSideEffect, proc continuesWith*(s, substr: string, start: Natural): bool {.noSideEffect,
rtl, extern: "nsuContinuesWith".} = rtl, extern: "nsuContinuesWith".} =
## Returns true iff ``s`` continues with ``substr`` at position ``start``. ## Returns true iff ``s`` continues with ``substr`` at position ``start``.
## ##
@ -693,8 +693,8 @@ proc continuesWith*(s, substr: string, start: int): bool {.noSideEffect,
if s[i+start] != substr[i]: return false if s[i+start] != substr[i]: return false
inc(i) inc(i)
proc addSep*(dest: var string, sep = ", ", startLen = 0) {.noSideEffect, proc addSep*(dest: var string, sep = ", ", startLen: Natural = 0)
inline.} = {.noSideEffect, inline.} =
## Adds a separator to `dest` only if its length is bigger than `startLen`. ## Adds a separator to `dest` only if its length is bigger than `startLen`.
## ##
## A shorthand for: ## A shorthand for:
@ -784,7 +784,7 @@ proc findAux(s, sub: string, start: int, a: SkipTable): int =
inc(j, a[s[j+m]]) inc(j, a[s[j+m]])
return -1 return -1
proc find*(s, sub: string, start: int = 0): int {.noSideEffect, proc find*(s, sub: string, start: Natural = 0): int {.noSideEffect,
rtl, extern: "nsuFindStr".} = rtl, extern: "nsuFindStr".} =
## Searches for `sub` in `s` starting at position `start`. ## Searches for `sub` in `s` starting at position `start`.
## ##
@ -793,7 +793,7 @@ proc find*(s, sub: string, start: int = 0): int {.noSideEffect,
preprocessSub(sub, a) preprocessSub(sub, a)
result = findAux(s, sub, start, a) result = findAux(s, sub, start, a)
proc find*(s: string, sub: char, start: int = 0): int {.noSideEffect, proc find*(s: string, sub: char, start: Natural = 0): int {.noSideEffect,
rtl, extern: "nsuFindChar".} = rtl, extern: "nsuFindChar".} =
## Searches for `sub` in `s` starting at position `start`. ## Searches for `sub` in `s` starting at position `start`.
## ##
@ -802,7 +802,7 @@ proc find*(s: string, sub: char, start: int = 0): int {.noSideEffect,
if sub == s[i]: return i if sub == s[i]: return i
return -1 return -1
proc find*(s: string, chars: set[char], start: int = 0): int {.noSideEffect, proc find*(s: string, chars: set[char], start: Natural = 0): int {.noSideEffect,
rtl, extern: "nsuFindCharSet".} = rtl, extern: "nsuFindCharSet".} =
## Searches for `chars` in `s` starting at position `start`. ## Searches for `chars` in `s` starting at position `start`.
## ##
@ -976,7 +976,7 @@ proc parseOctInt*(s: string): int {.noSideEffect,
of '\0': break of '\0': break
else: raise newException(ValueError, "invalid integer: " & s) else: raise newException(ValueError, "invalid integer: " & s)
proc toOct*(x: BiggestInt, len: int): string {.noSideEffect, proc toOct*(x: BiggestInt, len: Positive): string {.noSideEffect,
rtl, extern: "nsuToOct".} = rtl, extern: "nsuToOct".} =
## Converts `x` into its octal representation. ## Converts `x` into its octal representation.
## ##
@ -992,7 +992,7 @@ proc toOct*(x: BiggestInt, len: int): string {.noSideEffect,
shift = shift + 3 shift = shift + 3
mask = mask shl 3 mask = mask shl 3
proc toBin*(x: BiggestInt, len: int): string {.noSideEffect, proc toBin*(x: BiggestInt, len: Positive): string {.noSideEffect,
rtl, extern: "nsuToBin".} = rtl, extern: "nsuToBin".} =
## Converts `x` into its binary representation. ## Converts `x` into its binary representation.
## ##

View file

@ -39,7 +39,7 @@ proc runeLen*(s: string): int {.rtl, extern: "nuc$1".} =
else: inc i else: inc i
inc(result) inc(result)
proc runeLenAt*(s: string, i: int): int = proc runeLenAt*(s: string, i: Natural): int =
## returns the number of bytes the rune starting at ``s[i]`` takes. ## returns the number of bytes the rune starting at ``s[i]`` takes.
if ord(s[i]) <=% 127: result = 1 if ord(s[i]) <=% 127: result = 1
elif ord(s[i]) shr 5 == 0b110: result = 2 elif ord(s[i]) shr 5 == 0b110: result = 2
@ -105,7 +105,7 @@ template fastRuneAt*(s: string, i: int, result: expr, doInc = true) =
result = Rune(ord(s[i])) result = Rune(ord(s[i]))
when doInc: inc(i) when doInc: inc(i)
proc runeAt*(s: string, i: int): Rune = proc runeAt*(s: string, i: Natural): Rune =
## returns the unicode character in `s` at byte index `i` ## returns the unicode character in `s` at byte index `i`
fastRuneAt(s, i, result, false) fastRuneAt(s, i, result, false)

View file

@ -177,12 +177,6 @@ proc new*(T: typedesc): ref T =
## reference to it as result value ## reference to it as result value
new(result) new(result)
proc unsafeNew*[T](a: var ref T, size: int) {.magic: "New", noSideEffect.}
## creates a new object of type ``T`` and returns a safe (traced)
## reference to it in ``a``. This is **unsafe** as it allocates an object
## of the passed ``size``. This should only be used for optimization
## purposes when you know what you're doing!
proc internalNew*[T](a: var ref T) {.magic: "New", noSideEffect.} proc internalNew*[T](a: var ref T) {.magic: "New", noSideEffect.}
## leaked implementation detail. Do not use. ## leaked implementation detail. Do not use.
@ -517,7 +511,13 @@ type
ESynch: Exception ESynch: Exception
].} ].}
proc sizeof*[T](x: T): Natural {.magic: "SizeOf", noSideEffect.} proc unsafeNew*[T](a: var ref T, size: Natural) {.magic: "New", noSideEffect.}
## creates a new object of type ``T`` and returns a safe (traced)
## reference to it in ``a``. This is **unsafe** as it allocates an object
## of the passed ``size``. This should only be used for optimization
## purposes when you know what you're doing!
proc sizeof*[T](x: T): int {.magic: "SizeOf", noSideEffect.}
## returns the size of ``x`` in bytes. Since this is a low-level proc, ## returns the size of ``x`` in bytes. Since this is a low-level proc,
## its usage is discouraged - using ``new`` for the most cases suffices ## its usage is discouraged - using ``new`` for the most cases suffices
## that one never needs to know ``x``'s size. As a special semantic rule, ## that one never needs to know ``x``'s size. As a special semantic rule,
@ -551,7 +551,7 @@ proc dec*[T: Ordinal|uint|uint64](x: var T, y = 1) {.magic: "Dec", noSideEffect.
## exist, ``EOutOfRange`` is raised or a compile time error occurs. This is a ## exist, ``EOutOfRange`` is raised or a compile time error occurs. This is a
## short notation for: ``x = pred(x, y)``. ## short notation for: ``x = pred(x, y)``.
proc newSeq*[T](s: var seq[T], len: int) {.magic: "NewSeq", noSideEffect.} proc newSeq*[T](s: var seq[T], len: Natural) {.magic: "NewSeq", noSideEffect.}
## creates a new sequence of type ``seq[T]`` with length ``len``. ## creates a new sequence of type ``seq[T]`` with length ``len``.
## This is equivalent to ``s = @[]; setlen(s, len)``, but more ## This is equivalent to ``s = @[]; setlen(s, len)``, but more
## efficient since no reallocation is needed. ## efficient since no reallocation is needed.
@ -569,7 +569,7 @@ proc newSeq*[T](s: var seq[T], len: int) {.magic: "NewSeq", noSideEffect.}
## inputStrings[2] = "would crash" ## inputStrings[2] = "would crash"
## #inputStrings[3] = "out of bounds" ## #inputStrings[3] = "out of bounds"
proc newSeq*[T](len = 0): seq[T] = proc newSeq*[T](len = 0.Natural): seq[T] =
## creates a new sequence of type ``seq[T]`` with length ``len``. ## creates a new sequence of type ``seq[T]`` with length ``len``.
## ##
## Note that the sequence will be filled with zeroed entries, which can be a ## Note that the sequence will be filled with zeroed entries, which can be a
@ -943,7 +943,7 @@ proc `@` * [IDX, T](a: array[IDX, T]): seq[T] {.
## sequences with the array constructor: ``@[1, 2, 3]`` has the type ## sequences with the array constructor: ``@[1, 2, 3]`` has the type
## ``seq[int]``, while ``[1, 2, 3]`` has the type ``array[0..2, int]``. ## ``seq[int]``, while ``[1, 2, 3]`` has the type ``array[0..2, int]``.
proc setLen*[T](s: var seq[T], newlen: int) {. proc setLen*[T](s: var seq[T], newlen: Natural) {.
magic: "SetLengthSeq", noSideEffect.} magic: "SetLengthSeq", noSideEffect.}
## sets the length of `s` to `newlen`. ## sets the length of `s` to `newlen`.
## ``T`` may be any sequence type. ## ``T`` may be any sequence type.
@ -951,14 +951,14 @@ proc setLen*[T](s: var seq[T], newlen: int) {.
## ``s`` will be truncated. `s` cannot be nil! To initialize a sequence with ## ``s`` will be truncated. `s` cannot be nil! To initialize a sequence with
## a size, use ``newSeq`` instead. ## a size, use ``newSeq`` instead.
proc setLen*(s: var string, newlen: int) {. proc setLen*(s: var string, newlen: Natural) {.
magic: "SetLengthStr", noSideEffect.} magic: "SetLengthStr", noSideEffect.}
## sets the length of `s` to `newlen`. ## sets the length of `s` to `newlen`.
## If the current length is greater than the new length, ## If the current length is greater than the new length,
## ``s`` will be truncated. `s` cannot be nil! To initialize a string with ## ``s`` will be truncated. `s` cannot be nil! To initialize a string with
## a size, use ``newString`` instead. ## a size, use ``newString`` instead.
proc newString*(len: int): string {. proc newString*(len: Natural): string {.
magic: "NewString", importc: "mnewString", noSideEffect.} magic: "NewString", importc: "mnewString", noSideEffect.}
## returns a new string of length ``len`` but with uninitialized ## returns a new string of length ``len`` but with uninitialized
## content. One needs to fill the string character after character ## content. One needs to fill the string character after character
@ -966,7 +966,7 @@ proc newString*(len: int): string {.
## optimization purposes; the same effect can be achieved with the ## optimization purposes; the same effect can be achieved with the
## ``&`` operator or with ``add``. ## ``&`` operator or with ``add``.
proc newStringOfCap*(cap: int): string {. proc newStringOfCap*(cap: Natural): string {.
magic: "NewStringOfCap", importc: "rawNewString", noSideEffect.} magic: "NewStringOfCap", importc: "rawNewString", noSideEffect.}
## returns a new string of length ``0`` but with capacity `cap`.This ## returns a new string of length ``0`` but with capacity `cap`.This
## procedure exists only for optimization purposes; the same effect can ## procedure exists only for optimization purposes; the same effect can
@ -1156,21 +1156,21 @@ proc shallowCopy*[T](x: var T, y: T) {.noSideEffect, magic: "ShallowCopy".}
## is a reason why the default assignment does a deep copy of sequences ## is a reason why the default assignment does a deep copy of sequences
## and strings. ## and strings.
proc del*[T](x: var seq[T], i: int) {.noSideEffect.} = proc del*[T](x: var seq[T], i: Natural) {.noSideEffect.} =
## deletes the item at index `i` by putting ``x[high(x)]`` into position `i`. ## deletes the item at index `i` by putting ``x[high(x)]`` into position `i`.
## This is an O(1) operation. ## This is an O(1) operation.
let xl = x.len let xl = x.len
shallowCopy(x[i], x[xl-1]) shallowCopy(x[i], x[xl-1])
setLen(x, xl-1) setLen(x, xl-1)
proc delete*[T](x: var seq[T], i: int) {.noSideEffect.} = proc delete*[T](x: var seq[T], i: Natural) {.noSideEffect.} =
## deletes the item at index `i` by moving ``x[i+1..]`` by one position. ## deletes the item at index `i` by moving ``x[i+1..]`` by one position.
## This is an O(n) operation. ## This is an O(n) operation.
let xl = x.len let xl = x.len
for j in i..xl-2: shallowCopy(x[j], x[j+1]) for j in i..xl-2: shallowCopy(x[j], x[j+1])
setLen(x, xl-1) setLen(x, xl-1)
proc insert*[T](x: var seq[T], item: T, i = 0) {.noSideEffect.} = proc insert*[T](x: var seq[T], item: T, i = 0.Natural) {.noSideEffect.} =
## inserts `item` into `x` at position `i`. ## inserts `item` into `x` at position `i`.
let xl = x.len let xl = x.len
setLen(x, xl+1) setLen(x, xl+1)
@ -1316,19 +1316,19 @@ proc substr*(s: string, first, last: int): string {.
## or `limit`:idx: a string's length. ## or `limit`:idx: a string's length.
when not defined(nimrodVM): when not defined(nimrodVM):
proc zeroMem*(p: pointer, size: int) {.importc, noDecl, benign.} proc zeroMem*(p: pointer, size: Natural) {.importc, noDecl, benign.}
## overwrites the contents of the memory at ``p`` with the value 0. ## overwrites the contents of the memory at ``p`` with the value 0.
## Exactly ``size`` bytes will be overwritten. Like any procedure ## Exactly ``size`` bytes will be overwritten. Like any procedure
## dealing with raw memory this is *unsafe*. ## dealing with raw memory this is *unsafe*.
proc copyMem*(dest, source: pointer, size: int) {. proc copyMem*(dest, source: pointer, size: Natural) {.
importc: "memcpy", header: "<string.h>", benign.} importc: "memcpy", header: "<string.h>", benign.}
## copies the contents from the memory at ``source`` to the memory ## copies the contents from the memory at ``source`` to the memory
## at ``dest``. Exactly ``size`` bytes will be copied. The memory ## at ``dest``. Exactly ``size`` bytes will be copied. The memory
## regions may not overlap. Like any procedure dealing with raw ## regions may not overlap. Like any procedure dealing with raw
## memory this is *unsafe*. ## memory this is *unsafe*.
proc moveMem*(dest, source: pointer, size: int) {. proc moveMem*(dest, source: pointer, size: Natural) {.
importc: "memmove", header: "<string.h>", benign.} importc: "memmove", header: "<string.h>", benign.}
## copies the contents from the memory at ``source`` to the memory ## copies the contents from the memory at ``source`` to the memory
## at ``dest``. Exactly ``size`` bytes will be copied. The memory ## at ``dest``. Exactly ``size`` bytes will be copied. The memory
@ -1336,7 +1336,7 @@ when not defined(nimrodVM):
## and is thus somewhat more safe than ``copyMem``. Like any procedure ## and is thus somewhat more safe than ``copyMem``. Like any procedure
## dealing with raw memory this is still *unsafe*, though. ## dealing with raw memory this is still *unsafe*, though.
proc equalMem*(a, b: pointer, size: int): bool {. proc equalMem*(a, b: pointer, size: Natural): bool {.
importc: "equalMem", noDecl, noSideEffect.} importc: "equalMem", noDecl, noSideEffect.}
## compares the memory blocks ``a`` and ``b``. ``size`` bytes will ## compares the memory blocks ``a`` and ``b``. ``size`` bytes will
## be compared. If the blocks are equal, true is returned, false ## be compared. If the blocks are equal, true is returned, false
@ -1344,7 +1344,7 @@ when not defined(nimrodVM):
## *unsafe*. ## *unsafe*.
when hostOS != "standalone": when hostOS != "standalone":
proc alloc*(size: int): pointer {.noconv, rtl, tags: [], benign.} proc alloc*(size: Natural): pointer {.noconv, rtl, tags: [], benign.}
## allocates a new memory block with at least ``size`` bytes. The ## allocates a new memory block with at least ``size`` bytes. The
## block has to be freed with ``realloc(block, 0)`` or ## block has to be freed with ``realloc(block, 0)`` or
## ``dealloc(block)``. The block is not initialized, so reading ## ``dealloc(block)``. The block is not initialized, so reading
@ -1359,7 +1359,7 @@ when not defined(nimrodVM):
## The allocated memory belongs to its allocating thread! ## The allocated memory belongs to its allocating thread!
## Use `createSharedU` to allocate from a shared heap. ## Use `createSharedU` to allocate from a shared heap.
cast[ptr T](alloc(T.sizeof * size)) cast[ptr T](alloc(T.sizeof * size))
proc alloc0*(size: int): pointer {.noconv, rtl, tags: [], benign.} proc alloc0*(size: Natural): pointer {.noconv, rtl, tags: [], benign.}
## allocates a new memory block with at least ``size`` bytes. The ## allocates a new memory block with at least ``size`` bytes. The
## block has to be freed with ``realloc(block, 0)`` or ## block has to be freed with ``realloc(block, 0)`` or
## ``dealloc(block)``. The block is initialized with all bytes ## ``dealloc(block)``. The block is initialized with all bytes
@ -1374,7 +1374,7 @@ when not defined(nimrodVM):
## The allocated memory belongs to its allocating thread! ## The allocated memory belongs to its allocating thread!
## Use `createShared` to allocate from a shared heap. ## Use `createShared` to allocate from a shared heap.
cast[ptr T](alloc0(T.sizeof * size)) cast[ptr T](alloc0(T.sizeof * size))
proc realloc*(p: pointer, newSize: int): pointer {.noconv, rtl, tags: [], proc realloc*(p: pointer, newSize: Natural): pointer {.noconv, rtl, tags: [],
benign.} benign.}
## grows or shrinks a given memory block. If p is **nil** then a new ## grows or shrinks a given memory block. If p is **nil** then a new
## memory block is returned. In either way the block has at least ## memory block is returned. In either way the block has at least
@ -1402,7 +1402,7 @@ when not defined(nimrodVM):
## Use `deallocShared` to deallocate from a shared heap. ## Use `deallocShared` to deallocate from a shared heap.
proc free*[T](p: ptr T) {.inline, benign.} = proc free*[T](p: ptr T) {.inline, benign.} =
dealloc(p) dealloc(p)
proc allocShared*(size: int): pointer {.noconv, rtl, benign.} proc allocShared*(size: Natural): pointer {.noconv, rtl, benign.}
## allocates a new memory block on the shared heap with at ## allocates a new memory block on the shared heap with at
## least ``size`` bytes. The block has to be freed with ## least ``size`` bytes. The block has to be freed with
## ``reallocShared(block, 0)`` or ``deallocShared(block)``. The block ## ``reallocShared(block, 0)`` or ``deallocShared(block)``. The block
@ -1416,7 +1416,7 @@ when not defined(nimrodVM):
## is not initialized, so reading from it before writing to it is ## is not initialized, so reading from it before writing to it is
## undefined behaviour! ## undefined behaviour!
cast[ptr T](allocShared(T.sizeof * size)) cast[ptr T](allocShared(T.sizeof * size))
proc allocShared0*(size: int): pointer {.noconv, rtl, benign.} proc allocShared0*(size: Natural): pointer {.noconv, rtl, benign.}
## allocates a new memory block on the shared heap with at ## allocates a new memory block on the shared heap with at
## least ``size`` bytes. The block has to be freed with ## least ``size`` bytes. The block has to be freed with
## ``reallocShared(block, 0)`` or ``deallocShared(block)``. ## ``reallocShared(block, 0)`` or ``deallocShared(block)``.
@ -1429,7 +1429,7 @@ when not defined(nimrodVM):
## The block is initialized with all bytes ## The block is initialized with all bytes
## containing zero, so it is somewhat safer than ``createSharedU``. ## containing zero, so it is somewhat safer than ``createSharedU``.
cast[ptr T](allocShared0(T.sizeof * size)) cast[ptr T](allocShared0(T.sizeof * size))
proc reallocShared*(p: pointer, newSize: int): pointer {.noconv, rtl, proc reallocShared*(p: pointer, newSize: Natural): pointer {.noconv, rtl,
benign.} benign.}
## grows or shrinks a given memory block on the heap. If p is **nil** ## grows or shrinks a given memory block on the heap. If p is **nil**
## then a new memory block is returned. In either way the block has at ## then a new memory block is returned. In either way the block has at
@ -2493,37 +2493,37 @@ when not defined(JS): #and not defined(NimrodVM):
proc getFileSize*(f: File): int64 {.tags: [ReadIOEffect], benign.} proc getFileSize*(f: File): int64 {.tags: [ReadIOEffect], benign.}
## retrieves the file size (in bytes) of `f`. ## retrieves the file size (in bytes) of `f`.
proc readBytes*(f: File, a: var openArray[int8|uint8], start, len: int): int {. proc readBytes*(f: File, a: var openArray[int8|uint8], start, len: Natural): int {.
tags: [ReadIOEffect], benign.} tags: [ReadIOEffect], benign.}
## reads `len` bytes into the buffer `a` starting at ``a[start]``. Returns ## reads `len` bytes into the buffer `a` starting at ``a[start]``. Returns
## the actual number of bytes that have been read which may be less than ## the actual number of bytes that have been read which may be less than
## `len` (if not as many bytes are remaining), but not greater. ## `len` (if not as many bytes are remaining), but not greater.
proc readChars*(f: File, a: var openArray[char], start, len: int): int {. proc readChars*(f: File, a: var openArray[char], start, len: Natural): int {.
tags: [ReadIOEffect], benign.} tags: [ReadIOEffect], benign.}
## reads `len` bytes into the buffer `a` starting at ``a[start]``. Returns ## reads `len` bytes into the buffer `a` starting at ``a[start]``. Returns
## the actual number of bytes that have been read which may be less than ## the actual number of bytes that have been read which may be less than
## `len` (if not as many bytes are remaining), but not greater. ## `len` (if not as many bytes are remaining), but not greater.
proc readBuffer*(f: File, buffer: pointer, len: int): int {. proc readBuffer*(f: File, buffer: pointer, len: Natural): int {.
tags: [ReadIOEffect], benign.} tags: [ReadIOEffect], benign.}
## reads `len` bytes into the buffer pointed to by `buffer`. Returns ## reads `len` bytes into the buffer pointed to by `buffer`. Returns
## the actual number of bytes that have been read which may be less than ## the actual number of bytes that have been read which may be less than
## `len` (if not as many bytes are remaining), but not greater. ## `len` (if not as many bytes are remaining), but not greater.
proc writeBytes*(f: File, a: openArray[int8|uint8], start, len: int): int {. proc writeBytes*(f: File, a: openArray[int8|uint8], start, len: Natural): int {.
tags: [WriteIOEffect], benign.} tags: [WriteIOEffect], benign.}
## writes the bytes of ``a[start..start+len-1]`` to the file `f`. Returns ## writes the bytes of ``a[start..start+len-1]`` to the file `f`. Returns
## the number of actual written bytes, which may be less than `len` in case ## the number of actual written bytes, which may be less than `len` in case
## of an error. ## of an error.
proc writeChars*(f: File, a: openArray[char], start, len: int): int {. proc writeChars*(f: File, a: openArray[char], start, len: Natural): int {.
tags: [WriteIOEffect], benign.} tags: [WriteIOEffect], benign.}
## writes the bytes of ``a[start..start+len-1]`` to the file `f`. Returns ## writes the bytes of ``a[start..start+len-1]`` to the file `f`. Returns
## the number of actual written bytes, which may be less than `len` in case ## the number of actual written bytes, which may be less than `len` in case
## of an error. ## of an error.
proc writeBuffer*(f: File, buffer: pointer, len: int): int {. proc writeBuffer*(f: File, buffer: pointer, len: Natural): int {.
tags: [WriteIOEffect], benign.} tags: [WriteIOEffect], benign.}
## writes the bytes of buffer pointed to by the parameter `buffer` to the ## writes the bytes of buffer pointed to by the parameter `buffer` to the
## file `f`. Returns the number of actual written bytes, which may be less ## file `f`. Returns the number of actual written bytes, which may be less
@ -2545,7 +2545,7 @@ when not defined(JS): #and not defined(NimrodVM):
when not defined(nimfix): when not defined(nimfix):
{.deprecated: [fileHandle: getFileHandle].} {.deprecated: [fileHandle: getFileHandle].}
proc cstringArrayToSeq*(a: cstringArray, len: int): seq[string] = proc cstringArrayToSeq*(a: cstringArray, len: Natural): seq[string] =
## converts a ``cstringArray`` to a ``seq[string]``. `a` is supposed to be ## converts a ``cstringArray`` to a ``seq[string]``. `a` is supposed to be
## of length ``len``. ## of length ``len``.
newSeq(result, len) newSeq(result, len)
@ -2815,14 +2815,14 @@ elif defined(JS):
proc getTotalMem(): int = return -1 proc getTotalMem(): int = return -1
proc dealloc(p: pointer) = discard proc dealloc(p: pointer) = discard
proc alloc(size: int): pointer = discard proc alloc(size: Natural): pointer = discard
proc alloc0(size: int): pointer = discard proc alloc0(size: Natural): pointer = discard
proc realloc(p: pointer, newsize: int): pointer = discard proc realloc(p: pointer, newsize: Natural): pointer = discard
proc allocShared(size: int): pointer = discard proc allocShared(size: Natural): pointer = discard
proc allocShared0(size: int): pointer = discard proc allocShared0(size: Natural): pointer = discard
proc deallocShared(p: pointer) = discard proc deallocShared(p: pointer) = discard
proc reallocShared(p: pointer, newsize: int): pointer = discard proc reallocShared(p: pointer, newsize: Natural): pointer = discard
when defined(JS): when defined(JS):
include "system/jssys" include "system/jssys"
@ -3168,7 +3168,7 @@ when false:
payload() payload()
when hostOS != "standalone": when hostOS != "standalone":
proc insert*(x: var string, item: string, i = 0) {.noSideEffect.} = proc insert*(x: var string, item: string, i = 0.Natural) {.noSideEffect.} =
## inserts `item` into `x` at position `i`. ## inserts `item` into `x` at position `i`.
var xl = x.len var xl = x.len
setLen(x, xl+item.len) setLen(x, xl+item.len)

View file

@ -711,13 +711,13 @@ proc ptrSize(p: pointer): int =
if not isSmallChunk(c): if not isSmallChunk(c):
dec result, bigChunkOverhead() dec result, bigChunkOverhead()
proc alloc(allocator: var TMemRegion, size: int): pointer = proc alloc(allocator: var TMemRegion, size: Natural): pointer =
result = rawAlloc(allocator, size+sizeof(TFreeCell)) result = rawAlloc(allocator, size+sizeof(TFreeCell))
cast[ptr TFreeCell](result).zeroField = 1 # mark it as used cast[ptr TFreeCell](result).zeroField = 1 # mark it as used
sysAssert(not isAllocatedPtr(allocator, result), "alloc") sysAssert(not isAllocatedPtr(allocator, result), "alloc")
result = cast[pointer](cast[ByteAddress](result) +% sizeof(TFreeCell)) result = cast[pointer](cast[ByteAddress](result) +% sizeof(TFreeCell))
proc alloc0(allocator: var TMemRegion, size: int): pointer = proc alloc0(allocator: var TMemRegion, size: Natural): pointer =
result = alloc(allocator, size) result = alloc(allocator, size)
zeroMem(result, size) zeroMem(result, size)
@ -730,7 +730,7 @@ proc dealloc(allocator: var TMemRegion, p: pointer) =
rawDealloc(allocator, x) rawDealloc(allocator, x)
sysAssert(not isAllocatedPtr(allocator, x), "dealloc 3") sysAssert(not isAllocatedPtr(allocator, x), "dealloc 3")
proc realloc(allocator: var TMemRegion, p: pointer, newsize: int): pointer = proc realloc(allocator: var TMemRegion, p: pointer, newsize: Natural): pointer =
if newsize > 0: if newsize > 0:
result = alloc0(allocator, newsize) result = alloc0(allocator, newsize)
if p != nil: if p != nil:
@ -774,16 +774,16 @@ template instantiateForRegion(allocator: expr) =
proc deallocOsPages = deallocOsPages(allocator) proc deallocOsPages = deallocOsPages(allocator)
proc alloc(size: int): pointer = proc alloc(size: Natural): pointer =
result = alloc(allocator, size) result = alloc(allocator, size)
proc alloc0(size: int): pointer = proc alloc0(size: Natural): pointer =
result = alloc0(allocator, size) result = alloc0(allocator, size)
proc dealloc(p: pointer) = proc dealloc(p: pointer) =
dealloc(allocator, p) dealloc(allocator, p)
proc realloc(p: pointer, newsize: int): pointer = proc realloc(p: pointer, newsize: Natural): pointer =
result = realloc(allocator, p, newSize) result = realloc(allocator, p, newSize)
when false: when false:
@ -807,7 +807,7 @@ template instantiateForRegion(allocator: expr) =
var heapLock: TSysLock var heapLock: TSysLock
initSysLock(heapLock) initSysLock(heapLock)
proc allocShared(size: int): pointer = proc allocShared(size: Natural): pointer =
when hasThreadSupport: when hasThreadSupport:
acquireSys(heapLock) acquireSys(heapLock)
result = alloc(sharedHeap, size) result = alloc(sharedHeap, size)
@ -815,7 +815,7 @@ template instantiateForRegion(allocator: expr) =
else: else:
result = alloc(size) result = alloc(size)
proc allocShared0(size: int): pointer = proc allocShared0(size: Natural): pointer =
result = allocShared(size) result = allocShared(size)
zeroMem(result, size) zeroMem(result, size)
@ -827,7 +827,7 @@ template instantiateForRegion(allocator: expr) =
else: else:
dealloc(p) dealloc(p)
proc reallocShared(p: pointer, newsize: int): pointer = proc reallocShared(p: pointer, newsize: Natural): pointer =
when hasThreadSupport: when hasThreadSupport:
acquireSys(heapLock) acquireSys(heapLock)
result = realloc(sharedHeap, p, newsize) result = realloc(sharedHeap, p, newsize)

View file

@ -109,24 +109,24 @@ when defined(boehmgc):
when not defined(useNimRtl): when not defined(useNimRtl):
proc alloc(size: int): pointer = proc alloc(size: Natural): pointer =
result = boehmAlloc(size) result = boehmAlloc(size)
if result == nil: raiseOutOfMem() if result == nil: raiseOutOfMem()
proc alloc0(size: int): pointer = proc alloc0(size: Natural): pointer =
result = alloc(size) result = alloc(size)
zeroMem(result, size) zeroMem(result, size)
proc realloc(p: pointer, newsize: int): pointer = proc realloc(p: pointer, newsize: Natural): pointer =
result = boehmRealloc(p, newsize) result = boehmRealloc(p, newsize)
if result == nil: raiseOutOfMem() if result == nil: raiseOutOfMem()
proc dealloc(p: pointer) = boehmDealloc(p) proc dealloc(p: pointer) = boehmDealloc(p)
proc allocShared(size: int): pointer = proc allocShared(size: Natural): pointer =
result = boehmAlloc(size) result = boehmAlloc(size)
if result == nil: raiseOutOfMem() if result == nil: raiseOutOfMem()
proc allocShared0(size: int): pointer = proc allocShared0(size: Natural): pointer =
result = alloc(size) result = alloc(size)
zeroMem(result, size) zeroMem(result, size)
proc reallocShared(p: pointer, newsize: int): pointer = proc reallocShared(p: pointer, newsize: Natural): pointer =
result = boehmRealloc(p, newsize) result = boehmRealloc(p, newsize)
if result == nil: raiseOutOfMem() if result == nil: raiseOutOfMem()
proc deallocShared(p: pointer) = boehmDealloc(p) proc deallocShared(p: pointer) = boehmDealloc(p)
@ -196,24 +196,24 @@ when defined(boehmgc):
elif defined(nogc) and defined(useMalloc): elif defined(nogc) and defined(useMalloc):
when not defined(useNimRtl): when not defined(useNimRtl):
proc alloc(size: int): pointer = proc alloc(size: Natural): pointer =
result = cmalloc(size) result = cmalloc(size)
if result == nil: raiseOutOfMem() if result == nil: raiseOutOfMem()
proc alloc0(size: int): pointer = proc alloc0(size: Natural): pointer =
result = alloc(size) result = alloc(size)
zeroMem(result, size) zeroMem(result, size)
proc realloc(p: pointer, newsize: int): pointer = proc realloc(p: pointer, newsize: Natural): pointer =
result = crealloc(p, newsize) result = crealloc(p, newsize)
if result == nil: raiseOutOfMem() if result == nil: raiseOutOfMem()
proc dealloc(p: pointer) = cfree(p) proc dealloc(p: pointer) = cfree(p)
proc allocShared(size: int): pointer = proc allocShared(size: Natural): pointer =
result = cmalloc(size) result = cmalloc(size)
if result == nil: raiseOutOfMem() if result == nil: raiseOutOfMem()
proc allocShared0(size: int): pointer = proc allocShared0(size: Natural): pointer =
result = alloc(size) result = alloc(size)
zeroMem(result, size) zeroMem(result, size)
proc reallocShared(p: pointer, newsize: int): pointer = proc reallocShared(p: pointer, newsize: Natural): pointer =
result = crealloc(p, newsize) result = crealloc(p, newsize)
if result == nil: raiseOutOfMem() if result == nil: raiseOutOfMem()
proc deallocShared(p: pointer) = cfree(p) proc deallocShared(p: pointer) = cfree(p)

View file

@ -243,23 +243,23 @@ proc open(f: var File, filehandle: FileHandle, mode: FileMode): bool =
proc fwrite(buf: pointer, size, n: int, f: File): int {. proc fwrite(buf: pointer, size, n: int, f: File): int {.
importc: "fwrite", noDecl.} importc: "fwrite", noDecl.}
proc readBuffer(f: File, buffer: pointer, len: int): int = proc readBuffer(f: File, buffer: pointer, len: Natural): int =
result = fread(buffer, 1, len, f) result = fread(buffer, 1, len, f)
proc readBytes(f: File, a: var openArray[int8|uint8], start, len: int): int = proc readBytes(f: File, a: var openArray[int8|uint8], start, len: Natural): int =
result = readBuffer(f, addr(a[start]), len) result = readBuffer(f, addr(a[start]), len)
proc readChars(f: File, a: var openArray[char], start, len: int): int = proc readChars(f: File, a: var openArray[char], start, len: Natural): int =
result = readBuffer(f, addr(a[start]), len) result = readBuffer(f, addr(a[start]), len)
{.push stackTrace:off, profiler:off.} {.push stackTrace:off, profiler:off.}
proc writeBytes(f: File, a: openArray[int8|uint8], start, len: int): int = proc writeBytes(f: File, a: openArray[int8|uint8], start, len: Natural): int =
var x = cast[ptr array[0..1000_000_000, int8]](a) var x = cast[ptr array[0..1000_000_000, int8]](a)
result = writeBuffer(f, addr(x[start]), len) result = writeBuffer(f, addr(x[start]), len)
proc writeChars(f: File, a: openArray[char], start, len: int): int = proc writeChars(f: File, a: openArray[char], start, len: Natural): int =
var x = cast[ptr array[0..1000_000_000, int8]](a) var x = cast[ptr array[0..1000_000_000, int8]](a)
result = writeBuffer(f, addr(x[start]), len) result = writeBuffer(f, addr(x[start]), len)
proc writeBuffer(f: File, buffer: pointer, len: int): int = proc writeBuffer(f: File, buffer: pointer, len: Natural): int =
result = fwrite(buffer, 1, len, f) result = fwrite(buffer, 1, len, f)
proc write(f: File, s: string) = proc write(f: File, s: string) =