Skip to contentSkip to conversation

Re-factor osdep code + macro and brace fixes

PATCH Superseded [email protected] 3 participants 14 messages Oct 15, 2010 – Oct 16, 2010 Kbuild

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover Re-factor osdep code + macro and brace fixes

From: Jes Sorensen <[email protected]>

Hi,

Here is another set of patches which tries to split up osdep.c further into posix and win32 versions. It introduces os-{posix,win32}-lib.c files which are used for functions that are OS specific core library functionality, like gettimeofday(), and which is used by both QEMU and support applications like qemu-img. Other functions are moved to os-{posix,win32}.c. In addtion there are a couple of minor fixes for bad macro names.

In some cases braces were added to code when it was moved, to make it compliant with the QEMU bracing rules.

Cheers,
Jes

Jes Sorensen (9):
Move QEMU OS dependant library functions to OS specific files Move osdep socket code to os-{posix,win32}-lib.c qemu_pipe() is used only by POSIX code, so move to os-posix-lib.c We only support eventfd under POSIX, move qemu_eventfd() to
os-posix.c
Move qemu_gettimeofday() to OS specific files
Do not redefine reserved key-words TRUE/FALSE
Separate qemu_pidfile() into OS specific versions Consolidate oom_check() functions
Remove unncessary includes

 Makefile           |    6 +-
 Makefile.objs      |    9 ++-
 hw/bt-sdp.c        |   21 +++--
 os-posix-lib.c     |  111 +++++++++++++++++++++++
 os-posix.c         |   53 +++++++++++
 os-win32-lib.c     |  128 ++++++++++++++++++++++++++
 os-win32.c         |   24 +++++
 osdep.c            |  256 ----------------------------------------------------
 osdep.h            |   15 ---
 posix-aio-compat.c |    1 +
 qemu-common.h      |    1 +
 qemu-img.c         |    1 +
 qemu-malloc.c      |   14 +---
 qemu-os-posix.h    |    3 +
 qemu-os-win32.h    |    9 ++
 qemu-tool.c        |    1 +
 16 files changed, 357 insertions(+), 296 deletions(-)
 create mode 100644 os-posix-lib.c
 create mode 100644 os-win32-lib.c
Signature · 1 line
1.7.2.3
#1 lore raw

PATCH 1/9 Move QEMU OS dependant library functions to OS specific files

From: Jes Sorensen <[email protected]>

This moves library functions used by both QEMU and the QEMU tools, such as qemu-img, qemu-nbd etc. from osdep.c to os-{posix,win32}-lib.c

In addition it introduces oslib-obj.y to the Makefile set to be included by the various targets, instead of relying on these library functions magically getting included via block-obj-y.

---
 Makefile       |    6 ++--
 Makefile.objs  |    9 ++++++-
 os-posix-lib.c |   65 +++++++++++++++++++++++++++++++++++++++++++++++++++
 os-win32-lib.c |   71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 osdep.c        |   70 -------------------------------------------------------
 5 files changed, 147 insertions(+), 74 deletions(-)
 create mode 100644 os-posix-lib.c
 create mode 100644 os-win32-lib.c
Makefile +3 -3
index 252c817..0b3751d 100644
@@ -129,11 +129,11 @@ version-obj-$(CONFIG_WIN32) += version.o
 qemu-img.o: qemu-img-cmds.h
 qemu-img.o qemu-tool.o qemu-nbd.o qemu-io.o: $(GENERATED_HEADERS)
 
-qemu-img$(EXESUF): qemu-img.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y)
+qemu-img$(EXESUF): qemu-img.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y)
 
-qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y)
+qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y)
 
-qemu-io$(EXESUF): qemu-io.o cmd.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y)
+qemu-io$(EXESUF): qemu-io.o cmd.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y)
 
 qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
 	$(call quiet-command,sh $(SRC_PATH)/hxtool -h < $< > $@,"  GEN   $@")
Makefile.objs +8 -1
index 816194a..4281ae8 100644
@@ -5,10 +5,16 @@ qobject-obj-y += qjson.o json-lexer.o json-streamer.o json-parser.o
 qobject-obj-y += qerror.o
 
 #######################################################################
+# os-obj-y is code depending on the OS (win32 vs posix)
+oslib-obj-y = osdep.o
+oslib-obj-$(CONFIG_WIN32) += os-win32-lib.o
+oslib-obj-$(CONFIG_POSIX) += os-posix-lib.o
+
+#######################################################################
 # block-obj-y is code used by both qemu system emulation and qemu-img
 
 block-obj-y = cutils.o cache-utils.o qemu-malloc.o qemu-option.o module.o
-block-obj-y += nbd.o block.o aio.o aes.o osdep.o qemu-config.o
+block-obj-y += nbd.o block.o aio.o aes.o qemu-config.o
 block-obj-$(CONFIG_POSIX) += posix-aio-compat.o
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
 
@@ -50,6 +56,7 @@ common-obj-y += $(net-obj-y)
 common-obj-y += $(qobject-obj-y)
 common-obj-$(CONFIG_LINUX) += $(fsdev-obj-$(CONFIG_LINUX))
 common-obj-y += readline.o console.o cursor.o async.o qemu-error.o
+common-obj-y += $(oslib-obj-y)
 common-obj-$(CONFIG_WIN32) += os-win32.o
 common-obj-$(CONFIG_POSIX) += os-posix.o
 
os-posix-lib.c new file +65 -0

65 lines not on this page · raw message

os-win32-lib.c new file +71 -0

71 lines not on this page · raw message

osdep.c +0 -70

76 lines not on this page · raw message

Signature · 1 line
1.7.2.3
#2 lore raw

Re: [Qemu-devel] [PATCH 1/9] Move QEMU OS dependant library functions to OS specific files

Quoted 4 lines · On Fri, Oct 15, 2010 at 2:05 PM, <[email protected]> wrote:

From: Jes Sorensen <[email protected]>

This moves library functions used by both QEMU and the QEMU tools, such as qemu-img, qemu-nbd etc. from osdep.c to os-{posix,win32}-lib.c

$ find . \( -name '*posix*.c' -o -name '*win32*.c' \) -print
./os-win32.c
./block/raw-posix.c
./block/raw-win32.c
./posix-aio-compat.c
./os-posix.c
./net/tap-win32.c

Please use something like osdep-{posix,win32}.c or oslib-{posix,win32}.c.

Quoted 42 lines

In addition it introduces oslib-obj.y to the Makefile set to be included by the various targets, instead of relying on these library functions magically getting included via block-obj-y.

---
 Makefile       |    6 ++--
 Makefile.objs  |    9 ++++++-
 os-posix-lib.c |   65 +++++++++++++++++++++++++++++++++++++++++++++++++++ os-win32-lib.c |   71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ osdep.c        |   70 -------------------------------------------------------

 5 files changed, 147 insertions(+), 74 deletions(-)

 create mode 100644 os-posix-lib.c
 create mode 100644 os-win32-lib.c

Makefile +0 -0
index 252c817..0b3751d 100644
@@ -129,11 +129,11 @@ version-obj-$(CONFIG_WIN32) += version.o

 qemu-img.o: qemu-img-cmds.h
 qemu-img.o qemu-tool.o qemu-nbd.o qemu-io.o: $(GENERATED_HEADERS)

Makefile +3 -3
-qemu-img$(EXESUF): qemu-img.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y)
+qemu-img$(EXESUF): qemu-img.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y)
 
-qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y)
+qemu-nbd$(EXESUF): qemu-nbd.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y)
 
-qemu-io$(EXESUF): qemu-io.o cmd.o qemu-tool.o qemu-error.o $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y)
+qemu-io$(EXESUF): qemu-io.o cmd.o qemu-tool.o qemu-error.o $(oslib-obj-y) $(trace-obj-y) $(block-obj-y) $(qobject-obj-y) $(version-obj-y)

 qemu-img-cmds.h: $(SRC_PATH)/qemu-img-cmds.hx
       $(call quiet-command,sh $(SRC_PATH)/hxtool -h < $< > $@,"  GEN   $@")

Makefile.objs +0 -0
index 816194a..4281ae8 100644
@@ -5,10 +5,16 @@ qobject-obj-y += qjson.o json-lexer.o json-streamer.o json-parser.o

 qobject-obj-y += qerror.o

Quoted 2 lines

 #######################################################################
+# os-obj-y is code depending on the OS (win32 vs posix)

The comment, variable name and file names are also all different.

Quoted 50 lines
Makefile.objs +5 -0
+oslib-obj-y = osdep.o
+oslib-obj-$(CONFIG_WIN32) += os-win32-lib.o
+oslib-obj-$(CONFIG_POSIX) += os-posix-lib.o
+
+#######################################################################
 # block-obj-y is code used by both qemu system emulation and qemu-img

 block-obj-y = cutils.o cache-utils.o qemu-malloc.o qemu-option.o module.o

Makefile.objs +1 -1
-block-obj-y += nbd.o block.o aio.o aes.o osdep.o qemu-config.o
+block-obj-y += nbd.o block.o aio.o aes.o qemu-config.o

 block-obj-$(CONFIG_POSIX) += posix-aio-compat.o
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o

Makefile.objs +0 -0
@@ -50,6 +56,7 @@ common-obj-y += $(net-obj-y)
  common-obj-y += $(qobject-obj-y)
  common-obj-$(CONFIG_LINUX) += $(fsdev-obj-$(CONFIG_LINUX))

 common-obj-y += readline.o console.o cursor.o async.o qemu-error.o +common-obj-y += $(oslib-obj-y) common-obj-$(CONFIG_WIN32) += os-win32.o common-obj-$(CONFIG_POSIX) += os-posix.o

os-posix-lib.c new file +23 -0
new file mode 100644
index 0000000..83e5101
@@ -0,0 +1,65 @@
+/*
+ * os-posix-lib.c
+ *
+ * Copyright (c) 2003-2008 Fabrice Bellard
+ * Copyright (c) 2010 Red Hat, Inc.
+ *
+ * QEMU library functions on POSIX which are shared between QEMU and
+ * the QEMU tools.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

os-posix-lib.c line 33

Quoted 10 lines
os-posix-lib.c new file +10 -0
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "config-host.h"
+#include "sysemu.h"
+#include "trace.h"
+#include "net/slirp.h"
+#include "qemu-options.h"

I don't think all of these are really needed, please trim.

Quoted 66 lines
os-posix-lib.c new file +32 -0
+
+void *qemu_memalign(size_t alignment, size_t size)
+{
+    void *ptr;
+#if defined(_POSIX_C_SOURCE) && !defined(__sun__)
+    int ret;
+    ret = posix_memalign(&ptr, alignment, size);
+    if (ret != 0) {
+        fprintf(stderr, "Failed to allocate %zu B: %s\n",
+                size, strerror(ret));
+        abort();
+    }
+#elif defined(CONFIG_BSD)
+    ptr = oom_check(valloc(size));
+#else
+    ptr = oom_check(memalign(alignment, size));
+#endif
+    trace_qemu_memalign(alignment, size, ptr);
+    return ptr;
+}
+
+/* alloc shared memory pages */
+void *qemu_vmalloc(size_t size)
+{
+    return qemu_memalign(getpagesize(), size);
+}
+
+void qemu_vfree(void *ptr)
+{
+    trace_qemu_vfree(ptr);
+    free(ptr);
+}
os-win32-lib.c new file +28 -0
new file mode 100644
index 0000000..80e713a
@@ -0,0 +1,71 @@
+/*
+ * os-win32.c
+ *
+ * Copyright (c) 2003-2008 Fabrice Bellard
+ * Copyright (c) 2010 Red Hat, Inc.
+ *
+ * QEMU library functions for win32 which are shared between QEMU and
+ * the QEMU tools.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#include <windows.h>

os-win32-lib.c line 38

Quoted 10 lines
os-win32-lib.c new file +10 -0
+#include <unistd.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <time.h>
+#include <errno.h>
+#include <sys/time.h>
+#include "config-host.h"
+#include "sysemu.h"
+#include "trace.h"
+#include "qemu-options.h"

Ditto.

#3 lore raw

PATCH 2/9 Move osdep socket code to os-{posix, win32}-lib.c

In reply to [email protected]

From: Jes Sorensen <[email protected]>

---
 os-posix-lib.c |   15 +++++++++++++++
 os-win32-lib.c |   21 +++++++++++++++++++++
 osdep.c        |   38 --------------------------------------
 3 files changed, 36 insertions(+), 38 deletions(-)
os-posix-lib.c +15 -0
index 83e5101..79f9bac 100644
@@ -31,6 +31,7 @@
 #include "trace.h"
 #include "net/slirp.h"
 #include "qemu-options.h"
+#include "qemu_socket.h"
 
 void *qemu_memalign(size_t alignment, size_t size)
 {
@@ -63,3 +64,17 @@ void qemu_vfree(void *ptr)
     trace_qemu_vfree(ptr);
     free(ptr);
 }
+
+void socket_set_nonblock(int fd)
+{
+    int f;
+    f = fcntl(fd, F_GETFL);
+    fcntl(fd, F_SETFL, f | O_NONBLOCK);
+}
+
+void qemu_set_cloexec(int fd)
+{
+    int f;
+    f = fcntl(fd, F_GETFD);
+    fcntl(fd, F_SETFD, f | FD_CLOEXEC);
+}
os-win32-lib.c +21 -0
index 80e713a..e7419e5 100644
@@ -36,6 +36,7 @@
 #include "sysemu.h"
 #include "trace.h"
 #include "qemu-options.h"
+#include "qemu_socket.h"
 
 void *qemu_memalign(size_t alignment, size_t size)
 {
@@ -69,3 +70,23 @@ void qemu_vfree(void *ptr)
     trace_qemu_vfree(ptr);
     VirtualFree(ptr, 0, MEM_RELEASE);
 }
+
+void socket_set_nonblock(int fd)
+{
+    unsigned long opt = 1;
+    ioctlsocket(fd, FIONBIO, &opt);
+}
+
+int inet_aton(const char *cp, struct in_addr *ia)
+{
+    uint32_t addr = inet_addr(cp);
+    if (addr == 0xffffffff) {
+	return 0;
+    }
+    ia->s_addr = addr;
+    return 1;
+}
+
+void qemu_set_cloexec(int fd)
+{
+}
osdep.c +0 -38
index 785fa4d..bc95bdd 100644
@@ -162,44 +162,6 @@ int qemu_gettimeofday(qemu_timeval *tp)
 #endif /* _WIN32 */
 
 
-#ifdef _WIN32
-void socket_set_nonblock(int fd)
-{
-    unsigned long opt = 1;
-    ioctlsocket(fd, FIONBIO, &opt);
-}
-
-int inet_aton(const char *cp, struct in_addr *ia)
-{
-    uint32_t addr = inet_addr(cp);
-    if (addr == 0xffffffff)
-	return 0;
-    ia->s_addr = addr;
-    return 1;
-}
-
-void qemu_set_cloexec(int fd)
-{
-}
-
-#else
-
-void socket_set_nonblock(int fd)
-{
-    int f;
-    f = fcntl(fd, F_GETFL);
-    fcntl(fd, F_SETFL, f | O_NONBLOCK);
-}
-
-void qemu_set_cloexec(int fd)
-{
-    int f;
-    f = fcntl(fd, F_GETFD);
-    fcntl(fd, F_SETFD, f | FD_CLOEXEC);
-}
-
-#endif
-
 /*
  * Opens a file with FD_CLOEXEC set
  */
Signature · 1 line
1.7.2.3
#4 lore raw

PATCH 3/9 qemu_pipe() is used only by POSIX code, so move to os-posix-lib.c

In reply to [email protected]

From: Jes Sorensen <[email protected]>

---
 os-posix-lib.c |   22 ++++++++++++++++++++++
 osdep.c        |   22 ----------------------
 2 files changed, 22 insertions(+), 22 deletions(-)
os-posix-lib.c +22 -0
index 79f9bac..773e998 100644
@@ -78,3 +78,25 @@ void qemu_set_cloexec(int fd)
     f = fcntl(fd, F_GETFD);
     fcntl(fd, F_SETFD, f | FD_CLOEXEC);
 }
+
+/*
+ * Creates a pipe with FD_CLOEXEC set on both file descriptors
+ */
+int qemu_pipe(int pipefd[2])
+{
+    int ret;
+
+#ifdef CONFIG_PIPE2
+    ret = pipe2(pipefd, O_CLOEXEC);
+    if (ret != -1 || errno != ENOSYS) {
+        return ret;
+    }
+#endif
+    ret = pipe(pipefd);
+    if (ret == 0) {
+        qemu_set_cloexec(pipefd[0]);
+        qemu_set_cloexec(pipefd[1]);
+    }
+
+    return ret;
+}
osdep.c +0 -22
index bc95bdd..0019238 100644
@@ -250,28 +250,6 @@ int qemu_eventfd(int fds[2])
 
     return qemu_pipe(fds);
 }
-
-/*
- * Creates a pipe with FD_CLOEXEC set on both file descriptors
- */
-int qemu_pipe(int pipefd[2])
-{
-    int ret;
-
-#ifdef CONFIG_PIPE2
-    ret = pipe2(pipefd, O_CLOEXEC);
-    if (ret != -1 || errno != ENOSYS) {
-        return ret;
-    }
-#endif
-    ret = pipe(pipefd);
-    if (ret == 0) {
-        qemu_set_cloexec(pipefd[0]);
-        qemu_set_cloexec(pipefd[1]);
-    }
-
-    return ret;
-}
 #endif
 
 /*
Signature · 1 line
1.7.2.3
#5 lore raw

PATCH 4/9 We only support eventfd under POSIX, move qemu_eventfd() to os-posix.c

In reply to [email protected]

From: Jes Sorensen <[email protected]>

---
 os-posix.c |   32 ++++++++++++++++++++++++++++++++
 osdep.c    |   34 ----------------------------------
 2 files changed, 32 insertions(+), 34 deletions(-)
os-posix.c +32 -0
index 6321e99..612b641 100644
@@ -43,6 +43,10 @@
 #include <sys/prctl.h>
 #endif
 
+#ifdef CONFIG_EVENTFD
+#include <sys/eventfd.h>
+#endif
+
 static struct passwd *user_pwd;
 static const char *chroot_dir;
 static int daemonize;
@@ -329,3 +333,31 @@ void os_set_line_buffering(void)
 {
     setvbuf(stdout, NULL, _IOLBF, 0);
 }
+
+/*
+ * Creates an eventfd that looks like a pipe and has EFD_CLOEXEC set.
+ */
+int qemu_eventfd(int fds[2])
+{
+#ifdef CONFIG_EVENTFD
+    int ret;
+
+    ret = eventfd(0, 0);
+    if (ret >= 0) {
+        fds[0] = ret;
+        qemu_set_cloexec(ret);
+        if ((fds[1] = dup(ret)) == -1) {
+            close(ret);
+            return -1;
+        }
+        qemu_set_cloexec(fds[1]);
+        return 0;
+    }
+
+    if (errno != ENOSYS) {
+        return -1;
+    }
+#endif
+
+    return qemu_pipe(fds);
+}
osdep.c +0 -34
index 0019238..695c460 100644
@@ -44,10 +44,6 @@
 extern int madvise(caddr_t, size_t, int);
 #endif
 
-#ifdef CONFIG_EVENTFD
-#include <sys/eventfd.h>
-#endif
-
 #ifdef _WIN32
 #include <windows.h>
 #elif defined(CONFIG_BSD)
@@ -222,36 +218,6 @@ ssize_t qemu_write_full(int fd, const void *buf, size_t count)
     return total;
 }
 
-#ifndef _WIN32
-/*
- * Creates an eventfd that looks like a pipe and has EFD_CLOEXEC set.
- */
-int qemu_eventfd(int fds[2])
-{
-#ifdef CONFIG_EVENTFD
-    int ret;
-
-    ret = eventfd(0, 0);
-    if (ret >= 0) {
-        fds[0] = ret;
-        qemu_set_cloexec(ret);
-        if ((fds[1] = dup(ret)) == -1) {
-            close(ret);
-            return -1;
-        }
-        qemu_set_cloexec(fds[1]);
-        return 0;
-    }
-
-    if (errno != ENOSYS) {
-        return -1;
-    }
-#endif
-
-    return qemu_pipe(fds);
-}
-#endif
-
 /*
  * Opens a socket with FD_CLOEXEC set
  */
Signature · 1 line
1.7.2.3
#6 lore raw

PATCH 5/9 Move qemu_gettimeofday() to OS specific files

In reply to [email protected]

From: Jes Sorensen <[email protected]>

In addition add sysemu.h includes to file requiring a prototype for ffs()

---
 hw/bt-sdp.c        |    1 +
 os-win32-lib.c     |   27 +++++++++++++++++++++++++++
 osdep.c            |   31 -------------------------------
 osdep.h            |   15 ---------------
 posix-aio-compat.c |    1 +
 qemu-img.c         |    1 +
 qemu-os-posix.h    |    3 +++
 qemu-os-win32.h    |    9 +++++++++
 qemu-tool.c        |    1 +
 9 files changed, 43 insertions(+), 46 deletions(-)
hw/bt-sdp.c +1 -0
index cc0bf2f..6344da2 100644
@@ -19,6 +19,7 @@
 
 #include "qemu-common.h"
 #include "bt.h"
+#include "sysemu.h"
 
 struct bt_l2cap_sdp_state_s {
     struct bt_l2cap_conn_params_s *channel;
os-win32-lib.c +27 -0
index e7419e5..b5b6def 100644
@@ -90,3 +90,30 @@ int inet_aton(const char *cp, struct in_addr *ia)
 void qemu_set_cloexec(int fd)
 {
 }
+
+/* mingw32 needs ffs for compilations without optimization. */
+int ffs(int i)
+{
+    /* Use gcc's builtin ffs. */
+    return __builtin_ffs(i);
+}
+
+/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
+#define _W32_FT_OFFSET (116444736000000000ULL)
+
+int qemu_gettimeofday(qemu_timeval *tp)
+{
+  union {
+    unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
+    FILETIME ft;
+  }  _now;
+
+  if(tp) {
+      GetSystemTimeAsFileTime (&_now.ft);
+      tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL );
+      tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL);
+  }
+  /* Always return 0 as per Open Group Base Specifications Issue 6.
+     Do not set errno on error.  */
+  return 0;
+}
osdep.c +0 -31
index 695c460..4b6b246 100644
@@ -126,37 +126,6 @@ int qemu_create_pidfile(const char *filename)
     return 0;
 }
 
-#ifdef _WIN32
-
-/* mingw32 needs ffs for compilations without optimization. */
-int ffs(int i)
-{
-    /* Use gcc's builtin ffs. */
-    return __builtin_ffs(i);
-}
-
-/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
-#define _W32_FT_OFFSET (116444736000000000ULL)
-
-int qemu_gettimeofday(qemu_timeval *tp)
-{
-  union {
-    unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
-    FILETIME ft;
-  }  _now;
-
-  if(tp)
-    {
-      GetSystemTimeAsFileTime (&_now.ft);
-      tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL );
-      tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL);
-    }
-  /* Always return 0 as per Open Group Base Specifications Issue 6.
-     Do not set errno on error.  */
-  return 0;
-}
-#endif /* _WIN32 */
-
 
 /*
  * Opens a file with FD_CLOEXEC set
osdep.h +0 -15
index 6716281..8bd30d7 100644
@@ -127,19 +127,4 @@ int qemu_madvise(void *addr, size_t len, int advice);
 
 int qemu_create_pidfile(const char *filename);
 
-#ifdef _WIN32
-int ffs(int i);
-
-int setenv(const char *name, const char *value, int overwrite);
-
-typedef struct {
-    long tv_sec;
-    long tv_usec;
-} qemu_timeval;
-int qemu_gettimeofday(qemu_timeval *tp);
-#else
-typedef struct timeval qemu_timeval;
-#define qemu_gettimeofday(tp) gettimeofday(tp, NULL);
-#endif /* !_WIN32 */
-
 #endif
posix-aio-compat.c +1 -0
index 7b862b5..fa5494d 100644
@@ -24,6 +24,7 @@
 
 #include "qemu-queue.h"
 #include "osdep.h"
+#include "sysemu.h"
 #include "qemu-common.h"
 #include "trace.h"
 #include "block_int.h"
qemu-img.c +1 -0
index 578b8eb..5b2bed3 100644
@@ -24,6 +24,7 @@
 #include "qemu-common.h"
 #include "qemu-option.h"
 #include "osdep.h"
+#include "sysemu.h"
 #include "block_int.h"
 #include <stdio.h>
 
qemu-os-posix.h +3 -0
index ed5c058..609494e 100644
@@ -36,4 +36,7 @@ void os_setup_signal_handling(void);
 void os_daemonize(void);
 void os_setup_post(void);
 
+typedef struct timeval qemu_timeval;
+#define qemu_gettimeofday(tp) gettimeofday(tp, NULL);
+
 #endif
qemu-os-win32.h +9 -0
index c63778d..c2c2c20 100644
@@ -52,4 +52,13 @@ static inline void os_set_proc_name(const char *dummy) {}
 # define EPROTONOSUPPORT EINVAL
 #endif
 
+int ffs(int i);
+int setenv(const char *name, const char *value, int overwrite);
+
+typedef struct {
+    long tv_sec;
+    long tv_usec;
+} qemu_timeval;
+int qemu_gettimeofday(qemu_timeval *tp);
+
 #endif
qemu-tool.c +1 -0
index b39af86..2f3db30 100644
@@ -15,6 +15,7 @@
 #include "monitor.h"
 #include "qemu-timer.h"
 #include "qemu-log.h"
+#include "sysemu.h"
 
 #include <sys/time.h>
 
Signature · 1 line
1.7.2.3
#7 lore raw

[Qemu-devel] Re: [PATCH 5/9] Move qemu_gettimeofday() to OS specific files

qemu-os-posix.h void os_setup_signal_handling(void); · line 40

Quoted 2 lines · On 10/15/2010 04:05 PM, [email protected] wrote:
qemu-os-posix.h +2 -0
+typedef struct timeval qemu_timeval;
+#define qemu_gettimeofday(tp) gettimeofday(tp, NULL);

Argh, trailing semicolon. Please remove it or use an inline function.

Paolo

#8 lore raw

[Qemu-devel] Re: [PATCH 5/9] Move qemu_gettimeofday() to OS specific files

Quoted 5 lines · Paolo Bonzini

qemu-os-posix.h void os_setup_signal_handling(void); · line 40

Quoted 2 lines · On 10/15/2010 04:05 PM, [email protected] wrote:
qemu-os-posix.h +2 -0
+typedef struct timeval qemu_timeval;
+#define qemu_gettimeofday(tp) gettimeofday(tp, NULL);

Argh, trailing semicolon. Please remove it or use an inline function.

Good point, it is present in the existing code - it was a pure copy paste. I'll fix it.

Cheers,
Jes

#9 lore raw

PATCH 6/9 Do not redefine reserved key-words TRUE/FALSE

In reply to [email protected]

From: Jes Sorensen <[email protected]>

TRUE/FALSE are generally reserved keywords and shouldn't be defined in a driver like this. Rename the macros to SDP_TRUE and SDP_FALSE respectively.

---
 hw/bt-sdp.c |   20 ++++++++++----------
 1 files changed, 10 insertions(+), 10 deletions(-)
hw/bt-sdp.c +10 -10
index 6344da2..8d7074d 100644
@@ -787,11 +787,11 @@ static void sdp_service_db_build(struct bt_l2cap_sdp_state_s *sdp,
         .type       = SDP_DTYPE_UUID | SDP_DSIZE_16,	\
         .value.uint = val,				\
     },
-#define TRUE	{				\
+#define SDP_TRUE	{				\
         .type       = SDP_DTYPE_BOOL | SDP_DSIZE_1,	\
         .value.uint = 1,				\
     },
-#define FALSE	{				\
+#define SDP_FALSE	{				\
         .type       = SDP_DTYPE_BOOL | SDP_DSIZE_1,	\
         .value.uint = 0,				\
     },
@@ -843,8 +843,8 @@ SERVICE(hid,
     /* TODO: extract from l2cap_device->device.class[0] */
     ATTRIBUTE(DEVICE_SUBCLASS,		UINT8(0x40))
     ATTRIBUTE(COUNTRY_CODE,		UINT8(0x15))
-    ATTRIBUTE(VIRTUAL_CABLE,		TRUE)
-    ATTRIBUTE(RECONNECT_INITIATE,	FALSE)
+    ATTRIBUTE(VIRTUAL_CABLE,		SDP_TRUE)
+    ATTRIBUTE(RECONNECT_INITIATE,	SDP_FALSE)
     /* TODO: extract from hid->usbdev->report_desc */
     ATTRIBUTE(DESCRIPTOR_LIST,		LIST(
         LIST(UINT8(0x22) ARRAY(
@@ -884,12 +884,12 @@ SERVICE(hid,
     ATTRIBUTE(LANG_ID_BASE_LIST,	LIST(
         LIST(UINT16(0x0409) UINT16(0x0100))
     ))
-    ATTRIBUTE(SDP_DISABLE,		FALSE)
-    ATTRIBUTE(BATTERY_POWER,		TRUE)
-    ATTRIBUTE(REMOTE_WAKEUP,		TRUE)
-    ATTRIBUTE(BOOT_DEVICE,		TRUE)	/* XXX: untested */
+    ATTRIBUTE(SDP_DISABLE,		SDP_FALSE)
+    ATTRIBUTE(BATTERY_POWER,		SDP_TRUE)
+    ATTRIBUTE(REMOTE_WAKEUP,		SDP_TRUE)
+    ATTRIBUTE(BOOT_DEVICE,		SDP_TRUE)	/* XXX: untested */
     ATTRIBUTE(SUPERVISION_TIMEOUT,	UINT16(0x0c80))
-    ATTRIBUTE(NORMALLY_CONNECTABLE,	TRUE)
+    ATTRIBUTE(NORMALLY_CONNECTABLE,	SDP_TRUE)
     ATTRIBUTE(PROFILE_VERSION,		UINT16(0x0100))
 )
 
@@ -937,7 +937,7 @@ SERVICE(pnp,
     /* Profile specific */
     ATTRIBUTE(SPECIFICATION_ID, UINT16(0x0100))
     ATTRIBUTE(VERSION,         UINT16(0x0100))
-    ATTRIBUTE(PRIMARY_RECORD,  TRUE)
+    ATTRIBUTE(PRIMARY_RECORD,  SDP_TRUE)
 )
 
 static int bt_l2cap_sdp_new_ch(struct bt_l2cap_device_s *dev,
Signature · 1 line
1.7.2.3
#10 lore raw

PATCH 7/9 Separate qemu_pidfile() into OS specific versions

In reply to [email protected]

From: Jes Sorensen <[email protected]>

---
 os-posix.c |   21 +++++++++++++++++++++
 os-win32.c |   24 ++++++++++++++++++++++++
 osdep.c    |   38 --------------------------------------
 3 files changed, 45 insertions(+), 38 deletions(-)
os-posix.c +21 -0
index 612b641..38c29d1 100644
@@ -361,3 +361,24 @@ int qemu_eventfd(int fds[2])
 
     return qemu_pipe(fds);
 }
+
+int qemu_create_pidfile(const char *filename)
+{
+    char buffer[128];
+    int len;
+    int fd;
+
+    fd = qemu_open(filename, O_RDWR | O_CREAT, 0600);
+    if (fd == -1) {
+        return -1;
+    }
+    if (lockf(fd, F_TLOCK, 0) == -1) {
+        return -1;
+    }
+    len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
+    if (write(fd, buffer, len) != len) {
+        return -1;
+    }
+
+    return 0;
+}
os-win32.c +24 -0
index 3c6f50f..566d5e9 100644
@@ -240,3 +240,27 @@ void os_pidfile_error(void)
 {
     fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
 }
+
+int qemu_create_pidfile(const char *filename)
+{
+    char buffer[128];
+    int len;
+    HANDLE file;
+    OVERLAPPED overlap;
+    BOOL ret;
+    memset(&overlap, 0, sizeof(overlap));
+
+    file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
+		      OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
+
+    if (file == INVALID_HANDLE_VALUE) {
+        return -1;
+    }
+    len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
+    ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len,
+		      &overlap, NULL);
+    if (ret == 0) {
+        return -1;
+    }
+    return 0;
+}
osdep.c +0 -38
index 4b6b246..702c9d9 100644
@@ -88,44 +88,6 @@ int qemu_madvise(void *addr, size_t len, int advice)
 #endif
 }
 
-int qemu_create_pidfile(const char *filename)
-{
-    char buffer[128];
-    int len;
-#ifndef _WIN32
-    int fd;
-
-    fd = qemu_open(filename, O_RDWR | O_CREAT, 0600);
-    if (fd == -1)
-        return -1;
-
-    if (lockf(fd, F_TLOCK, 0) == -1)
-        return -1;
-
-    len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
-    if (write(fd, buffer, len) != len)
-        return -1;
-#else
-    HANDLE file;
-    OVERLAPPED overlap;
-    BOOL ret;
-    memset(&overlap, 0, sizeof(overlap));
-
-    file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL,
-		      OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
-
-    if (file == INVALID_HANDLE_VALUE)
-      return -1;
-
-    len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
-    ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len,
-		      &overlap, NULL);
-    if (ret == 0)
-      return -1;
-#endif
-    return 0;
-}
-
 
 /*
  * Opens a file with FD_CLOEXEC set
Signature · 1 line
1.7.2.3
#11 lore raw

PATCH 8/9 Consolidate oom_check() functions

In reply to [email protected]

From: Jes Sorensen <[email protected]>

This consolidates the duplicated oom_check() functions, as well as splitting them into OS dependant versions to avoid the #ifdef grossness that was present in the old osdep.c version.

---
 os-posix-lib.c |   13 +++++++++++--
 os-win32-lib.c |   13 +++++++++++--
 osdep.c        |   15 ---------------
 qemu-common.h  |    1 +
 qemu-malloc.c  |   14 +++-----------
 5 files changed, 26 insertions(+), 30 deletions(-)
os-posix-lib.c +11 -2
index 773e998..cb146dd 100644
@@ -33,6 +33,15 @@
 #include "qemu-options.h"
 #include "qemu_socket.h"
 
+void *qemu_oom_check(void *ptr)
+{
+    if (ptr == NULL) {
+        fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
+        abort();
+    }
+    return ptr;
+}
+
 void *qemu_memalign(size_t alignment, size_t size)
 {
     void *ptr;
@@ -45,9 +54,9 @@ void *qemu_memalign(size_t alignment, size_t size)
         abort();
     }
 #elif defined(CONFIG_BSD)
-    ptr = oom_check(valloc(size));
+    ptr = qemu_oom_check(valloc(size));
 #else
-    ptr = oom_check(memalign(alignment, size));
+    ptr = qemu_oom_check(memalign(alignment, size));
 #endif
     trace_qemu_memalign(alignment, size, ptr);
     return ptr;
os-win32-lib.c +11 -2
index b5b6def..a1c7ca8 100644
@@ -38,6 +38,15 @@
 #include "qemu-options.h"
 #include "qemu_socket.h"
 
+void *qemu_oom_check(void *ptr)
+{
+    if (ptr == NULL) {
+        fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
+        abort();
+    }
+    return ptr;
+}
+
 void *qemu_memalign(size_t alignment, size_t size)
 {
     void *ptr;
@@ -45,7 +54,7 @@ void *qemu_memalign(size_t alignment, size_t size)
     if (!size) {
         abort();
     }
-    ptr = oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
+    ptr = qemu_oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
     trace_qemu_memalign(alignment, size, ptr);
     return ptr;
 }
@@ -60,7 +69,7 @@ void *qemu_vmalloc(size_t size)
     if (!size) {
         abort();
     }
-    ptr = oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
+    ptr = qemu_oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE));
     trace_qemu_vmalloc(size, ptr);
     return ptr;
 }
osdep.c +0 -15
index 702c9d9..0d48561 100644
@@ -57,21 +57,6 @@ extern int madvise(caddr_t, size_t, int);
 #include "sysemu.h"
 #include "qemu_socket.h"
 
-#if !defined(_POSIX_C_SOURCE) || defined(_WIN32) || defined(__sun__)
-static void *oom_check(void *ptr)
-{
-    if (ptr == NULL) {
-#if defined(_WIN32)
-        fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError());
-#else
-        fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno));
-#endif
-        abort();
-    }
-    return ptr;
-}
-#endif
-
 int qemu_madvise(void *addr, size_t len, int advice)
 {
     if (advice == QEMU_MADV_INVALID) {
qemu-common.h +1 -0
index 81aafa0..ead1d83 100644
@@ -174,6 +174,7 @@ const char *path(const char *pathname);
 #define qemu_isascii(c)		isascii((unsigned char)(c))
 #define qemu_toascii(c)		toascii((unsigned char)(c))
 
+void *qemu_oom_check(void *ptr);
 void *qemu_malloc(size_t size);
 void *qemu_realloc(void *ptr, size_t size);
 void *qemu_mallocz(size_t size);
qemu-malloc.c +3 -11
index ecffb67..28fb05a 100644
@@ -25,14 +25,6 @@
 #include "trace.h"
 #include <stdlib.h>
 
-static void *oom_check(void *ptr)
-{
-    if (ptr == NULL) {
-        abort();
-    }
-    return ptr;
-}
-
 void qemu_free(void *ptr)
 {
     trace_qemu_free(ptr);
@@ -54,7 +46,7 @@ void *qemu_malloc(size_t size)
     if (!size && !allow_zero_malloc()) {
         abort();
     }
-    ptr = oom_check(malloc(size ? size : 1));
+    ptr = qemu_oom_check(malloc(size ? size : 1));
     trace_qemu_malloc(size, ptr);
     return ptr;
 }
@@ -65,7 +57,7 @@ void *qemu_realloc(void *ptr, size_t size)
     if (!size && !allow_zero_malloc()) {
         abort();
     }
-    newptr = oom_check(realloc(ptr, size ? size : 1));
+    newptr = qemu_oom_check(realloc(ptr, size ? size : 1));
     trace_qemu_realloc(ptr, size, newptr);
     return newptr;
 }
@@ -75,7 +67,7 @@ void *qemu_mallocz(size_t size)
     if (!size && !allow_zero_malloc()) {
         abort();
     }
-    return oom_check(calloc(1, size ? size : 1));
+    return qemu_oom_check(calloc(1, size ? size : 1));
 }
 
 char *qemu_strdup(const char *str)
Signature · 1 line
1.7.2.3
#12 lore raw

PATCH 9/9 Remove unncessary includes

In reply to [email protected]

From: Jes Sorensen <[email protected]>

No need to include stdlib.h for BSD as it is included by qemu-common.h, windows.h is handled by sysemu.h and osdep.c no longer needs malloc.h

---
 osdep.c |    8 --------
 1 files changed, 0 insertions(+), 8 deletions(-)
osdep.c +0 -8
index 0d48561..327583b 100644
@@ -44,14 +44,6 @@
 extern int madvise(caddr_t, size_t, int);
 #endif
 
-#ifdef _WIN32
-#include <windows.h>
-#elif defined(CONFIG_BSD)
-#include <stdlib.h>
-#else
-#include <malloc.h>
-#endif
-
 #include "qemu-common.h"
 #include "trace.h"
 #include "sysemu.h"
Signature · 1 line
1.7.2.3
#13 lore raw
In reply to [email protected]
Quoted 48 lines · On 10/15/2010 04:05 PM, [email protected] wrote:

From: Jes Sorensen<[email protected]>

Hi,

Here is another set of patches which tries to split up osdep.c further into posix and win32 versions. It introduces os-{posix,win32}-lib.c files which are used for functions that are OS specific core library functionality, like gettimeofday(), and which is used by both QEMU and support applications like qemu-img. Other functions are moved to os-{posix,win32}.c. In addtion there are a couple of minor fixes for bad macro names.

In some cases braces were added to code when it was moved, to make it compliant with the QEMU bracing rules.

Cheers,
Jes

Jes Sorensen (9):
Move QEMU OS dependant library functions to OS specific files Move osdep socket code to os-{posix,win32}-lib.c qemu_pipe() is used only by POSIX code, so move to os-posix-lib.c We only support eventfd under POSIX, move qemu_eventfd() to
os-posix.c
Move qemu_gettimeofday() to OS specific files
Do not redefine reserved key-words TRUE/FALSE
Separate qemu_pidfile() into OS specific versions Consolidate oom_check() functions
Remove unncessary includes

  Makefile           |    6 +-
  Makefile.objs      |    9 ++-
  hw/bt-sdp.c        |   21 +++--
  os-posix-lib.c     |  111 +++++++++++++++++++++++
  os-posix.c         |   53 +++++++++++
  os-win32-lib.c     |  128 ++++++++++++++++++++++++++
  os-win32.c         |   24 +++++
  osdep.c            |  256 ----------------------------------------------------
  osdep.h            |   15 ---
  posix-aio-compat.c |    1 +
  qemu-common.h      |    1 +
  qemu-img.c         |    1 +
  qemu-malloc.c      |   14 +---
  qemu-os-posix.h    |    3 +
  qemu-os-win32.h    |    9 ++
  qemu-tool.c        |    1 +
  16 files changed, 357 insertions(+), 296 deletions(-)
Quoted 3 lines

create mode 100644 os-posix-lib.c
create mode 100644 os-win32-lib.c

ACK except for the nit in patch 5/9.

Paolo