views:

322

answers:

2

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).

A: 

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.

dirkgently
Actually malloc and sbrk/virtualalloc are conceptually different. Malloc usually comes with overhead like size headers etc. I see no reason why someone couldn't make a version that allocates a single resizable global slab of memory
Unknown
+1  A: 

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.

Wergan
Well I know how to do defines, but virtualalloc and sbrk are not 100% equivalent functions.
Unknown