Skip to content

Commit

Permalink
Fixes in realloc
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Aug 12, 2020
1 parent 14b96fd commit 06a9075
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
10 changes: 6 additions & 4 deletions lib/allocators/arena.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ local function align_forward(addr: usize, align: usize): usize <inline>
return (addr + (align-1)) & ~(align-1)
end

local function memmove(dest: pointer, src: pointer, n: csize): pointer <cimport'memmove',cinclude'<string.h>',nodecl> end
local function memcpy(dest: pointer, src: pointer, n: csize): pointer <cimport,cinclude'<string.h>',nodecl> end

## local make_arena_allocator = generalize(function(Size, Align, error_on_failure)
## Align = Align or 8
Expand Down Expand Up @@ -100,14 +100,16 @@ local function memmove(dest: pointer, src: pointer, n: csize): pointer <cimport'
self.curr_offset = offset
end
return p
else -- move to a new allocation
elseif newsize > oldsize then -- growing, move to a new allocation
if unlikely(newsize == 0) then return nilptr end
local newp: pointer = self:alloc(newsize)
if likely(newp ~= nilptr and p ~= nilptr and oldsize ~= 0) then
-- move the mem to the new location, as the memory may overlap
memmove(newp, p, oldsize)
-- copy the mem to the new location
memcpy(newp, p, oldsize)
end
return newp
else -- shrinking, can return the same pointer
return p
end
end

Expand Down
4 changes: 2 additions & 2 deletions lib/allocators/gc.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ require 'allocators.general'

local jmp_buf <cimport,cinclude'<setjmp.h>',nodecl> = @record{dummy: usize}
local function setjmp(env: jmp_buf) <cimport,cinclude'<setjmp.h>',nodecl> end
local function memcpy(dest: pointer, src: pointer, n: csize): pointer <cimport'memcpy',cinclude'<string.h>',nodecl> end
local function memset(s: pointer, c: cint, n: csize): pointer <cimport'memset',cinclude'<string.h>',nodecl> end
local function memcpy(dest: pointer, src: pointer, n: csize): pointer <cimport,cinclude'<string.h>',nodecl> end
local function memset(s: pointer, c: cint, n: csize): pointer <cimport,cinclude'<string.h>',nodecl> end

local allocator: auto = general_allocator

Expand Down
8 changes: 4 additions & 4 deletions lib/allocators/general.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
-- It uses the C malloc/calloc/realloc/free functions.

local function memset(s: pointer, c: cint, n: csize): pointer <cimport'memset',cinclude'<string.h>',nodecl> end
local function malloc(size: csize): pointer <cimport'malloc',cinclude'<stdlib.h>',nodecl> end
local function calloc(nmemb: csize, size: csize): pointer <cimport'calloc',cinclude'<stdlib.h>',nodecl> end
local function realloc(ptr: pointer, size: csize): pointer <cimport'realloc',cinclude'<stdlib.h>',nodecl> end
local function free(ptr: pointer): void <cimport'free',cinclude'<stdlib.h>',nodecl> end
local function malloc(size: csize): pointer <cimport,cinclude'<stdlib.h>',nodecl> end
local function calloc(nmemb: csize, size: csize): pointer <cimport,cinclude'<stdlib.h>',nodecl> end
local function realloc(ptr: pointer, size: csize): pointer <cimport,cinclude'<stdlib.h>',nodecl> end
local function free(ptr: pointer): void <cimport,cinclude'<stdlib.h>',nodecl> end

global GeneralAllocator = @record{}

Expand Down
10 changes: 7 additions & 3 deletions lib/allocators/interface.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
require 'span'

## function implement_allocator_interface(allocator)
local function memcpy(dest: pointer, src: pointer, n: csize): pointer <cimport'memcpy',cinclude'<string.h>',nodecl> end
local function memset(s: pointer, c: cint, n: csize): pointer <cimport'memset',cinclude'<string.h>',nodecl> end
local function memcpy(dest: pointer, src: pointer, n: csize): pointer <cimport,cinclude'<string.h>',nodecl> end
local function memset(s: pointer, c: cint, n: csize): pointer <cimport,cinclude'<string.h>',nodecl> end

local is_span = #[concept(function(x) return x.type.is_span end)]#
local allocator = #[allocator]#
Expand All @@ -24,7 +24,11 @@ require 'span'
else
local newp: pointer = self:alloc(newsize)
if likely(newp ~= nilptr and p ~= nilptr and oldsize ~= 0) then
memcpy(newp, p, oldsize)
local copysize = oldsize
if newsize < oldsize then
copysize = newsize
end
memcpy(newp, p, copysize)
end
return newp
end
Expand Down
10 changes: 5 additions & 5 deletions lib/memory.nelua
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

--------------------------------------------------------------------------------
-- C imports
local function memcpy(dest: pointer, src: pointer, n: csize): pointer <cimport'memcpy',cinclude'<string.h>',nodecl> end
local function memmove(dest: pointer, src: pointer, n: csize): pointer <cimport'memmove',cinclude'<string.h>',nodecl> end
local function memset(s: pointer, c: cint, n: csize): pointer <cimport'memset',cinclude'<string.h>',nodecl> end
local function memcmp(s1: pointer, s2: pointer, n: csize): cint <cimport'memcmp',cinclude'<string.h>',nodecl> end
local function memchr(s: pointer, c: cint, n: csize): pointer <cimport'memchr',cinclude'<string.h>',nodecl> end
local function memcpy(dest: pointer, src: pointer, n: csize): pointer <cimport,cinclude'<string.h>',nodecl> end
local function memmove(dest: pointer, src: pointer, n: csize): pointer <cimport,cinclude'<string.h>',nodecl> end
local function memset(s: pointer, c: cint, n: csize): pointer <cimport,cinclude'<string.h>',nodecl> end
local function memcmp(s1: pointer, s2: pointer, n: csize): cint <cimport,cinclude'<string.h>',nodecl> end
local function memchr(s: pointer, c: cint, n: csize): pointer <cimport,cinclude'<string.h>',nodecl> end

--------------------------------------------------------------------------------
-- pointer functions
Expand Down

0 comments on commit 06a9075

Please sign in to comment.