OpenCL 的 Linux 函数拦截

发布于 2024-09-10 01:45:48 字数 314 浏览 0 评论 0原文

我对 C 还很陌生,所以要温柔。

我想使用Linux的库拦截方法,用我自己的库替换对OpenCL库的调用。据我所知,这可以使用 LD_PRELOAD 来完成。因此,我可以在我自己的库中重新实现 OpenCL 头文件中定义的 OpenCL 函数,然后可以对其进行链接。

问题是这个 OpenCL 标头还包含一些外部结构定义,例如

typedef struct _cl_mem * cl_mem;

未在 OpenCL 标头中定义。这些结构是否可能在 OpenCL 共享库中定义?如果不是,它们可能在哪里定义?

干杯

克里斯

I'm fairly new to C so be gentle.

I want to use the library interception method for Linux to replace calls to the OpenCL library with my own library. I understand that this can be done using LD_PRELOAD. So I can just re-implement the OpenCL functions as defined in the OpenCL header file within my own library which can then be linked against.

The problem is that this OpenCL header also contains some extern struct definitions, e.g.

typedef struct _cl_mem * cl_mem;

which are not defined within the OpenCL header. Is it possible these structs are defined within the OpenCL shared lib? If not, where might they be defined?

Cheers

Chris

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

他是夢罘是命 2024-09-17 01:45:48

该 typedef 声明了一个指向结构的类型,但其内容未声明。这意味着使用它的代码无法执行诸如检查其大小、复制结构或检查其内容之类的操作 - 它根本不知道它的大小。
这是 C 中创建不透明或私有类型的传统技术。您可以在 OpenCL 库中声明该结构,并且官方标头对该结构包含的内容没有限制。如果您需要的只是一个可以存储在指针本身中的 ID,那么它甚至可以是空的,尽管很少这样做。
标准 C 库中使用的相同技术的一个示例是 FILE 类型。它可能像整数文件描述符一样简单,也可能像包含整个文件系统状态的结构一样复杂;标准 C 代码不会知道。具体细节只有图书馆知道。

简而言之,您可以根据需要声明该结构 - 只要您实现处理该结构的每个函数即可。链接到您的库的程序从不处理该结构,仅处理指向它的指针。

That typedef declares a type pointing to a struct, the contents of which are undeclared. This means code using it can't do things like checking its size, copying the struct, or inspecting its contents - it simply has no idea what size it is.
This is a traditional technique in C to create an opaque, or private, type. You can declare the struct inside your OpenCL library, and the official header puts no restrictions on what that struct contains. It could even be empty, if all you need is an ID you can store in the pointer itself, though this is rarely done.
An example of the same technique used in the standard C library is the FILE type. It might be as simple as an integer file descriptor, or as complex as a struct containing the entire filesystem state; standard C code won't know. The particulars are known to the library only.

In short, you can declare that struct however you like - as long as you implement every function that handles that struct. The program that links to your library never handles the struct, only pointers to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文