/src/r-source/src/main/flexiblas.c
Line | Count | Source |
1 | | /* |
2 | | * R : A Computer Language for Statistical Data Analysis |
3 | | * Copyright (C) 2023 The R Core Team |
4 | | * |
5 | | * This program is free software; you can redistribute it and/or modify |
6 | | * it under the terms of the GNU General Public License as published by |
7 | | * the Free Software Foundation; either version 2 of the License, or |
8 | | * (at your option) any later version. |
9 | | * |
10 | | * This program is distributed in the hope that it will be useful, |
11 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | | * GNU General Public License for more details. |
14 | | * |
15 | | * You should have received a copy of the GNU General Public License |
16 | | * along with this program; if not, a copy is available at |
17 | | * https://www.R-project.org/Licenses/ |
18 | | */ |
19 | | |
20 | | #ifdef HAVE_CONFIG_H |
21 | | # include <config.h> |
22 | | #endif |
23 | | |
24 | | #include <Defn.h> |
25 | | |
26 | | #include <stdlib.h> |
27 | | #include <string.h> |
28 | | #ifdef HAVE_DLFCN_H |
29 | | #include <dlfcn.h> |
30 | | #endif |
31 | | |
32 | | |
33 | | attribute_hidden SEXP R_flexiblas_info(void) |
34 | 0 | { |
35 | 0 | void *fcb_addr = NULL; |
36 | |
|
37 | 0 | #if defined(HAVE_DLSYM) \ |
38 | 0 | && defined(HAVE_DECL_RTLD_DEFAULT) && HAVE_DECL_RTLD_DEFAULT |
39 | 0 | char *fcb_name = "flexiblas_current_backend"; |
40 | 0 | fcb_addr = dlsym(RTLD_DEFAULT, fcb_name); |
41 | 0 | #endif |
42 | |
|
43 | 0 | if (fcb_addr != NULL) { |
44 | 0 | char *prefix = "FlexiBLAS "; |
45 | 0 | size_t prefix_len = strlen(prefix); |
46 | 0 | int (*fcb) (char *, size_t); |
47 | 0 | size_t size = 64; |
48 | 0 | char *name = NULL; |
49 | |
|
50 | 0 | #ifdef __clang__ |
51 | 0 | # pragma clang diagnostic push |
52 | 0 | # pragma clang diagnostic ignored "-Wpedantic" |
53 | | #elif defined __GNUC__ |
54 | | # pragma GCC diagnostic push |
55 | | # pragma GCC diagnostic ignored "-Wpedantic" |
56 | | #endif |
57 | | /* ISO C forbids assignment between function pointer and 'void *', |
58 | | but this is how dlsym() returns a function pointer. */ |
59 | 0 | fcb = fcb_addr; |
60 | 0 | #ifdef __clang__ |
61 | 0 | # pragma clang diagnostic pop |
62 | | #elif defined __GNUC__ |
63 | | # pragma GCC diagnostic pop |
64 | | #endif |
65 | 0 | for(;;) { |
66 | 0 | if (name) |
67 | 0 | free(name); |
68 | 0 | size *= 2; |
69 | 0 | name = (char *) malloc(prefix_len + size); |
70 | 0 | if (!name) |
71 | 0 | break; |
72 | 0 | strcpy(name, prefix); |
73 | 0 | int res = fcb(name + prefix_len, size); |
74 | 0 | if (res < size - 1) { |
75 | 0 | SEXP ans = mkChar(name); |
76 | 0 | free(name); |
77 | 0 | return ans; |
78 | 0 | } |
79 | 0 | } |
80 | 0 | } |
81 | 0 | return R_NilValue; |
82 | 0 | } |
83 | | |