Sign in to follow this  

getting size of malloced memory area

This topic is 4835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Currently im working on implementing the memory allocator classes... for now I'd like to just get them in basic working order so I can work on other parts of my library. Is there a function that will give me the size of a malloced area for GCC/Cygwin setup? aka:
void * memory = malloc( 100 );
//...
size_t size_of_memory = some_magic_function( memory ); //<--- this
If not, I'll need to either do a workaround by returning "fake" values from the free/alloc functions, with the size prefixed (since this is what free/malloc does anyways on some systems), or go ahead and implement the finer details of my engine (which I'd really rather not deal with). The problem exists with current containers assuming that a class may be moved trivialy - it dosn't matter if it changes position in memory. For a few classes this isn't true, as it's (de)constructors (un)register it's address with (a) manager(s) class(es) (ghesuntight). With a little template magic (in the namespace ::industry::magic::... and the structs: ::industry::traits< ... >) the compiler reduces my code to a realloc in the case that ::industry::traits< ClassInQuestion>::trivial_move is of type ::industry::magic::true_t. And yes, I'm aware im mostly reinventing the wheel in the form of the STL (and prehaps parts of boost). That said, I like to pretend im implementing it better this time ;-).

Share this post


Link to post
Share on other sites
Quote:
Original post by KulSeran
If you malloc, calloc, realloc the memblock then
_msize( your_mem_pointer )

will give you the size_t of the block
Keep in mind...

Holy windows-specific, batman!

Share this post


Link to post
Share on other sites
Keep in mind that "size requested" and "size allocated" need not be the same. I personally don't know what systems "over" allocate, but the implementation is allowed to return more memory than requested. Also, I don't know what happens on lazy allocation schemes when you try to see how much has been allocated but it hasn't actually been allocated yet. Things to keep in mind. The most common advice I've seen on the subject is "You allocated it, so you should know how much has been allocated". But, then, this is all probably the pedant talking, plus it's late...

Share this post


Link to post
Share on other sites
I don't know about windows, but many Unix flavours including mac OS X (my dev machine), round the requested size up to (and here you might want to check) either 5% greater or the nearest power-of-two. But it is not necessarily enough memory for an entire additional object, so I would suggest storing the size you requested, and looking-up that.

SwiftCoder

Share this post


Link to post
Share on other sites
You could write your own version of malloc and free like this:

void* MyMalloc(size_t size)
{
size_t* pRet;

pRet = (size_t*)malloc(size+4);
*pRet = size;
return pRet+1;
}

void MyFree(void* pMem)
{
size_t* pRealMem = ((size_t*)pMem)-1;

free(pRealMem);
}

size_t GetAllocSize(void* pMem)
{
size_t* pRealMem = ((size_t*)pMem)-1;

return *pRealMem;
}

#define malloc MyMalloc
#define free MyFree


You simply allocate a bit more space to hold the allocation size, and use the macros to convert calls to malloc() and free() to calls to your own function. You can then use GetAllocSize() to see how much memory was allocated.
This should work fine, so long as malloc() doesn't align data (I don't *think* it does). And you should probably override calloc() and any other functions that return a pointer that should be freed with free(), if you intend to use them.

Share this post


Link to post
Share on other sites
Quote:
Original post by Evil Steve
This should work fine, so long as malloc() doesn't align data (I don't *think* it does).

Again, this seems to depend on the system, BSD/Mac and I would guess most flavours of unix align malloc'd memory.

SwiftCoder

Share this post


Link to post
Share on other sites
so the description of:

size_t _msize( void * )


would be to return the actual amount of area allocated, not "the amount of memory requested"?

Guess my temporary code will probably crash in evil ways then (since I'm using it to determine where to call dtors and ctors). I'll go ahead and implement Evil Steve's solution (the workaround I mentioned in my first post).

Quote:
Originally posted by Way Walker
the most common advice I've seen on the subject is "You allocated it, so you should know how much has been allocated"


Only problem is my alloc/free/realloc functions take the direct pointer (ala C (re)alloc/free or C++ new/delete)... which leaves me either the workaround, or an implementation-dependant-solution for when the implementation allready stores the data I need (making the workaround as redundant as:

struct
{
size_t size;
size_t size; //dosn't even compile ;_;
//...
}

Share this post


Link to post
Share on other sites

This topic is 4835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this