Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在用户空间中使用bitops.h_C++_C_Linux Kernel_Bit - Fatal编程技术网

C++ 如何在用户空间中使用bitops.h

C++ 如何在用户空间中使用bitops.h,c++,c,linux-kernel,bit,C++,C,Linux Kernel,Bit,我有一个程序,需要使用bitops.h文件中的test_和set_bit函数执行一些位操作。如何使用#include在我的C文件中包含此文件。它似乎没有找到它,即使我可以在内核源代码中找到它 是因为它不允许包含在用户空间程序中。建议 我正在使用#include,然后使用gcc myfile.c进行编译 它没有为bitops.h文件显示这样的文件或目录。我可以在内核目录下找到它,但是我建议您将\include更改为\include 如果问题仍然存在,您必须使用gcc myfile.cpp-I“您的

我有一个程序,需要使用bitops.h文件中的test_和set_bit函数执行一些位操作。如何使用
#include
在我的
C
文件中包含此文件。它似乎没有找到它,即使我可以在内核源代码中找到它

是因为它不允许包含在用户空间程序中。建议

我正在使用
#include
,然后使用
gcc myfile.c
进行编译


它没有为bitops.h文件显示这样的文件或目录。我可以在内核目录下找到它,但是我建议您将
\include
更改为
\include


如果问题仍然存在,您必须使用
gcc myfile.cpp-I“您的linux目录”
来告诉
gcc
您的内核文件在哪里

我建议您将
#include
更改为
#include


如果问题仍然存在,您必须使用
gcc myfile.cpp-I“您的linux目录”
来告诉
gcc
您的内核文件在哪里

小心使用
bitops.h
。它可以从定义良好的源生成未定义的行为

例如,对于,移位不能为负数,不能等于单词的长度,也不能超过单词的长度。这意味着32位值只能移位[0,31],包括[0,31]

在上面的示例中,除[0,31]之外的任何内容都是“未定义的”。我相信编译器可以自由地处理未定义的代码,包括删除有问题的代码、调用
abort()
,或者自己大便

许多程序滥用轮班和轮换。测试/检查的一个好方法是使用John Regehr和李鹏的。使用功能(
-fcatch undefined ansic behavior
)很容易构建Clang,因此可以使用工具来检查这些内容


下面是3.8内核
(于2013年2月19日下载)中的代码。如果程序员发送的移位大小为
0
(合法),
将创建非法代码,因为
(word小心
bitops.h
。它可以从定义良好的源生成未定义的行为

例如,对于,移位不能为负数,不能等于字的长度,也不能超过字的长度。这意味着32位值只能移位[0,31],包括[0,31]

在上面的例子中,除了[0,31]之外的任何东西都是“未定义的”。我相信编译器可以自由地对未定义的代码执行它想要的操作,包括删除有问题的代码、调用
abort()
,或者自己大便

许多程序滥用移位和轮换。测试/检查的一个很好的方法是使用John Regehr和Peng Li的。使用功能(
-fcatch undefined ansic behavior
)很容易构建Clang,因此可以使用工具检查这些内容


话虽如此,下面是3.8内核
(2013年2月19日下载)中的代码。如果程序员发送的移位大小为
0
(合法),
将创建非法代码,因为
(word我试图包含/usr/src/linux headers-$(uname-r)(在内核构建期间安装)使用
-I kernel\u header\u路径
搜索路径,但都需要一个或多个其他内核头。它看起来像/include/linux/bitops.h是所有特定于arch的位操作的建议包含文件

因此,我同意@nos:使用内核代码进行用户空间位旋转并不容易。但是,arch/./include/asm/bitops.h中特定于arch的汇编程序可以用于用户空间位操作


当然,与编写一个小的通用位op-include文件来实现相同的结果相比,这可能不值得开发工作。

我尝试从/usr/src/linux-headers-$(uname-r)中包含各种内核头文件(在内核构建期间安装)使用
-I kernel\u header\u path
搜索路径,但都需要一个或多个其他内核头。它看起来像/include/linux/bitops.h是所有arch特定位操作的建议包含文件

因此,我同意@nos:使用内核代码进行用户空间位旋转并不容易。但是,arch/./include/asm/bitops.h中特定于arch的汇编程序可以用于用户空间位操作


OTOH,与编写一个小的通用位op include文件来获得相同的结果相比,它可能不值得开发工作。

我建议使用gcc。@David。请详细说明一下。我不明白gcc中内置了一整套原子位操作。例如,原子地获取当前值并执行逻辑l或当前值以获取新值。@David。如何使用它们。是否需要包含任何值headers@DavidSchwartz使用原子内置来执行位操作不是太过分了吗?我建议使用gcc。@David。你能详细说明一下吗?我不明白gcc内置了一整套原子位操作。供检查ple以原子方式获取当前值,并对当前值执行逻辑或运算以获取新值。@David。我如何使用它们。我需要包含任何值吗headers@DavidSchwartz使用原子内置程序只执行按位操作不是太过分了吗?您使用了
gcc-I
,但它仍然找不到linux/bitops.h?bitops.h通常是未作为/usr/include下的用户空间标头安装。使用它不像指定单个-I参数那么简单,因为许多纯内核标头需要内核构建机制设置所需的路径,依此类推到@brandy。ASM是避免由于C/C++要求而出现“未定义语句”和“非法程序”的方法(请参阅下面的答案)。您使用的是
gcc-I
,但它仍然找不到linux/bitops.h?bitops.h通常不作为/usr/include.I下的用户空间头安装
/**
 * rol64 - rotate a 64-bit value left
 * @word: value to rotate
 * @shift: bits to roll
 */
static inline __u64 rol64(__u64 word, unsigned int shift)
{
    return (word << shift) | (word >> (64 - shift));
}

/**
 * ror64 - rotate a 64-bit value right
 * @word: value to rotate
 * @shift: bits to roll
 */
static inline __u64 ror64(__u64 word, unsigned int shift)
{
    return (word >> shift) | (word << (64 - shift));
}

/**
 * rol32 - rotate a 32-bit value left
 * @word: value to rotate
 * @shift: bits to roll
 */
static inline __u32 rol32(__u32 word, unsigned int shift)
{
    return (word << shift) | (word >> (32 - shift));
}

/**
 * ror32 - rotate a 32-bit value right
 * @word: value to rotate
 * @shift: bits to roll
 */
static inline __u32 ror32(__u32 word, unsigned int shift)
{
    return (word >> shift) | (word << (32 - shift));
}

/**
 * rol16 - rotate a 16-bit value left
 * @word: value to rotate
 * @shift: bits to roll
 */
static inline __u16 rol16(__u16 word, unsigned int shift)
{
    return (word << shift) | (word >> (16 - shift));
}

/**
 * ror16 - rotate a 16-bit value right
 * @word: value to rotate
 * @shift: bits to roll
 */
static inline __u16 ror16(__u16 word, unsigned int shift)
{
    return (word >> shift) | (word << (16 - shift));
}

/**
 * rol8 - rotate an 8-bit value left
 * @word: value to rotate
 * @shift: bits to roll
 */
static inline __u8 rol8(__u8 word, unsigned int shift)
{
    return (word << shift) | (word >> (8 - shift));
}

/**
 * ror8 - rotate an 8-bit value right
 * @word: value to rotate
 * @shift: bits to roll
 */
static inline __u8 ror8(__u8 word, unsigned int shift)
{
    return (word >> shift) | (word << (8 - shift));
}