Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Clyybber committed Oct 1, 2019
1 parent 64d5e25 commit 328dba1
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/pure/memfiles.nim
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ iterator memSlices*(mfile: MemFile, delim = '\l', eat = '\r'): MemSlice {.inline
ms.data = mfile.mem
var remaining = mfile.size
while remaining > 0:
ending = c_memchr(ms.data, delim, remaining)
ending = c_memchr(ms.data, delim, csize remaining)
if ending == nil: # unterminated final slice
ms.size = remaining # Weird case..check eat?
yield ms
Expand Down
2 changes: 1 addition & 1 deletion lib/pure/strutils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1872,7 +1872,7 @@ proc find*(s: string, sub: char, start: Natural = 0, last = 0): int {.noSideEffe
when hasCStringBuiltin:
let L = last-start+1
if L > 0:
let found = c_memchr(s[start].unsafeAddr, sub, L)
let found = c_memchr(s[start].unsafeAddr, sub, csize L)
if not found.isNil:
return cast[ByteAddress](found) -% cast[ByteAddress](s.cstring)
else:
Expand Down
4 changes: 2 additions & 2 deletions lib/system.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2233,7 +2233,7 @@ type # these work for most platforms:
## This is the same as the type ``short`` in *C*.
cint* {.importc: "int", nodecl.} = int32
## This is the same as the type ``int`` in *C*.
csize* {.importc: "size_t", nodecl.} = int
csize* {.importc: "size_t", nodecl.} = uint
## This is the same as the type ``size_t`` in *C*.
clonglong* {.importc: "long long", nodecl.} = int64
## This is the same as the type ``long long`` in *C*.
Expand Down Expand Up @@ -3597,7 +3597,7 @@ when not defined(JS): #and not defined(nimscript):
when declared(memTrackerOp):
memTrackerOp("copyMem", dest, size)
proc moveMem(dest, source: pointer, size: Natural) =
c_memmove(dest, source, size)
c_memmove(dest, source, csize size)
when declared(memTrackerOp):
memTrackerOp("moveMem", dest, size)
proc equalMem(a, b: pointer, size: Natural): bool =
Expand Down
18 changes: 17 additions & 1 deletion lib/system/ansi_c.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,24 @@ when not defined(nimHasHotCodeReloading):

proc c_memchr*(s: pointer, c: cint, n: csize): pointer {.
importc: "memchr", header: "<string.h>".}
proc c_memchr*(s: pointer, c: cint, n: cint): pointer {.
importc: "memchr", header: "<string.h>", deprecated: "csize is now uint".}
proc c_memcmp*(a, b: pointer, size: csize): cint {.
importc: "memcmp", header: "<string.h>", noSideEffect.}
proc c_memcmp*(a, b: pointer, size: cint): cint {.
importc: "memcmp", header: "<string.h>", noSideEffect, deprecated: "csize is now uint".}
proc c_memcpy*(a, b: pointer, size: csize): pointer {.
importc: "memcpy", header: "<string.h>", discardable.}
proc c_memcpy*(a, b: pointer, size: cint): pointer {.
importc: "memcpy", header: "<string.h>", discardable, deprecated: "csize is now uint".}
proc c_memmove*(a, b: pointer, size: csize): pointer {.
importc: "memmove", header: "<string.h>",discardable.}
proc c_memmove*(a, b: pointer, size: cint): pointer {.
importc: "memmove", header: "<string.h>",discardable, deprecated: "csize is now uint".}
proc c_memset*(p: pointer, value: cint, size: csize): pointer {.
importc: "memset", header: "<string.h>", discardable.}
proc c_memset*(p: pointer, value: cint, size: cint): pointer {.
importc: "memset", header: "<string.h>", discardable, deprecated: "csize is now uint".}
proc c_strcmp*(a, b: cstring): cint {.
importc: "strcmp", header: "<string.h>", noSideEffect.}
proc c_strlen*(a: cstring): csize {.
Expand Down Expand Up @@ -134,16 +144,22 @@ proc c_sprintf*(buf, frmt: cstring): cint {.

proc c_malloc*(size: csize): pointer {.
importc: "malloc", header: "<stdlib.h>".}
proc c_malloc*(size: cint): pointer {.
importc: "malloc", header: "<stdlib.h>", deprecated: "csize is now uint".}
proc c_free*(p: pointer) {.
importc: "free", header: "<stdlib.h>".}
proc c_realloc*(p: pointer, newsize: csize): pointer {.
importc: "realloc", header: "<stdlib.h>".}
proc c_realloc*(p: pointer, newsize: cint): pointer {.
importc: "realloc", header: "<stdlib.h>", deprecated: "csize is now uint".}

proc c_fwrite*(buf: pointer, size, n: csize, f: CFilePtr): cint {.
importc: "fwrite", header: "<stdio.h>".}
proc c_fwrite*(buf: pointer, size, n: cint, f: CFilePtr): cint {.
importc: "fwrite", header: "<stdio.h>", deprecated: "csize is now uint".}

proc rawWrite*(f: CFilePtr, s: cstring) {.compilerproc, nonReloadable, inline.} =
# we cannot throw an exception here!
discard c_fwrite(s, 1, s.len, f)
discard c_fwrite(s, 1, csize s.len, f)

{.pop.}
8 changes: 8 additions & 0 deletions lib/system/io.nim
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,14 @@ proc c_feof(f: File): cint {.
when not declared(c_fwrite):
proc c_fwrite(buf: pointer, size, n: csize, f: File): cint {.
importc: "fwrite", header: "<stdio.h>".}
proc c_fwrite(buf: pointer, size, n: cint, f: File): cint {.
importc: "fwrite", header: "<stdio.h>", deprecated: "csize is now uint".}

# C routine that is used here:
proc c_fread(buf: pointer, size, n: csize, f: File): csize {.
importc: "fread", header: "<stdio.h>", tags: [ReadIOEffect].}
proc c_fread(buf: pointer, size, n: cint, f: File): int {.
importc: "fread", header: "<stdio.h>", tags: [ReadIOEffect], deprecated: "csize is now uint".}
when defined(windows):
when not defined(amd64):
proc c_fseek(f: File, offset: int64, whence: cint): cint {.
Expand All @@ -109,6 +113,8 @@ proc c_ferror(f: File): cint {.
importc: "ferror", header: "<stdio.h>", tags: [].}
proc c_setvbuf(f: File, buf: pointer, mode: cint, size: csize): cint {.
importc: "setvbuf", header: "<stdio.h>", tags: [].}
proc c_setvbuf(f: File, buf: pointer, mode: cint, size: cint): cint {.
importc: "setvbuf", header: "<stdio.h>", tags: [], deprecated: "csize is now uint".}

proc c_fprintf(f: File, frmt: cstring): cint {.
importc: "fprintf", header: "<stdio.h>", varargs, discardable.}
Expand Down Expand Up @@ -292,6 +298,8 @@ proc readLine*(f: File, line: var TaintedString): bool {.tags: [ReadIOEffect],
## ``false`` is returned `line` contains no new data.
proc c_memchr(s: pointer, c: cint, n: csize): pointer {.
importc: "memchr", header: "<string.h>".}
proc c_memchr(s: pointer, c: cint, n: cint): pointer {.
importc: "memchr", header: "<string.h>", deprecated: "csize is now uint".}

var pos = 0

Expand Down
6 changes: 3 additions & 3 deletions lib/system/memory.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ when useLibC:

proc nimCopyMem*(dest, source: pointer, size: Natural) {.nonReloadable, compilerproc, inline.} =
when useLibC:
c_memcpy(dest, source, size)
c_memcpy(dest, source, csize size)
else:
let d = cast[ptr UncheckedArray[byte]](dest)
let s = cast[ptr UncheckedArray[byte]](source)
Expand All @@ -21,7 +21,7 @@ proc nimCopyMem*(dest, source: pointer, size: Natural) {.nonReloadable, compiler

proc nimSetMem*(a: pointer, v: cint, size: Natural) {.nonReloadable, inline.} =
when useLibC:
c_memset(a, v, size)
c_memset(a, v, csize size)
else:
let a = cast[ptr UncheckedArray[byte]](a)
var i = 0
Expand All @@ -35,7 +35,7 @@ proc nimZeroMem*(p: pointer, size: Natural) {.compilerproc, nonReloadable, inlin

proc nimCmpMem*(a, b: pointer, size: Natural): cint {.compilerproc, nonReloadable, inline.} =
when useLibC:
c_memcmp(a, b, size)
c_memcmp(a, b, csize size)
else:
let a = cast[ptr UncheckedArray[byte]](a)
let b = cast[ptr UncheckedArray[byte]](b)
Expand Down
6 changes: 3 additions & 3 deletions lib/system/osalloc.nim
Original file line number Diff line number Diff line change
Expand Up @@ -221,18 +221,18 @@ elif defined(posix):
proc munmap(adr: pointer, len: csize): cint {.header: "<sys/mman.h>".}

proc osAllocPages(size: int): pointer {.inline.} =
result = mmap(nil, size, PROT_READ or PROT_WRITE,
result = mmap(nil, csize size, PROT_READ or PROT_WRITE,
MAP_PRIVATE or MAP_ANONYMOUS, -1, 0)
if result == nil or result == cast[pointer](-1):
raiseOutOfMem()

proc osTryAllocPages(size: int): pointer {.inline.} =
result = mmap(nil, size, PROT_READ or PROT_WRITE,
result = mmap(nil, csize size, PROT_READ or PROT_WRITE,
MAP_PRIVATE or MAP_ANONYMOUS, -1, 0)
if result == cast[pointer](-1): result = nil

proc osDeallocPages(p: pointer, size: int) {.inline.} =
when reallyOsDealloc: discard munmap(p, size)
when reallyOsDealloc: discard munmap(p, csize size)

elif defined(windows):
const
Expand Down
4 changes: 2 additions & 2 deletions lib/wrappers/openssl.nim
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ proc md5_File*(file: string): string {.raises: [IOError,Exception].} =

discard md5_Init(ctx)
while(let bytes = f.readChars(buf, 0, sz); bytes > 0):
discard md5_Update(ctx, buf[0].addr, bytes)
discard md5_Update(ctx, buf[0].addr, csize bytes)

discard md5_Final(buf[0].addr, ctx)
f.close
Expand All @@ -731,7 +731,7 @@ proc md5_Str*(str: string): string =
var i = 0
while i < str.len:
let L = min(str.len - i, 512)
discard md5_Update(ctx, input[i].addr, L)
discard md5_Update(ctx, input[i].addr, csize L)
i += L

discard md5_Final(addr res, ctx)
Expand Down
2 changes: 1 addition & 1 deletion tests/manyloc/keineschweine/lib/estreams.nim
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ proc newBuffer*(pkt: PPacket): PBuffer =
copyMem(addr result.data[0], pkt.data, pkt.dataLength)
proc toPacket*(buffer: PBuffer; flags: TPacketFlag): PPacket =
buffer.data.setLen buffer.pos
result = createPacket(cstring(buffer.data), buffer.pos, flags)
result = createPacket(cstring(buffer.data), csize buffer.pos, flags)

proc isDirty*(buffer: PBuffer): bool {.inline.} =
result = (buffer.pos != 0)
Expand Down

0 comments on commit 328dba1

Please sign in to comment.