[lib-opt] library optimalization and clean-up
authorRadoslaw Pajak <r.pajak@samsung.com>
Mon, 25 Nov 2013 12:10:25 +0000 (13:10 +0100)
committerRadoslaw Pajak <r.pajak@samsung.com>
Mon, 25 Nov 2013 12:35:21 +0000 (13:35 +0100)
Library optimalization, cleanups, small corrections, renames.

Change-Id: Ie48465a2d7bf30c916bebeeee2e31c22255477cc
Signed-off-by: Radoslaw Pajak <r.pajak@samsung.com>
dbus/.cproject [deleted file]
dbus/dbus-transport-kdbus.c

diff --git a/dbus/.cproject b/dbus/.cproject
deleted file mode 100644 (file)
index c16f79d..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
-       <storageModule moduleId="org.eclipse.cdt.core.settings">
-               <cconfiguration id="cdt.managedbuild.toolchain.gnu.base.456749915">
-                       <storageModule buildSystemId="org.eclipse.cdt.managedbuilder.core.configurationDataProvider" id="cdt.managedbuild.toolchain.gnu.base.456749915" moduleId="org.eclipse.cdt.core.settings" name="Default">
-                               <externalSettings/>
-                               <extensions>
-                                       <extension id="org.eclipse.cdt.core.ELF" point="org.eclipse.cdt.core.BinaryParser"/>
-                                       <extension id="org.eclipse.cdt.core.GmakeErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
-                                       <extension id="org.eclipse.cdt.core.CWDLocator" point="org.eclipse.cdt.core.ErrorParser"/>
-                                       <extension id="org.eclipse.cdt.core.GCCErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
-                                       <extension id="org.eclipse.cdt.core.GASErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
-                                       <extension id="org.eclipse.cdt.core.GLDErrorParser" point="org.eclipse.cdt.core.ErrorParser"/>
-                               </extensions>
-                       </storageModule>
-                       <storageModule moduleId="cdtBuildSystem" version="4.0.0">
-                               <configuration buildProperties="" description="" id="cdt.managedbuild.toolchain.gnu.base.456749915" name="Default" parent="org.eclipse.cdt.build.core.emptycfg">
-                                       <folderInfo id="cdt.managedbuild.toolchain.gnu.base.456749915.2116564601" name="/" resourcePath="">
-                                               <toolChain id="cdt.managedbuild.toolchain.gnu.base.1084026941" name="cdt.managedbuild.toolchain.gnu.base" superClass="cdt.managedbuild.toolchain.gnu.base">
-                                                       <targetPlatform archList="all" binaryParser="org.eclipse.cdt.core.ELF" id="cdt.managedbuild.target.gnu.platform.base.483630444" name="Debug Platform" osList="linux,hpux,aix,qnx" superClass="cdt.managedbuild.target.gnu.platform.base"/>
-                                                       <builder buildPath="${workspace_loc:/libdbus}" id="cdt.managedbuild.target.gnu.builder.base.1295573544" keepEnvironmentInBuildfile="false" managedBuildOn="false" name="Gnu Make Builder" superClass="cdt.managedbuild.target.gnu.builder.base"/>
-                                                       <tool id="cdt.managedbuild.tool.gnu.archiver.base.933685167" name="GCC Archiver" superClass="cdt.managedbuild.tool.gnu.archiver.base"/>
-                                                       <tool id="cdt.managedbuild.tool.gnu.cpp.compiler.base.426313899" name="GCC C++ Compiler" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.base">
-                                                               <inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.553326917" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
-                                                       </tool>
-                                                       <tool id="cdt.managedbuild.tool.gnu.c.compiler.base.1587182930" name="GCC C Compiler" superClass="cdt.managedbuild.tool.gnu.c.compiler.base">
-                                                               <option id="gnu.c.compiler.option.include.paths.1062871126" name="Include paths (-I)" superClass="gnu.c.compiler.option.include.paths" valueType="includePath">
-                                                                       <listOptionValue builtIn="false" value="/usr/src/linux-headers-3.10.0-031000rc6-generic/include"/>
-                                                                       <listOptionValue builtIn="false" value="/usr/include"/>
-                                                                       <listOptionValue builtIn="false" value="/usr/include/x86_64-linux-gnu"/>
-                                                               </option>
-                                                               <inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.870757898" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
-                                                       </tool>
-                                                       <tool id="cdt.managedbuild.tool.gnu.c.linker.base.2082327427" name="GCC C Linker" superClass="cdt.managedbuild.tool.gnu.c.linker.base"/>
-                                                       <tool id="cdt.managedbuild.tool.gnu.cpp.linker.base.1094477497" name="GCC C++ Linker" superClass="cdt.managedbuild.tool.gnu.cpp.linker.base">
-                                                               <inputType id="cdt.managedbuild.tool.gnu.cpp.linker.input.1676617176" superClass="cdt.managedbuild.tool.gnu.cpp.linker.input">
-                                                                       <additionalInput kind="additionalinputdependency" paths="$(USER_OBJS)"/>
-                                                                       <additionalInput kind="additionalinput" paths="$(LIBS)"/>
-                                                               </inputType>
-                                                       </tool>
-                                                       <tool id="cdt.managedbuild.tool.gnu.assembler.base.240122192" name="GCC Assembler" superClass="cdt.managedbuild.tool.gnu.assembler.base">
-                                                               <inputType id="cdt.managedbuild.tool.gnu.assembler.input.2060489052" superClass="cdt.managedbuild.tool.gnu.assembler.input"/>
-                                                       </tool>
-                                               </toolChain>
-                                       </folderInfo>
-                               </configuration>
-                       </storageModule>
-                       <storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
-               </cconfiguration>
-       </storageModule>
-       <storageModule moduleId="cdtBuildSystem" version="4.0.0">
-               <project id="libdbus.null.6942114" name="libdbus"/>
-       </storageModule>
-       <storageModule moduleId="refreshScope" versionNumber="1">
-               <resource resourceType="PROJECT" workspacePath="/libdbus"/>
-       </storageModule>
-       <storageModule moduleId="scannerConfiguration">
-               <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId=""/>
-               <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.base.456749915;cdt.managedbuild.toolchain.gnu.base.456749915.2116564601;cdt.managedbuild.tool.gnu.cpp.compiler.base.426313899;cdt.managedbuild.tool.gnu.cpp.compiler.input.553326917">
-                       <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="org.eclipse.cdt.managedbuilder.core.GCCManagedMakePerProjectProfileCPP"/>
-               </scannerConfigBuildInfo>
-               <scannerConfigBuildInfo instanceId="cdt.managedbuild.toolchain.gnu.base.456749915;cdt.managedbuild.toolchain.gnu.base.456749915.2116564601;cdt.managedbuild.tool.gnu.c.compiler.base.1587182930;cdt.managedbuild.tool.gnu.c.compiler.input.870757898">
-                       <autodiscovery enabled="true" problemReportingEnabled="true" selectedProfileId="org.eclipse.cdt.managedbuilder.core.GCCManagedMakePerProjectProfileC"/>
-               </scannerConfigBuildInfo>
-       </storageModule>
-</cproject>
index 824f822..15584f2 100644 (file)
 
 #define RECEIVE_POOL_SIZE (10 * 1024LU * 1024LU) //size of the memory area for received non-memfd messages
 #define MEMFD_SIZE_THRESHOLD (2 * 1024 * 1024LU) // over this memfd is used to send (if it is not broadcast)
-//todo add compilation-time check if MEMFD_SIZE_THERSHOLD is lower than max payload vector size defined in kdbus.h
+#define MAX_BYTES_PER_ITERATION 16384
+
+#if (MEMFD_SIZE_THRESHOLD > KDBUS_MSG_MAX_PAYLOAD_VEC_SIZE)
+  #error  Memfd size threshold higher than max kdbus message payload vector size
+#endif
 
 #define KDBUS_MSG_DECODE_DEBUG 0
 
@@ -136,7 +140,7 @@ static dbus_bool_t add_message_to_received(DBusMessage *message, DBusConnection*
 /**
  * Generates local error message as a reply to message given as parameter
  * and adds generated error message to received messages queue.
- * @param error_Type type of error, preferably DBUS_ERROR_(...)
+ * @param error_type type of error, preferably DBUS_ERROR_(...)
  * @param template Template of error description. It can has formatting
  *       characters to print object string into it. Can be NULL.
  * @param object String to print into error description. Can be NULL.
@@ -527,33 +531,38 @@ static dbus_bool_t bus_register_kdbus(char* name, DBusTransportKdbus* transportS
  *                     0 if Hello message was handled correctly,
  *                     -1 if Hello message was not handle correctly.
  */
-static int emulateOrgFreedesktopDBus(DBusTransport *transport, DBusMessage *message)
+static int capture_hello_message(DBusTransport *transport, const char* destination, DBusMessage *message)
 {
-  if(!strcmp(dbus_message_get_member(message), "Hello"))
+  if(!strcmp(destination, DBUS_SERVICE_DBUS))
     {
-      char* name = NULL;
+      if(!strcmp(dbus_message_get_interface(message), DBUS_INTERFACE_DBUS))
+        {
+          if(!strcmp(dbus_message_get_member(message), "Hello"))
+            {
+              char* name = NULL;
 
-      name = malloc(snprintf(name, 0, ":1.%llu0", ULLONG_MAX));
-      if(name == NULL)
-        return -1;
-      strcpy(name, ":1.");
-      if(!bus_register_kdbus(&name[3], (DBusTransportKdbus*)transport))
-        goto out;
-      if(!register_kdbus_policy(&name[3], transport, geteuid()))
-        goto out;
+              name = malloc(snprintf(name, 0, ":1.%llu0", ULLONG_MAX));
+              if(name == NULL)
+                return -1;
+              strcpy(name, ":1.");
+              if(!bus_register_kdbus(&name[3], (DBusTransportKdbus*)transport))
+                goto out;
+              if(!register_kdbus_policy(&name[3], transport, geteuid()))
+                goto out;
 
-      ((DBusTransportKdbus*)transport)->sender = name;
+              ((DBusTransportKdbus*)transport)->sender = name;
 
-      if(!reply_1_data(message, DBUS_TYPE_STRING, &name, transport->connection))
-        return 0;
+              if(!reply_1_data(message, DBUS_TYPE_STRING, &name, transport->connection))
+                return 0;  //on success we can not free name
 
-    out:
-      free(name);
+              out:
+              free(name);
+              return -1;
+            }
+        }
     }
-  else
-    return 1;  //send to daemon
 
-  return -1;
+  return 1;  //send message to daemon
 }
 
 #if KDBUS_MSG_DECODE_DEBUG == 1
@@ -1291,26 +1300,20 @@ do_writing (DBusTransport *transport)
 
       if(pDestination)
         {
-          if(!strcmp(pDestination, DBUS_SERVICE_DBUS))
+          int ret;
+
+          ret = capture_hello_message(transport, pDestination, message);
+          if(ret < 0)  //error
+            {
+              bytes_written = -1;
+              goto written;
+            }
+          else if(ret == 0)  //hello message captured and handled correctly
             {
-              if(!strcmp(dbus_message_get_interface(message), DBUS_INTERFACE_DBUS))
-                {
-                  int ret;
-
-                  ret = emulateOrgFreedesktopDBus(transport, message);
-                  if(ret < 0)
-                    {
-                      bytes_written = -1;
-                      goto written;
-                    }
-                  else if(ret == 0)
-                    {
-                      bytes_written = total_bytes_to_write;
-                      goto written;
-                    }
-                  //else send to "daemon" as to normal recipient
-                }
+              bytes_written = total_bytes_to_write;
+              goto written;
             }
+          //else send as regular message
         }
 
       bytes_written = kdbus_write_msg(kdbus_transport, message, pDestination);
@@ -1540,7 +1543,8 @@ kdbus_handle_watch (DBusTransport *transport,
 }
 
 /**
- * Copy-paste from socket transport renamed to kdbus_transport.
+ * Copy-paste from socket transport, but socket_transport renamed to kdbus_transport
+ * and _dbus_close_socket replaced with close().
  */
 static void
 kdbus_disconnect (DBusTransport *transport)
@@ -1551,7 +1555,13 @@ kdbus_disconnect (DBusTransport *transport)
 
   free_watches (transport);
 
-  _dbus_close_socket (kdbus_transport->fd, NULL);
+  again:
+   if (close (kdbus_transport->fd) < 0)
+     {
+       if (errno == EINTR)
+         goto again;
+     }
+
   kdbus_transport->fd = -1;
 }
 
