-
-
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.
More usage of concepts on lib sources
- Loading branch information
Showing
12 changed files
with
146 additions
and
190 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,77 @@ | ||
##[[ | ||
-------------------------------------------------------------------------------- | ||
-- 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 is_span = #[concept(function(x) return x.type:is_span() end)]# | ||
local allocator = #[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.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 | ||
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 | ||
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 | ||
function allocator.spanrealloc(s: is_span, size: usize) | ||
local T: type = #[s.type.subtype]# | ||
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.spanrealloc: allocation fail') | ||
s.size = size | ||
end | ||
return s | ||
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.spanrealloc: allocation fail') | ||
s.size = size | ||
function allocator.spanrealloc0(s: is_span, size: usize) | ||
local T: type = #[s.type.subtype]# | ||
if unlikely(size == 0) then | ||
allocator.dealloc(s.data) | ||
s.data = nilptr | ||
s.size = 0 | ||
else | ||
s.data = (@T*)(allocator.realloc0(s.data, size * #T, s.size * #T)) | ||
check(s.data, 'allocator.spanrealloc0: allocation fail') | ||
s.size = size | ||
end | ||
return s | ||
end | ||
return s | ||
end | ||
|
||
function allocator.spanrealloc0(s: auto, size: usize) | ||
local T: type = #[check_span_subtype(s)]# | ||
if unlikely(size == 0) then | ||
function allocator.spandealloc(s: is_span) | ||
allocator.dealloc(s.data) | ||
s.data = nilptr | ||
s.size = 0 | ||
else | ||
s.data = (@T*)(allocator.realloc0(s.data, size * #T, s.size * #T)) | ||
check(s.data, 'allocator.spanrealloc0: allocation fail') | ||
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') | ||
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 | ||
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 | ||
|
||
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
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
Oops, something went wrong.