OSSP CVS Repository |
|
This simple fix in mm_insert_chunk allows to avoid memory fragmentation which can cause allocation failures:
static void mm_insert_chunk(MM mm, mem_chunk *mcInsert)
{
mem_chunk *mc;
merge with previous and next chunk /
mem_chunk *mcPrev,*mcPrevPrev;
mem_chunk *mcNext;
if (!mm_core_lock((void *)mm, MM_LOCK_RW))
return;
mcPrevPrev = mc = &(mm->mp_freechunks);
while (mc->mc_u.mc_next != NULL && (char *)(mc->mc_u.mc_next) <(char *)mcInsert) {
mcPrevPrev = mc;
}
mc = mc->mc_u.mc_next;
mcPrev = mc;
mcNext = mc->mc_u.mc_next;
if (mcPrev == mcInsert || mcNext == mcInsert) {
mm_core_unlock((void *)mm);
}
ERR(MM_ERR_ALLOC, "chunk of memory already in free list");
return;
if ((char *)mcPrev+(mcPrev->mc_size) == (char *)mcInsert && (mcNext != NULL && (char *)mcInsert+(mcInsert->mc_size) == (char *)mcNext)) {
/
mcPrev->mc_size += mcInsert->mc_size + mcNext->mc_size;
mcPrev->mc_u.mc_next = mcNext->mc_u.mc_next;
mm->mp_freechunks.mc_usize -= 1;
}
else if ((char *)mcPrev+(mcPrev->mc_size) == (char *)mcInsert) {
if ((char *)mcInsert+(mcInsert->mc_size) == (mm->mp_offset + (char *)mm)) {
merge with previous and spare block to decrease fragmentation /
/
mcPrevPrev->mc_u.mc_next = mcPrev->mc_u.mc_next;
mm->mp_offset -= (mcInsert->mc_size + mcPrev->mc_size);
mm->mp_freechunks.mc_usize -= 1;
}
else {
/
merge with previous chunk /
mcPrev->mc_size += mcInsert->mc_size;
}
}
else if (mcNext != NULL && (char *)mcInsert+(mcInsert->mc_size) == (char *)mcNext) {
/
merge with next chunk /
mcInsert->mc_size += mcNext->mc_size;
mcInsert->mc_u.mc_next = mcNext->mc_u.mc_next;
mcPrev->mc_u.mc_next = mcInsert;
}
else {
if ((char *)mcInsert+(mcInsert->mc_size) == (mm->mp_offset + (char *)mm)) {
merge with spare block to decrease fragmentation /
/
mm->mp_offset -= mcInsert->mc_size;
}
else {
/
no merging possible, so insert as new chunk */
mcInsert->mc_u.mc_next = mcNext;
mcPrev->mc_u.mc_next = mcInsert;
mm->mp_freechunks.mc_usize += 1;
}
}
mm_core_unlock((void *)mm);
return;
}
|
Now taken over into OSSP mm 1.4.0 in a slightly reformatted way. Thanks.
|
Type: new Version: 1.3.1 Status: fixed Created: 2005-Aug-26 01:46 Severity: 3 Last Change: 2005-Sep-02 22:11 Priority: 4 Subsystem: mm Assigned To: rse Derived From: Creator: anonymous
2005-Sep-02 21:50 | • | Check-in [5166]: Optimize insertion of free memory chunks by merging with the spare area if possible to decrease memory fragmentation. (By rse) |