@@ -1623,60 +1633,39 @@ kdbus_do_iteration (DBusTransport *transport,
                  kdbus_transport->write_watch,
                  kdbus_transport->fd);
 
-  /* the passed in DO_READING/DO_WRITING flags indicate whether to
-   * read/write messages, but regardless of those we may need to block
-   * for reading/writing to do auth.  But if we do reading for auth,
-   * we don't want to read any messages yet if not given DO_READING.
-   */
-
    poll_fd.fd = kdbus_transport->fd;
    poll_fd.events = 0;
 
-   if (_dbus_transport_try_to_authenticate (transport))
-   {
-      /* This is kind of a hack; if we have stuff to write, then try
-       * to avoid the poll. This is probably about a 5% speedup on an
-       * echo client/server.
-       *
-       * If both reading and writing were requested, we want to avoid this
-       * since it could have funky effects:
-       *   - both ends spinning waiting for the other one to read
-       *     data so they can finish writing
-       *   - prioritizing all writing ahead of reading
-       */
-      if ((flags & DBUS_ITERATION_DO_WRITING) &&
-          !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
-          !transport->disconnected &&
-          _dbus_connection_has_messages_to_send_unlocked (transport->connection))
-      {
-         do_writing (transport);
-
-         if (transport->disconnected ||
-              !_dbus_connection_has_messages_to_send_unlocked (transport->connection))
-            goto out;
-      }
-
-      /* If we get here, we decided to do the poll() after all */
-      _dbus_assert (kdbus_transport->read_watch);
-      if (flags & DBUS_ITERATION_DO_READING)
-            poll_fd.events |= _DBUS_POLLIN;
-
-      _dbus_assert (kdbus_transport->write_watch);
-      if (flags & DBUS_ITERATION_DO_WRITING)
-         poll_fd.events |= _DBUS_POLLOUT;
-   }
-   else
-   {
-      DBusAuthState auth_state;
-
-      auth_state = _dbus_auth_do_work (transport->auth);
-
-      if (transport->receive_credentials_pending || auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
-            poll_fd.events |= _DBUS_POLLIN;
-
-      if (transport->send_credentials_pending || auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
-            poll_fd.events |= _DBUS_POLLOUT;
-   }
+   /* This is kind of a hack; if we have stuff to write, then try
+    * to avoid the poll. This is probably about a 5% speedup on an
+    * echo client/server.
+    *
+    * If both reading and writing were requested, we want to avoid this
+    * since it could have funky effects:
+    *   - both ends spinning waiting for the other one to read
+    *     data so they can finish writing
+    *   - prioritizing all writing ahead of reading
+    */
+   if ((flags & DBUS_ITERATION_DO_WRITING) &&
+       !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
+       !transport->disconnected &&
+       _dbus_connection_has_messages_to_send_unlocked (transport->connection))
+     {
+       do_writing (transport);
+
+       if (transport->disconnected ||
+           !_dbus_connection_has_messages_to_send_unlocked (transport->connection))
+         goto out;
+     }
+
+   /* If we get here, we decided to do the poll() after all */
+   _dbus_assert (kdbus_transport->read_watch);
+   if (flags & DBUS_ITERATION_DO_READING)
+     poll_fd.events |= _DBUS_POLLIN;
+
+   _dbus_assert (kdbus_transport->write_watch);
+   if (flags & DBUS_ITERATION_DO_WRITING)
+     poll_fd.events |= _DBUS_POLLOUT;
 
    if (poll_fd.events)
    {
@@ -1700,10 +1689,7 @@ kdbus_do_iteration (DBusTransport *transport,
       poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
 
       if (poll_res < 0 && _dbus_get_is_errno_eintr ())
-      {
-         _dbus_verbose ("Error from _dbus_poll(): %s\n", _dbus_strerror_from_errno ());
-        goto again;
-      }
+        goto again;
 
       if (flags & DBUS_ITERATION_BLOCK)
       {
@@ -1803,7 +1789,7 @@ static const DBusTransportVTable kdbus_vtable = {
  * @returns the new transport, or #NULL if no memory.
  */
 static DBusTransport*
-_dbus_transport_new_kdbus_transport (int fd, const DBusString *address)
+new_kdbus_transport (int fd, const DBusString *address)
 {
        DBusTransportKdbus *kdbus_transport;
 
@@ -1833,8 +1819,8 @@ _dbus_transport_new_kdbus_transport (int fd, const DBusString *address)
   kdbus_transport->fd = fd;
 
   /* These values should probably be tunable or something. */
-  kdbus_transport->max_bytes_read_per_iteration = 16384;
-  kdbus_transport->max_bytes_written_per_iteration = 16384;
+  kdbus_transport->max_bytes_read_per_iteration = MAX_BYTES_PER_ITERATION;
+  kdbus_transport->max_bytes_written_per_iteration = MAX_BYTES_PER_ITERATION;
 
   kdbus_transport->kdbus_mmap_ptr = NULL;
   kdbus_transport->memfd = -1;
@@ -1911,7 +1897,7 @@ static DBusTransport* _dbus_transport_new_for_kdbus (const char *path, DBusError
 
        _dbus_verbose ("Successfully connected to kdbus bus %s\n", path);
 
-       transport = _dbus_transport_new_kdbus_transport (fd, &address);
+       transport = new_kdbus_transport (fd, &address);
        if (transport == NULL)
     {
                dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
@@ -1923,10 +1909,15 @@ static DBusTransport* _dbus_transport_new_for_kdbus (const char *path, DBusError
        return transport;
 
        failed_1:
-               _dbus_close_socket (fd, NULL);
-       failed_0:
-               _dbus_string_free (&address);
-       return NULL;
+  again:
+   if (close (fd) < 0)
+     {
+       if (errno == EINTR)
+         goto again;
+     }
+  failed_0:
+       _dbus_string_free (&address);
+  return NULL;
 }