1 /* ========================================================================== */
2 /* === CHOLMOD/Include/cholmod_function.h ================================ */
3 /* ========================================================================== */
4 
5 /* -----------------------------------------------------------------------------
6  * CHOLMOD/Include/cholmod_function.h
7  * Copyright (C) 2014, Timothy A. Davis
8  * This specific file (CHOLMOD/Include/cholmod_function.h) has no license
9  * restrictions at all.  You may freely include this in your applications, and
10  * modify it at will.
11  * -------------------------------------------------------------------------- */
12 
13 /* Memory management, printing, and math function pointers were removed from
14    the CHOLMOD Common struct as of version 2.2.0 and later.  They now appear in
15    SuiteSparse_config.h instead.  This file assists in backward compatibility,
16    so that you can use either old or new versions of CHOLMOD and SuiteSparse in
17    an application that uses the function pointers.  You can copy the file into
18    your own application that uses older versions of CHOLMOD, or the current
19    version, so that you have a transparent method for setting these function
20    pointers for any version of CHOLMOD and SuiteSparse.
21 
22    In both old and new versions of CHOLMOD (and SuiteSparse), the intent of
23    these function pointers is that they are not to be called directly.
24    Instead, you should use (for example), the cholmod_malloc function.  That
25    function is a wrapper that then uses the cc->malloc_memory or
26    SuiteSparse_config.malloc_func function pointers.
27 
28    In each of the macros below, 'cc' is a pointer to the CHOLMOD Common struct.
29 
30    Usage:  to assign, say, 'malloc' as your memory allocator, use this:
31 
32         #include "cholmod_function.h"
33         ...
34         cholmod_common *cc, Common ;
35         cc = &Common ;
36         cholmod_start (cc) ;
37         ...
38         CHOLMOD_FUNCTION_DEFAULTS ;
39         CHOLMOD_FUNCTION_MALLOC (cc) = mymalloc ;
40 
41     instead of this, in older versions of CHOLMOD:
42 
43         cc->malloc_memory = mymalloc ;
44 
45     or in newer versions of CHOLMOD:
46 
47         SuiteSparse_config.malloc_func = mymalloc ;
48 */
49 
50 #ifndef CHOLMOD_FUNCTION_H
51 #define CHOLMOD_FUNCTION_H
52 
53 #include "cholmod.h"
54 
55 /* -------------------------------------------------------------------------- */
56 /* location of function pointers, depending on the CHOLMOD version */
57 /* -------------------------------------------------------------------------- */
58 
59 #if (CHOLMOD_VERSION < (CHOLMOD_VER_CODE(2,2)))
60 
61     #define CHOLMOD_FUNCTION_MALLOC(cc)     cc->malloc_memory
62     #define CHOLMOD_FUNCTION_REALLOC(cc)    cc->realloc_memory
63     #define CHOLMOD_FUNCTION_FREE(cc)       cc->free_memory
64     #define CHOLMOD_FUNCTION_CALLOC(cc)     cc->calloc_memory
65     #define CHOLMOD_FUNCTION_PRINTF(cc)     cc->print_function
66     #define CHOLMOD_FUNCTION_DIVCOMPLEX(cc) cc->complex_divide
67     #define CHOLMOD_FUNCTION_HYPOTENUSE(cc) cc->hypotenuse
68 
69 #else
70 
71     #include "SuiteSparse_config.h"
72     #define CHOLMOD_FUNCTION_MALLOC(cc)     SuiteSparse_config.malloc_func
73     #define CHOLMOD_FUNCTION_REALLOC(cc)    SuiteSparse_config.realloc_func
74     #define CHOLMOD_FUNCTION_FREE(cc)       SuiteSparse_config.free_func
75     #define CHOLMOD_FUNCTION_CALLOC(cc)     SuiteSparse_config.calloc_func
76     #define CHOLMOD_FUNCTION_PRINTF(cc)     SuiteSparse_config.printf_func
77     #define CHOLMOD_FUNCTION_DIVCOMPLEX(cc) SuiteSparse_config.divcomplex_func
78     #define CHOLMOD_FUNCTION_HYPOTENUSE(cc) SuiteSparse_config.hypot_func
79 
80 #endif
81 
82 /* -------------------------------------------------------------------------- */
83 /* default math functions, depending on the CHOLMOD version */
84 /* -------------------------------------------------------------------------- */
85 
86 #if (CHOLMOD_VERSION < (CHOLMOD_VER_CODE(2,2)))
87 
88     #define CHOLMOD_FUNCTION_DEFAULT_DIVCOMPLEX cholmod_l_divcomplex
89     #define CHOLMOD_FUNCTION_DEFAULT_HYPOTENUSE cholmod_l_hypot
90 
91 #else
92 
93     #define CHOLMOD_FUNCTION_DEFAULT_DIVCOMPLEX SuiteSparse_divcomplex
94     #define CHOLMOD_FUNCTION_DEFAULT_HYPOTENUSE SuiteSparse_hypot
95 
96 #endif
97 
98 /* -------------------------------------------------------------------------- */
99 /* default memory manager functions */
100 /* -------------------------------------------------------------------------- */
101 
102 #ifndef NMALLOC
103     #ifdef MATLAB_MEX_FILE
104         /* MATLAB mexFunction */
105         #define CHOLMOD_FUNCTION_DEFAULT_MALLOC  mxMalloc
106         #define CHOLMOD_FUNCTION_DEFAULT_CALLOC  mxCalloc
107         #define CHOLMOD_FUNCTION_DEFAULT_REALLOC mxRealloc
108         #define CHOLMOD_FUNCTION_DEFAULT_FREE    mxFree
109     #else
110         /* standard ANSI C */
111         #define CHOLMOD_FUNCTION_DEFAULT_MALLOC  malloc
112         #define CHOLMOD_FUNCTION_DEFAULT_CALLOC  calloc
113         #define CHOLMOD_FUNCTION_DEFAULT_REALLOC realloc
114         #define CHOLMOD_FUNCTION_DEFAULT_FREE    free
115     #endif
116 #else
117     /* no memory manager defined at compile time */
118     #define CHOLMOD_FUNCTION_DEFAULT_MALLOC  NULL
119     #define CHOLMOD_FUNCTION_DEFAULT_CALLOC  NULL
120     #define CHOLMOD_FUNCTION_DEFAULT_REALLOC NULL
121     #define CHOLMOD_FUNCTION_DEFAULT_FREE    NULL
122 #endif
123 
124 /* -------------------------------------------------------------------------- */
125 /* default printf function */
126 /* -------------------------------------------------------------------------- */
127 
128 #ifdef MATLAB_MEX_FILE
129     #define CHOLMOD_FUNCTION_DEFAULT_PRINTF mexPrintf
130 #else
131     #define CHOLMOD_FUNCTION_DEFAULT_PRINTF printf
132 #endif
133 
134 /* -------------------------------------------------------------------------- */
135 /* set all the defaults */
136 /* -------------------------------------------------------------------------- */
137 
138 /* Use this macro to initialize all the function pointers to their defaults
139    for any version of CHOLMOD.  For CHOLMD 2.2.0 and later, it sets function
140    pointers in the SuiteSparse_config struct.  For older versions, it sets
141    function pointers in the CHOLMOD Common.  This assignment is not
142    thread-safe, and should be done before launching any threads. */
143 
144 #define CHOLMOD_FUNCTION_DEFAULTS \
145 { \
146     CHOLMOD_FUNCTION_MALLOC (cc)     = CHOLMOD_FUNCTION_DEFAULT_MALLOC ; \
147     CHOLMOD_FUNCTION_REALLOC (cc)    = CHOLMOD_FUNCTION_DEFAULT_REALLOC ; \
148     CHOLMOD_FUNCTION_FREE (cc)       = CHOLMOD_FUNCTION_DEFAULT_FREE ; \
149     CHOLMOD_FUNCTION_CALLOC (cc)     = CHOLMOD_FUNCTION_DEFAULT_CALLOC ; \
150     CHOLMOD_FUNCTION_PRINTF (cc)     = CHOLMOD_FUNCTION_DEFAULT_PRINTF ; \
151     CHOLMOD_FUNCTION_DIVCOMPLEX (cc) = CHOLMOD_FUNCTION_DEFAULT_DIVCOMPLEX ; \
152     CHOLMOD_FUNCTION_HYPOTENUSE (cc) = CHOLMOD_FUNCTION_DEFAULT_HYPOTENUSE ; \
153 }
154 
155 #endif
156