1 //------------------------------------------------------------------------------
2 // GB_Pending_realloc: reallocate 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 // Reallocate a list of pending tuples. If it fails, the list is freed.
11
12 #include "GB_Pending.h"
13
GB_Pending_realloc(GB_Pending * PHandle,int64_t nnew,GB_Context Context)14 bool GB_Pending_realloc // reallocate a list of pending tuples
15 (
16 GB_Pending *PHandle, // Pending tuple list to reallocate
17 int64_t nnew, // # of new tuples to accomodate
18 GB_Context Context
19 )
20 {
21
22 //--------------------------------------------------------------------------
23 // check inputs
24 //--------------------------------------------------------------------------
25
26 ASSERT (PHandle != NULL) ;
27 GB_Pending Pending = (*PHandle) ;
28
29 //--------------------------------------------------------------------------
30 // ensure the list can hold at least nnew more tuples
31 //--------------------------------------------------------------------------
32
33 int64_t newsize = nnew + Pending->n ;
34
35 if (newsize > Pending->nmax)
36 {
37
38 //----------------------------------------------------------------------
39 // double the size if the list is not large enough
40 //----------------------------------------------------------------------
41
42 newsize = GB_IMAX (newsize, 2 * Pending->nmax) ;
43
44 //----------------------------------------------------------------------
45 // reallocate the i,j,x arrays
46 //----------------------------------------------------------------------
47
48 bool ok1 = true ;
49 bool ok2 = true ;
50 bool ok3 = true ;
51
52 GB_REALLOC (Pending->i, newsize, Pending->nmax, int64_t,
53 &(Pending->i_size), &ok1, Context) ;
54 if (Pending->j != NULL)
55 {
56 GB_REALLOC (Pending->j, newsize, Pending->nmax, int64_t,
57 &(Pending->j_size), &ok2, Context) ;
58 }
59 size_t s = Pending->size ;
60 GB_REALLOC (Pending->x, newsize*s, (Pending->nmax)*s, GB_void,
61 &(Pending->x_size), &ok3, Context) ;
62
63 if (!ok1 || !ok2 || !ok3)
64 {
65 // out of memory
66 GB_Pending_free (PHandle) ;
67 return (false) ;
68 }
69
70 //----------------------------------------------------------------------
71 // record the new size of the Pending tuple list
72 //----------------------------------------------------------------------
73
74 Pending->nmax = newsize ;
75 }
76
77 //--------------------------------------------------------------------------
78 // return result
79 //--------------------------------------------------------------------------
80
81 return (true) ;
82 }
83
84