Skip to content
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

Merged
merged 1 commit into from Oct 17, 2017
Merged

Add c allocator #542

merged 1 commit into from Oct 17, 2017

Conversation

tiehuis
Copy link
Member

@tiehuis tiehuis commented Oct 17, 2017

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).

std/mem.zig Outdated
}

fn cFree(self: &Allocator, old_mem: []u8) {
const old_ptr = @ptrCast(&c_void, &old_mem[0]);
Copy link
Member

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]

Copy link
Member Author

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.

Copy link
Member

@andrewrk andrewrk Oct 17, 2017

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| {
Copy link
Member

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.

Copy link
Member Author

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"));
Copy link
Member

@andrewrk andrewrk Oct 17, 2017

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).

@andrewrk
Copy link
Member

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?

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: ?&timespec) -> 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;
Copy link
Member Author

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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect

@andrewrk andrewrk merged commit 09c0cf2 into master Oct 17, 2017
@andrewrk andrewrk deleted the c_allocator branch October 17, 2017 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants