1 //------------------------------------------------------------------------------
2 // GB_bix_alloc: allocate a matrix to hold a given number of entries
3 //------------------------------------------------------------------------------
4 
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: Apache-2.0
7 
8 //------------------------------------------------------------------------------
9 
10 // Does not modify A->p or A->h (unless an error occurs).  Frees A->b, A->x,
11 // and A->i and reallocates them to the requested size.  Frees any pending
12 // tuples and deletes all entries (including zombies, if any).  If numeric is
13 // false, then A->x is freed but not reallocated.
14 
15 // If this method fails, all content of A is freed (including A->p and A->h).
16 
17 #include "GB.h"
18 
19 GB_PUBLIC   // accessed by the MATLAB tests in GraphBLAS/Test only
GB_bix_alloc(GrB_Matrix A,const GrB_Index nzmax,const bool is_bitmap,const bool bitmap_calloc,const bool is_sparse,const bool numeric,GB_Context Context)20 GrB_Info GB_bix_alloc       // allocate A->b, A->i, and A->x space in a matrix
21 (
22     GrB_Matrix A,           // matrix to allocate space for
23     const GrB_Index nzmax,  // number of entries the matrix can hold
24     const bool is_bitmap,   // if true, allocate A->b, otherwise A->b is NULL
25     const bool bitmap_calloc,   // if true, calloc A->b, otherwise use malloc
26     const bool is_sparse,   // if true, allocate A->i, otherwise A->i is NULL
27     const bool numeric,     // if true, allocate A->x, otherwise A->x is NULL
28     GB_Context Context
29 )
30 {
31 
32     //--------------------------------------------------------------------------
33     // check inputs
34     //--------------------------------------------------------------------------
35 
36     ASSERT (A != NULL) ;
37     if (nzmax > GxB_INDEX_MAX)
38     {
39         // problem too large
40         return (GrB_OUT_OF_MEMORY) ;
41     }
42 
43     //--------------------------------------------------------------------------
44     // allocate the A->b, A->x, and A->i content of the matrix
45     //--------------------------------------------------------------------------
46 
47     // Free the existing A->b, A->x, and A->i content, if any.
48     // Leave A->p and A->h unchanged.
49     GB_bix_free (A) ;
50 
51     // allocate the new A->x and A->i content
52     A->nzmax = GB_IMAX (nzmax, 1) ;
53 
54     bool ok = true ;
55     if (is_sparse)
56     {
57         A->i = GB_MALLOC (A->nzmax, int64_t, &(A->i_size)) ;
58         ok = (A->i != NULL) ;
59         if (ok && A->nzmax == 1) A->i [0] = 0 ;
60     }
61     else if (is_bitmap)
62     {
63         if (bitmap_calloc)
64         {
65             // content is fully defined
66             A->b = GB_CALLOC (A->nzmax, int8_t, &(A->b_size)) ;
67             A->magic = GB_MAGIC ;
68         }
69         else
70         {
71             // bitmap is not defined and will be computed by the caller
72             A->b = GB_MALLOC (A->nzmax, int8_t, &(A->b_size)) ;
73         }
74         ok = (A->b != NULL) ;
75     }
76 
77     if (numeric)
78     {
79         A->x = GB_MALLOC (A->nzmax * A->type->size, GB_void, &(A->x_size)) ;
80         ok = ok && (A->x != NULL) ;
81     }
82 
83     if (!ok)
84     {
85         // out of memory
86         GB_phbix_free (A) ;
87         return (GrB_OUT_OF_MEMORY) ;
88     }
89 
90     return (GrB_SUCCESS) ;
91 }
92 
93