From 22684370b0eb5938bc7a4172dc968e4c557a02ee Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Mon, 28 Aug 2017 21:44:35 +0800 Subject: [PATCH] remove ArrayDummySize with unchecked arrays (#5818) --- doc/manual/ffi.txt | 6 +----- lib/pure/collections/rtarrays.nim | 2 +- lib/pure/collections/sharedstrings.nim | 4 +--- lib/pure/ioselectors.nim | 2 +- lib/system.nim | 14 +++++++------- lib/system/cellsets.nim | 5 ++--- lib/system/mmdisp.nim | 2 +- lib/system/sysio.nim | 8 ++++---- lib/system/widestrs.nim | 2 +- tests/array/tunchecked.nim | 5 +++++ tests/metatype/ttypedesc2.nim | 3 +-- 11 files changed, 25 insertions(+), 28 deletions(-) create mode 100644 tests/array/tunchecked.nim diff --git a/doc/manual/ffi.txt b/doc/manual/ffi.txt index e9b52eaca1..06fed14309 100644 --- a/doc/manual/ffi.txt +++ b/doc/manual/ffi.txt @@ -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. diff --git a/lib/pure/collections/rtarrays.nim b/lib/pure/collections/rtarrays.nim index 89a02553a0..3849117a00 100644 --- a/lib/pure/collections/rtarrays.nim +++ b/lib/pure/collections/rtarrays.nim @@ -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 diff --git a/lib/pure/collections/sharedstrings.nim b/lib/pure/collections/sharedstrings.nim index 10ab307672..a9e194fb44 100644 --- a/lib/pure/collections/sharedstrings.nim +++ b/lib/pure/collections/sharedstrings.nim @@ -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 diff --git a/lib/pure/ioselectors.nim b/lib/pure/ioselectors.nim index cbef5ce0d9..ef80722219 100644 --- a/lib/pure/ioselectors.nim +++ b/lib/pure/ioselectors.nim @@ -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)) diff --git a/lib/system.nim b/lib/system.nim index 42d2ff0b1c..419fa3c941 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -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) diff --git a/lib/system/cellsets.nim b/lib/system/cellsets.nim index ab6191aab9..f26cb86ab0 100644 --- a/lib/system/cellsets.nim +++ b/lib/system/cellsets.nim @@ -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 diff --git a/lib/system/mmdisp.nim b/lib/system/mmdisp.nim index fb2f3471f5..d2160fdac4 100644 --- a/lib/system/mmdisp.nim +++ b/lib/system/mmdisp.nim @@ -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].} diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index c9049a1342..7b6d93fe01 100644 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -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: diff --git a/lib/system/widestrs.nim b/lib/system/widestrs.nim index d11ed7d1b5..a8b28c2797 100644 --- a/lib/system/widestrs.nim +++ b/lib/system/widestrs.nim @@ -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 = diff --git a/tests/array/tunchecked.nim b/tests/array/tunchecked.nim new file mode 100644 index 0000000000..f5ac3642d5 --- /dev/null +++ b/tests/array/tunchecked.nim @@ -0,0 +1,5 @@ +{.boundchecks: on.} +type Unchecked {.unchecked.} = array[0, char] + +var x = cast[ptr Unchecked](alloc(100)) +x[5] = 'x' diff --git a/tests/metatype/ttypedesc2.nim b/tests/metatype/ttypedesc2.nim index e576ec91a0..7650a6f6b4 100644 --- a/tests/metatype/ttypedesc2.nim +++ b/tests/metatype/ttypedesc2.nim @@ -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