- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add c allocator #542
Add c allocator #542
Conversation
std/mem.zig
Outdated
} | ||
|
||
fn cFree(self: &Allocator, old_mem: []u8) { | ||
const old_ptr = @ptrCast(&c_void, &old_mem[0]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes more sense to use old_mem.ptr
instead of &old_mem[0]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Always forget this exists. Is there a list of member values for each type listed anywhere right now? For example len
on slices, bit_count
on types etc. Don't think I've noticed anything specifically in the documentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm I think that is one of the documentation TODOs.
std/mem.zig
Outdated
|
||
fn cRealloc(self: &Allocator, old_mem: []u8, new_size: usize, alignment: usize) -> %[]u8 { | ||
const old_ptr = @ptrCast(&c_void, &old_mem[0]); | ||
if (c.realloc(old_ptr, usize(new_size))) |mem| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is libc realloc
guaranteed to succeed if new_size <= old_mem.len
? If yes, this code is correct. If no, we should handle failure in the case that new_size <= old_mem.len
by returning the same slice with a new len, to conform to the zig allocator semantics for realloc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
C99 standard seems to state that memory is always allocated and moved even on smaller sizes. Added this logic.
std/mem.zig
Outdated
.freeFn = cFree, | ||
}; | ||
|
||
const c = @cImport(@cInclude("stdlib.h")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think in this case we should @import("c/index.zig")
(see https://github.com/zig-lang/zig/blob/master/std/c/index.zig) and add malloc
, realloc
, and free
. This avoids the overhead of parsing .h files, and as you pointed out, can still work when no .h files are present.
We can let there be linker errors if the programmer fails to link libc, no need for @compileError
. This allows the most flexibility (maybe the programmer is only targeting an object).
You are correct, the lazy evaluation causes the import to never happen if the functions are never called. |
@@ -43,3 +43,7 @@ pub extern "c" fn sigaction(sig: c_int, noalias act: &const Sigaction, noalias o | |||
pub extern "c" fn nanosleep(rqtp: &const timespec, rmtp: ?×pec) -> c_int; | |||
pub extern "c" fn setreuid(ruid: c_uint, euid: c_uint) -> c_int; | |||
pub extern "c" fn setregid(rgid: c_uint, egid: c_uint) -> c_int; | |||
|
|||
pub extern "c" fn malloc(usize) -> ?&c_void; | |||
pub extern "c" fn realloc(&c_void, usize) -> ?&c_void; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Left these as non-null since they compile down to the same thing and don't believe we are interested in the case of passing null through.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
perfect
Did consider adding a better
@compileError
if we weren't linking with libc. Thoughts on that?Also, am I right in thinking that a local
cInclude
will be preferred within the functions as they will be compiled out and the include will not be performed at all? Just considering the platforms which may not have an include directory set up properly (or assuming no c present).