1. 实验代码
#include <stdio.h> #include <stdlib.h> #include <string.h> #define NR_CPUS 32 #define BITS_PER_BYTE 8 #define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE) #define __KERNEL_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) #define BITS_TO_LONGS(nr) __KERNEL_DIV_ROUND_UP(nr, BITS_PER_TYPE(long)) //include/linux/types.h #define DECLARE_BITMAP(name, bits) unsigned long name[BITS_TO_LONGS(bits)] //unsigned long bits[1]; //include/linux/cpumask.h typedef struct cpumask { DECLARE_BITMAP(bits, NR_CPUS); } cpumask_t; //cpumask 和 cpumask_t 的定义 typedef struct cpumask cpumask_var_t[1]; //cpumask_var_t == struct cpumask[1] /* //编译预处理后: struct cpumask { unsigned long bits[1]; } cpumask_t == struct cpumask cpumask_var_t == struct cpumask[1] */ static void cpumask_copy(struct cpumask *dstp, struct cpumask *srcp) { memcpy(dstp, srcp, sizeof(*dstp)); } static void set_bit(int nr, unsigned long *addr) { addr[nr / BITS_PER_TYPE(long)] |= 1UL << (nr % BITS_PER_TYPE(long)); } static int test_bit(int nr, unsigned long *addr) { return 1UL & (addr[nr / BITS_PER_TYPE(long)] >> (nr & (BITS_PER_TYPE(long)-1))); } static void cpumask_set_cpu(int cpu, struct cpumask *dstp) { set_bit(cpu, dstp->bits); } static int cpumask_test_cpu(int cpu, struct cpumask *dstp) { return test_bit(cpu, dstp->bits); } static void cpumask_clear(struct cpumask *dstp) { memset(dstp->bits, 0, sizeof(dstp->bits)); } static void struct_cpumask_test(int num) { int i; struct cpumask mask; cpumask_clear(&mask); for (i = 0; i < num; i++) { cpumask_set_cpu(i, &mask); } printf("struct_cpumask mask=0x%lx\n", mask.bits[0]); //0xff } static void cpumask_t_test(int num) { int i; cpumask_t t_mask; cpumask_clear(&t_mask); for (i = 0; i < num; i++) { cpumask_set_cpu(i, &t_mask); } printf("cpumask_t t_mask=0x%lx\n", t_mask.bits[0]); //0xff } static void cpumask_var_t_test(int num) { int i; cpumask_var_t var_t_mask; cpumask_clear(var_t_mask); //没有& for (i = 0; i < num; i++) { cpumask_set_cpu(i, var_t_mask); //没有& } printf("cpumask_var_t var_t_mask=0x%lx\n", var_t_mask[0].bits[0]); //0xff } void main(int argc, char *argv[]) { int num = 8; if (argc == 2) { num = atoi(argv[1]); } struct_cpumask_test(num); cpumask_t_test(num); cpumask_var_t_test(num); }
测试:
~/origin_tmp/8.cpumask$ gcc cpumask_test.c -o pp ~/origin_tmp/8.cpumask$ ./pp struct_cpumask mask=0xff cpumask_t t_mask=0xff cpumask_var_t var_t_mask=0xff ~/origin_tmp/8.cpumask$ ./pp 1 struct_cpumask mask=0x1 cpumask_t t_mask=0x1 cpumask_var_t var_t_mask=0x1 ~/origin_tmp/8.cpumask$ ./pp 2 struct_cpumask mask=0x3 cpumask_t t_mask=0x3 cpumask_var_t var_t_mask=0x3
标签:struct,int,mask,cpumask,var,bits From: https://www.cnblogs.com/hellokitty2/p/16919268.html