[kvm-unit-tests PATCH v2 3/6] lib/alloc: prepare to extend alloc.c's purpose

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Pick better names for phys_alloc state to tidy up a bit
before we add new functions.

(No functional change.)

Signed-off-by: Andrew Jones <drjones@xxxxxxxxxx>
---
 lib/alloc.c | 97 ++++++++++++++++++++++++++++++++-----------------------------
 1 file changed, 51 insertions(+), 46 deletions(-)

diff --git a/lib/alloc.c b/lib/alloc.c
index 5b3ef5d00f8a..1d990a803825 100644
--- a/lib/alloc.c
+++ b/lib/alloc.c
@@ -10,62 +10,65 @@
 #define MIN(a, b)		((a) < (b) ? (a) : (b))
 #define MAX(a, b)		((a) > (b) ? (a) : (b))
 
-#define PHYS_ALLOC_NR_REGIONS	256
+#define PHYS_ALLOC_NR_LOGS	256
 
 struct phys_alloc_region {
 	phys_addr_t base;
 	phys_addr_t size;
 };
 
-static struct phys_alloc_region regions[PHYS_ALLOC_NR_REGIONS];
-static int nr_regions;
+static struct phys_alloc_region phys_alloc_log[PHYS_ALLOC_NR_LOGS];
+static int phys_alloc_nr_logs;
 
-static struct spinlock lock;
-static phys_addr_t base, top, align_min;
+static struct spinlock phys_alloc_lock;
+static phys_addr_t phys_alloc_base;
+static phys_addr_t phys_alloc_top;
+static phys_addr_t phys_alloc_align_min;
 
 void phys_alloc_show(void)
 {
 	int i;
 
-	spin_lock(&lock);
+	spin_lock(&phys_alloc_lock);
 	printf("phys_alloc minimum alignment: 0x%" PRIx64 "\n",
-		(u64)align_min);
-	for (i = 0; i < nr_regions; ++i)
+		(u64)phys_alloc_align_min);
+	for (i = 0; i < phys_alloc_nr_logs; ++i)
 		printf("%016" PRIx64 "-%016" PRIx64 " [%s]\n",
-			(u64)regions[i].base,
-			(u64)(regions[i].base + regions[i].size - 1),
+			(u64)phys_alloc_log[i].base,
+			(u64)(phys_alloc_log[i].base +
+			      phys_alloc_log[i].size - 1),
 			"USED");
 	printf("%016" PRIx64 "-%016" PRIx64 " [%s]\n",
-		(u64)base, (u64)(top - 1), "FREE");
-	spin_unlock(&lock);
+		(u64)phys_alloc_base, (u64)(phys_alloc_top - 1), "FREE");
+	spin_unlock(&phys_alloc_lock);
 }
 
