Austin Schuh | 9049e20 | 2022-02-20 17:34:16 -0800 | [diff] [blame] | 1 | #ifndef GLOB_OPTS_H |
| 2 | # define GLOB_OPTS_H |
| 3 | |
| 4 | # ifdef __cplusplus |
| 5 | extern "C" { |
| 6 | # endif /* ifdef __cplusplus */ |
| 7 | |
| 8 | /* |
| 9 | Define OSQP compiler flags |
| 10 | */ |
| 11 | |
| 12 | // cmake generated compiler flags |
| 13 | #include "osqp_configure.h" |
| 14 | |
| 15 | /* DATA CUSTOMIZATIONS (depending on memory manager)----------------------- */ |
| 16 | |
| 17 | // We do not need memory allocation functions if EMBEDDED is enabled |
| 18 | # ifndef EMBEDDED |
| 19 | |
| 20 | /* define custom printfs and memory allocation (e.g. matlab/python) */ |
| 21 | # ifdef MATLAB |
| 22 | # include "mex.h" |
| 23 | static void* c_calloc(size_t num, size_t size) { |
| 24 | void *m = mxCalloc(num, size); |
| 25 | mexMakeMemoryPersistent(m); |
| 26 | return m; |
| 27 | } |
| 28 | |
| 29 | static void* c_malloc(size_t size) { |
| 30 | void *m = mxMalloc(size); |
| 31 | mexMakeMemoryPersistent(m); |
| 32 | return m; |
| 33 | } |
| 34 | |
| 35 | static void* c_realloc(void *ptr, size_t size) { |
| 36 | void *m = mxRealloc(ptr, size); |
| 37 | mexMakeMemoryPersistent(m); |
| 38 | return m; |
| 39 | } |
| 40 | # define c_free mxFree |
| 41 | # elif defined PYTHON |
| 42 | // Define memory allocation for python. Note that in Python 2 memory manager |
| 43 | // Calloc is not implemented |
| 44 | # include <Python.h> |
| 45 | # if PY_MAJOR_VERSION >= 3 |
| 46 | // https://docs.python.org/3/c-api/memory.html |
| 47 | // The following function sets are wrappers to the system allocator. These functions are thread-safe, the GIL does not need to be held. |
| 48 | // The default raw memory allocator uses the following functions: malloc(), calloc(), realloc() and free(); call malloc(1) (or calloc(1, 1)) when requesting zero bytes. |
| 49 | # define c_malloc PyMem_RawMalloc |
| 50 | # define c_calloc PyMem_RawCalloc |
| 51 | # define c_free PyMem_RawFree |
| 52 | # define c_realloc PyMem_RawRealloc |
| 53 | # else /* if PY_MAJOR_VERSION >= 3 */ |
| 54 | # define c_malloc PyMem_Malloc |
| 55 | # define c_free PyMem_Free |
| 56 | # define c_realloc PyMem_Realloc |
| 57 | static void* c_calloc(size_t num, size_t size) { |
| 58 | void *m = PyMem_Malloc(num * size); |
| 59 | memset(m, 0, num * size); |
| 60 | return m; |
| 61 | } |
| 62 | # endif /* if PY_MAJOR_VERSION >= 3 */ |
| 63 | |
| 64 | # elif !defined OSQP_CUSTOM_MEMORY |
| 65 | /* If no custom memory allocator defined, use |
| 66 | * standard linux functions. Custom memory allocator definitions |
| 67 | * appear in the osqp_configure.h generated file. */ |
| 68 | # include <stdlib.h> |
| 69 | # define c_malloc malloc |
| 70 | # define c_calloc calloc |
| 71 | # define c_free free |
| 72 | # define c_realloc realloc |
| 73 | # endif /* ifdef MATLAB */ |
| 74 | |
| 75 | # endif // end ifndef EMBEDDED |
| 76 | |
| 77 | |
| 78 | /* Use customized number representation ----------------------------------- */ |
| 79 | # ifdef DLONG // long integers |
| 80 | typedef long long c_int; /* for indices */ |
| 81 | # else // standard integers |
| 82 | typedef int c_int; /* for indices */ |
| 83 | # endif /* ifdef DLONG */ |
| 84 | |
| 85 | |
| 86 | # ifndef DFLOAT // Doubles |
| 87 | typedef double c_float; /* for numerical values */ |
| 88 | # else // Floats |
| 89 | typedef float c_float; /* for numerical values */ |
| 90 | # endif /* ifndef DFLOAT */ |
| 91 | |
| 92 | |
| 93 | /* Use customized operations */ |
| 94 | |
| 95 | # ifndef c_absval |
| 96 | # define c_absval(x) (((x) < 0) ? -(x) : (x)) |
| 97 | # endif /* ifndef c_absval */ |
| 98 | |
| 99 | # ifndef c_max |
| 100 | # define c_max(a, b) (((a) > (b)) ? (a) : (b)) |
| 101 | # endif /* ifndef c_max */ |
| 102 | |
| 103 | # ifndef c_min |
| 104 | # define c_min(a, b) (((a) < (b)) ? (a) : (b)) |
| 105 | # endif /* ifndef c_min */ |
| 106 | |
| 107 | // Round x to the nearest multiple of N |
| 108 | # ifndef c_roundmultiple |
| 109 | # define c_roundmultiple(x, N) ((x) + .5 * (N)-c_fmod((x) + .5 * (N), (N))) |
| 110 | # endif /* ifndef c_roundmultiple */ |
| 111 | |
| 112 | |
| 113 | /* Use customized functions ----------------------------------------------- */ |
| 114 | |
| 115 | # if EMBEDDED != 1 |
| 116 | |
| 117 | # include <math.h> |
| 118 | # ifndef DFLOAT // Doubles |
| 119 | # define c_sqrt sqrt |
| 120 | # define c_fmod fmod |
| 121 | # else // Floats |
| 122 | # define c_sqrt sqrtf |
| 123 | # define c_fmod fmodf |
| 124 | # endif /* ifndef DFLOAT */ |
| 125 | |
| 126 | # endif // end EMBEDDED |
| 127 | |
| 128 | # ifdef PRINTING |
| 129 | # include <stdio.h> |
| 130 | # include <string.h> |
| 131 | |
| 132 | /* informational print function */ |
| 133 | # ifdef MATLAB |
| 134 | # define c_print mexPrintf |
| 135 | # elif defined PYTHON |
| 136 | # include <Python.h> |
| 137 | # define c_print(...) \ |
| 138 | { \ |
| 139 | PyGILState_STATE gilstate = PyGILState_Ensure(); \ |
| 140 | PySys_WriteStdout(__VA_ARGS__); \ |
| 141 | PyGILState_Release(gilstate); \ |
| 142 | } |
| 143 | # elif defined R_LANG |
| 144 | # include <R_ext/Print.h> |
| 145 | # define c_print Rprintf |
| 146 | # else /* ifdef MATLAB */ |
Austin Schuh | 39f26f6 | 2022-02-24 21:34:46 -0800 | [diff] [blame] | 147 | # define c_print(...) fprintf(stderr, __VA_ARGS__) |
Austin Schuh | 9049e20 | 2022-02-20 17:34:16 -0800 | [diff] [blame] | 148 | # endif /* c_print configuration */ |
| 149 | |
| 150 | /* error printing function */ |
| 151 | # ifdef R_LANG |
| 152 | /* Some CRAN builds complain about __VA_ARGS__, so just print */ |
| 153 | /* out the error messages on R without the __FUNCTION__ trace */ |
| 154 | # define c_eprint Rprintf |
| 155 | # else |
| 156 | # define c_eprint(...) c_print("ERROR in %s: ", __FUNCTION__); \ |
| 157 | c_print(__VA_ARGS__); c_print("\n"); |
| 158 | # endif /* c_eprint configuration */ |
| 159 | |
| 160 | # endif /* PRINTING */ |
| 161 | |
| 162 | |
| 163 | # ifdef __cplusplus |
| 164 | } |
| 165 | # endif /* ifdef __cplusplus */ |
| 166 | |
| 167 | #endif /* ifndef GLOB_OPTS_H */ |