gcc 中的大尺寸指针

发布于 2025-01-04 21:18:32 字数 271 浏览 1 评论 0原文

我想在 gcc 中定义一个大尺寸的指针(64 位或 128 位),它不依赖于平台。 我认为 MSDN 中有类似 __ptr128 或 __ptr64 的东西。

sizeof(__ptr128) is 16 bytes.
sizeof(__ptr64 ) is 8  bytes.

是否可以?

当您在 64 位操作系统中使用需要 8 字节指针参数的内核函数并且您有一个使用 32 位地址的 32 位应用程序并且您想要使用此内核函数时,它会很有用。

I want to define a great size pointer(64 bit or 128 bit) in gcc which is not depend on platform.
I think there is something like __ptr128 or __ptr64 in MSDN.

sizeof(__ptr128) is 16 bytes.
sizeof(__ptr64 ) is 8  bytes.

is it possible?

it can be useful when you use kernel functions in 64 bit OS which requires 8 bytes pointer argument and you have a 32 bit application which uses 32 bits address and you want to use this kernel function.

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

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

发布评论

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

评论(4

无人问我粥可暖 2025-01-11 21:18:32

你的问题没有意义。根据定义,指针是指向某事物的内存地址 - 大小必须取决于平台。如何在支持 64 位寻址的硬件平台上取消引用 128 位指针?!

您可以创建 64 或 128 位,但指针与底层硬件的内存寻址方案直接相关。

编辑

通过您的附加声明,我想我明白您想要做什么。不幸的是,我怀疑这是可能的。如果您要使用的内核函数采用 64 位指针参数,则它很可能是 64 位函数(除非您正在为某些不寻常的硬件进行开发)。

尽管技术上可以将 64 位指令混合到 32 位可执行文件中,但没有编译器实际上会让您这样做。 64 位 API 调用将使用 64 位代码、64 位寄存器和 64 位堆栈 - 编译器和操作系统管理从 32 位环境到 64 位环境的任意切换将非常尴尬环境。

您应该考虑寻找 32 位环境的等效 API。也许您可以发布您想要使用的内核函数原型(名称+参数),有人可以帮助您找到更好的解决方案。

为了避免混淆,MSDN 中的 __ptr64 与平台无关:

在 32 位系统上,使用 __ptr64 声明的指针会被截断为
32位指针。

Your question makes no sense. Pointers, by definition, are a memory address to something - the size must depend upon the platform. How would you dereference a 128-bit pointer on a hardware platform supporting 64-bit addressing?!

You can create 64 or 128-bit values, but a pointer is directly related to the memory addressing scheme of the underlying hardware.

EDIT

With your additional statement, I think I see what you're trying to do. Unfortunately, I doubt it's possible. If the kernel function you want to use takes a 64-bit pointer argument, it's highly likely to be a 64-bit function (unless you're developing for some unusual hardware).

Even though it's technically possible to mix 64-bit instructions into a 32-bit executable, no compiler will actually let you do this. A 64-bit API call will use 64-bit code, 64-bit registers and a 64-bit stack - it would be extremely awkward for the compiler and operating system to manage arbitrary switching from a 32-bit environment to a 64-bit environment.

You should look at finding the equivalent API for a 32-bit environment. Perhaps you could post the kernel function prototype (name+parameters) you want to use and someone could help you find a better solution.

Just so there's no confusion, __ptr64 in MSDN is not platform independent:

On a 32-bit system, a pointer declared with __ptr64 is truncated to a
32-bit pointer.

就此别过 2025-01-11 21:18:32

无法发表评论,但不能在“32 位可执行文件”中使用 64 位指令的声明具有误导性,因为“32 位可执行文件”的定义需要解释。如果您指的是使用 32 位指针的可执行文件,那么在使用 32 位指针的同时,没有任何内容表明您不能使用操作 64 位值的指令。处理器不知道其中的区别。

Linux 甚至支持一种可以拥有 32 位用户空间和 64 位内核空间的模式。因此,每个应用程序都可以访问 4GB RAM,但系统可以访问更多。这将指针的大小保持在 4 个字节,但不限制 64 位数据操作的使用。

Can't comment, but the statement that you can't use 64 bit instructions in a "32 bit executable" is misleading since the definition of "32 bit executable" is subject to interpretation. If you mean an executable that uses 32 bit pointers, then there is nothing at all that says you can't use instructions that manipulate 64 bit values while using 32 bit pointers. The processor doesn't know the difference.

Linux even supports a mode where you can have a 32 bit userspace and a 64 bit kernel space. Thus, each app has access to 4GB of RAM, but the system can access much more. This keeps the size of your pointers down to 4 bytes but does not restrict the use of 64 bit data manipulations.

亽野灬性zι浪 2025-01-11 21:18:32

我迟到了,但这个问题在嵌入式平台上很有意义。

如果将 CPU 与同一 SOC 中的一些附加加速器组合在一起,它们不一定需要具有相同的地址空间或地址空间大小

对于加速器中的固件,您需要从 CPU 和加速器的角度来看覆盖其地址空间的指针。它们的大小不一定相同。

例如,对于64位CPU和32位加速器,固件的指针可以覆盖32位长的地址空间,CPU的指针覆盖64位地址空间。 C 没有两个或多个 void * 类型,具体取决于您要通信的地址空间。

人们通常通过将 void * 转换为 uintN_t 来解决这个问题,其中 N 根据需要大小,并在系统的不同部分之间传递。

I'm late to the party but the question makes quite a lot of sense in embedded platforms.

If you combine a CPU with some additional accelerators in the same SOC, they don't necessarily need to have the same address space or address space size.

For the firmware in the accelerator you would want pointers that cover its address space from the CPU and the accelerator's perspective. They are not necessarily the same size.

For example, with a 64 bit CPU and a 32 bit accelerator, the pointer for the firmware can cover 32 bit long address space and the pointer for CPU covers 64 bit address space. C does not have two or more void * types depending on the address spaces you want to talk to.

People generally solve this by casting void * to uintN_t with N as large as needed and passing this around between different parts of the system.

﹎☆浅夏丿初晴 2025-01-11 21:18:32

没有,因为 gcc 不是为嵌入式体系结构设计的。在某些架构中,存在多个大小的指针,例如 m16c:ram 有 16 位地址,而 rom(flash) 在同一地址空间中有 20 位地址。较小的指针的性能和大小使用更好。

There is none, because gcc was not designed for embedded architectures. There are architectures where multiple sized pointers exist like for example m16c: ram has 16 bit addresses and rom(flash) has 20 bit addresses in the same address space. The performance and size usage is better for smaller pointers.

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