malloc

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
justin
Member
Member
Posts: 43
Joined: Sun Jan 11, 2009 2:09 pm

malloc

Post by justin »

I am at the point where I need to write a malloc function. If I understand things correctly, most malloc implementations store information on free and allocated blocks of memory in a dynamically growing structure such as a linked list. What I don't understand is where to store these structures. Do most people simply designate some place in the virtual address space to store this information? Or is there a more clever solution?

Any advice would be appreciated
Thanks,
Justin
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: malloc

Post by bewing »

Unfortunately, there are an infinity of solutions. The one that I thought was the most clever (and that I am using) is to store the malloc list information in a tiny structure just before the beginning of the malloc'ed memory area. You allocate space for the list entry at the same time as you create the requested mem allocation.

So, for example, when a program issues a free(malloc_mem_ptr), all you have to do is say malloc_hdr_struct_ptr = malloc_mem_ptr - malloc_hdr_struct_size;
And then you have instant access to the list entry for that (formerly) malloc'ed area, so you can unlink it from the list.

This way, the programmer is free to completely trash his entire malloc list structure by accident. Some people (such as C++ programmers) say that it is the system's responsibility to protect programmers from themselves. I disagree completely -- which is one reason I chose this design.
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: malloc

Post by Creature »

bewing wrote:This way, the programmer is free to completely trash his entire malloc list structure by accident. Some people (such as C++ programmers) say that it is the system's responsibility to protect programmers from themselves. I disagree completely -- which is one reason I chose this design.
I'm a C++ programmer and I think you have a point ;). I do think the operating system needs at least some security of the memory, but not doing it is a way of making the programmer more aware of what he/she is doing. Some of today's programmers don't even care (in C/C++) about deleting/freeing their allocated memory anymore, as the 'OS will clean it up', but this is a good way to keep programmers alert.

But anyhow, if you don't have any protection and the programmer can easily mess something up. That also means malware can do it, which is not really the programmer's fault.

I guess it's how you look at it.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
User avatar
bewing
Member
Member
Posts: 1401
Joined: Wed Feb 07, 2007 1:45 pm
Location: Eugene, OR, US

Re: malloc

Post by bewing »

In a properly designed OS, no application can trash another application's address space. This is what makes an entire system safe from bad programmers and malware. But that rule says nothing about a program (including malware) trashing its own address space. Who cares if a badly written program crashes itself? Not me.
User avatar
Love4Boobies
Member
Member
Posts: 2111
Joined: Fri Mar 07, 2008 5:36 pm
Location: Bucharest, Romania

Re: malloc

Post by Love4Boobies »

bewing wrote:In a properly designed OS, no application can trash another application's address space. This is what makes an entire system safe from bad programmers and malware. But that rule says nothing about a program (including malware) trashing its own address space. Who cares if a badly written program crashes itself? Not me.
Then what's the point of say, implementing support for monitors in a multithreaded OS? It's up to the application to handle its own affairs. I, as a programmer, if given the choice, would choose an OS with as much protection as possible.
"Computers in the future may weigh no more than 1.5 tons.", Popular Mechanics (1949)
[ Project UDI ]
User avatar
Creature
Member
Member
Posts: 548
Joined: Sat Dec 27, 2008 2:34 pm
Location: Belgium

Re: malloc

Post by Creature »

bewing wrote:In a properly designed OS, no application can trash another application's address space. This is what makes an entire system safe from bad programmers and malware. But that rule says nothing about a program (including malware) trashing its own address space. Who cares if a badly written program crashes itself? Not me.
You have a good point there. I don't see why the OS should protect an application from itself, as long as it doesn't (try to) trash anything else in the OS, just let it crash on its own space and the OS will clean it up if needed for new applications afterwards.
When the chance of succeeding is 99%, there is still a 50% chance of that success happening.
ru2aqare
Member
Member
Posts: 342
Joined: Fri Jul 11, 2008 5:15 am
Location: Hungary

Re: malloc

Post by ru2aqare »

Love4Boobies wrote:Then what's the point of say, implementing support for monitors in a multithreaded OS? It's up to the application to handle its own affairs. I, as a programmer, if given the choice, would choose an OS with as much protection as possible.
Then, would you rather switch to kernel mode and let the kernel allocate a chunk of memory for you each time you call malloc and related functions - and lose serious performance? Even if you did that, it would still not solve the problem of the application overwriting its own memory blocks.
jal
Member
Member
Posts: 1385
Joined: Wed Oct 31, 2007 9:09 am

Re: malloc

Post by jal »

bewing wrote:to store the malloc list information in a tiny structure just before the beginning of the malloc'ed memory area. (...) This way, the programmer is free to completely trash his entire malloc list structure by accident.
I use a slightly different approach, and have the free/occupied memory list at the top of the mallocable memory area (growing down). I think I copied that idea from Turbo Pascal, but I may be wrong. I just think it's very inelegant interleaving data and meta data, but that's just me I guess.


JAL
Post Reply