Line data Source code
1 : //------------------------------------------------------------------------------ 2 : // LAGraph_Malloc: wrapper for malloc 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 26882 : int LAGraph_Malloc 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 26882 : LG_CLEAR_MSG ; 32 26882 : LG_ASSERT (p != NULL, GrB_NULL_POINTER) ; 33 26882 : (*p) = NULL ; 34 : 35 : // make sure at least one item is allocated 36 26882 : nitems = LAGRAPH_MAX (1, nitems) ; 37 : 38 : // make sure at least one byte is allocated 39 26882 : size_of_item = LAGRAPH_MAX (1, size_of_item) ; 40 : 41 : // compute the size and check for integer overflow 42 : size_t size ; 43 26882 : if (!LG_Multiply_size_t (&size, nitems, size_of_item)) 44 : { 45 : // overflow 46 1 : return (GrB_OUT_OF_MEMORY) ; 47 : } 48 : 49 : // malloc the space 50 26881 : (*p) = LAGraph_Malloc_function (size) ; 51 26881 : return (((*p) == NULL) ? GrB_OUT_OF_MEMORY : GrB_SUCCESS) ; 52 : }