Re: C is too old? opinions?
- From: "Rob Thorpe" <robert.thorpe@xxxxxxxxxxxx>
- Date: 14 Jul 2006 08:18:56 -0700
Logan Shaw wrote:
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.
What you describe is very similar to malloc in early versions of BSD.
Remember that each bucket must be connected to some sort of set data
structure. Describing the set of memory blocks. You must work out how
to pick an item from that set data structure in bounded time. That
isn't a simple thing to do. Lists can't do it and neither can chaining
hashes. But chaining hashes can be made to have more reasonable
behaviour.
Its easy to do one thing with the set data structure in bounded time,
ie insert or remove, but hard to do both in bounded time.
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...
Yes, BSD malloc used to waste amounts of memory of this order on a few
types of load.
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.
Yes, and that can be done in bounded time. You can know what the
bounded time is if you know how many buckets you have.
What you need to know is plenty of limits, the size of memory, the
largest block that a program can request from malloc etc. With those
its not too hard.
As some earlier poster mentioned not bothering and only using static
allocation in real-time parts of code can be a simple option.
.
- Follow-Ups:
- Re: C is too old? opinions?
- From: Logan Shaw
- 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?
- From: Logan Shaw
- Re: C is too old? opinions?
- Prev by Date: Re: CVS
- Next by Date: data structure question
- Previous by thread: Re: C is too old? opinions?
- Next by thread: Re: C is too old? opinions?
- Index(es):
Relevant Pages
|