LCOV - code coverage report
Current view: top level - basic - memory-util.c (source / functions) Hit Total Coverage
Test: systemd_full.info Lines: 15 15 100.0 %
Date: 2019-08-23 13:36:53 Functions: 2 2 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 9 10 90.0 %

           Branch data     Line data    Source code
       1                 :            : #include <unistd.h>
       2                 :            : 
       3                 :            : #include "memory-util.h"
       4                 :            : 
       5                 :     374795 : size_t page_size(void) {
       6                 :            :         static thread_local size_t pgsz = 0;
       7                 :            :         long r;
       8                 :            : 
       9         [ +  + ]:     374795 :         if (_likely_(pgsz > 0))
      10                 :     374092 :                 return pgsz;
      11                 :            : 
      12                 :        703 :         r = sysconf(_SC_PAGESIZE);
      13         [ -  + ]:        703 :         assert(r > 0);
      14                 :            : 
      15                 :        703 :         pgsz = (size_t) r;
      16                 :        703 :         return pgsz;
      17                 :            : }
      18                 :            : 
      19                 :         16 : bool memeqzero(const void *data, size_t length) {
      20                 :            :         /* Does the buffer consist entirely of NULs?
      21                 :            :          * Copied from https://github.com/systemd/casync/, copied in turn from
      22                 :            :          * https://github.com/rustyrussell/ccan/blob/master/ccan/mem/mem.c#L92,
      23                 :            :          * which is licensed CC-0.
      24                 :            :          */
      25                 :            : 
      26                 :         16 :         const uint8_t *p = data;
      27                 :            :         size_t i;
      28                 :            : 
      29                 :            :         /* Check first 16 bytes manually */
      30         [ +  + ]:        144 :         for (i = 0; i < 16; i++, length--) {
      31         [ +  + ]:        140 :                 if (length == 0)
      32                 :          4 :                         return true;
      33         [ +  + ]:        136 :                 if (p[i])
      34                 :          8 :                         return false;
      35                 :            :         }
      36                 :            : 
      37                 :            :         /* Now we know first 16 bytes are NUL, memcmp with self.  */
      38                 :          4 :         return memcmp(data, p + i, length) == 0;
      39                 :            : }
      40                 :            : 
      41                 :            : #if !HAVE_EXPLICIT_BZERO
      42                 :            : /*
      43                 :            :  * The pointer to memset() is volatile so that compiler must de-reference the pointer and can't assume that
      44                 :            :  * it points to any function in particular (such as memset(), which it then might further "optimize"). This
      45                 :            :  * approach is inspired by openssl's crypto/mem_clr.c.
      46                 :            :  */
      47                 :            : typedef void *(*memset_t)(void *,int,size_t);
      48                 :            : 
      49                 :            : static volatile memset_t memset_func = memset;
      50                 :            : 
      51                 :            : void* explicit_bzero_safe(void *p, size_t l) {
      52                 :            :         if (l > 0)
      53                 :            :                 memset_func(p, '\0', l);
      54                 :            : 
      55                 :            :         return p;
      56                 :            : }
      57                 :            : #endif

Generated by: LCOV version 1.14