kernel: mem_domain: optimize sane partition checking

This commit optimizes the process of checking that the
added partitions in a mem_domain are sane. It places the
sane_partition checking inside the loop of adding the
partitions in the mem_domain, so that the checkings are
not performed twice, and no partition is checked against
itself.

Signed-off-by: Ioannis Glaropoulos <Ioannis.Glaropoulos@nordicsemi.no>
diff --git a/kernel/mem_domain.c b/kernel/mem_domain.c
index b38eda0..33f369a 100644
--- a/kernel/mem_domain.c
+++ b/kernel/mem_domain.c
@@ -81,7 +81,7 @@
 
 	key = irq_lock();
 
-	domain->num_partitions = num_parts;
+	domain->num_partitions = 0;
 	(void)memset(domain->partitions, 0, sizeof(domain->partitions));
 
 	if (num_parts) {
@@ -92,16 +92,14 @@
 			__ASSERT((parts[i]->start + parts[i]->size) >
 				 parts[i]->start, "");
 
-			domain->partitions[i] = *parts[i];
-		}
-
 #if defined(CONFIG_EXECUTE_XOR_WRITE)
-		for (i = 0; i < num_parts; i++) {
 			__ASSERT(sane_partition_domain(domain,
-						       &domain->partitions[i]),
+						       parts[i]),
 				 "");
-		}
 #endif
+			domain->partitions[i] = *parts[i];
+			domain->num_partitions++;
+		}
 	}
 
 	sys_dlist_init(&domain->mem_domain_q);