指针的灵活使用(分析Device tree案例)

/** Checks if the given "compat" string matches one of the strings in
 * the device's "compatible" property
 */

int of_device_is_compatible(const struct device_node *device,

const char *compat)
{
const char* cp;
int cplen, l;


cp = of_get_property(device, "compatible", &cplen);
if (cp == NULL)
return 0;
while (cplen > 0) {
if (of_compat_cmp(cp, compat, strlen(compat)) == 0)
return 1;
l = strlen(cp) + 1;
cp += l;
cplen -= l;
}


return 0;

}


其中of_get_propert()函数的实现:

/*
 * Find a property with a given name for a given node
 * and return the value.
 */
const void *of_get_property(const struct device_node *np, const char *name,
int *lenp)
{
struct property *pp = of_find_property(np, name, lenp);


return pp ? pp->value : NULL;
}

其中property结构为:

struct property {
char *name;
int length;
void *value;
struct property *next;
unsigned long _flags;
unsigned int unique_id;
};


其中为什么of_device_is_compatible()函数为什么要执行语句:cp += l;

原因有两个:

1,of_get_property()函数的返回值类型是void *;

2,strlen(cp) + 1的含义为:读取cp指针所指向的一片数据区,遇到'/0',结束读值;即总长度为实际长度加‘/0’结束符,+1.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值