views:

1507

answers:

5

I want to make my own malloc/free so I can make a precise copying allocator.

Any gurus have any tips and suggestions?

I have a few questions for now:

  1. Should I just malloc large chunks of memory, and then distribute from that so I don't have to call the system calls?
  2. How are copying collectors usually done? I would imagine that this part is a bit complicated to do efficiently. My naive implementation would just malloc a block the size of the remaining objects which would require 2x the space.
+12  A: 
Charlie Martin
Since malloc implementations usually have a header with the data length, then wouldn't a movement simply involve a memcpy?
Unknown
Yup. But now, where is the pointer you gave the caller to malloc going to point?
Charlie Martin
A: 

Don't use idea #1, you'd be wasting a lot of resources that might potentially remain unused for the entire execution of an application, all the while stopping other processes from utilising it (depending on how much you plan to take).

dreamlax
Many OSs work on the basis that you only use memory if you actually use it, so staking out a large chunk of address space is harmless per se. In general, if writing your own allocator, you do want to limit system calls from it.
David Thornley
@ David, do you have more information on that comment? I wonder if its true.
Unknown
Yes, it is true. Look up how virtual memory works, or specifically how OS's use 'overcommit'.
janneb
Moreover, operating systems can allocate process only whole pages of memory. Most commonly, page size is 4kb. So, even if you want to allocate a single byte, OS has to give you 4kb (one page).
el.pescado
+1  A: 

It'd be helpful to know a little more about what you're trying to do. Is this for a single type? Or set of relatively uniform types?

  • Memory pools are a tried-and-true technique if you need to speed up allocs for a give type (one possible downside is you can be hopping all over memory, affecting cache performance)
  • Application-wide allocation techniques usually include rounding all allocs up to profile-driven bucket sizes so that you keep fragmentation down.
  • Multi-threaded apps usually have per-thread pools that can be alloc'd from without risking contention with other threads

The doc's for tcmalloc are pretty good at describing the current state of the art for these techniques.

There isn't too much complicated stuff here, but you're probably reinventing the wheel. There are a few open source libraries that do this.

aaron
+4  A: 

Here is a nice paper on this.

Dynamic Storage Allocation: A Survey and Critical Review

chappar
+5  A: 

Read about Doug Lea's malloc

http://g.oswego.edu/dl/html/malloc.html

iain
nice article, upvoted.
Unknown
Doug Lea was one of my favorite professors at SUNY Oswego. I learned a lot from his classes/books.
unforgiven3
wow how lucky was that. he is a concurrent god.
iain