Debian Codegraph @

lib/cpu_features_common.h

raw
1 /*
2 * cpu_features_common.h - code shared by all lib/$arch/cpu_features.c
3 *
4 * Copyright 2020 Eric Biggers
5 *
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
13 * conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
26 */
27
28 #ifndef LIB_CPU_FEATURES_COMMON_H
29 #define LIB_CPU_FEATURES_COMMON_H
30
31 #if defined(TEST_SUPPORT__DO_NOT_USE) && !defined(FREESTANDING)
32 /* for strdup() and strtok_r() */
33 # undef _ANSI_SOURCE
34 # ifndef __APPLE__
35 # undef _GNU_SOURCE
36 # define _GNU_SOURCE
37 # endif
38 # include <stdio.h>
39 # include <stdlib.h>
40 # include <string.h>
41 #endif
42
43 #include "lib_common.h"
44
45 struct cpu_feature {
46 u32 bit;
47 const char *name;
48 };
49
50 #if defined(TEST_SUPPORT__DO_NOT_USE) && !defined(FREESTANDING)
51 /* Disable any features that are listed in $LIBDEFLATE_DISABLE_CPU_FEATURES. */
52 static inline void
53 disable_cpu_features_for_testing(u32 *features,
54 const struct cpu_feature *feature_table,
55 size_t feature_table_length)
56 {
57 char *env_value, *strbuf, *p, *saveptr = NULL;
58 size_t i;
59
60 env_value = getenv("LIBDEFLATE_DISABLE_CPU_FEATURES");
61 if (!env_value)
62 return;
63 strbuf = strdup(env_value);
64 if (!strbuf)
65 abort();
66 p = strtok_r(strbuf, ",", &saveptr);
67 while (p) {
68 for (i = 0; i < feature_table_length; i++) {
69 if (strcmp(p, feature_table[i].name) == 0) {
70 *features &= ~feature_table[i].bit;
71 break;
72 }
73 }
74 if (i == feature_table_length) {
75 fprintf(stderr,
76 "unrecognized feature in LIBDEFLATE_DISABLE_CPU_FEATURES: \"%s\"\n",
77 p);
78 abort();
79 }
80 p = strtok_r(NULL, ",", &saveptr);
81 }
82 free(strbuf);
83 }
84 #else /* TEST_SUPPORT__DO_NOT_USE */
85 static inline void
86 disable_cpu_features_for_testing(u32 *features,
87 const struct cpu_feature *feature_table,
88 size_t feature_table_length)
89 {
90 }
91 #endif /* !TEST_SUPPORT__DO_NOT_USE */
92
93 #endif /* LIB_CPU_FEATURES_COMMON_H */