G
Gordon Burditt
I work on an ARM processor based embedded system. Code is mainly in
*ONLY* that region? It's OK and useful to move the arrays from the
Zero Initialized region to the Non-Zero Initialized region?
E.g. change from
char netbuffer[2048];
to
char netbuffer[2048] = {1};
To meet what you asked for, but what I doubt was intended:
Initialize the arrays to something Non-Zero. Then in an initialization
routine, call memset() on them. This has the dubious advantages of:
- Arrays move from the Zero Initialized region (which just needs to be zeroed
out) to the Non-Zero Initialized region (which needs loading with data from
somewhere).
- The amount of RAM required doesn't get any smaller.
- If code is loaded from disk, it takes longer and the executable is bigger.
- If Non-Zero Initialized data is loaded from ROM, you need more ROM space
for the shadow image of initialized data.
- The code section gets a little bigger for the initialization code.
- The startup time gets a little longer for the initialization code.
You still have to allocate the array, whether you have a pointer to it
or not. If the array isn't static, what is it? Automatic? Allocated
with malloc()?
One way you CAN save memory deals with the required lifetime of
these arrays. If they are all needed simultaneously, your overall
RAM requirement won't go down. If they have separate lifetimes,
you could possibly save a lot with auto (but these take up space,
too!) or malloc() allocation, keeping the array in existence only
while it is needed. But this probably involves a major rewrite of
the code.
C language. The project has a huge source base. We are supposed to
optimise it. Most datastructures are declared as static and which
directly go into the Zero Initialised region. We need to cut the size
of this ZI region by at least 30%.
*ONLY* that region? It's OK and useful to move the arrays from the
Zero Initialized region to the Non-Zero Initialized region?
E.g. change from
char netbuffer[2048];
to
char netbuffer[2048] = {1};
The one way i see of doing this is by removing these static arrays
and passing a pointer to the data structure whenever required. but
since these global arrays are used through out the code. A re-write
seems inevitable!
To meet what you asked for, but what I doubt was intended:
Initialize the arrays to something Non-Zero. Then in an initialization
routine, call memset() on them. This has the dubious advantages of:
- Arrays move from the Zero Initialized region (which just needs to be zeroed
out) to the Non-Zero Initialized region (which needs loading with data from
somewhere).
- The amount of RAM required doesn't get any smaller.
- If code is loaded from disk, it takes longer and the executable is bigger.
- If Non-Zero Initialized data is loaded from ROM, you need more ROM space
for the shadow image of initialized data.
- The code section gets a little bigger for the initialization code.
- The startup time gets a little longer for the initialization code.
two questions I had.
1. Am I right in doing this(passing pointer instead of making the data
structure static) ?
You still have to allocate the array, whether you have a pointer to it
or not. If the array isn't static, what is it? Automatic? Allocated
with malloc()?
2. Is there any alternative to this ?
One way you CAN save memory deals with the required lifetime of
these arrays. If they are all needed simultaneously, your overall
RAM requirement won't go down. If they have separate lifetimes,
you could possibly save a lot with auto (but these take up space,
too!) or malloc() allocation, keeping the array in existence only
while it is needed. But this probably involves a major rewrite of
the code.