Unknown
Unknown

Reputation: 46821

Cross-platform memory allocator sbrk/virtualalloc

I am wondering if there is a cross-platform allocator that is one step lower than 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).

Upvotes: 0

Views: 2382

Answers (3)

Roman Pfneudl
Roman Pfneudl

Reputation: 717

I recently Found this article:

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

It basically implements sbrk() under Windows using VirtualAlloc.

Upvotes: -1

Tabitha
Tabitha

Reputation: 2603

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.

Upvotes: 2

dirkgently
dirkgently

Reputation: 111306

C gives you malloc and free, C++ adds new, new[], delete and delete[] and the placement forms in addition to what C provides.

Anything more and you are out of the realms of the language proper. You are either treading in OS-land or murking in assembler. 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

And you'd want another cross-platform solution to implement this and another ... you get the point. This is not a viable scheme.

Upvotes: 0

Related Questions