authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-05-04 16:13:02+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2019-05-07 13:09:19+02:00
logcfcf02489d32c2d0b71ce00b66033b790f7e1135
treebb79e70bb5235910c971c770dfd0ab68774634ed
parentd8ab301aa81758a7918de446271201c044e7835f

std: Implement on-demand TLS allocation


1 files changed, 13 insertions(+), 4 deletions(-)

std/os/linux/tls.zig+13-4
...@@ -1,6 +1,6 @@...@@ -1,6 +1,6 @@
1const std = @import("std");1const std = @import("std");
2const mem = std.mem;2const mem = std.mem;
3const posix = std.posix;3const posix = std.os.posix;
4const elf = std.elf;4const elf = std.elf;
5const builtin = @import("builtin");5const builtin = @import("builtin");
6const assert = std.debug.assert;6const assert = std.debug.assert;
...@@ -234,9 +234,18 @@ pub fn copyTLS(addr: usize) usize {...@@ -234,9 +234,18 @@ pub fn copyTLS(addr: usize) usize {
234 return addr + tls_img.tcb_offset + tls_tp_offset;234 return addr + tls_img.tcb_offset + tls_tp_offset;
235}235}
236236
237var main_thread_tls_buffer: [64]u8 align(32) = undefined;237var main_thread_tls_buffer: [256]u8 align(32) = undefined;
238238
239pub fn allocateTLS(size: usize) usize {239pub fn allocateTLS(size: usize) usize {
240 assert(size < main_thread_tls_buffer.len);240 // Small TLS allocation, use our local buffer
241 return @ptrToInt(&main_thread_tls_buffer);241 if (size < main_thread_tls_buffer.len) {
242 return @ptrToInt(&main_thread_tls_buffer);
243 }
244
245 const addr = posix.mmap(null, size, posix.PROT_READ | posix.PROT_WRITE,
246 posix.MAP_PRIVATE | posix.MAP_ANONYMOUS, -1, 0);
247
248 if (posix.getErrno(addr) != 0) @panic("allocateTLS failed to allocate memory");
249
250 return addr;
242}251}