@@ -115,17 +115,18 @@ when defined(boehmgc):
115115 proc realloc (p: pointer , newSize: Natural ): pointer =
116116 result = boehmRealloc (p, newSize)
117117 if result == nil : raiseOutOfMem ()
118- proc dealloc (p: pointer ) = boehmDealloc (p)
119-
120- proc allocShared (size: Natural ): pointer =
121- result = boehmAlloc (size)
122- if result == nil : raiseOutOfMem ()
123- proc allocShared0 (size: Natural ): pointer =
124- result = allocShared (size)
125- proc reallocShared (p: pointer , newSize: Natural ): pointer =
118+ proc realloc0 (p: pointer , oldSize, newSize: Natural ): pointer =
126119 result = boehmRealloc (p, newSize)
127120 if result == nil : raiseOutOfMem ()
128- proc deallocShared (p: pointer ) = boehmDealloc (p)
121+ if newsize > oldsize:
122+ zeroMem (cast [pointer ](cast [int ](result ) + oldsize), newsize - oldsize)
123+ proc dealloc (p: pointer ) = boehmDealloc (p)
124+
125+ proc allocShared (size: Natural ): pointer = alloc (size)
126+ proc allocShared0 (size: Natural ): pointer = alloc (size)
127+ proc reallocShared (p: pointer , newSize: Natural ): pointer = realloc (p, newSize)
128+ proc reallocShared0 (p: pointer , oldSize, newSize: Natural ): pointer = realloc0 (p, newSize, oldSize)
129+ proc deallocShared (p: pointer ) = dealloc (p)
129130
130131 when hasThreadSupport:
131132 proc getFreeSharedMem (): int =
@@ -274,6 +275,9 @@ elif defined(gogc):
274275 proc realloc (p: pointer , newsize: Natural ): pointer =
275276 doAssert false , " not implemented"
276277
278+ proc realloc0 (p: pointer , oldsize, newsize: Natural ): pointer =
279+ doAssert false , " not implemented"
280+
277281 proc dealloc (p: pointer ) =
278282 discard
279283
@@ -286,6 +290,9 @@ elif defined(gogc):
286290 proc reallocShared (p: pointer , newsize: Natural ): pointer =
287291 result = realloc (p, newsize)
288292
293+ proc reallocShared0 (p: pointer , oldsize, newsize: Natural ): pointer =
294+ result = realloc0 (p, oldsize, newsize)
295+
289296 proc deallocShared (p: pointer ) = dealloc (p)
290297
291298 when hasThreadSupport:
@@ -356,39 +363,21 @@ elif defined(gogc):
356363
357364elif (defined (nogc) or defined (gcDestructors)) and defined (useMalloc):
358365
359- # libc realloc() does not zero out memory, so this is handled here. Every
360- # allocated buffer is prepended with the size of the allocation which is used
361- # to deduce which part of the buffer to zero.
362-
363366 when not defined (useNimRtl):
364367
365- proc alloc (size: Natural ): pointer =
366- var x = c_malloc (size + sizeof (size)).csize_t
367- if x == nil : raiseOutOfMem ()
368- cast [ptr int ](x)[] = size
369- result = cast [pointer ](cast [int ](x) + sizeof (size))
370-
371- proc alloc0 (size: Natural ): pointer =
372- result = alloc (size)
373- zeroMem (result , size)
374-
375- proc realloc (p: pointer , newsize: Natural ): pointer =
376- var x = cast [pointer ](cast [int ](p) - sizeof (newsize))
377- let oldsize = cast [ptr int ](x)[]
378- x = c_realloc (x, (newsize + sizeof (newsize)).csize_t )
379- if x == nil : raiseOutOfMem ()
380- cast [ptr int ](x)[] = newsize
381- result = cast [pointer ](cast [int ](x) + sizeof (newsize))
368+ proc alloc (size: Natural ): pointer = c_malloc (size.csize_t )
369+ proc alloc0 (size: Natural ): pointer = c_calloc (size.csize_t , 1 )
370+ proc realloc (p: pointer , newsize: Natural ): pointer = c_realloc (p, newSize.csize_t )
371+ proc realloc0 (p: pointer , oldsize, newsize: Natural ): pointer =
372+ result = realloc (p, newsize.csize_t )
382373 if newsize > oldsize:
383374 zeroMem (cast [pointer ](cast [int ](result ) + oldsize), newsize - oldsize)
375+ proc dealloc (p: pointer ) = c_free (p)
384376
385- proc dealloc (p: pointer ) = c_free (cast [pointer ](cast [int ](p) - sizeof (int )))
386-
387- # Shared allocators map to the regular ones
388-
389- proc allocShared (size: Natural ): pointer = alloc (size.csize_t )
377+ proc allocShared (size: Natural ): pointer = alloc (size)
390378 proc allocShared0 (size: Natural ): pointer = alloc0 (size)
391- proc reallocShared (p: pointer , newsize: Natural ): pointer = realloc (p, newsize.csize_t )
379+ proc reallocShared (p: pointer , newsize: Natural ): pointer = realloc (p, newsize)
380+ proc reallocShared0 (p: pointer , oldsize, newsize: Natural ): pointer = realloc0 (p, oldsize, newsize)
392381 proc deallocShared (p: pointer ) = dealloc (p)
393382
394383 proc GC_disable () = discard
0 commit comments