zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit cfcf02489d32c2d0b71ce00b66033b790f7e1135 (tree)
parent d8ab301aa81758a7918de446271201c044e7835f
Author: LemonBoy <thatlemon@gmail.com>
Date:   Sat,  4 May 2019 16:13:02 +0200

std: Implement on-demand TLS allocation

Diffstat:
Mstd/os/linux/tls.zig | 17+++++++++++++----
1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/std/os/linux/tls.zig b/std/os/linux/tls.zig @@ -1,6 +1,6 @@ const std = @import("std"); const mem = std.mem; -const posix = std.posix; +const posix = std.os.posix; const elf = std.elf; const builtin = @import("builtin"); const assert = std.debug.assert; @@ -234,9 +234,18 @@ pub fn copyTLS(addr: usize) usize { return addr + tls_img.tcb_offset + tls_tp_offset; } -var main_thread_tls_buffer: [64]u8 align(32) = undefined; +var main_thread_tls_buffer: [256]u8 align(32) = undefined; pub fn allocateTLS(size: usize) usize { - assert(size < main_thread_tls_buffer.len); - return @ptrToInt(&main_thread_tls_buffer); + // Small TLS allocation, use our local buffer + if (size < main_thread_tls_buffer.len) { + return @ptrToInt(&main_thread_tls_buffer); + } + + const addr = posix.mmap(null, size, posix.PROT_READ | posix.PROT_WRITE, + posix.MAP_PRIVATE | posix.MAP_ANONYMOUS, -1, 0); + + if (posix.getErrno(addr) != 0) @panic("allocateTLS failed to allocate memory"); + + return addr; }