commit 074798ed626bafae8efcea1413bf3f9571338d70 from: Evan Burkey date: Sun Jul 7 09:17:47 2024 UTC move macos to own folder commit - a99d1d3784eebf2400653df97968a5584f1e20d8 commit + 074798ed626bafae8efcea1413bf3f9571338d70 blob - 93cec31601524a80bbd3daeb1c92b6551f3cad53 blob + a8dc82399ae04425657d6b38ea7d34122bda06b6 --- CMakeLists.txt +++ CMakeLists.txt @@ -20,7 +20,7 @@ set(SOURCES ) if ((${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")) - add_library(flint ${SOURCES} src/macos.c) + add_library(flint ${SOURCES} src/macos/macos.c) else() add_library(flint ${SOURCES}) endif() blob - d0cfea920978a4bc0a5254e3bcb8d94bbd0dbc88 blob + f907d6e7a4c6e2704272a6de3579879d28511185 --- src/crypto.c +++ src/crypto.c @@ -20,7 +20,7 @@ static const char b64_table[] = { typedef struct { unsigned char *ptr; - int count; + size_t count; } b64_buf; static int new_b64_buf(b64_buf *b) { @@ -204,7 +204,7 @@ unsigned char *b64_decode(const char *s, size_t sz, si unsigned char *hex_decode(const char *orig, size_t *sz) { size_t buf_sz = strlen(orig) + 1; - char *sptr = orig; + const char *sptr = orig; if (strncmp(orig, "0x", 2) == 0) { buf_sz -= 2; sptr += 2; blob - 3bbcf1c246a5b5475ef1fb51e62831f676c6dcf6 (mode 644) blob + /dev/null --- src/macos.c +++ /dev/null @@ -1,82 +0,0 @@ -#include -#include -#include -#include -#include - -#include "lfmacos.h" - -#define NS_TO_SECONDS 1000000000.0 -#define NEW_PROCESS_SENTINEL (-1.0) - -ProcessData *new_ProcessData() { - ProcessData *pd = malloc(sizeof(ProcessData)); - pd->last_total_consumed = NEW_PROCESS_SENTINEL; - return pd; -} - -int update_process(pid_t pid, ProcessData *proc) { - struct proc_taskinfo taskinfo; - const int r = proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &taskinfo, PROC_PIDTASKINFO_SIZE); - if (r != PROC_PIDTASKINFO_SIZE) { - return 1; - } - - mach_timebase_info_data_t info; - mach_timebase_info(&info); - const double ns_per_tick = (double)info.numer / (double)info.denom; - - time(&(proc->timestamp)); - proc->total_kernel_time = (taskinfo.pti_total_system * ns_per_tick) / NS_TO_SECONDS; - proc->total_user_time = (taskinfo.pti_total_user * ns_per_tick) / NS_TO_SECONDS; - proc->virtual_memory = taskinfo.pti_virtual_size; - proc->resident_memory = taskinfo.pti_resident_size; - - if (proc->last_total_consumed != NEW_PROCESS_SENTINEL) { - time_t t = proc->timestamp - proc->last_timestamp; - proc->percent_cpu = 100.0 * (proc->total_user_time + proc->total_kernel_time - proc->last_total_consumed) / t; - } else { - proc->percent_cpu = 0.0; - } - - proc->last_timestamp = proc->timestamp; - proc->last_total_consumed = proc->total_kernel_time + proc->total_user_time; - - return 0; -} - -/* reallocarray is reimplemented here for macOS because Apple doesn't expose - * their implementation. This is taken straight from the OpenBSD source as - * shown in the below copyright notice - */ - -/* $OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook Exp $ */ -/* - * Copyright (c) 2008 Otto Moerbeek - * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ - -/* OPENBSD ORIGINAL: lib/libc/stdlib/reallocarray.c */ - -#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) - -void *reallocarray(void *optr, size_t nmemb, size_t size) -{ - if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && - nmemb > 0 && SIZE_MAX / nmemb < size) { - errno = ENOMEM; - return NULL; - } - return realloc(optr, size * nmemb); -} blob - /dev/null blob + 3bbcf1c246a5b5475ef1fb51e62831f676c6dcf6 (mode 644) --- /dev/null +++ src/macos/macos.c @@ -0,0 +1,82 @@ +#include +#include +#include +#include +#include + +#include "lfmacos.h" + +#define NS_TO_SECONDS 1000000000.0 +#define NEW_PROCESS_SENTINEL (-1.0) + +ProcessData *new_ProcessData() { + ProcessData *pd = malloc(sizeof(ProcessData)); + pd->last_total_consumed = NEW_PROCESS_SENTINEL; + return pd; +} + +int update_process(pid_t pid, ProcessData *proc) { + struct proc_taskinfo taskinfo; + const int r = proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &taskinfo, PROC_PIDTASKINFO_SIZE); + if (r != PROC_PIDTASKINFO_SIZE) { + return 1; + } + + mach_timebase_info_data_t info; + mach_timebase_info(&info); + const double ns_per_tick = (double)info.numer / (double)info.denom; + + time(&(proc->timestamp)); + proc->total_kernel_time = (taskinfo.pti_total_system * ns_per_tick) / NS_TO_SECONDS; + proc->total_user_time = (taskinfo.pti_total_user * ns_per_tick) / NS_TO_SECONDS; + proc->virtual_memory = taskinfo.pti_virtual_size; + proc->resident_memory = taskinfo.pti_resident_size; + + if (proc->last_total_consumed != NEW_PROCESS_SENTINEL) { + time_t t = proc->timestamp - proc->last_timestamp; + proc->percent_cpu = 100.0 * (proc->total_user_time + proc->total_kernel_time - proc->last_total_consumed) / t; + } else { + proc->percent_cpu = 0.0; + } + + proc->last_timestamp = proc->timestamp; + proc->last_total_consumed = proc->total_kernel_time + proc->total_user_time; + + return 0; +} + +/* reallocarray is reimplemented here for macOS because Apple doesn't expose + * their implementation. This is taken straight from the OpenBSD source as + * shown in the below copyright notice + */ + +/* $OpenBSD: reallocarray.c,v 1.2 2014/12/08 03:45:00 bcook Exp $ */ +/* + * Copyright (c) 2008 Otto Moerbeek + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* OPENBSD ORIGINAL: lib/libc/stdlib/reallocarray.c */ + +#define MUL_NO_OVERFLOW ((size_t)1 << (sizeof(size_t) * 4)) + +void *reallocarray(void *optr, size_t nmemb, size_t size) +{ + if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && + nmemb > 0 && SIZE_MAX / nmemb < size) { + errno = ENOMEM; + return NULL; + } + return realloc(optr, size * nmemb); +} blob - eb1520fa7b9d97b1af7fb883c1ddd5878b624d77 blob + 5fa7f48c7a3818d6b9211a70803e00e0572e2362 --- src/parsing.c +++ src/parsing.c @@ -25,4 +25,5 @@ int simple_english_scoring(const char *s) { score += ses_score_sw(*c); } return score; -} \ No newline at end of file +} +