1 //------------------------------------------------------------------------------
2 // GB_Pending_alloc: allocate a list of pending tuples
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 #include "GB_Pending.h"
11 
GB_Pending_alloc(GB_Pending * PHandle,GrB_Type type,GrB_BinaryOp op,bool is_matrix,int64_t nmax)12 bool GB_Pending_alloc       // create a list of pending tuples
13 (
14     GB_Pending *PHandle,    // output
15     GrB_Type type,          // type of pending tuples
16     GrB_BinaryOp op,        // operator for assembling pending tuples
17     bool is_matrix,         // true if Pending->j must be allocated
18     int64_t nmax            // # of pending tuples to hold
19 )
20 {
21 
22     //--------------------------------------------------------------------------
23     // check inputs
24     //--------------------------------------------------------------------------
25 
26     ASSERT (PHandle != NULL) ;
27     (*PHandle) = NULL ;
28 
29     //--------------------------------------------------------------------------
30     // allocate the Pending header
31     //--------------------------------------------------------------------------
32 
33     size_t header_size ;
34     GB_Pending Pending = GB_MALLOC (1, struct GB_Pending_struct, &header_size) ;
35     if (Pending == NULL)
36     {
37         // out of memory
38         return (false) ;
39     }
40 
41     //--------------------------------------------------------------------------
42     // initialize the contents of the Pending tuples
43     //--------------------------------------------------------------------------
44 
45     nmax = GB_IMAX (nmax, GB_PENDING_INIT) ;
46     Pending->header_size = header_size ;
47     Pending->n = 0 ;                    // no pending tuples yet
48     Pending->nmax = nmax ;              // initial size of list
49     Pending->sorted = true ;            // keep track if tuples are sorted
50     Pending->type = type ;              // type of pending tuples
51     Pending->size = type->size ;        // size of pending tuple type
52     Pending->op = op ;                  // pending operator (NULL is OK)
53     Pending->i_size = 0 ;
54     Pending->j_size = 0 ;
55     Pending->x_size = 0 ;
56 
57     Pending->i = GB_MALLOC (nmax, int64_t, &(Pending->i_size)) ;
58     Pending->j = NULL ;
59     if (is_matrix)
60     {
61         Pending->j = GB_MALLOC (nmax, int64_t, &(Pending->j_size)) ;
62     }
63     Pending->x = GB_MALLOC (nmax * Pending->size, GB_void, &(Pending->x_size)) ;
64 
65     if (Pending->i == NULL || Pending->x == NULL
66         || (is_matrix && Pending->j == NULL))
67     {
68         // out of memory
69         GB_Pending_free (&Pending) ;
70         return (false) ;
71     }
72 
73     //--------------------------------------------------------------------------
74     // return result
75     //--------------------------------------------------------------------------
76 
77     (*PHandle) = Pending ;
78     return (true) ;
79 }
80 
81