1 //------------------------------------------------------------------------------
2 // GB_dealloc_memory: wrapper for free, using the free_pool
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 // A wrapper for free.  If p is NULL on input, it is not freed.
11 
12 // The memory is freed by returning it to the free_pool if it is small enough
13 // and an exact power of two.  Otherwise, it is freed via GB_free_memory,
14 // and not returned to the free_pool.
15 
16 #include "GB.h"
17 
18 GB_PUBLIC   // accessed by the MATLAB tests in GraphBLAS/Test only
GB_dealloc_memory(void ** p,size_t size_allocated)19 void GB_dealloc_memory      // free memory, return to free_pool or free it
20 (
21     // input/output
22     void **p,               // pointer to allocated block of memory to free
23     // input
24     size_t size_allocated   // # of bytes actually allocated
25 )
26 {
27 
28     if (p != NULL && (*p) != NULL)
29     {
30         bool returned_to_free_pool = false ;
31 
32         if (GB_IS_POWER_OF_TWO (size_allocated))
33         {
34 
35             //------------------------------------------------------------------
36             // return the memory to the free_pool, if possible
37             //------------------------------------------------------------------
38 
39             int k = GB_CEIL_LOG2 (size_allocated) ;
40             if (GB_Global_free_pool_limit_get (k) > 0)
41             {
42 //              printf ("put to free pool %p %d\n", *p, k) ;
43                 returned_to_free_pool = GB_Global_free_pool_put (*p, k) ;
44             }
45         }
46 
47         if (!returned_to_free_pool)
48         {
49 
50             //------------------------------------------------------------------
51             // otherwise free the memory back to the memory manager
52             //------------------------------------------------------------------
53 
54             GB_free_memory (p, size_allocated) ;
55         }
56 
57 //      GB_Global_free_pool_dump (2) ; GB_Global_memtable_dump ( ) ;
58 
59         (*p) = NULL ;
60     }
61 }
62 
63