Re: Memory management strategy
- From: roberson@xxxxxxxxxxxxxxxxxx (Walter Roberson)
- Date: 30 May 2005 16:25:43 GMT
In article <23am91lnhhu5afenncqo7lq166vma7tn0l@xxxxxxx>,
Paul Mesken <usurper@xxxxxxxxxx> wrote:
>On 30 May 2005 07:09:41 -0700, ira2402@xxxxxxxxx wrote:
>>We are developing sw for a small embedded OS and we have limited
>>memory.
>>We are looking for algorithms, links, and articles about this.
>Well, as far as C goes, I could think of a couple of tips :
Generally good recommendations, but a few of them do not always work.
>- Don't use speed optimizations of your compiler. These might involve
>trading memory for speed (loop unrolling, for example).
Inlining of short routines can -sometimes- take less memory than
calling the routine, as the registers can sometimes be used directly
"as-is", without having to save important values and generate the
call and have the return stack.
Some compilers allow control over when unrolling is done -- e.g.,
may allow you to place an upper limit on the complexity of the code
to be unrolled.
Thus I would not say "don't use" the optimizations: I would say
"investigate the available options and their effects" -- and be
prepared for the possibility that the next compiler update down the
road, the tradeoffs might change.
>- Use the minimum sized datatype you need. All too often, int's are
>used automatically by the programmer where short's and char's could
>have been used. You might even be able to pack some values with
>bitfields.
That can depend upon how often the data is used and how much
of one type there is, and upon the architecture. Some systems need
longer instructions to access smaller data than they were optimized for.
Particularily for bitfields.
>- Avoid recursive functions, if you can. Although recursive functions
>often reduce code size, they might turn out to be very memory hungry.
Rewriting a recursive function into its iterative form (which always
possible) can require more memory and lower efficiency than leaving
it recursive -- in the iterative form, you have to do dynamic memory
management, linked lists or growing arrays in place, with all the code
and memory overhead that represents.
>- Using global variables might reduce memory usage (in passing less
>parameters) in some cases but this is considered a bit ugly and is a
>bit of a micro optimization :-)
Using global variables can also increase code size, depending on the
architecture. Sometimes there are shorter instructions for referring
to objects "near" a base register (e.g., within 16 bits offset)
whereas a full address may be required for a global. On the other
hand, some architectures have short instructions for referring to
objects in "low memory" (e.g., first 64 Kb), and on those architectures
a global that can be located within that "low memory" block might have
the smallest instruction. You really need to know your architecture
to get this kind of thing right.
--
Oh, to be a Blobel!
.
- Follow-Ups:
- Re: Memory management strategy
- From: Paul Mesken
- Re: Memory management strategy
- References:
- Memory management strategy
- From: ira2402
- Re: Memory management strategy
- From: Paul Mesken
- Memory management strategy
- Prev by Date: Re: sscanf and int64 in Win32 and Unix - different results?
- Next by Date: read multiple lines of a file
- Previous by thread: Re: Memory management strategy
- Next by thread: Re: Memory management strategy
- Index(es):
Relevant Pages
|