高通 mct 里面充斥了宏#define MCT_OBJECT_CAST(obj) ((mct_object_t*)(obj)),对mct_object_t 的调用的理解可以最简单的参考如下的代码,mct_object_t 是如何在不同的结构体传递。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct _mct_object mct_object_t;
typedef struct _mct_stream mct_stream_t;
typedef struct _mct_module mct_module_t;
struct _mct_object {
unsigned int parentsnum;
unsigned int childrennum;
char* name;
};
struct _mct_module {
mct_object_t object;
unsigned int numsrcports;
};
struct _mct_stream {
mct_object_t object;
unsigned int streamid;
unsigned int frmaeid;;
};
void mct_object_set_parent(mct_object_t *object, mct_object_t *parent)
{
object->name = parent->name;
object->parentsnum = parent->parentsnum;
object->childrennum = parent->childrennum;
printf("stream %p module %p\n",object,parent);
}
int main()
{
mct_stream_t* stream = (mct_stream_t *)malloc(sizeof(mct_stream_t));
mct_module_t* module = (mct_module_t *)malloc(sizeof(mct_module_t));
printf("stream %p module %p\n",stream,module);
module->numsrcports = 1;
module->object.parentsnum = 1;
module->object.childrennum = 1;
module->object.name = "module";
stream->streamid = 2;
stream->frmaeid = 2;
stream->object.parentsnum = 2;
stream->object.childrennum = 2;
stream->object.name= "stream";
printf("stream->object.parentsnum=%d stream->object.childrennum=%d stream->object.name=%s\n",stream->object.parentsnum,stream->object.childrennum,stream->object.name);
mct_object_set_parent((mct_object_t*)stream,(mct_object_t*)module);
printf("stream->object.parentsnum=%d stream->object.childrennum=%d stream->object.name=%s\n",stream->object.parentsnum,stream->object.childrennum,stream->object.name);
printf("stream %p module %p\n",stream,module);
free(stream);
free(module);
return 0;
}
打印 log 如下
stream 0x20fd010 module 0x20fd030
stream->object.parentsnum=2 stream->object.childrennum=2 stream->object.name=stream
stream 0x20fd010 module 0x20fd030
stream->object.parentsnum=1 stream->object.childrennum=1 stream->object.name=module
stream 0x20fd010 module 0x20fd030