[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);
|
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;
|
continue;
|
||||||
if (addr + size < chunks[i].addr)
|
|
||||||
continue;
|
/* Define chunk properties */
|
||||||
if (addr >= chunks[i].addr + chunks[i].size)
|
ch_start = chunk[i].addr;
|
||||||
continue;
|
ch_size = chunk[i].size;
|
||||||
start = max(addr, chunks[i].addr);
|
ch_end = ch_start + ch_size - 1;
|
||||||
end = min(addr + size, chunks[i].addr + chunks[i].size);
|
ch_type = chunk[i].type;
|
||||||
new_size = end - start;
|
|
||||||
if (new_size == 0)
|
/* Is memory chunk hit by memory hole? */
|
||||||
continue;
|
if (addr + size <= ch_start)
|
||||||
if (start == chunks[i].addr &&
|
continue; /* No: memory hole in front of chunk */
|
||||||
end == chunks[i].addr + chunks[i].size) {
|
if (addr > ch_end)
|
||||||
/* Remove chunk */
|
continue; /* No: memory hole after chunk */
|
||||||
chunks[i].type = type;
|
|
||||||
} else if (start == chunks[i].addr) {
|
/* Yes: Define local hole properties */
|
||||||
/* Make chunk smaller at start */
|
lh_start = max(addr, chunk[i].addr);
|
||||||
if (i >= MEMORY_CHUNKS - 1)
|
lh_end = min(addr + size - 1, ch_end);
|
||||||
panic("Unable to create memory hole");
|
lh_size = lh_end - lh_start + 1;
|
||||||
memmove(&chunks[i + 1], &chunks[i],
|
|
||||||
sizeof(struct mem_chunk) *
|
if (lh_start == ch_start && lh_end == ch_end) {
|
||||||
(MEMORY_CHUNKS - (i + 1)));
|
/* Hole covers complete memory chunk */
|
||||||
chunks[i + 1].addr = chunks[i].addr + new_size;
|
mem_chunk_init(&chunk[i], lh_start, lh_size, type);
|
||||||
chunks[i + 1].size = chunks[i].size - new_size;
|
} else if (lh_end == ch_end) {
|
||||||
chunks[i].size = new_size;
|
/* Hole starts in memory chunk and convers chunk end */
|
||||||
chunks[i].type = type;
|
mem_chunk_move(chunk, i + 1, i);
|
||||||
i += 1;
|
mem_chunk_init(&chunk[i], ch_start, ch_size - lh_size,
|
||||||
} else if (end == chunks[i].addr + chunks[i].size) {
|
ch_type);
|
||||||
/* Make chunk smaller at end */
|
mem_chunk_init(&chunk[i + 1], lh_start, lh_size, type);
|
||||||
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;
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue