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 2548 : 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 2548 : LG_CLEAR_MSG ; 32 2548 : LG_ASSERT (p != NULL, GrB_NULL_POINTER) ; 33 2548 : (*p) = NULL ; 34 : 35 : // make sure at least one item is allocated 36 2548 : nitems = LAGRAPH_MAX (1, nitems) ; 37 : 38 : // make sure at least one byte is allocated 39 2548 : size_of_item = LAGRAPH_MAX (1, size_of_item) ; 40 : 41 : // compute the size and check for integer overflow 42 : size_t size ; 43 2548 : if (!LG_Multiply_size_t (&size, nitems, size_of_item)) 44 : { 45 : // overflow 46 1 : return (GrB_OUT_OF_MEMORY) ; 47 : } 48 : 49 2547 : 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 2546 : (*p) = LAGraph_Calloc_function (nitems, size_of_item) ; 59 2546 : return (((*p) == NULL) ? GrB_OUT_OF_MEMORY : GrB_SUCCESS) ; 60 : }