我想在自己的虚拟内存页面上分配一个NSObject
。这可能吗?如果我们仍然可以使用NSZone
,这似乎很琐碎,但已弃用了它们。我尝试使用NSZoneFromPointer
,但始终返回nil
。根据Tips for Allocating Memory:
对于较大的内存分配,其中的大小不止几个
虚拟内存页面,malloc自动使用vm_allocate
例程获取请求的内存。
因此,看来我应该能够使我的对象像这样大到几页:
@interface MyObject : NSObject {
int[40960]; // 4096 is the default page size, so this is 10 pages.
}
@implementation MyObject
@end
我知道这是一个hack,但是会持续工作吗?有没有更好的办法?
最佳答案
从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
是答案。
关于ios - 如何在iOS中将NSObject分配到其自己的虚拟内存页面上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19362430/