LCOV - code coverage report
Current view: top level - src/utility - LAGraph_Calloc.c (source / functions) Hit Total Coverage
Test: LAGraph code coverage report. Commit id: 3b461aa. Current time (UTC): 2024-01-25T16:04:32Z Lines: 14 14 100.0 %
Date: 2024-01-25 16:05:28 Functions: 1 1 100.0 %

          Line data    Source code
       1             : //------------------------------------------------------------------------------
       2             : // LAGraph_Calloc:  wrapper for calloc
       3             : //------------------------------------------------------------------------------
       4             : 
       5             : // LAGraph, (c) 2019-2022 by The LAGraph Contributors, All Rights Reserved.
       6             : // SPDX-License-Identifier: BSD-2-Clause
       7             : //
       8             : // For additional details (including references to third party source code and
       9             : // other files) see the LICENSE file or contact permission@sei.cmu.edu. See
      10             : // Contributors.txt for a full list of contributors. Created, in part, with
      11             : // funding and support from the U.S. Government (see Acknowledgments.txt file).
      12             : // DM22-0790
      13             : 
      14             : // Contributed by Timothy A. Davis, Texas A&M University
      15             : 
      16             : //------------------------------------------------------------------------------
      17             : 
      18             : #include "LG_internal.h"
      19             : 
      20        2550 : int LAGraph_Calloc
      21             : (
      22             :     // output:
      23             :     void **p,               // pointer to allocated block of memory
      24             :     // input:
      25             :     size_t nitems,          // number of items
      26             :     size_t size_of_item,    // size of each item
      27             :     char *msg
      28             : )
      29             : {
      30             :     // check inputs
      31        2550 :     LG_CLEAR_MSG ;
      32        2550 :     LG_ASSERT (p != NULL, GrB_NULL_POINTER) ;
      33        2550 :     (*p) = NULL ;
      34             : 
      35             :     // make sure at least one item is allocated
      36        2550 :     nitems = LAGRAPH_MAX (1, nitems) ;
      37             : 
      38             :     // make sure at least one byte is allocated
      39        2550 :     size_of_item = LAGRAPH_MAX (1, size_of_item) ;
      40             : 
      41             :     // compute the size and check for integer overflow
      42             :     size_t size ;
      43        2550 :     if (!LG_Multiply_size_t (&size, nitems, size_of_item))
      44             :     {
      45             :         // overflow
      46           1 :         return (GrB_OUT_OF_MEMORY) ;
      47             :     }
      48             : 
      49        2549 :     if (LAGraph_Calloc_function == NULL)
      50             :     {
      51             :         // calloc function not available; use malloc and memset
      52           1 :         LG_TRY (LAGraph_Malloc (p, nitems, size_of_item, msg)) ;
      53           1 :         memset (*p, 0, size) ;
      54           1 :         return (GrB_SUCCESS) ;
      55             :     }
      56             : 
      57             :     // use the calloc function
      58        2548 :     (*p) = LAGraph_Calloc_function (nitems, size_of_item) ;
      59        2548 :     return (((*p) == NULL) ? GrB_OUT_OF_MEMORY : GrB_SUCCESS) ;
      60             : }

Generated by: LCOV version 1.14