mirror of
https://github.com/nim-lang/Nim.git
synced 2025-12-28 17:04:41 +00:00
Raising exceptions halfway through a memory allocation is undefined
behavior since exceptions themselves require multiple allocations and
the allocator functions are not reentrant.
It is of course also expensive performance-wise to introduce lots of
exception-raising code everywhere since it breaks many optimisations and
bloats the code.
Finally, performing pointer arithmetic with signed integers is incorrect
for example on on a 32-bit systems that allows up to 3gb of address
space for applications (large address extensions) and unnecessary
elsewhere - broadly, stuff inside the memory allocator is generated by
the compiler or controlled by the standard library meaning that
applications should not be forced to pay this price.
If we wanted to check for overflow, the right way would be in the
initial allocation location where both the size and count of objects is
known.
The code is updated to use the same arithmetic operator style as for
refc with unchecked operations rather than disabling overflow checking
wholesale in the allocator module - there are reasons for both, but
going with the existing flow seems like an easier place to start.
(cherry picked from commit 8b9972c8b6)
47 lines
1.0 KiB
Nim
47 lines
1.0 KiB
Nim
#
|
|
#
|
|
# Nim's Runtime Library
|
|
# (c) Copyright 2019 Andreas Rumpf
|
|
#
|
|
# See the file "copying.txt", included in this
|
|
# distribution, for details about the copyright.
|
|
#
|
|
|
|
# ------------------- cell seq handling ---------------------------------------
|
|
|
|
type
|
|
PCellArray = ptr UncheckedArray[PCell]
|
|
CellSeq {.final, pure.} = object
|
|
len, cap: int
|
|
d: PCellArray
|
|
|
|
proc contains(s: CellSeq, c: PCell): bool {.inline.} =
|
|
for i in 0 ..< s.len:
|
|
if s.d[i] == c:
|
|
return true
|
|
return false
|
|
|
|
proc resize(s: var CellSeq) =
|
|
s.cap = s.cap div 2 +% s.cap
|
|
let d = cast[PCellArray](alloc(cast[Natural](s.cap *% sizeof(PCell))))
|
|
copyMem(d, s.d, s.len *% sizeof(PCell))
|
|
dealloc(s.d)
|
|
s.d = d
|
|
|
|
proc add(s: var CellSeq, c: PCell) {.inline.} =
|
|
if s.len >= s.cap:
|
|
resize(s)
|
|
s.d[s.len] = c
|
|
inc(s.len)
|
|
|
|
proc init(s: var CellSeq, cap: int = 1024) =
|
|
s.len = 0
|
|
s.cap = cap
|
|
s.d = cast[PCellArray](alloc0(cast[Natural](cap *% sizeof(PCell))))
|
|
|
|
proc deinit(s: var CellSeq) =
|
|
dealloc(s.d)
|
|
s.d = nil
|
|
s.len = 0
|
|
s.cap = 0
|