1 //------------------------------------------------------------------------------
2 // GB_Matrix_free: free a GrB_Matrix or GrB_Vector
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 // Free all the content of a matrix.  After GB_Matrix_free (&A), the header A
11 // is freed and set to NULL if the header of A was originally dynamically
12 // allocated.  Otherwise, A is not freed.
13 
14 #include "GB.h"
15 
GB_Matrix_free(GrB_Matrix * Ahandle)16 void GB_Matrix_free             // free a matrix
17 (
18     GrB_Matrix *Ahandle         // handle of matrix to free
19 )
20 {
21 
22     if (Ahandle != NULL)
23     {
24         GrB_Matrix A = *Ahandle ;
25         if (A != NULL && (A->magic == GB_MAGIC || A->magic == GB_MAGIC2))
26         {
27             // free all content of A
28             size_t header_size = A->header_size ;
29             GB_phbix_free (A) ;
30             if (!(A->static_header))
31             {
32                 // free the header of A itself, unless it is static
33                 A->magic = GB_FREED ;       // to help detect dangling pointers
34                 GB_FREE (Ahandle, header_size) ;
35                 (*Ahandle) = NULL ;
36             }
37         }
38     }
39 }
40 
41