我想在自己的虚拟内存页面上分配NSObject
。这可能吗?如果我们仍然可以使用NSZone
s似乎微不足道,但它们已被弃用。我尝试使用NSZoneFromPointer
,但它始终返回nil
。根据{{3}}:
对于大内存分配,其中大的内容不止一些 虚拟内存页面,malloc自动使用vm_allocate 例程以获取所请求的内存。
因此,似乎我应该能够让我的对象像几页一样大:
@interface MyObject : NSObject {
int[40960]; // 4096 is the default page size, so this is 10 pages.
}
@implementation MyObject
@end
我意识到这是一个黑客攻击,但它会不断运作?还有更好的方法吗?
答案 0 :(得分:2)
来自twitter:
objc_constructInstance(...)可能会让你做你想做的......
来自<objc/runtime.h>
:
/**
* Creates an instance of a class at the specific location provided.
*
* @param cls The class that you wish to allocate an instance of.
* @param bytes The location at which to allocate an instance of \e cls.
* Must point to at least \c class_getInstanceSize(cls) bytes of well-aligned,
* zero-filled memory.
*
* @return \e bytes on success, \c nil otherwise. (For example, \e cls or \e bytes
* might be \c nil)
*
* @see class_createInstance
*/
OBJC_EXPORT id objc_constructInstance(Class cls, void *bytes)
__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_0)
OBJC_ARC_UNAVAILABLE;
看起来objc_constructInstance
就是答案。
答案 1 :(得分:-2)
我不认为作为用户级应用程序,您应该对虚拟内存分配有任何控制权。 iOS中的应用程序位于沙盒中。