Re: C is too old? opinions?
- From: Logan Shaw <lshaw-usenet@xxxxxxxxxxxxx>
- Date: Fri, 14 Jul 2006 01:14:30 GMT
Rob Thorpe wrote:
Dynamic memory allocation has many of the same problems as GC for
real-time.
Lets say malloc and free work by keeping a single linked list of free
space.
That means that in the worst case one of malloc or free must walk the
whole list to find space. The list could be very long, making the
worst case bad and dependent on the size of memory.
To make it better you could use a malloc with several buckets for
different sizes of allocation, this is like a chaining hash map. Each
bucket has a free list for that size of allocation. Then the worst
case is the length of one of these bucket lists. But this has problems
too. Imagine if all the allocations are in the smallest bucket, the
problem then reduces to the "single linked list" method of malloc/free.
Seems like there's an easy enough solution to this. Just break it up
into buckets, where each bucket has blocks sized from one power of 2
to the next. If the minimum allocation size is 8 bytes, the first
bucket has blocks sized 8 through 15 in it, the second has blocks
sized 16 through 31, the third has 32 through 63, and so on. And
don't worry about ordering the lists of things in the buckets.
Just put them in there at random.
Then when it's time to do an allocation, grab something out of a bucket
that is guaranteed to give you a block big enough. For example, if
someone asks for a chunk of size 15, then even if there might be some
15-sized chunks in the 8 to 15 bucket, ignore that and grab something
from the 16 to 23 bucket.
The nice thing about this is that it's O(1). Or at least O(log M)
where M is the total size of the memory in the system[1].
The bad thing about this is that it's fairly wasteful with memory.
The exact amount it wastes in the worst case is hard to determine.
It's certain 1/2 and it might be as high as 3/4, actually...
Of course, this idea doesn't solve the problem of reassembling the
pieces you have to fragment when adjacent pieces are freed...
- Logan
[1] If someone asks for a chunk of size 16 and the 16 to 31 bucket
is empty, you probably want to try the 32 to 63 bucket, then
the 64 to 127 bucket, and so on. You can take the leftovers
and put them in a smaller bucket.
.
- Follow-Ups:
- Re: C is too old? opinions?
- From: Rob Thorpe
- Re: C is too old? opinions?
- References:
- Re: C is too old? opinions?
- From: Rob Thorpe
- Re: C is too old? opinions?
- From: goose
- Re: C is too old? opinions?
- From: Phlip
- Re: C is too old? opinions?
- From: jimmaureenrogers@xxxxxxxxxxxxxxxx
- Re: C is too old? opinions?
- From: Rob Thorpe
- Re: C is too old? opinions?
- Prev by Date: Re: C is too old? opinions?
- Next by Date: Re: About implementing RegEx & pattern-match using finite automata
- Previous by thread: Re: C is too old? opinions?
- Next by thread: Re: C is too old? opinions?
- Index(es):
Relevant Pages
|