C语言中的#line宏

在 C 语言中, __FILE_ 表示当前正在被编译的源文件名字的字符串字面值,__LINE__ 表示当前源文件行数的十进制常量,而 #line 可以改变上述两个宏的内容,即为其指定新的值。主要有以下两种形式:

#line linenum

#line linenum filename
 
上述调用将会使编译器认为:下一行源代码的行号是从 linenum 开始,并且,当前的输入文件有 filename 给出。以下为示例代码:
#include <stdio.h>

int main(void)
{
    printf("-- %s: %d -- \n", __FILE__, __LINE__);
#line 201 "foo.c"
    printf("-- %s: %d -- \n", __FILE__, __LINE__);
#line 101 "foo.c"
    printf("-- %s: %d -- \n", __FILE__, __LINE__);

    return 0;
}

运行结果:

$ gcc -o main ./main.c
$ ./main
-- ./main.c: 5 --
-- foo.c: 201 --
-- foo.c: 101 --
 
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 语言,获取 cacheline 的大小需要使用特定的操作系统相关的函数或者编译器相关的。以下是一些常见的方法: 1. 使用 Linux 系统的 sysconf() 函数获取 cacheline 大小: ```c #include <unistd.h> #include <stdio.h> int main() { long cacheline_size = sysconf(_SC_LEVEL1_DCACHE_LINESIZE); printf("cacheline size on this system is %ld bytes\n", cacheline_size); return 0; } ``` 2. 使用 GCC 编译器的 __builtin_cpu_supports() 函数获取 cacheline 大小: ```c #include <stdio.h> #include <stdint.h> #include <stdbool.h> int main() { bool has_cacheline_size = false; uint32_t size = 0; if (__builtin_cpu_supports("avx")) { size = __builtin_ia32_cpuid_count(0x80000006, 0, &eax, &ebx, &ecx, &edx); if (size && (eax & 0xff) > 0) { has_cacheline_size = true; size = (eax & 0xff) * 64; } } if (has_cacheline_size) { printf("cacheline size on this system is %u bytes\n", size); } else { printf("cannot determine cacheline size on this system\n"); } return 0; } ``` 注意:这个方法需要编译器支持 AVX 指令集。 3. 使用 Windows 系统的 GetLogicalProcessorInformation() 函数获取 cacheline 大小: ```c #include <windows.h> #include <stdio.h> int main() { PSYSTEM_LOGICAL_PROCESSOR_INFORMATION buffer = NULL; DWORD length = 0; BOOL result = GetLogicalProcessorInformation(buffer, &length); if (!result && GetLastError() == ERROR_INSUFFICIENT_BUFFER) { buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION)malloc(length); result = GetLogicalProcessorInformation(buffer, &length); } if (result) { DWORD offset = 0; while (offset < length) { PSYSTEM_LOGICAL_PROCESSOR_INFORMATION info = buffer + offset / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); if (info->Relationship == RelationCache && info->Cache.Level == 1 && info->Cache.Type == CacheData) { printf("cacheline size on this system is %u bytes\n", info->Cache.LineSize); break; } offset += sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION); } } else { printf("cannot determine cacheline size on this system\n"); } free(buffer); return 0; } ``` 注意:这个方法需要在 Windows 系统上运行。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值