Mail Archives: djgpp/1999/11/16/13:45:12
On Mon, 15 Nov 1999, Rafael García wrote:
> text[nline]=malloc(width);
> ...
> strcpy(text[nline],buf);
> nline++;
> ...
>
>
> during load, there may be some line that don't fix in the prepared width so
> after reading all the file I need to start over changing to the extended
> width (all lines must have max width), so I free all lines:
>
> for (n=0;n<nlines;n++) free(text[n]);
This seems like a terribly inefficient way of using memory.
One way of making it better would be to allocate the entire memory as
one large chunk (number of lines multiplied by the line length), and
then realloc it as needed. Then you won't need to worry about
fragmentation.
Also, you don't explain why do you need each line be of the same
size. I seriously doubt that this is required; perhaps a slightly
different design would remove the problem altogether.
> My question is: is there any method to 'pack'/'unfragment' the heap so I
> can know the really amount of used memory?
No. The DPMI environment doesn't have any way of knowing where in the
address space will the DPMI server alloocate any given chunk of
memory. So you are totally at the mercy of the DPMI server and the
numbers it reports when you call _go32_dpmi_remaining_physical_memory.
- Raw text -