[pulseaudio-commits] [Git][pulseaudio/pulseaudio][master] 2 commits: core-util: Avoid usage of pa_strbuf in pa_escape()

Tanu Kaskinen gitlab at gitlab.freedesktop.org
Sat May 25 12:56:57 UTC 2019



Tanu Kaskinen pushed to branch master at PulseAudio / pulseaudio


Commits:
ab9fed95 by Georg Chini at 2019-05-25T12:28:41Z
core-util: Avoid usage of pa_strbuf in pa_escape()

The current code uses a pa_strbuf to construct the escaped string. This
will generate a linked list member for each character which may be very
inefficient.
This patch avoids the use of pa_strbuf by allocating a sufficiently large
string which can be filled with the output data.

- - - - -
755884d1 by Georg Chini at 2019-05-25T12:28:41Z
core: add simple message interface

This patch adds a new feature to the core which allows to send messages
to objects. An object can register/unregister a message handler with
pa_message_handler_{register, unregister}() while a message can be sent
to the handler using the pa_message_handler_send_message() function.
A message has 4 arguments (apart from passing the core):

object_path: The path identifying the object that will receive the message
message: message command
message_parameters: A string containing additional parameters
response: Pointer to a response string that will be filled by the
          message handler. The caller is responsible to free the string.

The patch is a precondition for the following patches that allow clients
to send messages to pulseaudio objects.

There is no restriction on object names, except that an object path
always starts with a "/". The intention is to use a path-like syntax,
for example /core/sink_1 for a sink or /name/instances/index for modules.
The exact naming convention still needs to be agreed.

- - - - -


7 changed files:

- src/Makefile.am
- src/pulsecore/core-util.c
- src/pulsecore/core.c
- src/pulsecore/core.h
- src/pulsecore/meson.build
- + src/pulsecore/message-handler.c
- + src/pulsecore/message-handler.h


Changes:

=====================================
src/Makefile.am
=====================================
@@ -981,6 +981,7 @@ libpulsecore_ at PA_MAJORMINOR@_la_SOURCES = \
 		pulsecore/core-scache.c pulsecore/core-scache.h \
 		pulsecore/core-subscribe.c pulsecore/core-subscribe.h \
 		pulsecore/core.c pulsecore/core.h \
+		pulsecore/message-handler.c pulsecore/message-handler.h \
 		pulsecore/hook-list.c pulsecore/hook-list.h \
 		pulsecore/ltdl-helper.c pulsecore/ltdl-helper.h \
 		pulsecore/modargs.c pulsecore/modargs.h \


