-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
455 additions
and
298 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
##[[ | ||
strict = true | ||
unitname = 'nelua' | ||
-------------------------------------------------------------------------------- | ||
-- compile time checks utilities | ||
local function check_span_subtype(v) | ||
staticassert(v.type:is_span(), | ||
"in argument '%s': expected 'span(T)' but got type '%s'", | ||
v.name, v.type) | ||
return v.type.subtype | ||
end | ||
local function check_type_match(a, b) | ||
staticassert(a.type == b.type, | ||
"argument '%s' type '%s' differs from argument '%s' type '%s", | ||
a.name, b.name, a.type, b.type) | ||
end | ||
local function check_span_subtype_match(s, x) | ||
local stype = check_span_subtype(s) | ||
staticassert(stype == x.type, | ||
"in argument '%s': type '%s' differs from argument '%s' type '%s", | ||
s.name, x.name, stype, x.type) | ||
end | ||
]] | ||
|
||
## function implement_allocator_interface(allocator) | ||
|
||
local allocator = #[allocator]# | ||
|
||
function allocator.spanalloc(T: type, size: usize) | ||
local s: span(T) | ||
if likely(size > 0_u) then | ||
s.data = (@T*)(allocator.alloc(size * #T)) | ||
check(s.data, 'allocator.spanalloc: allocation fail') | ||
s.size = size | ||
end | ||
return s | ||
end | ||
|
||
function allocator.spanalloc0(T: type, size: usize) | ||
local s: span(T) | ||
if likely(size > 0_u) then | ||
s.data = (@T*)(allocator.alloc0(size * #T)) | ||
check(s.data, 'allocator.spanalloc0: allocation fail') | ||
s.size = size | ||
end | ||
return s | ||
end | ||
|
||
function allocator.spanrealloc(s: auto, size: usize) | ||
local T: type = #[check_span_subtype(s)]# | ||
if unlikely(size == 0) then | ||
allocator.dealloc(s.data) | ||
s.data = nilptr | ||
s.size = 0 | ||
else | ||
s.data = (@T*)(allocator.realloc(s.data, size * #T)) | ||
check(s.data, 'allocator.spanrealloc0: allocation fail') | ||
s.size = size | ||
end | ||
return s | ||
end | ||
|
||
function allocator.spanrealloc0(s: auto, size: usize) | ||
local T: type = #[check_span_subtype(s)]# | ||
if unlikely(size == 0) then | ||
allocator.dealloc(s.data) | ||
s.data = nilptr | ||
s.size = 0 | ||
else | ||
s.data = (@T*)(allocator.realloc(s.data, size * #T)) | ||
check(s.data, 'allocator.spanrealloc0: allocation fail') | ||
if likely(size > s.size) then | ||
memset(&s[s.size], 0, (size - s.size) * #T) | ||
end | ||
s.size = size | ||
end | ||
return s | ||
end | ||
|
||
function allocator.spandealloc(s: auto) | ||
## check_span_subtype(s) | ||
allocator.dealloc(s.data) | ||
end | ||
|
||
function allocator.new(T: type, size: auto) | ||
## if not size.type:is_nil() then | ||
## staticassert(size.type:is_integral(), 'allocator.new: size must be an integral type') | ||
## if not size.type:is_unsigned() then | ||
check(size > 0, 'allocator.new: size must be greater than 0') | ||
## end | ||
return allocator.spanalloc0(T, (@usize)(size)) | ||
## else | ||
return (@T*)(allocator.alloc0(#T)) | ||
## end | ||
end | ||
|
||
function allocator.delete(s: auto) | ||
## staticassert(s.type:is_pointer() or s.type:is_span(), "allocator.delete: invalid type '%s'", s.type) | ||
## if s.type:is_span() then | ||
allocator.spandealloc(s) | ||
## else -- pointer | ||
allocator.dealloc(s) | ||
## end | ||
end | ||
|
||
## end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
## strict = true | ||
## unitname = 'nelua' | ||
|
||
require 'gc' | ||
require 'allocators.allocator_interface' | ||
|
||
global gc_allocator = @record{} | ||
|
||
function gc_allocator.alloc(size: usize): pointer | ||
check(size > 0_u, 'gc_allocator.alloc: size cannot be zero') | ||
local p: pointer = gc:alloc(size) | ||
check(p, 'gc_allocator.alloc: allocation fail') | ||
return p | ||
end | ||
|
||
function gc_allocator.alloc0(size: usize): pointer | ||
check(size > 0_u, 'gc_allocator.alloc0: size must be greater than 0') | ||
local p: pointer = gc:alloc0(size) | ||
check(p, 'gc_allocator.alloc0: allocation fail') | ||
return p | ||
end | ||
|
||
function gc_allocator.realloc(p: pointer, size: usize): pointer | ||
check(size > 0_u, 'gc_allocator.realloc: size must be greater than 0') | ||
p = gc:realloc(p, size) | ||
check(size == 0 or p, 'gc_allocator.realloc: allocation fail') | ||
return p | ||
end | ||
|
||
function gc_allocator.realloc0(p: pointer, newsize: usize, oldsize: usize): pointer | ||
check(newsize > 0_u, 'gc_allocator.realloc0: size must be greater than 0') | ||
p = gc:realloc0(p, newsize, oldsize) | ||
check(newsize == 0_u or p, 'gc_allocator.realloc0: allocation fail') | ||
return p | ||
end | ||
|
||
function gc_allocator.dealloc(p: pointer) | ||
gc:dealloc(p) | ||
end | ||
|
||
## implement_allocator_interface(gc_allocator) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
## strict = true | ||
## unitname = 'nelua' | ||
|
||
require 'allocators.allocator_interface' | ||
|
||
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 | ||
|
||
global generic_allocator = @record{} | ||
|
||
function generic_allocator.alloc(size: usize): pointer | ||
check(size > 0_u, 'generic_allocator.alloc: size cannot be zero') | ||
local p: pointer = malloc(size) | ||
check(p, 'generic_allocator.alloc: allocation fail') | ||
return p | ||
end | ||
|
||
function generic_allocator.alloc0(size: usize): pointer | ||
check(size > 0_u, 'generic_allocator.alloc0: size must be greater than 0') | ||
local p: pointer = calloc(size, 1) | ||
check(p, 'generic_allocator.alloc0: allocation fail') | ||
return p | ||
end | ||
|
||
function generic_allocator.realloc(p: pointer, size: usize): pointer | ||
check(size > 0_u, 'generic_allocator.realloc: size must be greater than 0') | ||
p = realloc(p, size) | ||
check(size == 0 or p, 'generic_allocator.realloc: allocation fail') | ||
return p | ||
end | ||
|
||
function generic_allocator.realloc0(p: pointer, newsize: usize, oldsize: usize): pointer | ||
check(newsize > 0_u, 'generic_allocator.realloc0: size must be greater than 0') | ||
p = realloc(p, newsize) | ||
check(newsize == 0_u or p, 'generic_allocator.realloc0: allocation fail') | ||
if likely(newsize > oldsize) then | ||
memset(&(@byte[0]*)(p)[oldsize], 0, newsize - oldsize) | ||
end | ||
return p | ||
end | ||
|
||
function generic_allocator.dealloc(p: pointer) | ||
free(p) | ||
end | ||
|
||
## implement_allocator_interface(generic_allocator) |
Oops, something went wrong.