1 1.5 christos /* $NetBSD: lib_strbuf.c,v 1.6 2024/08/18 20:47:13 christos Exp $ */ 2 1.1 kardel 3 1.1 kardel /* 4 1.6 christos * lib_strbuf.c - init_lib() and library string storage 5 1.1 kardel */ 6 1.1 kardel #ifdef HAVE_CONFIG_H 7 1.1 kardel #include <config.h> 8 1.1 kardel #endif 9 1.1 kardel 10 1.6 christos #include <isc/mutex.h> 11 1.1 kardel #include <isc/net.h> 12 1.1 kardel #include <isc/result.h> 13 1.2 christos 14 1.2 christos #include "ntp_fp.h" 15 1.1 kardel #include "ntp_stdlib.h" 16 1.1 kardel #include "lib_strbuf.h" 17 1.1 kardel 18 1.6 christos #define LIB_NUMBUF 10 19 1.2 christos 20 1.1 kardel /* 21 1.1 kardel * Storage declarations 22 1.1 kardel */ 23 1.6 christos static char lib_stringbuf_storage[LIB_NUMBUF][LIB_BUFLENGTH]; 24 1.6 christos static char * lib_stringbuf[LIB_NUMBUF]; 25 1.6 christos int lib_inited; 26 1.6 christos static isc_mutex_t lib_mutex; 27 1.6 christos int ipv4_works; 28 1.6 christos int ipv6_works; 29 1.6 christos int debug; 30 1.1 kardel 31 1.1 kardel /* 32 1.1 kardel * initialization routine. Might be needed if the code is ROMized. 33 1.1 kardel */ 34 1.1 kardel void 35 1.1 kardel init_lib(void) 36 1.1 kardel { 37 1.6 christos u_int u; 38 1.6 christos 39 1.6 christos if (lib_inited) { 40 1.2 christos return; 41 1.6 christos } 42 1.1 kardel ipv4_works = (ISC_R_SUCCESS == isc_net_probeipv4()); 43 1.1 kardel ipv6_works = (ISC_R_SUCCESS == isc_net_probeipv6()); 44 1.2 christos init_systime(); 45 1.6 christos /* 46 1.6 christos * Avoid -Wrestrict warnings by keeping a pointer to each buffer 47 1.6 christos * so the compiler can see copying from one buffer to another is 48 1.6 christos * not violating restrict qualifiers on, e.g. memcpy() args. 49 1.6 christos */ 50 1.6 christos for (u = 0; u < COUNTOF(lib_stringbuf); u++) { 51 1.6 christos lib_stringbuf[u] = lib_stringbuf_storage[u]; 52 1.6 christos } 53 1.6 christos (void)isc_mutex_init(&lib_mutex); 54 1.2 christos lib_inited = TRUE; 55 1.1 kardel } 56 1.6 christos 57 1.6 christos 58 1.6 christos char * 59 1.6 christos lib_getbuf(void) 60 1.6 christos { 61 1.6 christos static int lib_nextbuf; 62 1.6 christos int mybuf; 63 1.6 christos 64 1.6 christos if (!lib_inited) { 65 1.6 christos init_lib(); 66 1.6 christos } 67 1.6 christos isc_mutex_lock(&lib_mutex); 68 1.6 christos mybuf = lib_nextbuf; 69 1.6 christos lib_nextbuf = (1 + mybuf) % COUNTOF(lib_stringbuf); 70 1.6 christos isc_mutex_unlock(&lib_mutex); 71 1.6 christos zero_mem(lib_stringbuf[mybuf], LIB_BUFLENGTH); 72 1.6 christos 73 1.6 christos return lib_stringbuf[mybuf]; 74 1.6 christos } 75