-void phys_alloc_init(phys_addr_t base_addr, phys_addr_t size)
+void phys_alloc_init(phys_addr_t base, phys_addr_t size)
 {
-	spin_lock(&lock);
+	spin_lock(&phys_alloc_lock);
 
-	if (!align_min)
-		align_min = DEFAULT_MINIMUM_ALIGNMENT;
+	if (!phys_alloc_align_min)
+		phys_alloc_align_min = DEFAULT_MINIMUM_ALIGNMENT;
 
-	assert(!top);
-	assert(!(base_addr & (align_min - 1)));
+	assert(!phys_alloc_top);
+	assert(!(base & (phys_alloc_align_min - 1)));
 	assert(size);
 
-	base = base_addr;
-	top = base + size;
+	phys_alloc_base = base;
+	phys_alloc_top = base + size;
 
-	spin_unlock(&lock);
+	spin_unlock(&phys_alloc_lock);
 }
 
 void phys_alloc_set_minimum_alignment(phys_addr_t align)
 {
-	spin_lock(&lock);
+	spin_lock(&phys_alloc_lock);
 
-	assert(!align_min);
+	assert(!phys_alloc_align_min);
 	assert(align && !(align & (align - 1)));
-	align_min = align;
+	phys_alloc_align_min = align;
 
-	spin_unlock(&lock);
+	spin_unlock(&phys_alloc_lock);
 }
 
 static phys_addr_t phys_alloc_aligned_safe(phys_addr_t size,
@@ -73,44 +76,45 @@ static phys_addr_t phys_alloc_aligned_safe(phys_addr_t size,
 {
 	static bool warned = false;
 	phys_addr_t addr, size_orig = size;
-	u64 top_safe = top;
+	u64 top_safe = phys_alloc_top;
 
 	if (safe && sizeof(long) == 4)
-		top_safe = MIN(top, 1ULL << 32);
+		top_safe = MIN(phys_alloc_top, 1ULL << 32);
 
-	assert(top_safe && base < top_safe);
+	assert(top_safe && phys_alloc_base < top_safe);
 
-	spin_lock(&lock);
+	spin_lock(&phys_alloc_lock);
 
-	align = MAX(align, align_min);
+	align = MAX(align, phys_alloc_align_min);
 
-	addr = ALIGN(base, align);
-	size += addr - base;
+	addr = ALIGN(phys_alloc_base, align);
+	size += addr - phys_alloc_base;
 
-	if ((top_safe - base) < size) {
+	if ((top_safe - phys_alloc_base) < size) {
 		printf("phys_alloc: requested=0x%" PRIx64
 		       " (align=0x%" PRIx64 "), "
 		       "need=0x%" PRIx64 ", but free=0x%" PRIx64 ". "
 		       "top=0x%" PRIx64 ", top_safe=0x%" PRIx64 "\n",
-		       (u64)size_orig, (u64)align, (u64)size, top_safe - base,
-		       (u64)top, top_safe);
-		spin_unlock(&lock);
+		       (u64)size_orig, (u64)align, (u64)size,
+		       top_safe - phys_alloc_base, (u64)phys_alloc_top,
+		       top_safe);
+		spin_unlock(&phys_alloc_lock);
 		return INVALID_PHYS_ADDR;
 	}
 
-	base += size;
+	phys_alloc_base += size;
 
-	if (nr_regions < PHYS_ALLOC_NR_REGIONS) {
-		regions[nr_regions].base = addr;
-		regions[nr_regions].size = size_orig;
-		++nr_regions;
+	if (phys_alloc_nr_logs < PHYS_ALLOC_NR_LOGS) {
+		phys_alloc_log[phys_alloc_nr_logs].base = addr;
+		phys_alloc_log[phys_alloc_nr_logs].size = size_orig;
+		++phys_alloc_nr_logs;
 	} else if (!warned) {
 		printf("WARNING: phys_alloc: No free log entries, "
 		       "can no longer log allocations...\n");
 		warned = true;
 	}
 
-	spin_unlock(&lock);
+	spin_unlock(&phys_alloc_lock);
 
 	return addr;
 }
@@ -138,17 +142,18 @@ phys_addr_t phys_zalloc_aligned(phys_addr_t size, phys_addr_t align)
 
 phys_addr_t phys_alloc(phys_addr_t size)
 {
-	return phys_alloc_aligned(size, align_min);
+	return phys_alloc_aligned(size, phys_alloc_align_min);
 }
 
 phys_addr_t phys_zalloc(phys_addr_t size)
 {
-	return phys_zalloc_aligned(size, align_min);
+	return phys_zalloc_aligned(size, phys_alloc_align_min);
 }
 
 static void *early_malloc(size_t size)
 {
-	phys_addr_t addr = phys_alloc_aligned_safe(size, align_min, true);
+	phys_addr_t addr = phys_alloc_aligned_safe(size,
+					phys_alloc_align_min, true);
 	if (addr == INVALID_PHYS_ADDR)
 		return NULL;
 
@@ -158,7 +163,7 @@ static void *early_malloc(size_t size)
 static void *early_calloc(size_t nmemb, size_t size)
 {
 	phys_addr_t addr = phys_zalloc_aligned_safe(nmemb * size,
-						    align_min, true);
+					phys_alloc_align_min, true);
 	if (addr == INVALID_PHYS_ADDR)
 		return NULL;
 
-- 
2.7.4

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux