[S390] Rework create_mem_hole() function
This patch makes the create_mem_hole() function more readable and fixes some minor bugs (e.g. off-by-one problems). Signed-off-by: Michael Holzheu <holzheu@linux.vnet.ibm.com> Signed-off-by: Martin Schwidefsky <schwidefsky@de.ibm.com>
This commit is contained in:
parent
c86cce2a20
commit
44e5ddc4e9
|
@ -63,71 +63,83 @@ void detect_memory_layout(struct mem_chunk chunk[])
|
|||
}
|
||||
EXPORT_SYMBOL(detect_memory_layout);
|
||||
|
||||
/*
|
||||
* Move memory chunks array from index "from" to index "to"
|
||||
*/
|
||||
static void mem_chunk_move(struct mem_chunk chunk[], int to, int from)
|
||||
{
|
||||
int cnt = MEMORY_CHUNKS - to;
|
||||
|
||||
memmove(&chunk[to], &chunk[from], cnt * sizeof(struct mem_chunk));
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize memory chunk
|
||||
*/
|
||||
static void mem_chunk_init(struct mem_chunk *chunk, unsigned long addr,
|
||||
unsigned long size, int type)
|
||||
{
|
||||
chunk->type = type;
|
||||
chunk->addr = addr;
|
||||
chunk->size = size;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create memory hole with given address, size, and type
|
||||
*/
|
||||
void create_mem_hole(struct mem_chunk chunks[], unsigned long addr,
|
||||
void create_mem_hole(struct mem_chunk chunk[], unsigned long addr,
|
||||
unsigned long size, int type)
|
||||
{
|
||||
unsigned long start, end, new_size;
|
||||
int i;
|
||||
unsigned long lh_start, lh_end, lh_size, ch_start, ch_end, ch_size;
|
||||
int i, ch_type;
|
||||
|
||||
for (i = 0; i < MEMORY_CHUNKS; i++) {
|
||||
if (chunks[i].size == 0)
|
||||
if (chunk[i].size == 0)
|
||||
continue;
|
||||
if (addr + size < chunks[i].addr)
|
||||
continue;
|
||||
if (addr >= chunks[i].addr + chunks[i].size)
|
||||
continue;
|
||||
start = max(addr, chunks[i].addr);
|
||||
end = min(addr + size, chunks[i].addr + chunks[i].size);
|
||||
new_size = end - start;
|
||||
if (new_size == 0)
|
||||
continue;
|
||||
if (start == chunks[i].addr &&
|
||||
end == chunks[i].addr + chunks[i].size) {
|
||||
/* Remove chunk */
|
||||
chunks[i].type = type;
|
||||
} else if (start == chunks[i].addr) {
|
||||
/* Make chunk smaller at start */
|
||||
if (i >= MEMORY_CHUNKS - 1)
|
||||
panic("Unable to create memory hole");
|
||||
memmove(&chunks[i + 1], &chunks[i],
|
||||
sizeof(struct mem_chunk) *
|
||||
(MEMORY_CHUNKS - (i + 1)));
|
||||
chunks[i + 1].addr = chunks[i].addr + new_size;
|
||||
chunks[i + 1].size = chunks[i].size - new_size;
|
||||
chunks[i].size = new_size;
|
||||
chunks[i].type = type;
|
||||
i += 1;
|
||||
} else if (end == chunks[i].addr + chunks[i].size) {
|
||||
/* Make chunk smaller at end */
|
||||
if (i >= MEMORY_CHUNKS - 1)
|
||||
panic("Unable to create memory hole");
|
||||
memmove(&chunks[i + 1], &chunks[i],
|
||||
sizeof(struct mem_chunk) *
|
||||
(MEMORY_CHUNKS - (i + 1)));
|
||||
chunks[i + 1].addr = start;
|
||||
chunks[i + 1].size = new_size;
|
||||
chunks[i + 1].type = type;
|
||||
chunks[i].size -= new_size;
|
||||
|
||||
/* Define chunk properties */
|
||||
ch_start = chunk[i].addr;
|
||||
ch_size = chunk[i].size;
|
||||
ch_end = ch_start + ch_size - 1;
|
||||
ch_type = chunk[i].type;
|
||||
|
||||
/* Is memory chunk hit by memory hole? */
|
||||
if (addr + size <= ch_start)
|
||||
continue; /* No: memory hole in front of chunk */
|
||||
if (addr > ch_end)
|
||||
continue; /* No: memory hole after chunk */
|
||||
|
||||
/* Yes: Define local hole properties */
|
||||
lh_start = max(addr, chunk[i].addr);
|
||||
lh_end = min(addr + size - 1, ch_end);
|
||||
lh_size = lh_end - lh_start + 1;
|
||||
|
||||
if (lh_start == ch_start && lh_end == ch_end) {
|
||||
/* Hole covers complete memory chunk */
|
||||
mem_chunk_init(&chunk[i], lh_start, lh_size, type);
|
||||
} else if (lh_end == ch_end) {
|
||||
/* Hole starts in memory chunk and convers chunk end */
|
||||
mem_chunk_move(chunk, i + 1, i);
|
||||
mem_chunk_init(&chunk[i], ch_start, ch_size - lh_size,
|
||||
ch_type);
|
||||
mem_chunk_init(&chunk[i + 1], lh_start, lh_size, type);
|
||||
i += 1;
|
||||
} else if (lh_start == ch_start) {
|
||||
/* Hole ends in memory chunk */
|
||||
mem_chunk_move(chunk, i + 1, i);
|
||||
mem_chunk_init(&chunk[i], lh_start, lh_size, type);
|
||||
mem_chunk_init(&chunk[i + 1], lh_end + 1,
|
||||
ch_size - lh_size, ch_type);
|
||||
break;
|
||||
} else {
|
||||
/* Create memory hole */
|
||||
if (i >= MEMORY_CHUNKS - 2)
|
||||
panic("Unable to create memory hole");
|
||||
memmove(&chunks[i + 2], &chunks[i],
|
||||
sizeof(struct mem_chunk) *
|
||||
(MEMORY_CHUNKS - (i + 2)));
|
||||
chunks[i + 1].addr = addr;
|
||||
chunks[i + 1].size = size;
|
||||
chunks[i + 1].type = type;
|
||||
chunks[i + 2].addr = addr + size;
|
||||
chunks[i + 2].size =
|
||||
chunks[i].addr + chunks[i].size - (addr + size);
|
||||
chunks[i + 2].type = chunks[i].type;
|
||||
chunks[i].size = addr - chunks[i].addr;
|
||||
i += 2;
|
||||
/* Hole splits memory chunk */
|
||||
mem_chunk_move(chunk, i + 2, i);
|
||||
mem_chunk_init(&chunk[i], ch_start,
|
||||
lh_start - ch_start, ch_type);
|
||||
mem_chunk_init(&chunk[i + 1], lh_start, lh_size, type);
|
||||
mem_chunk_init(&chunk[i + 2], lh_end + 1,
|
||||
ch_end - lh_end, ch_type);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue