LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Does Linux have anything similar to IRIX's mallocblksize? (https://www.linuxquestions.org/questions/programming-9/does-linux-have-anything-similar-to-irixs-mallocblksize-1112/)

mark_i 03-01-2001 10:56 PM

The malloc manpage on IRIX defines function mallocblksize as :

size_t mallocblksize (void *ptr);

mallocblksize - returns the actual size of the block pointed to by ptr. The returned size may be greater than the original requested size due to padding and alignment.

This function is not documented on my Mandrake 7.2 distribution, and I found nothing close in the headers. Is there anything on Linux that would serve the same purpose?

Any help is appreciated.

</\\/\\>

crabboy 03-02-2001 11:34 PM

Stumped me
 
I cracked out the Stevens book for this question and even it had no answers. The only way I could think of getting the size of the extra space allocated, would be to call mallinfo before and after you call. Not what you wanted huh?

Here is my sample program w/output.

#include <malloc.h>

main()
{
void *ptr;
void *ptr2;
struct mallinfo mall;
int total_size = 0;

mall = mallinfo( );
printf("Mallinfo.uordblks : %d\n", mall.uordblks );

ptr = malloc( 100 );

total_size = malloc_usable_size( ptr );
printf ("Size is : [%d]\n", total_size);

mall = mallinfo( );
printf("Mallinfo : %d\n", mall.uordblks );

ptr2 = malloc( 100 );

mall = mallinfo( );
printf("Mallinfo : %d\n", mall.uordblks );

free( ptr );
free( ptr2 );
}

# malloctest

Mallinfo.uordblks : 0
Size is : [100]
Mallinfo : 104
Mallinfo : 208

mark_i 03-03-2001 12:46 PM

Found it
 
Looking through the kernel I found that the whole size is :

((long*)ptr) - [1] - 1

so the implementation of mallocblksize on Linux looks like this:

size_t mallocblksize(ptr){
return ((long*)ptr) - [1] - 1;
}

Thanks for the help!


Iulian_B 08-12-2001 01:12 AM

Hi,

I saw your solution for mallocblksize and, as I am not a good programer, I am asking for your help, if you please.
The two files listed below contains calls to mallocblksize, as you can see.
I am running Red Hat 7.0 (w/ kernel 2.2.16), gcc, g77, libstdc++ from the original distribution, and libg++_2.8.1.3.
The two files belong to a SGI-designed program. The makefiles contains appropriate flags for the GNU compiler (I guess GNU for SGI).

My problem is: the compilation fails when the mallocblksize is encountered (is normal since mallocblksize is a characteristic function for IRIX).
Please, can you (or will you) tell me where should I introduce yours

>> size_t mallocblksize
>> {
>> return ((long*)ptr-[1]-1;
>> }

so the compilation process pass on?

Should these lines be introduced in a particular malloc.h, or perhaps, in a particulat stdlib.h?

Thanks for helping me,
Iulian

P.S. I found this topic searching with www.metacrawler.com, and I've got only 10 (ten) refferences.
P.S.2 SORRY for the very long post.

// this particular file was saved in PC format

// ===========================================
// this is Memory.C file
// ===========================================

#include <malloc.h>
#include <stdio.h>
#include <sys/types.h>
#include <Utils.d/Memory.h>

long long currentSize = 0;
long long deletedSize = 0;

void * operator new( size_t size )
{
void *ptr = malloc( size );

size_t allocatedMemory = mallocblksize(ptr);
currentSize += allocatedMemory;

//fprintf(stderr,"Allocate %14d bytes %14d bytes\n",
// allocatedMemory,currentSize);

return ptr;
}

void operator delete(void *p)
{
size_t deletedMemory = mallocblksize(p);
deletedSize += deletedMemory;
currentSize -= deletedMemory;
//fprintf(stderr,"De-allocate %14d bytes %14d bytes\n",
// deletedMemory,currentSize);
free(p);
}

long long
memoryUsed()
{
return currentSize;
}

long long
memoryDeleted()
{
return deletedSize;
}

// end of Memory.C file
// ==========================================


// ==========================================
// this is Memory.h file
// ==========================================

#ifndef _MEMORY_H_
#define _MEMORY_H_

// function to return memory used
long long memoryUsed();
long long memoryDeleted();

#endif

// end of Memory.h file
// ===========================================

mark_i 08-12-2001 12:10 PM

Hi Iulian,

Simply replace the implementation of operator new and operator delete with the versions below:

void * operator new( size_t size )
{
void *ptr = malloc( size );

size_t allocatedMemory = ((long*)ptr) - [1] - 1;
currentSize += allocatedMemory;

//fprintf(stderr,"Allocate %14d bytes %14d bytes\n",
// allocatedMemory,currentSize);

return ptr;
}

void operator delete(void *p)
{
size_t deletedMemory = ((long*)p) - [1] - 1;
deletedSize += deletedMemory;
currentSize -= deletedMemory;
//fprintf(stderr,"De-allocate %14d bytes %14d bytes\n",
// deletedMemory,currentSize);
free(p);
}

Iulian_B 08-14-2001 05:07 AM

I did so, and I've got something like this:

>> Memory.C:13: parse error in front of '['

twice (both for new and delete operators)

thanks anyway ...


All times are GMT -5. The time now is 03:32 PM.