指针的灵活使用(分析Device tree案例)
2014-08-28 17:36
501 查看
/** 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.
* 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.
相关文章推荐
- 指针的灵活使用(分析Device tree案例)
- C/C++指针的指针(**p)和指针的引用(*&)使用案例分析
- C/C++指针的指针(**p)和指针的引用(*&)使用案例分析
- 指针与const限定符的使用分析
- undo 表空间使用案例分析(使用率100%,enq:US-contention,释放undo)
- Android 数据存储和文件使用案例分析
- Android 数据存储和文件使用案例分析
- Delphi7中使用TXMLDocument遍历子节点出现读空指针错误的分析
- spring心得2--bean的生命周期@Spring监听器的作用@Spring初始化容器案例分析@web项目使用
- realloc 使用详解(分析realloc invalid pointer、指针无效等错误)
- 【典型案例】NASA使用MODIS 和在线影像分析技术实现全球洪水监测
- realloc 使用详解(分析realloc invalid pointer、指针无效等错误)
- C#中使用XML——基于DOM的案例分析
- undo 表空间使用案例分析(UNDOTBS1使用率100%,enq:US-contention,释放undo)
- undo 表空间使用案例分析(UNDOTBS1使用率100%,enq:US-contention,释放undo)
- realloc 使用详解(分析realloc invalid pointer、指针无效等错误)
- C++中使用基类指针操作子类数组的分析
- .NET如何使用内存---餐馆案例分析[转载]
- 如何更有效使用 Rational AppScan 扫描大型网站,第 2 部分: 案例分析
- html中textarea的使用及常见问题及案例分析