简体   繁体   English

跨平台内存分配器sbrk / virtualalloc

[英]Cross-platform memory allocator sbrk/virtualalloc

I am wondering if there is a cross-platform allocator that is one step lower than malloc/free. 我想知道是否有一个比malloc / free低一级的跨平台分配器。

For example, I want something that would simply call sbrk in Linux and VirtualAlloc in Windows (There might be two more similar syscalls, but its just an example). 例如,我想要在Linux中简单地调用sbrk,而在Windows中简单地调用VirtualAlloc(可能还有两个类似的syscall,但这只是一个例子)。

I'm not familiar with the functions in question but: 我对所讨论的功能不熟悉,但:

#if defined (__WIN32__)
  #define F(X) VirtualAlloc(X)
#elif defined (__LINUX__) /* or whatever linux's define is */
  #define F(X) sbrk(X)
#endif

Not sure if the syntax is 100% (I'm new to macros & c), but the general idea should work. 不知道语法是否为100%(我是宏和c语言的新手),但一般的想法应该可行。

C gives you malloc and free , C++ adds new , new[] , delete and delete[] and the placement forms in addition to what C provides. C给您mallocfree ,C ++除了C提供的内容外,还添加了newnew[]deletedelete[]以及放置形式。

Anything more and you are out of the realms of the language proper. 仅此而已,您就超出了适当语言的范围。 You are either treading in OS-land or murking in assembler. 您要么在OS领域涉足,要么在汇编器中默默无闻。 There is no question of such things being cross platform. 毫无疑问,这种事情是跨平台的。

I am wondering what good would it do if such allocator existed? 我想知道如果存在这样的分配器将会有什么好处?

You could implement your own malloc/free without worrying about the underlying OS 您可以实现自己的malloc / free而不用担心底层操作系统

And you'd want another cross-platform solution to implement this and another ... you get the point. 并且您想要另一个跨平台的解决方案来实现这一点和另一个...您明白了。 This is not a viable scheme. 这不是一个可行的方案。

I recently Found this article: 我最近发现了这篇文章:

http://www.genesys-e.org/jwalter//mix4win.htm http://www.genesys-e.org/jwalter//mix4win.htm

It basically implements sbrk() under Windows using VirtualAlloc. 它基本上使用VirtualAlloc在Windows下实现sbrk()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM