* [Qemu-devel] [PATCH] Remove osdep.c/qemu-img code duplication @ 2008-04-02 17:40 Kevin Wolf 2008-04-08 18:49 ` Aurelien Jarno 0 siblings, 1 reply; 3+ messages in thread From: Kevin Wolf @ 2008-04-02 17:40 UTC (permalink / raw) To: qemu-devel [-- Attachment #1: Type: text/plain, Size: 382 bytes --] There are few helper functions (e.g. qemu_malloc) in osdep.c which are not OS dependent at all. Even the first CVS commit comment for osdep.c says "most functions are generic in fact". They are duplicated in qemu-img because osdep.c isn't used there. This patch moves those functions to cutils.c and removes the duplicates from qemu-img. Signed-off-by: Kevin Wolf <kwolf@suse.de> [-- Attachment #2: qemu-remove-duplication.patch --] [-- Type: text/x-patch, Size: 3900 bytes --] Index: Makefile.target =================================================================== --- Makefile.target (Revision 4156) +++ Makefile.target (Arbeitskopie) @@ -430,6 +430,7 @@ endif OBJS+= libqemu.a +OBJS+= ../libqemu_common.a # Note: this is a workaround. The real fix is to avoid compiling # cpu_signal_handler() in cpu-exec.c. Index: osdep.c =================================================================== --- osdep.c (Revision 4156) +++ osdep.c (Arbeitskopie) @@ -45,21 +45,6 @@ #include <malloc.h> #endif -void *get_mmap_addr(unsigned long size) -{ - return NULL; -} - -void qemu_free(void *ptr) -{ - free(ptr); -} - -void *qemu_malloc(size_t size) -{ - return malloc(size); -} - #if defined(_WIN32) void *qemu_memalign(size_t alignment, size_t size) { @@ -217,26 +202,6 @@ #endif -void *qemu_mallocz(size_t size) -{ - void *ptr; - ptr = qemu_malloc(size); - if (!ptr) - return NULL; - memset(ptr, 0, size); - return ptr; -} - -char *qemu_strdup(const char *str) -{ - char *ptr; - ptr = qemu_malloc(strlen(str) + 1); - if (!ptr) - return NULL; - strcpy(ptr, str); - return ptr; -} - int qemu_create_pidfile(const char *filename) { char buffer[128]; Index: osdep.h =================================================================== --- osdep.h (Revision 4156) +++ osdep.h (Arbeitskopie) @@ -47,17 +47,10 @@ #define qemu_printf printf -void *qemu_malloc(size_t size); -void *qemu_mallocz(size_t size); -void qemu_free(void *ptr); -char *qemu_strdup(const char *str); - void *qemu_memalign(size_t alignment, size_t size); void *qemu_vmalloc(size_t size); void qemu_vfree(void *ptr); -void *get_mmap_addr(unsigned long size); - int qemu_create_pidfile(const char *filename); #ifdef _WIN32 Index: cutils.c =================================================================== --- cutils.c (Revision 4156) +++ cutils.c (Arbeitskopie) @@ -95,3 +95,38 @@ t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec; return t; } + +void *get_mmap_addr(unsigned long size) +{ + return NULL; +} + +void qemu_free(void *ptr) +{ + free(ptr); +} + +void *qemu_malloc(size_t size) +{ + return malloc(size); +} + +void *qemu_mallocz(size_t size) +{ + void *ptr; + ptr = qemu_malloc(size); + if (!ptr) + return NULL; + memset(ptr, 0, size); + return ptr; +} + +char *qemu_strdup(const char *str) +{ + char *ptr; + ptr = qemu_malloc(strlen(str) + 1); + if (!ptr) + return NULL; + strcpy(ptr, str); + return ptr; +} Index: qemu-common.h =================================================================== --- qemu-common.h (Revision 4156) +++ qemu-common.h (Arbeitskopie) @@ -86,6 +86,14 @@ int stristart(const char *str, const char *val, const char **ptr); time_t mktimegm(struct tm *tm); +void *qemu_malloc(size_t size); +void *qemu_mallocz(size_t size); +void qemu_free(void *ptr); +char *qemu_strdup(const char *str); + +void *get_mmap_addr(unsigned long size); + + /* Error handling. */ void hw_error(const char *fmt, ...) Index: qemu-img.c =================================================================== --- qemu-img.c (Revision 4156) +++ qemu-img.c (Arbeitskopie) @@ -30,41 +30,6 @@ #include <windows.h> #endif -void *get_mmap_addr(unsigned long size) -{ - return NULL; -} - -void qemu_free(void *ptr) -{ - free(ptr); -} - -void *qemu_malloc(size_t size) -{ - return malloc(size); -} - -void *qemu_mallocz(size_t size) -{ - void *ptr; - ptr = qemu_malloc(size); - if (!ptr) - return NULL; - memset(ptr, 0, size); - return ptr; -} - -char *qemu_strdup(const char *str) -{ - char *ptr; - ptr = qemu_malloc(strlen(str) + 1); - if (!ptr) - return NULL; - strcpy(ptr, str); - return ptr; -} - static void __attribute__((noreturn)) error(const char *fmt, ...) { va_list ap; ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] [PATCH] Remove osdep.c/qemu-img code duplication 2008-04-02 17:40 [Qemu-devel] [PATCH] Remove osdep.c/qemu-img code duplication Kevin Wolf @ 2008-04-08 18:49 ` Aurelien Jarno 2008-04-09 11:15 ` Kevin Wolf 0 siblings, 1 reply; 3+ messages in thread From: Aurelien Jarno @ 2008-04-08 18:49 UTC (permalink / raw) To: qemu-devel On Wed, Apr 02, 2008 at 07:40:24PM +0200, Kevin Wolf wrote: > There are few helper functions (e.g. qemu_malloc) in osdep.c which are > not OS dependent at all. Even the first CVS commit comment for osdep.c > says "most functions are generic in fact". They are duplicated in > qemu-img because osdep.c isn't used there. > > This patch moves those functions to cutils.c and removes the duplicates > from qemu-img. After applying this patch, some files in tcg/ starts to emit warning about undefined function. The patch is a good idea though. > Index: Makefile.target > =================================================================== > --- Makefile.target (Revision 4156) > +++ Makefile.target (Arbeitskopie) > @@ -430,6 +430,7 @@ > endif > > OBJS+= libqemu.a > +OBJS+= ../libqemu_common.a > > # Note: this is a workaround. The real fix is to avoid compiling > # cpu_signal_handler() in cpu-exec.c. > Index: osdep.c > =================================================================== > --- osdep.c (Revision 4156) > +++ osdep.c (Arbeitskopie) > @@ -45,21 +45,6 @@ > #include <malloc.h> > #endif > > -void *get_mmap_addr(unsigned long size) > -{ > - return NULL; > -} > - > -void qemu_free(void *ptr) > -{ > - free(ptr); > -} > - > -void *qemu_malloc(size_t size) > -{ > - return malloc(size); > -} > - > #if defined(_WIN32) > void *qemu_memalign(size_t alignment, size_t size) > { > @@ -217,26 +202,6 @@ > > #endif > > -void *qemu_mallocz(size_t size) > -{ > - void *ptr; > - ptr = qemu_malloc(size); > - if (!ptr) > - return NULL; > - memset(ptr, 0, size); > - return ptr; > -} > - > -char *qemu_strdup(const char *str) > -{ > - char *ptr; > - ptr = qemu_malloc(strlen(str) + 1); > - if (!ptr) > - return NULL; > - strcpy(ptr, str); > - return ptr; > -} > - > int qemu_create_pidfile(const char *filename) > { > char buffer[128]; > Index: osdep.h > =================================================================== > --- osdep.h (Revision 4156) > +++ osdep.h (Arbeitskopie) > @@ -47,17 +47,10 @@ > > #define qemu_printf printf > > -void *qemu_malloc(size_t size); > -void *qemu_mallocz(size_t size); > -void qemu_free(void *ptr); > -char *qemu_strdup(const char *str); > - > void *qemu_memalign(size_t alignment, size_t size); > void *qemu_vmalloc(size_t size); > void qemu_vfree(void *ptr); > > -void *get_mmap_addr(unsigned long size); > - > int qemu_create_pidfile(const char *filename); > > #ifdef _WIN32 > Index: cutils.c > =================================================================== > --- cutils.c (Revision 4156) > +++ cutils.c (Arbeitskopie) > @@ -95,3 +95,38 @@ > t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec; > return t; > } > + > +void *get_mmap_addr(unsigned long size) > +{ > + return NULL; > +} > + > +void qemu_free(void *ptr) > +{ > + free(ptr); > +} > + > +void *qemu_malloc(size_t size) > +{ > + return malloc(size); > +} > + > +void *qemu_mallocz(size_t size) > +{ > + void *ptr; > + ptr = qemu_malloc(size); > + if (!ptr) > + return NULL; > + memset(ptr, 0, size); > + return ptr; > +} > + > +char *qemu_strdup(const char *str) > +{ > + char *ptr; > + ptr = qemu_malloc(strlen(str) + 1); > + if (!ptr) > + return NULL; > + strcpy(ptr, str); > + return ptr; > +} > Index: qemu-common.h > =================================================================== > --- qemu-common.h (Revision 4156) > +++ qemu-common.h (Arbeitskopie) > @@ -86,6 +86,14 @@ > int stristart(const char *str, const char *val, const char **ptr); > time_t mktimegm(struct tm *tm); > > +void *qemu_malloc(size_t size); > +void *qemu_mallocz(size_t size); > +void qemu_free(void *ptr); > +char *qemu_strdup(const char *str); > + > +void *get_mmap_addr(unsigned long size); > + > + > /* Error handling. */ > > void hw_error(const char *fmt, ...) > Index: qemu-img.c > =================================================================== > --- qemu-img.c (Revision 4156) > +++ qemu-img.c (Arbeitskopie) > @@ -30,41 +30,6 @@ > #include <windows.h> > #endif > > -void *get_mmap_addr(unsigned long size) > -{ > - return NULL; > -} > - > -void qemu_free(void *ptr) > -{ > - free(ptr); > -} > - > -void *qemu_malloc(size_t size) > -{ > - return malloc(size); > -} > - > -void *qemu_mallocz(size_t size) > -{ > - void *ptr; > - ptr = qemu_malloc(size); > - if (!ptr) > - return NULL; > - memset(ptr, 0, size); > - return ptr; > -} > - > -char *qemu_strdup(const char *str) > -{ > - char *ptr; > - ptr = qemu_malloc(strlen(str) + 1); > - if (!ptr) > - return NULL; > - strcpy(ptr, str); > - return ptr; > -} > - > static void __attribute__((noreturn)) error(const char *fmt, ...) > { > va_list ap; -- .''`. Aurelien Jarno | GPG: 1024D/F1BCDB73 : :' : Debian developer | Electrical Engineer `. `' aurel32@debian.org | aurelien@aurel32.net `- people.debian.org/~aurel32 | www.aurel32.net ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [Qemu-devel] [PATCH] Remove osdep.c/qemu-img code duplication 2008-04-08 18:49 ` Aurelien Jarno @ 2008-04-09 11:15 ` Kevin Wolf 0 siblings, 0 replies; 3+ messages in thread From: Kevin Wolf @ 2008-04-09 11:15 UTC (permalink / raw) To: qemu-devel [-- Attachment #1: Type: text/plain, Size: 770 bytes --] Aurelien Jarno schrieb: > On Wed, Apr 02, 2008 at 07:40:24PM +0200, Kevin Wolf wrote: >> There are few helper functions (e.g. qemu_malloc) in osdep.c which are >> not OS dependent at all. Even the first CVS commit comment for osdep.c >> says "most functions are generic in fact". They are duplicated in >> qemu-img because osdep.c isn't used there. >> >> This patch moves those functions to cutils.c and removes the duplicates >> from qemu-img. > > After applying this patch, some files in tcg/ starts to emit warning > about undefined function. Oops... Actually, there are quite a few more files which don't include the header but should now. And I found even a bit more of cutils.c code duplicated in tcg.c while checking the warnings. New patch is attached. Kevin [-- Attachment #2: qemu-remove-duplication-v2.patch --] [-- Type: text/x-patch, Size: 9276 bytes --] Index: target-sh4/translate.c =================================================================== --- target-sh4/translate.c (Revision 4159) +++ target-sh4/translate.c (Arbeitskopie) @@ -32,6 +32,7 @@ #include "exec-all.h" #include "disas.h" #include "tcg-op.h" +#include "qemu-common.h" typedef struct DisasContext { struct TranslationBlock *tb; Index: target-cris/translate.c =================================================================== --- target-cris/translate.c (Revision 4159) +++ target-cris/translate.c (Arbeitskopie) @@ -32,6 +32,7 @@ #include "tcg-op.h" #include "helper.h" #include "crisv32-decode.h" +#include "qemu-common.h" #define CRIS_STATS 0 #if CRIS_STATS Index: Makefile.target =================================================================== --- Makefile.target (Revision 4159) +++ Makefile.target (Arbeitskopie) @@ -430,6 +430,7 @@ endif OBJS+= libqemu.a +OBJS+= ../libqemu_common.a # Note: this is a workaround. The real fix is to avoid compiling # cpu_signal_handler() in cpu-exec.c. Index: target-alpha/translate.c =================================================================== --- target-alpha/translate.c (Revision 4159) +++ target-alpha/translate.c (Arbeitskopie) @@ -26,6 +26,7 @@ #include "exec-all.h" #include "disas.h" #include "tcg-op.h" +#include "qemu-common.h" #define DO_SINGLE_STEP #define GENERATE_NOP Index: target-sparc/helper.c =================================================================== --- target-sparc/helper.c (Revision 4159) +++ target-sparc/helper.c (Arbeitskopie) @@ -27,6 +27,7 @@ #include "cpu.h" #include "exec-all.h" +#include "qemu-common.h" //#define DEBUG_MMU Index: target-m68k/helper.c =================================================================== --- target-m68k/helper.c (Revision 4159) +++ target-m68k/helper.c (Arbeitskopie) @@ -25,6 +25,7 @@ #include "config.h" #include "cpu.h" #include "exec-all.h" +#include "qemu-common.h" enum m68k_cpuid { M68K_CPUID_M5206, Index: linux-user/main.c =================================================================== --- linux-user/main.c (Revision 4159) +++ linux-user/main.c (Arbeitskopie) @@ -25,6 +25,7 @@ #include <unistd.h> #include "qemu.h" +#include "qemu-common.h" #define DEBUG_LOGFILE "/tmp/qemu.log" Index: osdep.c =================================================================== --- osdep.c (Revision 4159) +++ osdep.c (Arbeitskopie) @@ -45,21 +45,6 @@ #include <malloc.h> #endif -void *get_mmap_addr(unsigned long size) -{ - return NULL; -} - -void qemu_free(void *ptr) -{ - free(ptr); -} - -void *qemu_malloc(size_t size) -{ - return malloc(size); -} - #if defined(_WIN32) void *qemu_memalign(size_t alignment, size_t size) { @@ -217,26 +202,6 @@ #endif -void *qemu_mallocz(size_t size) -{ - void *ptr; - ptr = qemu_malloc(size); - if (!ptr) - return NULL; - memset(ptr, 0, size); - return ptr; -} - -char *qemu_strdup(const char *str) -{ - char *ptr; - ptr = qemu_malloc(strlen(str) + 1); - if (!ptr) - return NULL; - strcpy(ptr, str); - return ptr; -} - int qemu_create_pidfile(const char *filename) { char buffer[128]; Index: osdep.h =================================================================== --- osdep.h (Revision 4159) +++ osdep.h (Arbeitskopie) @@ -47,17 +47,10 @@ #define qemu_printf printf -void *qemu_malloc(size_t size); -void *qemu_mallocz(size_t size); -void qemu_free(void *ptr); -char *qemu_strdup(const char *str); - void *qemu_memalign(size_t alignment, size_t size); void *qemu_vmalloc(size_t size); void qemu_vfree(void *ptr); -void *get_mmap_addr(unsigned long size); - int qemu_create_pidfile(const char *filename); #ifdef _WIN32 Index: target-ppc/helper.c =================================================================== --- target-ppc/helper.c (Revision 4159) +++ target-ppc/helper.c (Arbeitskopie) @@ -28,6 +28,7 @@ #include "cpu.h" #include "exec-all.h" #include "helper_regs.h" +#include "qemu-common.h" //#define DEBUG_MMU //#define DEBUG_BATS Index: target-ppc/translate.c =================================================================== --- target-ppc/translate.c (Revision 4159) +++ target-ppc/translate.c (Arbeitskopie) @@ -27,6 +27,7 @@ #include "exec-all.h" #include "disas.h" #include "tcg-op.h" +#include "qemu-common.h" /* Include definitions for instructions classes and implementations flags */ //#define DO_SINGLE_STEP Index: cutils.c =================================================================== --- cutils.c (Revision 4159) +++ cutils.c (Arbeitskopie) @@ -95,3 +95,38 @@ t += 3600 * tm->tm_hour + 60 * tm->tm_min + tm->tm_sec; return t; } + +void *get_mmap_addr(unsigned long size) +{ + return NULL; +} + +void qemu_free(void *ptr) +{ + free(ptr); +} + +void *qemu_malloc(size_t size) +{ + return malloc(size); +} + +void *qemu_mallocz(size_t size) +{ + void *ptr; + ptr = qemu_malloc(size); + if (!ptr) + return NULL; + memset(ptr, 0, size); + return ptr; +} + +char *qemu_strdup(const char *str) +{ + char *ptr; + ptr = qemu_malloc(strlen(str) + 1); + if (!ptr) + return NULL; + strcpy(ptr, str); + return ptr; +} Index: target-mips/translate.c =================================================================== --- target-mips/translate.c (Revision 4159) +++ target-mips/translate.c (Arbeitskopie) @@ -30,6 +30,7 @@ #include "exec-all.h" #include "disas.h" #include "tcg-op.h" +#include "qemu-common.h" //#define MIPS_DEBUG_DISAS //#define MIPS_DEBUG_SIGN_EXTENSIONS Index: tcg/tcg.c =================================================================== --- tcg/tcg.c (Revision 4159) +++ tcg/tcg.c (Arbeitskopie) @@ -39,7 +39,7 @@ #endif #include "config.h" -#include "osdep.h" +#include "qemu-common.h" /* Note: the long term plan is to reduce the dependancies on the QEMU CPU definitions. Currently they are used for qemu_ld/st @@ -147,36 +147,6 @@ #include "tcg-target.c" -/* XXX: factorize */ -static void pstrcpy(char *buf, int buf_size, const char *str) -{ - int c; - char *q = buf; - - if (buf_size <= 0) - return; - - for(;;) { - c = *str++; - if (c == 0 || q >= buf + buf_size - 1) - break; - *q++ = c; - } - *q = '\0'; -} - -#if TCG_TARGET_REG_BITS == 32 -/* strcat and truncate. */ -static char *pstrcat(char *buf, int buf_size, const char *s) -{ - int len; - len = strlen(buf); - if (len < buf_size) - pstrcpy(buf + len, buf_size - len, s); - return buf; -} -#endif - /* pool based memory allocation */ void *tcg_malloc_internal(TCGContext *s, int size) { Index: qemu-common.h =================================================================== --- qemu-common.h (Revision 4159) +++ qemu-common.h (Arbeitskopie) @@ -86,6 +86,14 @@ int stristart(const char *str, const char *val, const char **ptr); time_t mktimegm(struct tm *tm); +void *qemu_malloc(size_t size); +void *qemu_mallocz(size_t size); +void qemu_free(void *ptr); +char *qemu_strdup(const char *str); + +void *get_mmap_addr(unsigned long size); + + /* Error handling. */ void hw_error(const char *fmt, ...) Index: exec.c =================================================================== --- exec.c (Revision 4159) +++ exec.c (Arbeitskopie) @@ -35,6 +35,7 @@ #include "cpu.h" #include "exec-all.h" +#include "qemu-common.h" #if defined(CONFIG_USER_ONLY) #include <qemu.h> #endif Index: target-i386/helper2.c =================================================================== --- target-i386/helper2.c (Revision 4159) +++ target-i386/helper2.c (Arbeitskopie) @@ -28,6 +28,7 @@ #include "cpu.h" #include "exec-all.h" #include "svm.h" +#include "qemu-common.h" //#define DEBUG_MMU Index: qemu-img.c =================================================================== --- qemu-img.c (Revision 4159) +++ qemu-img.c (Arbeitskopie) @@ -30,41 +30,6 @@ #include <windows.h> #endif -void *get_mmap_addr(unsigned long size) -{ - return NULL; -} - -void qemu_free(void *ptr) -{ - free(ptr); -} - -void *qemu_malloc(size_t size) -{ - return malloc(size); -} - -void *qemu_mallocz(size_t size) -{ - void *ptr; - ptr = qemu_malloc(size); - if (!ptr) - return NULL; - memset(ptr, 0, size); - return ptr; -} - -char *qemu_strdup(const char *str) -{ - char *ptr; - ptr = qemu_malloc(strlen(str) + 1); - if (!ptr) - return NULL; - strcpy(ptr, str); - return ptr; -} - static void __attribute__((noreturn)) error(const char *fmt, ...) { va_list ap; Index: target-arm/helper.c =================================================================== --- target-arm/helper.c (Revision 4159) +++ target-arm/helper.c (Arbeitskopie) @@ -6,6 +6,7 @@ #include "exec-all.h" #include "gdbstub.h" #include "helpers.h" +#include "qemu-common.h" static uint32_t cortexa8_cp15_c0_c1[8] = { 0x1031, 0x11, 0x400, 0, 0x31100003, 0x20000000, 0x01202000, 0x11 }; Index: kqemu.c =================================================================== --- kqemu.c (Revision 4159) +++ kqemu.c (Arbeitskopie) @@ -40,6 +40,7 @@ #include "cpu.h" #include "exec-all.h" +#include "qemu-common.h" #ifdef USE_KQEMU ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-04-09 11:21 UTC | newest] Thread overview: 3+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2008-04-02 17:40 [Qemu-devel] [PATCH] Remove osdep.c/qemu-img code duplication Kevin Wolf 2008-04-08 18:49 ` Aurelien Jarno 2008-04-09 11:15 ` Kevin Wolf
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).