From ed4872abc6668a9af1bd9701ec3144fdd2bebd7e Mon Sep 17 00:00:00 2001 From: Nathan Ringo Date: Wed, 20 Nov 2024 15:09:54 -0600 Subject: [PATCH] fix(stats): fix the layout of struct timespec Previously, the layout was only correct on 64-bit platforms. This is based on include/alltypes.h.in in musl. --- lua/lazy/stats.lua | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/lua/lazy/stats.lua b/lua/lazy/stats.lua index 015a2be..2029688 100644 --- a/lua/lazy/stats.lua +++ b/lua/lazy/stats.lua @@ -34,12 +34,29 @@ end function M.cputime() if M.C == nil then pcall(function() + local pad = "" + -- If we're a 32-bit platform, we need to pad by 4 bytes. + if ffi.abi("32bit") then + pad = "int _tv_pad;" + end + + local pad_before_nsec = "" + local pad_after_nsec = "" + -- Where we place the padding depends on the endianness. + if ffi.abi("le") then + pad_after_nsec = pad + else + pad_before_nsec = pad + end + ffi.cdef([[ - typedef long time_t; + typedef int64_t time_t; typedef int clockid_t; typedef struct timespec { time_t tv_sec; /* seconds */ + ]] .. pad_before_nsec .. [[ long tv_nsec; /* nanoseconds */ + ]] .. pad_after_nsec .. [[ } nanotime; int clock_gettime(clockid_t clk_id, struct timespec *tp); ]])