LCOV - code coverage report
Current view: top level - posix - platform_posix.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 27 29 93.1 %
Date: 2026-02-22 12:14:12 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /**
       2             :  * @file platform_posix.c
       3             :  * @brief PeerTalk POSIX Platform Implementation
       4             :  *
       5             :  * Platform abstraction for Linux and macOS systems.
       6             :  */
       7             : 
       8             : #include "pt_internal.h"
       9             : 
      10             : #if defined(PT_PLATFORM_POSIX)
      11             : 
      12             : #include "net_posix.h"
      13             : #include <sys/time.h>
      14             : #include <stdlib.h>
      15             : #include <unistd.h>
      16             : #include <signal.h>
      17             : 
      18             : /* ========================================================================== */
      19             : /* Platform Operations                                                        */
      20             : /* ========================================================================== */
      21             : 
      22         230 : static int posix_init(struct pt_context *ctx) {
      23             :     /* Ignore SIGPIPE to prevent process termination on broken pipe writes */
      24         230 :     signal(SIGPIPE, SIG_IGN);
      25             : 
      26             :     /* Initialize networking layer */
      27         230 :     if (pt_posix_net_init(ctx) < 0) {
      28           0 :         PT_CTX_ERR(ctx, PT_LOG_CAT_GENERAL, "Failed to initialize POSIX networking");
      29           0 :         return -1;
      30             :     }
      31             : 
      32         230 :     PT_CTX_INFO(ctx, PT_LOG_CAT_GENERAL, "POSIX platform initialized");
      33         230 :     return 0;
      34             : }
      35             : 
      36         230 : static void posix_shutdown(struct pt_context *ctx) {
      37             :     /* Shutdown networking layer */
      38         230 :     pt_posix_net_shutdown(ctx);
      39             : 
      40         230 :     PT_CTX_INFO(ctx, PT_LOG_CAT_GENERAL, "POSIX platform shutdown");
      41         230 : }
      42             : 
      43         741 : static int posix_poll(struct pt_context *ctx) {
      44             :     /* Delegate to networking poll (Session 4.1+) */
      45         741 :     return pt_posix_poll(ctx);
      46             : }
      47             : 
      48         889 : static pt_tick_t posix_get_ticks(void) {
      49             :     struct timeval tv;
      50         889 :     gettimeofday(&tv, NULL);
      51             : 
      52             :     /* Return milliseconds - wraps every ~49 days, which is acceptable */
      53         889 :     return (pt_tick_t)((tv.tv_sec * 1000) + (tv.tv_usec / 1000));
      54             : }
      55             : 
      56           1 : static unsigned long posix_get_free_mem(void) {
      57             :     /* POSIX: return effectively unlimited */
      58           1 :     return 1024UL * 1024UL * 1024UL; /* 1GB */
      59             : }
      60             : 
      61           1 : static unsigned long posix_get_max_block(void) {
      62             :     /* POSIX: return effectively unlimited */
      63           1 :     return 1024UL * 1024UL * 1024UL; /* 1GB */
      64             : }
      65             : 
      66         101 : static int posix_poll_fast(struct pt_context *ctx) {
      67             :     /* Delegate to networking fast poll */
      68         101 :     return pt_posix_poll_fast(ctx);
      69             : }
      70             : 
      71             : /* Platform operations structure */
      72             : pt_platform_ops pt_posix_ops = {
      73             :     posix_init,
      74             :     posix_shutdown,
      75             :     posix_poll,
      76             :     posix_poll_fast,
      77             :     posix_get_ticks,
      78             :     posix_get_free_mem,
      79             :     posix_get_max_block,
      80             :     pt_posix_send_udp,  /* Session 4.4 implements UDP messaging */
      81             :     /* Async send pipeline: not needed on POSIX (kernel handles buffering) */
      82             :     NULL,               /* tcp_send_async */
      83             :     NULL,               /* poll_send_completions */
      84             :     NULL,               /* send_slots_available */
      85             :     NULL,               /* pipeline_init */
      86             :     NULL                /* pipeline_cleanup */
      87             : };
      88             : 
      89             : /* ========================================================================== */
      90             : /* Platform-Specific Allocation                                               */
      91             : /* ========================================================================== */
      92             : 
      93         230 : void *pt_plat_alloc(size_t size) {
      94         230 :     return malloc(size);
      95             : }
      96             : 
      97         230 : void pt_plat_free(void *ptr) {
      98         230 :     free(ptr);
      99         230 : }
     100             : 
     101         230 : size_t pt_plat_extra_size(void) {
     102             :     /* Return size needed for POSIX networking data */
     103         230 :     return pt_posix_extra_size();
     104             : }
     105             : 
     106             : #endif /* PT_PLATFORM_POSIX */

Generated by: LCOV version 1.14