[PATCH] kexec-tools: Remove duplicate ultoa() definitions and redefine it

Tiezhu Yang yangtiezhu at loongson.cn
Fri Jul 15 03:05:14 PDT 2022


There exist duplicate ultoa() definitions in many archs, remove them,
and also redefine ultoa() in kexec/kexec.h to make it more readable.

Signed-off-by: Tiezhu Yang <yangtiezhu at loongson.cn>
---
 kexec/arch/i386/crashdump-x86.c    | 19 -------------------
 kexec/arch/mips/crashdump-mips.c   | 19 -------------------
 kexec/arch/mips/kexec-elf-mips.c   | 19 -------------------
 kexec/arch/ppc64/crashdump-ppc64.c | 19 -------------------
 kexec/arch/sh/crashdump-sh.c       | 19 -------------------
 kexec/kexec.h                      | 16 ++++++++++++++++
 6 files changed, 16 insertions(+), 95 deletions(-)

diff --git a/kexec/arch/i386/crashdump-x86.c b/kexec/arch/i386/crashdump-x86.c
index 9826f6d..df1f24c 100644
--- a/kexec/arch/i386/crashdump-x86.c
+++ b/kexec/arch/i386/crashdump-x86.c
@@ -601,25 +601,6 @@ static int delete_memmap(struct memory_range *memmap_p, int *nr_memmap,
 	return 0;
 }
 
-/* Converts unsigned long to ascii string. */
-static void ultoa(unsigned long i, char *str)
-{
-	int j = 0, k;
-	char tmp;
-
-	do {
-		str[j++] = i % 10 + '0';
-	} while ((i /=10) > 0);
-	str[j] = '\0';
-
-	/* Reverse the string. */
-	for (j = 0, k = strlen(str) - 1; j < k; j++, k--) {
-		tmp = str[k];
-		str[k] = str[j];
-		str[j] = tmp;
-	}
-}
-
 static void cmdline_add_memmap_internal(char *cmdline, unsigned long startk,
 					unsigned long endk, int type)
 {
diff --git a/kexec/arch/mips/crashdump-mips.c b/kexec/arch/mips/crashdump-mips.c
index d9f4515..00f4335 100644
--- a/kexec/arch/mips/crashdump-mips.c
+++ b/kexec/arch/mips/crashdump-mips.c
@@ -224,25 +224,6 @@ static int get_crash_memory_ranges(struct memory_range **range, int *ranges)
 	return 0;
 }
 
-/* Converts unsigned long to ascii string. */
-static void ultoa(unsigned long i, char *str)
-{
-	int j = 0, k;
-	char tmp;
-
-	do {
-		str[j++] = i % 10 + '0';
-	} while ((i /= 10) > 0);
-	str[j] = '\0';
-
-	/* Reverse the string. */
-	for (j = 0, k = strlen(str) - 1; j < k; j++, k--) {
-		tmp = str[k];
-		str[k] = str[j];
-		str[j] = tmp;
-	}
-}
-
 /* Adds the appropriate mem= options to command line, indicating the
  * memory region the new kernel can use to boot into. */
 static int cmdline_add_mem(char *cmdline, unsigned long addr,
diff --git a/kexec/arch/mips/kexec-elf-mips.c b/kexec/arch/mips/kexec-elf-mips.c
index 31f4c47..230d806 100644
--- a/kexec/arch/mips/kexec-elf-mips.c
+++ b/kexec/arch/mips/kexec-elf-mips.c
@@ -40,25 +40,6 @@ static const int probe_debug = 0;
 #define CMDLINE_PREFIX "kexec "
 static char cmdline_buf[COMMAND_LINE_SIZE] = CMDLINE_PREFIX;
 
-/* Converts unsigned long to ascii string. */
-static void ultoa(unsigned long i, char *str)
-{
-	int j = 0, k;
-	char tmp;
-
-	do {
-		str[j++] = i % 10 + '0';
-	} while ((i /= 10) > 0);
-	str[j] = '\0';
-
-	/* Reverse the string. */
-	for (j = 0, k = strlen(str) - 1; j < k; j++, k--) {
-		tmp = str[k];
-		str[k] = str[j];
-		str[j] = tmp;
-	}
-}
-
 /* Adds initrd parameters to command line. */
 static int cmdline_add_initrd(char *cmdline, unsigned long addr, char *new_para)
 {
diff --git a/kexec/arch/ppc64/crashdump-ppc64.c b/kexec/arch/ppc64/crashdump-ppc64.c
index 91f9521..6d47898 100644
--- a/kexec/arch/ppc64/crashdump-ppc64.c
+++ b/kexec/arch/ppc64/crashdump-ppc64.c
@@ -436,25 +436,6 @@ err:
 	return -1;
 }
 
-/* Converts unsigned long to ascii string. */
-static void ultoa(uint64_t i, char *str)
-{
-	int j = 0, k;
-	char tmp;
-
-	do {
-		str[j++] = i % 10 + '0';
-	} while ((i /=10) > 0);
-	str[j] = '\0';
-
-	/* Reverse the string. */
-	for (j = 0, k = strlen(str) - 1; j < k; j++, k--) {
-		tmp = str[k];
-		str[k] = str[j];
-		str[j] = tmp;
-	}
-}
-
 static int add_cmdline_param(char *cmdline, uint64_t addr, char *cmdstr,
 				char *byte)
 {
diff --git a/kexec/arch/sh/crashdump-sh.c b/kexec/arch/sh/crashdump-sh.c
index aa25dea..36e9aaf 100644
--- a/kexec/arch/sh/crashdump-sh.c
+++ b/kexec/arch/sh/crashdump-sh.c
@@ -90,25 +90,6 @@ static struct crash_elf_info elf_info32 =
 	page_offset: PAGE_OFFSET,
 };
 
-/* Converts unsigned long to ascii string. */
-static void ultoa(unsigned long i, char *str)
-{
-	int j = 0, k;
-	char tmp;
-
-	do {
-		str[j++] = i % 10 + '0';
-	} while ((i /=10) > 0);
-	str[j] = '\0';
-
-	/* Reverse the string. */
-	for (j = 0, k = strlen(str) - 1; j < k; j++, k--) {
-		tmp = str[k];
-		str[k] = str[j];
-		str[j] = tmp;
-	}
-}
-
 static int add_cmdline_param(char *cmdline, uint64_t addr, char *cmdstr,
 				char *byte)
 {
diff --git a/kexec/kexec.h b/kexec/kexec.h
index 0f97a97..8a05644 100644
--- a/kexec/kexec.h
+++ b/kexec/kexec.h
@@ -329,4 +329,20 @@ int xen_kexec_status(uint64_t kexec_flags);
 
 extern unsigned long long get_kernel_sym(const char *text);
 
+/* Converts unsigned long to ascii string. */
+static inline void ultoa(unsigned long val, char *str)
+{
+	char buf[36];
+	int len = 0, pos = 0;
+
+	do {
+		buf[len++] = val % 10;
+		val /= 10;
+	} while (val);
+
+	while (len)
+		str[pos++] = buf[--len] + '0';
+	str[pos] = 0;
+}
+
 #endif /* KEXEC_H */
-- 
2.1.0




More information about the kexec mailing list