summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--doc/nimc.txt4
-rw-r--r--lib/pure/asyncdispatch.nim4
-rw-r--r--lib/system/mmdisp.nim25
-rw-r--r--lib/windows/winlean.nim6
-rw-r--r--web/news.txt5
5 files changed, 34 insertions, 10 deletions
diff --git a/doc/nimc.txt b/doc/nimc.txt
index e7cb57037..48dbaeb21 100644
--- a/doc/nimc.txt
+++ b/doc/nimc.txt
@@ -243,7 +243,9 @@ Define               Effect
 ``useFork``          Makes ``osproc`` use ``fork`` instead of ``posix_spawn``.
 ``useNimRtl``        Compile and link against ``nimrtl.dll``.
 ``useMalloc``        Makes Nim use C's `malloc`:idx: instead of Nim's
-                     own memory manager. This only works with ``gc:none``.
+                     own memory manager, ableit prefixing each allocation with
+                     its size to support clearing memory on reallocation.
+                     This only works with ``gc:none``.
 ``useRealtimeGC``    Enables support of Nim's GC for *soft* realtime
                      systems. See the documentation of the `gc <gc.html>`_
                      for further information.
diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim
index 64966c6b5..b662c3095 100644
--- a/lib/pure/asyncdispatch.nim
+++ b/lib/pure/asyncdispatch.nim
@@ -374,7 +374,7 @@ proc adjustedTimeout(p: PDispatcherBase, timeout: int): int {.inline.} =
 when defined(windows) or defined(nimdoc):
   import winlean, sets, hashes
   type
-    CompletionKey = Dword
+    CompletionKey = ULONG_PTR
 
     CompletionData* = object
       fd*: AsyncFD # TODO: Rename this.
@@ -439,7 +439,7 @@ when defined(windows) or defined(nimdoc):
       else: at.int32
 
     var lpNumberOfBytesTransferred: Dword
-    var lpCompletionKey: ULONG
+    var lpCompletionKey: ULONG_PTR
     var customOverlapped: PCustomOverlapped
     let res = getQueuedCompletionStatus(p.ioPort,
         addr lpNumberOfBytesTransferred, addr lpCompletionKey,
diff --git a/lib/system/mmdisp.nim b/lib/system/mmdisp.nim
index 85d16d2fe..d7010a1a3 100644
--- a/lib/system/mmdisp.nim
+++ b/lib/system/mmdisp.nim
@@ -389,15 +389,30 @@ elif defined(nogc) and defined(useMalloc):
 
   when not defined(useNimRtl):
     proc alloc(size: Natural): pointer =
-      result = cmalloc(size)
-      if result == nil: raiseOutOfMem()
+      var x = cmalloc(size + sizeof(size))
+      if x == nil: raiseOutOfMem()
+
+      cast[ptr int](x)[] = size
+      result = cast[pointer](cast[int](x) + sizeof(size))
+
     proc alloc0(size: Natural): pointer =
       result = alloc(size)
       zeroMem(result, size)
     proc realloc(p: pointer, newsize: Natural): pointer =
-      result = crealloc(p, newsize)
-      if result == nil: raiseOutOfMem()
-    proc dealloc(p: pointer) = cfree(p)
+      var x = cast[pointer](cast[int](p) - sizeof(newsize))
+      let oldsize = cast[ptr int](x)[]
+
+      x = crealloc(x, newsize + sizeof(newsize))
+
+      if x == nil: raiseOutOfMem()
+
+      cast[ptr int](x)[] = newsize
+      result = cast[pointer](cast[int](x) + sizeof(newsize))
+
+      if newsize > oldsize:
+        zeroMem(cast[pointer](cast[int](result) + oldsize), newsize - oldsize)
+
+    proc dealloc(p: pointer) = cfree(cast[pointer](cast[int](p) - sizeof(int)))
 
     proc allocShared(size: Natural): pointer =
       result = cmalloc(size)
diff --git a/lib/windows/winlean.nim b/lib/windows/winlean.nim
index a08a067fa..de9898dce 100644
--- a/lib/windows/winlean.nim
+++ b/lib/windows/winlean.nim
@@ -36,6 +36,8 @@ type
   DWORD* = int32
   PDWORD* = ptr DWORD
   LPINT* = ptr int32
+  ULONG_PTR* = uint
+  PULONG_PTR* = ptr uint
   HDC* = Handle
   HGLRC* = Handle
 
@@ -759,12 +761,12 @@ const
   ERROR_NETNAME_DELETED* = 64
 
 proc createIoCompletionPort*(FileHandle: Handle, ExistingCompletionPort: Handle,
-                             CompletionKey: DWORD,
+                             CompletionKey: ULONG_PTR,
                              NumberOfConcurrentThreads: DWORD): Handle{.stdcall,
     dynlib: "kernel32", importc: "CreateIoCompletionPort".}
 
 proc getQueuedCompletionStatus*(CompletionPort: Handle,
-    lpNumberOfBytesTransferred: PDWORD, lpCompletionKey: PULONG,
+    lpNumberOfBytesTransferred: PDWORD, lpCompletionKey: PULONG_PTR,
                                 lpOverlapped: ptr POVERLAPPED,
                                 dwMilliseconds: DWORD): WINBOOL{.stdcall,
     dynlib: "kernel32", importc: "GetQueuedCompletionStatus".}
diff --git a/web/news.txt b/web/news.txt
index 0a83503c1..c9561c7a5 100644
--- a/web/news.txt
+++ b/web/news.txt
@@ -32,6 +32,11 @@ Changes affecting backwards compatibility
   raises a ``KeyError`` exception. You can compile with the ``-d:nimJsonGet``
   flag to get a list of usages of ``[]``, as well as to restore the operator's
   previous behaviour.
+- When using ``useMalloc``, an additional header containing the size of the
+  allocation will be allocated, to support zeroing memory on realloc as expected
+  by the language. With this change, ``alloc`` and ``dealloc`` are no longer
+  aliases for ``malloc`` and ``free`` - use ``c_malloc`` and ``c_free`` if
+  you need that.
 
 
 Library Additions