Commit 44e5ddc4 authored by Michael Holzheu's avatar Michael Holzheu Committed by Martin Schwidefsky

[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: default avatarMichael Holzheu <holzheu@linux.vnet.ibm.com>
Signed-off-by: default avatarMartin Schwidefsky <schwidefsky@de.ibm.com>
parent c86cce2a
...@@ -63,71 +63,83 @@ void detect_memory_layout(struct mem_chunk chunk[]) ...@@ -63,71 +63,83 @@ void detect_memory_layout(struct mem_chunk chunk[])
} }
EXPORT_SYMBOL(detect_memory_layout); 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 * 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 size, int type)
{ {
unsigned long start, end, new_size; unsigned long lh_start, lh_end, lh_size, ch_start, ch_end, ch_size;
int i; int i, ch_type;
for (i = 0; i < MEMORY_CHUNKS; i++) { 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; continue;
start = max(addr, chunks[i].addr);
end = min(addr + size, chunks[i].addr + chunks[i].size); /* Define chunk properties */
new_size = end - start; ch_start = chunk[i].addr;
if (new_size == 0) ch_size = chunk[i].size;
continue; ch_end = ch_start + ch_size - 1;
if (start == chunks[i].addr && ch_type = chunk[i].type;
end == chunks[i].addr + chunks[i].size) {
/* Remove chunk */ /* Is memory chunk hit by memory hole? */
chunks[i].type = type; if (addr + size <= ch_start)
} else if (start == chunks[i].addr) { continue; /* No: memory hole in front of chunk */
/* Make chunk smaller at start */ if (addr > ch_end)
if (i >= MEMORY_CHUNKS - 1) continue; /* No: memory hole after chunk */
panic("Unable to create memory hole");
memmove(&chunks[i + 1], &chunks[i], /* Yes: Define local hole properties */
sizeof(struct mem_chunk) * lh_start = max(addr, chunk[i].addr);
(MEMORY_CHUNKS - (i + 1))); lh_end = min(addr + size - 1, ch_end);
chunks[i + 1].addr = chunks[i].addr + new_size; lh_size = lh_end - lh_start + 1;
chunks[i + 1].size = chunks[i].size - new_size;
chunks[i].size = new_size; if (lh_start == ch_start && lh_end == ch_end) {
chunks[i].type = type; /* Hole covers complete memory chunk */
i += 1; mem_chunk_init(&chunk[i], lh_start, lh_size, type);
} else if (end == chunks[i].addr + chunks[i].size) { } else if (lh_end == ch_end) {
/* Make chunk smaller at end */ /* Hole starts in memory chunk and convers chunk end */
if (i >= MEMORY_CHUNKS - 1) mem_chunk_move(chunk, i + 1, i);
panic("Unable to create memory hole"); mem_chunk_init(&chunk[i], ch_start, ch_size - lh_size,
memmove(&chunks[i + 1], &chunks[i], ch_type);
sizeof(struct mem_chunk) * mem_chunk_init(&chunk[i + 1], lh_start, lh_size, type);
(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;
i += 1; 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 { } else {
/* Create memory hole */ /* Hole splits memory chunk */
if (i >= MEMORY_CHUNKS - 2) mem_chunk_move(chunk, i + 2, i);
panic("Unable to create memory hole"); mem_chunk_init(&chunk[i], ch_start,
memmove(&chunks[i + 2], &chunks[i], lh_start - ch_start, ch_type);
sizeof(struct mem_chunk) * mem_chunk_init(&chunk[i + 1], lh_start, lh_size, type);
(MEMORY_CHUNKS - (i + 2))); mem_chunk_init(&chunk[i + 2], lh_end + 1,
chunks[i + 1].addr = addr; ch_end - lh_end, ch_type);
chunks[i + 1].size = size; break;
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;
} }
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment