mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-25 04:15:09 +00:00
remove ArrayDummySize with unchecked arrays (#5818)
This commit is contained in:
committed by
Andreas Rumpf
parent
b2aae477d1
commit
22684370b0
@@ -132,7 +132,7 @@ translated into a C array of undetermined size:
|
||||
|
||||
.. code-block:: nim
|
||||
type
|
||||
ArrayPart{.unchecked.} = array[0..0, int]
|
||||
ArrayPart{.unchecked.} = array[0, int]
|
||||
MySeq = object
|
||||
len, cap: int
|
||||
data: ArrayPart
|
||||
@@ -146,10 +146,6 @@ Produces roughly this C code:
|
||||
NI data[];
|
||||
} MySeq;
|
||||
|
||||
The bounds checking done at compile time is not disabled for now, so to access
|
||||
``s.data[C]`` (where ``C`` is a constant) the array's index needs to
|
||||
include ``C``.
|
||||
|
||||
The base type of the unchecked array may not contain any GC'ed memory but this
|
||||
is currently not checked.
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ type
|
||||
L: Natural
|
||||
spart: seq[T]
|
||||
apart: array[ArrayPartSize, T]
|
||||
UncheckedArray* {.unchecked.}[T] = array[0..100_000_000, T]
|
||||
UncheckedArray* {.unchecked.}[T] = array[0, T]
|
||||
|
||||
template usesSeqPart(x): untyped = x.L > ArrayPartSize
|
||||
|
||||
|
||||
@@ -9,10 +9,8 @@
|
||||
|
||||
## Shared string support for Nim.
|
||||
|
||||
const ArrayDummySize = when defined(cpu16): 10_000 else: 100_000_000
|
||||
|
||||
type
|
||||
UncheckedCharArray {.unchecked.} = array[0..ArrayDummySize, char]
|
||||
UncheckedCharArray = UncheckedArray[char]
|
||||
|
||||
type
|
||||
Buffer = ptr object
|
||||
|
||||
@@ -208,7 +208,7 @@ else:
|
||||
import locks
|
||||
|
||||
type
|
||||
SharedArray {.unchecked.}[T] = array[0..100, T]
|
||||
SharedArray[T] = UncheckedArray[T]
|
||||
|
||||
proc allocSharedArray[T](nsize: int): ptr SharedArray[T] =
|
||||
result = cast[ptr SharedArray[T]](allocShared0(sizeof(T) * nsize))
|
||||
|
||||
@@ -268,6 +268,9 @@ type
|
||||
seq*{.magic: "Seq".}[T] ## Generic type to construct sequences.
|
||||
set*{.magic: "Set".}[T] ## Generic type to construct bit sets.
|
||||
|
||||
UncheckedArray* {.unchecked.}[T] = array[0, T]
|
||||
## Array with no boudns checking
|
||||
|
||||
when defined(nimArrIdx):
|
||||
# :array|openarray|string|seq|cstring|tuple
|
||||
proc `[]`*[I: Ordinal;T](a: T; i: I): T {.
|
||||
@@ -381,8 +384,6 @@ include "system/inclrtl"
|
||||
const NoFakeVars* = defined(nimscript) ## true if the backend doesn't support \
|
||||
## "fake variables" like 'var EBADF {.importc.}: cint'.
|
||||
|
||||
const ArrayDummySize = when defined(cpu16): 10_000 else: 100_000_000
|
||||
|
||||
when not defined(JS):
|
||||
type
|
||||
TGenericSeq {.compilerproc, pure, inheritable.} = object
|
||||
@@ -390,10 +391,9 @@ when not defined(JS):
|
||||
when defined(gogc):
|
||||
elemSize: int
|
||||
PGenericSeq {.exportc.} = ptr TGenericSeq
|
||||
UncheckedCharArray {.unchecked.} = array[0..ArrayDummySize, char]
|
||||
# len and space without counting the terminating zero:
|
||||
NimStringDesc {.compilerproc, final.} = object of TGenericSeq
|
||||
data: UncheckedCharArray
|
||||
data: UncheckedArray[char]
|
||||
NimString = ptr NimStringDesc
|
||||
|
||||
when not defined(JS) and not defined(nimscript):
|
||||
@@ -1600,8 +1600,7 @@ type # these work for most platforms:
|
||||
culonglong* {.importc: "unsigned long long", nodecl.} = uint64
|
||||
## This is the same as the type ``unsigned long long`` in *C*.
|
||||
|
||||
cstringArray* {.importc: "char**", nodecl.} = ptr
|
||||
array[0..ArrayDummySize, cstring]
|
||||
cstringArray* {.importc: "char**", nodecl.} = ptr UncheckedArray[cstring]
|
||||
## This is binary compatible to the type ``char**`` in *C*. The array's
|
||||
## high value is large enough to disable bounds checking in practice.
|
||||
## Use `cstringArrayToSeq` to convert it into a ``seq[string]``.
|
||||
@@ -3045,7 +3044,8 @@ when not defined(JS): #and not defined(nimscript):
|
||||
## creates a NULL terminated cstringArray from `a`. The result has to
|
||||
## be freed with `deallocCStringArray` after it's not needed anymore.
|
||||
result = cast[cstringArray](alloc0((a.len+1) * sizeof(cstring)))
|
||||
let x = cast[ptr array[0..ArrayDummySize, string]](a)
|
||||
|
||||
let x = cast[ptr UncheckedArray[string]](a)
|
||||
for i in 0 .. a.high:
|
||||
result[i] = cast[cstring](alloc0(x[i].len+1))
|
||||
copyMem(result[i], addr(x[i][0]), x[i].len)
|
||||
|
||||
@@ -30,13 +30,12 @@ type
|
||||
key: ByteAddress # start address at bit 0
|
||||
bits: array[BitIndex, int] # a bit vector
|
||||
|
||||
PPageDescArray = ptr array[ArrayDummySize, PPageDesc]
|
||||
PPageDescArray = ptr UncheckedArray[PPageDesc]
|
||||
CellSet {.final, pure.} = object
|
||||
counter, max: int
|
||||
head: PPageDesc
|
||||
data: PPageDescArray
|
||||
|
||||
PCellArray = ptr array[ArrayDummySize, PCell]
|
||||
PCellArray = ptr UncheckedArray[PCell]
|
||||
CellSeq {.final, pure.} = object
|
||||
len, cap: int
|
||||
d: PCellArray
|
||||
|
||||
@@ -34,7 +34,7 @@ const
|
||||
|
||||
type
|
||||
PPointer = ptr pointer
|
||||
ByteArray = array[0..ArrayDummySize, byte]
|
||||
ByteArray = UncheckedArray[byte]
|
||||
PByte = ptr ByteArray
|
||||
PString = ptr string
|
||||
{.deprecated: [TByteArray: ByteArray].}
|
||||
|
||||
@@ -86,11 +86,11 @@ proc writeBuffer(f: File, buffer: pointer, len: Natural): int =
|
||||
checkErr(f)
|
||||
|
||||
proc writeBytes(f: File, a: openArray[int8|uint8], start, len: Natural): int =
|
||||
var x = cast[ptr array[ArrayDummySize, int8]](a)
|
||||
result = writeBuffer(f, addr(x[start]), len)
|
||||
var x = cast[ptr UncheckedArray[int8]](a)
|
||||
result = writeBuffer(f, addr(x[int(start)]), len)
|
||||
proc writeChars(f: File, a: openArray[char], start, len: Natural): int =
|
||||
var x = cast[ptr array[ArrayDummySize, int8]](a)
|
||||
result = writeBuffer(f, addr(x[start]), len)
|
||||
var x = cast[ptr UncheckedArray[int8]](a)
|
||||
result = writeBuffer(f, addr(x[int(start)]), len)
|
||||
|
||||
proc write(f: File, s: string) =
|
||||
if writeBuffer(f, cstring(s), s.len) != s.len:
|
||||
|
||||
@@ -15,7 +15,7 @@ when not declared(NimString):
|
||||
|
||||
type
|
||||
Utf16Char* = distinct int16
|
||||
WideCString* = ref array[ArrayDummySize, Utf16Char]
|
||||
WideCString* = ref UncheckedArray[Utf16Char]
|
||||
{.deprecated: [TUtf16Char: Utf16Char].}
|
||||
|
||||
proc len*(w: WideCString): int =
|
||||
|
||||
5
tests/array/tunchecked.nim
Normal file
5
tests/array/tunchecked.nim
Normal file
@@ -0,0 +1,5 @@
|
||||
{.boundchecks: on.}
|
||||
type Unchecked {.unchecked.} = array[0, char]
|
||||
|
||||
var x = cast[ptr Unchecked](alloc(100))
|
||||
x[5] = 'x'
|
||||
@@ -18,11 +18,10 @@ when true:
|
||||
uoffset_t* = uint32
|
||||
FlatBufferBuilder* = object
|
||||
|
||||
uarray* {.unchecked.} [T] = array [0..0, T]
|
||||
Array* [T] = object
|
||||
o*: uoffset_t
|
||||
len*: int
|
||||
data*: ptr uarray[T]
|
||||
data*: ptr UncheckedArray[T]
|
||||
|
||||
proc ca* (fbb: ptr FlatBufferBuilder, T: typedesc, len: int): Array[T] {.noinit.} =
|
||||
result.len = len
|
||||
|
||||
Reference in New Issue
Block a user