如何在x86_64上的前4GB中“分配" [英] How to 'malloc' within first 4GB on x86_64

查看:59
本文介绍了如何在x86_64上的前4GB中“分配"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

平台:Mac OS X
郎:Obj-C/C

Platform: Mac OS X
Lang: Obj-C/C

是否有可能使'malloc'在进程地址空间的前4GB内分配内存?

Is it possible to somehow make 'malloc' to allocate memory within first 4GB of process address space ?

我正在仿真i386堆栈,需要确保地址位于允许的32位范围内.

I'm emulating i386 stack and need to guarantee that address will lie within allowed 32bit range.

使用mmap + MAP_FIXED要求在任何"malloc"之前保留内存,这不是很方便.带有约束的"malloc"将更加方便.

Using mmap+MAP_FIXED requires to RESERVE memory before any 'malloc', it's not quite convenient. 'malloc' with constraints would be much more handy.

推荐答案

不可能,除非您编写自己的malloc实现代码(或深入研究某些现有malloc的实现细节,然后对其进行更改以适合您的需求).

It is not possible, unless you code your own implementation of malloc (or dive into the implementation details of some existing malloc then change it to suit your needs).

大多数malloc -s实现使用系统mmap(或sbrk)系统调用(请参见Linux上的 syscalls(2),和内存(3) (对于MacOSX),并且这些地址给出了任意的内存地址(例如,由于 ASLR ,有用).

Most malloc-s implementations are using the system mmap (or sbrk) syscalls (see e.g. syscalls(2) on Linux, and memory(3) for MacOSX), and these are giving some arbitrary memory addresses (e.g. because of ASLR, which is very useful).

PS.在Linux上,您可以将 mmap(2)MAP_32BIT,但MacOSX

PS. On Linux, you might use mmap(2) with MAP_NORESERVE or MAP_32BIT, but MacOSX mmap(2) does not seem to have them.

这篇关于如何在x86_64上的前4GB中“分配"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