=====================================
src/pulsecore/core-util.c
=====================================
@@ -3093,23 +3093,39 @@ char *pa_replace(const char*s, const char*a, const char *b) {
 char *pa_escape(const char *p, const char *chars) {
     const char *s;
     const char *c;
-    pa_strbuf *buf = pa_strbuf_new();
+    char *out_string, *output;
+    int char_count = strlen(p);
 
+    /* Maximum number of characters in output string
+     * including trailing 0. */
+    char_count = 2 * char_count + 1;
+
+    /* allocate output string */
+    out_string = pa_xmalloc(char_count);
+    output = out_string;
+
+    /* write output string */
     for (s = p; *s; ++s) {
         if (*s == '\\')
-            pa_strbuf_putc(buf, '\\');
+            *output++ = '\\';
         else if (chars) {
             for (c = chars; *c; ++c) {
                 if (*s == *c) {
-                    pa_strbuf_putc(buf, '\\');
+                    *output++ = '\\';
                     break;
                 }
             }
         }
-        pa_strbuf_putc(buf, *s);
+        *output++ = *s;
     }
 
-    return pa_strbuf_to_string_free(buf);
+    *output = 0;
+
+    /* Remove trailing garbage */
+    output = pa_xstrdup(out_string);
+
+    pa_xfree(out_string);
+    return output;
 }
 
 char *pa_unescape(char *p) {


=====================================
src/pulsecore/core.c
=====================================
@@ -103,6 +103,7 @@ pa_core* pa_core_new(pa_mainloop_api *m, bool shared, bool enable_memfd, size_t
 
     c->namereg = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
     c->shared = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
+    c->message_handlers = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
 
     c->default_source = NULL;
     c->default_sink = NULL;
@@ -204,6 +205,9 @@ static void core_free(pa_object *o) {
     pa_assert(pa_hashmap_isempty(c->shared));
     pa_hashmap_free(c->shared);
 
+    pa_assert(pa_hashmap_isempty(c->message_handlers));
+    pa_hashmap_free(c->message_handlers);
+
     pa_assert(pa_hashmap_isempty(c->modules_pending_unload));
     pa_hashmap_free(c->modules_pending_unload);
 


=====================================
src/pulsecore/core.h
=====================================
@@ -164,7 +164,7 @@ struct pa_core {
     pa_idxset *clients, *cards, *sinks, *sources, *sink_inputs, *source_outputs, *modules, *scache;
 
     /* Some hashmaps for all sorts of entities */
-    pa_hashmap *namereg, *shared;
+    pa_hashmap *namereg, *shared, *message_handlers;
 
     /* The default sink/source as configured by the user. If the user hasn't
      * explicitly configured anything, these are set to NULL. These are strings


=====================================
src/pulsecore/meson.build
=====================================
@@ -20,6 +20,7 @@ libpulsecore_sources = [
   'filter/lfe-filter.c',
   'hook-list.c',
   'ltdl-helper.c',
+  'message-handler.c',
   'mix.c',
   'modargs.c',
   'modinfo.c',
@@ -75,6 +76,7 @@ libpulsecore_headers = [
   'filter/lfe-filter.h',
   'hook-list.h',
   'ltdl-helper.h',
+  'message-handler.h',
   'mix.h',
   'modargs.h',
   'modinfo.h',


=====================================
src/pulsecore/message-handler.c
=====================================
@@ -0,0 +1,104 @@
+/***
+  This file is part of PulseAudio.
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2.1 of the License,
+  or (at your option) any later version.
+
+  PulseAudio is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#include <pulse/xmalloc.h>
+
+#include <pulsecore/core.h>
+#include <pulsecore/core-util.h>
+#include <pulsecore/log.h>
+#include <pulsecore/macro.h>
+
+#include "message-handler.h"
+
+/* Message handler functions */
+
+/* Register message handler for the specified object. object_path must be a unique name starting with "/". */
+void pa_message_handler_register(pa_core *c, const char *object_path, const char *description, pa_message_handler_cb_t cb, void *userdata) {
+    struct pa_message_handler *handler;
+
+    pa_assert(c);
+    pa_assert(object_path);
+    pa_assert(cb);
+    pa_assert(userdata);
+
+    /* Ensure that the object path is not empty and starts with "/". */
+    pa_assert(object_path[0] == '/');
+
+    handler = pa_xnew0(struct pa_message_handler, 1);
+    handler->userdata = userdata;
+    handler->callback = cb;
+    handler->object_path = pa_xstrdup(object_path);
+    handler->description = pa_xstrdup(description);
+
+    pa_assert_se(pa_hashmap_put(c->message_handlers, handler->object_path, handler) == 0);
+}
+
+/* Unregister a message handler */
+void pa_message_handler_unregister(pa_core *c, const char *object_path) {
+    struct pa_message_handler *handler;
+
+    pa_assert(c);
+    pa_assert(object_path);
+
+    pa_assert_se(handler = pa_hashmap_remove(c->message_handlers, object_path));
+
+    pa_xfree(handler->object_path);
+    pa_xfree(handler->description);
+    pa_xfree(handler);
+}
+
+/* Send a message to an object identified by object_path */
+int pa_message_handler_send_message(pa_core *c, const char *object_path, const char *message, const char *message_parameters, char **response) {
+    struct pa_message_handler *handler;
+
+    pa_assert(c);
+    pa_assert(object_path);
+    pa_assert(message);
+    pa_assert(response);
+
+    *response = NULL;
+
+    if (!(handler = pa_hashmap_get(c->message_handlers, object_path)))
+        return -PA_ERR_NOENTITY;
+
+    /* The handler is expected to return an error code and may also
+       return an error string in response */
+    return handler->callback(handler->object_path, message, message_parameters, response, handler->userdata);
+}
+
+/* Set handler description */
+int pa_message_handler_set_description(pa_core *c, const char *object_path, const char *description) {
+    struct pa_message_handler *handler;
+
+    pa_assert(c);
+    pa_assert(object_path);
+
+    if (!(handler = pa_hashmap_get(c->message_handlers, object_path)))
+        return -PA_ERR_NOENTITY;
+
+    pa_xfree(handler->description);
+    handler->description = pa_xstrdup(description);
+
+    return PA_OK;
+}


=====================================
src/pulsecore/message-handler.h
=====================================
@@ -0,0 +1,50 @@
+#ifndef foocoremessageshfoo
+#define foocoremessageshfoo
+
+/***
+  This file is part of PulseAudio.
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2.1 of the License,
+  or (at your option) any later version.
+
+  PulseAudio is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <pulsecore/core.h>
+
+/* Message handler types and functions */
+
+/* Prototype for message callback */
+typedef int (*pa_message_handler_cb_t)(
+        const char *object_path,
+        const char *message,
+        const char *message_parameters,
+        char **response,
+        void *userdata);
+
+/* Message handler object */
+struct pa_message_handler {
+    char *object_path;
+    char *description;
+    pa_message_handler_cb_t callback;
+    void *userdata;
+};
+
+/* Handler registration */
+void pa_message_handler_register(pa_core *c, const char *object_path, const char *description, pa_message_handler_cb_t cb, void *userdata);
+void pa_message_handler_unregister(pa_core *c, const char *object_path);
+
+/* Send message to the specified object path */
+int pa_message_handler_send_message(pa_core *c, const char *object_path, const char *message, const char *message_parameters, char **response);
+
+/* Set handler description */
+int pa_message_handler_set_description(pa_core *c, const char *object_path, const char *description);
+#endif



View it on GitLab: https://gitlab.freedesktop.org/pulseaudio/pulseaudio/compare/f34ea0f0c303bf557fb4af6bda5e9588c7c20ffb...755884d131aec00d0ecd9190ae349644637e1291

-- 
View it on GitLab: https://gitlab.freedesktop.org/pulseaudio/pulseaudio/compare/f34ea0f0c303bf557fb4af6bda5e9588c7c20ffb...755884d131aec00d0ecd9190ae349644637e1291
You're receiving this email because of your account on gitlab.freedesktop.org.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/pulseaudio-commits/attachments/20190525/d5f93e3d/attachment-0001.html>


More information about the pulseaudio-commits mailing list