LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-01-2001, 10:56 PM   #1
mark_i
LQ Newbie
 
Registered: Mar 2001
Location: Dallas
Posts: 3

Rep: Reputation: 0

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.

</\\/\\>
 
Old 03-02-2001, 11:34 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 121Reputation: 121
Thumbs down 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
 
Old 03-03-2001, 12:46 PM   #3
mark_i
LQ Newbie
 
Registered: Mar 2001
Location: Dallas
Posts: 3

Original Poster
Rep: Reputation: 0
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!

 
Old 08-12-2001, 01:12 AM   #4
Iulian_B
LQ Newbie
 
Registered: Aug 2001
Posts: 18

Rep: Reputation: 0
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
// ===========================================
 
Old 08-12-2001, 12:10 PM   #5
mark_i
LQ Newbie
 
Registered: Mar 2001
Location: Dallas
Posts: 3

Original Poster
Rep: Reputation: 0
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);
}
 
Old 08-14-2001, 05:07 AM   #6
Iulian_B
LQ Newbie
 
Registered: Aug 2001
Posts: 18

Rep: Reputation: 0
Unhappy

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
anything similar to analogx in linux ? majalee Linux - Networking 0 02-14-2005 01:29 AM
thanks you guys.I need similar with flashget in linux. muyu Linux - Software 2 04-07-2004 07:22 AM
Any games similar to these on Linux? eBopBob Linux - Games 3 02-06-2004 11:37 PM
Is there a Linux program similar to MS Access? randon Linux - General 9 01-16-2004 01:27 PM
SmoothWall for linux, or anything similar? bripage Linux - General 1 10-03-2002 09:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration