1 //------------------------------------------------------------------------------
2 // GB_memory.h: memory allocation
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 #ifndef GB_MEMORY_H
11 #define GB_MEMORY_H
12 
13 //------------------------------------------------------------------------------
14 // memory management
15 //------------------------------------------------------------------------------
16 
17 GB_PUBLIC   // accessed by the MATLAB tests in GraphBLAS/Test only
18 void *GB_calloc_memory      // pointer to allocated block of memory
19 (
20     size_t nitems,          // number of items to allocate
21     size_t size_of_item,    // sizeof each item
22     // output
23     size_t *size_allocated, // # of bytes actually allocated
24     GB_Context Context
25 ) ;
26 
27 GB_PUBLIC   // accessed by the MATLAB tests in GraphBLAS/Test only
28 void *GB_malloc_memory      // pointer to allocated block of memory
29 (
30     size_t nitems,          // number of items to allocate
31     size_t size_of_item,    // sizeof each item
32     // output
33     size_t *size_allocated  // # of bytes actually allocated
34 ) ;
35 
36 GB_PUBLIC   // accessed by the MATLAB tests in GraphBLAS/Test only
37 void *GB_realloc_memory     // pointer to reallocated block of memory, or
38                             // to original block if the realloc failed.
39 (
40     size_t nitems_new,      // new number of items in the object
41     size_t nitems_old,      // old number of items in the object
42     size_t size_of_item,    // sizeof each item
43     // input/output
44     void *p,                // old object to reallocate
45     // output
46     size_t *size_allocated, // # of bytes actually allocated
47     bool *ok,               // true if successful, false otherwise
48     GB_Context Context
49 ) ;
50 
51 GB_PUBLIC   // accessed by the MATLAB tests in GraphBLAS/Test only
52 void GB_free_memory         // free memory, bypassing the free_pool
53 (
54     // input/output
55     void **p,               // pointer to allocated block of memory to free
56     // input
57     size_t size_allocated   // # of bytes actually allocated
58 ) ;
59 
60 GB_PUBLIC   // accessed by the MATLAB tests in GraphBLAS/Test only
61 void GB_dealloc_memory      // free memory, return to free_pool or free it
62 (
63     // input/output
64     void **p,               // pointer to allocated block of memory to free
65     // input
66     size_t size_allocated   // # of bytes actually allocated
67 ) ;
68 
69 GB_PUBLIC   // accessed by the MATLAB tests in GraphBLAS/Test only
70 void GB_free_pool_finalize (void) ;
71 
72 //------------------------------------------------------------------------------
73 // malloc/calloc/realloc/free: for permanent contents of GraphBLAS objects
74 //------------------------------------------------------------------------------
75 
76 #if 0
77 
78     #define GB_FREE(p,s) \
79     { \
80         printf ("dealloc (%s, line %d): %p size %lu\n", \
81             __FILE__, __LINE__, p, s) ; \
82         GB_dealloc_memory (p, s) ; \
83     }
84 
85     #define GB_CALLOC(n,type,s) \
86         (type *) GB_calloc_memory (n, sizeof (type), s, Context) ; \
87         ; printf ("calloc  (%s, line %d): size %lu\n", \
88             __FILE__, __LINE__, *(s)) ; \
89 
90     #define GB_MALLOC(n,type,s) \
91         (type *) GB_malloc_memory (n, sizeof (type), s) ; \
92         ; printf ("malloc  (%s, line %d): size %lu\n", \
93             __FILE__, __LINE__, *(s)) ; \
94 
95     #define GB_REALLOC(p,nnew,nold,type,s,ok,Context) \
96         p = (type *) GB_realloc_memory (nnew, nold, sizeof (type), \
97             (void *) p, s, ok, Context) ; \
98         ; printf ("realloc (%s, line %d): size %lu\n", \
99             __FILE__, __LINE__, *(s)) ; \
100 
101 #else
102 
103     #define GB_FREE(p,s) \
104         GB_dealloc_memory ((void **) p, s)
105 
106     #define GB_CALLOC(n,type,s) \
107         (type *) GB_calloc_memory (n, sizeof (type), s, Context)
108 
109     #define GB_MALLOC(n,type,s) \
110         (type *) GB_malloc_memory (n, sizeof (type), s)
111 
112     #define GB_REALLOC(p,nnew,nold,type,s,ok,Context) \
113         p = (type *) GB_realloc_memory (nnew, nold, sizeof (type), \
114             (void *) p, s, ok, Context)
115 
116 #endif
117 
118 //------------------------------------------------------------------------------
119 // malloc/calloc/realloc/free: for workspace
120 //------------------------------------------------------------------------------
121 
122 // These macros currently do the same thing as the 4 macros above, but that may
123 // change in the future.  Even if they always do the same thing, it's useful to
124 // tag the source code for the allocation of workspace differently from the
125 // allocation of permament space for a GraphBLAS object, such as a GrB_Matrix.
126 
127 #define GB_CALLOC_WERK(n,type,s) GB_CALLOC(n,type,s)
128 #define GB_MALLOC_WERK(n,type,s) GB_MALLOC(n,type,s)
129 #define GB_REALLOC_WERK(p,nnew,nold,type,s,ok,Context) \
130              GB_REALLOC(p,nnew,nold,type,s,ok,Context)
131 #define GB_FREE_WERK(p,s) GB_FREE(p,s)
132 
133 #endif
134 
135