API constification set #3
Some API functions perform validation routines which may modify the
'error' attribute of a context object. For API functions where the
"primary" object is not a context object, and the object holds a non-
const context pointer, this internal mechanism could be successfully
hidden - such functions were constified in my previous two patch sets.
However, some functions, namely those related directly to context
objects, had to be passed over previously. These could not be
constified because the validation routines accessed the context object
directly to change the error attribute.
This patch set addresses this shortcoming.
Firstly the patch set moves the context's error attribute behind a
pointer. The validation routines are then changed to store the error
value via this pointer. That then allows a collection of further API
functions to be constified.
(actually pa_context_errno and the two rttime ones could have been done
previously on second inspection)
Although the indirection of the error attribute is obviously ever so
slightly worse off for efficiency, it is surely worth the price. After
all, the error setting of the validation checks is just an artifact of
an internal mechanism and should not be allowed to influence the public
API like it currently does.
From b1659b17ef72f44ab299384026027eb4f8afde4f Mon Sep 17 00:00:00 2001
From: Lyndon Brown <[email protected]>
Date: Thu, 7 Jun 2018 02:43:56 +0100
Subject: context: hide error attr behind pointer
Paves the way towards more of the API using const pointers.
(See email discussion).
diff --git a/src/pulse/context.c b/src/pulse/context.c
index adbeb153..d298ae3e 100644
--- a/src/pulse/context.c
+++ b/src/pulse/context.c
@@ -139,6 +139,9 @@ pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char *
c = pa_xnew0(pa_context, 1);
PA_REFCNT_INIT(c);
+ c->error = pa_xnew0(pa_context_error, 1);
+ assert(c->error);
+
c->proplist = p ? pa_proplist_copy(p) : pa_proplist_new();
if (name)
@@ -156,7 +159,7 @@ pa_context *pa_context_new_with_proplist(pa_mainloop_api *mainloop, const char *
PA_LLIST_HEAD_INIT(pa_stream, c->streams);
PA_LLIST_HEAD_INIT(pa_operation, c->operations);
- c->error = PA_OK;
+ c->error->error = PA_OK;
c->state = PA_CONTEXT_UNCONNECTED;
reset_callbacks(c);
@@ -315,7 +318,7 @@ int pa_context_set_error(pa_context *c, int error) {
pa_assert(error < PA_ERR_MAX);
if (c)
- c->error = error;
+ c->error->error = error;
return error;
}
@@ -1072,7 +1075,7 @@ int pa_context_errno(pa_context *c) {
pa_assert(PA_REFCNT_VALUE(c) >= 1);
- return c->error;
+ return c->error->error;
}
void pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb, void *userdata) {
diff --git a/src/pulse/internal.h b/src/pulse/internal.h
index 01d2b6e4..04f35e9a 100644
--- a/src/pulse/internal.h
+++ b/src/pulse/internal.h
@@ -55,6 +55,10 @@
#define PA_PROTOCOL_FLAG_SHM 0x80000000U
#define PA_PROTOCOL_FLAG_MEMFD 0x40000000U
+typedef struct pa_context_error {
+ int error;
+} pa_context_error;
+
struct pa_context {
PA_REFCNT_DECLARE;
@@ -80,7 +84,7 @@ struct pa_context {
uint32_t version;
uint32_t ctag;
uint32_t csyncid;
- int error;
+ pa_context_error *error;
pa_context_state_t state;
pa_context_notify_cb_t state_callback;
From 590ee9dd0c9d513872ee0150c52f895a01dd04a8 Mon Sep 17 00:00:00 2001
From: Lyndon Brown <[email protected]>
Date: Thu, 7 Jun 2018 02:50:54 +0100
Subject: add internal pa_context_error_set_error function
pa_context_set_error now redirects to this
Further paving the way towards more of the API using const pointers.
(See email discussion).
diff --git a/src/pulse/context.c b/src/pulse/context.c
index d298ae3e..45ff3fbb 100644
--- a/src/pulse/context.c
+++ b/src/pulse/context.c
@@ -313,16 +313,20 @@ void pa_context_set_state(pa_context *c, pa_context_state_t st) {
pa_context_unref(c);
}
-int pa_context_set_error(pa_context *c, int error) {
+int pa_context_error_set_error(pa_context_error *ce, int error) {
pa_assert(error >= 0);
pa_assert(error < PA_ERR_MAX);
- if (c)
- c->error->error = error;
+ if (ce)
+ ce->error = error;
return error;
}
+int pa_context_set_error(pa_context *c, int error) {
+ return pa_context_error_set_error(c->error, error);
+}
+
void pa_context_fail(pa_context *c, int error) {
pa_assert(c);
pa_assert(PA_REFCNT_VALUE(c) >= 1);
diff --git a/src/pulse/internal.h b/src/pulse/internal.h
index 04f35e9a..725b7b46 100644
--- a/src/pulse/internal.h
+++ b/src/pulse/internal.h
@@ -281,6 +281,8 @@ pa_operation* pa_context_send_simple_command(pa_context *c, uint32_t command, vo
void pa_stream_set_state(pa_stream *s, pa_stream_state_t st);
+int pa_context_error_set_error(pa_context_error *ce, int error);
+
pa_tagstruct *pa_tagstruct_command(pa_context *c, uint32_t command, uint32_t *tag);
#define PA_CHECK_VALIDITY(context, expression, error) \
From 7788a51b946187a9fc2bd85cb03d683e65cde4f8 Mon Sep 17 00:00:00 2001
From: Lyndon Brown <[email protected]>
Date: Thu, 7 Jun 2018 02:54:44 +0100
Subject: use pa_context_error_set_error for validation
The indirection here, accessing the error storage via a non-const pointer
held by the context object, will now enable more of the API to become
const.
Use of the validation routines does not change.
diff --git a/src/pulse/internal.h b/src/pulse/internal.h
index 725b7b46..b10cde00 100644
--- a/src/pulse/internal.h
+++ b/src/pulse/internal.h
@@ -285,16 +285,16 @@ int pa_context_error_set_error(pa_context_error *ce, int error);
pa_tagstruct *pa_tagstruct_command(pa_context *c, uint32_t command, uint32_t *tag);
-#define PA_CHECK_VALIDITY(context, expression, error) \
- do { \
- if (!(expression)) \
- return -pa_context_set_error((context), (error)); \
+#define PA_CHECK_VALIDITY(context, expression, error) \
+ do { \
+ if (!(expression)) \
+ return -pa_context_error_set_error((context)->error, (error)); \
} while(false)
#define PA_CHECK_VALIDITY_RETURN_ANY(context, expression, error, value) \
do { \
if (!(expression)) { \
- pa_context_set_error((context), (error)); \
+ pa_context_error_set_error((context)->error, (error)); \
return value; \
} \
} while(false)
@@ -302,15 +302,15 @@ pa_tagstruct *pa_tagstruct_command(pa_context *c, uint32_t command, uint32_t *ta
#define PA_CHECK_VALIDITY_RETURN_NULL(context, expression, error) \
PA_CHECK_VALIDITY_RETURN_ANY(context, expression, error, NULL)
-#define PA_FAIL(context, error) \
- do { \
- return -pa_context_set_error((context), (error)); \
+#define PA_FAIL(context, error) \
+ do { \
+ return -pa_context_error_set_error((context)->error, (error)); \
} while(false)
-#define PA_FAIL_RETURN_ANY(context, error, value) \
- do { \
- pa_context_set_error((context), (error)); \
- return value; \
+#define PA_FAIL_RETURN_ANY(context, error, value) \
+ do { \
+ pa_context_error_set_error((context)->error, (error)); \
+ return value; \
} while(false)
#define PA_FAIL_RETURN_NULL(context, error) \
From 8b13787346c108a627ed5f507baeab0d6144a28a Mon Sep 17 00:00:00 2001
From: Lyndon Brown <[email protected]>
Date: Thu, 7 Jun 2018 03:51:09 +0100
Subject: context: pa_context_errno: constify
diff --git a/src/pulse/context.c b/src/pulse/context.c
index 45ff3fbb..d1c93648 100644
--- a/src/pulse/context.c
+++ b/src/pulse/context.c
@@ -1072,7 +1072,7 @@ pa_context_state_t pa_context_get_state(const pa_context *c) {
return c->state;
}
-int pa_context_errno(pa_context *c) {
+int pa_context_errno(const pa_context *c) {
if (!c)
return PA_ERR_INVALID;
diff --git a/src/pulse/context.h b/src/pulse/context.h
index 7a38ff23..37333fca 100644
--- a/src/pulse/context.h
+++ b/src/pulse/context.h
@@ -190,7 +190,7 @@ void pa_context_set_state_callback(pa_context *c, pa_context_notify_cb_t cb, voi
void pa_context_set_event_callback(pa_context *p, pa_context_event_cb_t cb, void *userdata);
/** Return the error number of the last failed operation */
-int pa_context_errno(pa_context *c);
+int pa_context_errno(const pa_context *c);
/** Return non-zero if some data is pending to be written to the connection */
int pa_context_is_pending(pa_context *c);
From db0f2c61b97dcc2450269e3c8ea7d2448f6beddf Mon Sep 17 00:00:00 2001
From: Lyndon Brown <[email protected]>
Date: Thu, 7 Jun 2018 03:52:50 +0100
Subject: context: pa_context_is_pending: constify
diff --git a/src/pulse/context.c b/src/pulse/context.c
index d1c93648..78531c08 100644
--- a/src/pulse/context.c
+++ b/src/pulse/context.c
@@ -1110,7 +1110,7 @@ void pa_context_set_event_callback(pa_context *c, pa_context_event_cb_t cb, void
c->event_userdata = userdata;
}
-int pa_context_is_pending(pa_context *c) {
+int pa_context_is_pending(const pa_context *c) {
pa_assert(c);
pa_assert(PA_REFCNT_VALUE(c) >= 1);
diff --git a/src/pulse/context.h b/src/pulse/context.h
index 37333fca..d8301adc 100644
--- a/src/pulse/context.h
+++ b/src/pulse/context.h
@@ -193,7 +193,7 @@ void pa_context_set_event_callback(pa_context *p, pa_context_event_cb_t cb, void
int pa_context_errno(const pa_context *c);
/** Return non-zero if some data is pending to be written to the connection */
-int pa_context_is_pending(pa_context *c);
+int pa_context_is_pending(const pa_context *c);
/** Return the current context status */
pa_context_state_t pa_context_get_state(const pa_context *c);
From 439bbe705afc07ee711e2da59666048afe612e72 Mon Sep 17 00:00:00 2001
From: Lyndon Brown <[email protected]>
Date: Thu, 7 Jun 2018 03:53:45 +0100
Subject: context: pa_context_is_local: constify
diff --git a/src/pulse/context.c b/src/pulse/context.c
index 78531c08..fe9cf426 100644
--- a/src/pulse/context.c
+++ b/src/pulse/context.c
@@ -1279,7 +1279,7 @@ pa_operation* pa_context_set_default_source(pa_context *c, const char *name, pa_
return o;
}
-int pa_context_is_local(pa_context *c) {
+int pa_context_is_local(const pa_context *c) {
pa_assert(c);
pa_assert(PA_REFCNT_VALUE(c) >= 1);
diff --git a/src/pulse/context.h b/src/pulse/context.h
index d8301adc..8ab82cdd 100644
--- a/src/pulse/context.h
+++ b/src/pulse/context.h
@@ -227,7 +227,7 @@ pa_operation* pa_context_set_default_sink(pa_context *c, const char *name, pa_co
pa_operation* pa_context_set_default_source(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata);
/** Returns 1 when the connection is to a local daemon. Returns negative when no connection has been made yet. */
-int pa_context_is_local(pa_context *c);
+int pa_context_is_local(const pa_context *c);
/** Set a different application name for context on the server. */
pa_operation* pa_context_set_name(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata);
From 882ad427d17d816c32ce2a296c748bd64ae36967 Mon Sep 17 00:00:00 2001
From: Lyndon Brown <[email protected]>
Date: Thu, 7 Jun 2018 03:55:42 +0100
Subject: context: pa_context_get_server: constify
diff --git a/src/pulse/context.c b/src/pulse/context.c
index fe9cf426..8af74c71 100644
--- a/src/pulse/context.c
+++ b/src/pulse/context.c
@@ -1323,7 +1323,7 @@ const char* pa_get_library_version(void) {
return pa_get_headers_version();
}
-const char* pa_context_get_server(pa_context *c) {
+const char* pa_context_get_server(const pa_context *c) {
pa_assert(c);
pa_assert(PA_REFCNT_VALUE(c) >= 1);
diff --git a/src/pulse/context.h b/src/pulse/context.h
index 8ab82cdd..811272dc 100644
--- a/src/pulse/context.h
+++ b/src/pulse/context.h
@@ -233,7 +233,7 @@ int pa_context_is_local(const pa_context *c);
pa_operation* pa_context_set_name(pa_context *c, const char *name, pa_context_success_cb_t cb, void *userdata);
/** Return the server name this context is connected to. */
-const char* pa_context_get_server(pa_context *c);
+const char* pa_context_get_server(const pa_context *c);
/** Return the protocol version of the library. */
uint32_t pa_context_get_protocol_version(const pa_context *c);
From 006e74bf014429bc7701ce714769f1fd7bee224e Mon Sep 17 00:00:00 2001
From: Lyndon Brown <[email protected]>
Date: Thu, 7 Jun 2018 03:56:16 +0100
Subject: context: pa_context_get_server_protocol_version: constify
diff --git a/src/pulse/context.c b/src/pulse/context.c
index 8af74c71..d9bdf064 100644
--- a/src/pulse/context.c
+++ b/src/pulse/context.c
@@ -1342,7 +1342,7 @@ uint32_t pa_context_get_protocol_version(const pa_context *c) {
return PA_PROTOCOL_VERSION;
}
-uint32_t pa_context_get_server_protocol_version(pa_context *c) {
+uint32_t pa_context_get_server_protocol_version(const pa_context *c) {
pa_assert(c);
pa_assert(PA_REFCNT_VALUE(c) >= 1);
diff --git a/src/pulse/context.h b/src/pulse/context.h
index 811272dc..1f87fa9e 100644
--- a/src/pulse/context.h
+++ b/src/pulse/context.h
@@ -240,7 +240,7 @@ uint32_t pa_context_get_protocol_version(const pa_context *c);
/** Return the protocol version of the connected server.
* Returns PA_INVALID_INDEX on error. */
-uint32_t pa_context_get_server_protocol_version(pa_context *c);
+uint32_t pa_context_get_server_protocol_version(const pa_context *c);
/** Update the property list of the client, adding new entries. Please
* note that it is highly recommended to set as many properties
From 1bfe6e6a2b2c16224b784adc0e80cdc036b38838 Mon Sep 17 00:00:00 2001
From: Lyndon Brown <[email protected]>
Date: Thu, 7 Jun 2018 03:56:51 +0100
Subject: context: pa_context_get_index: constify
diff --git a/src/pulse/context.c b/src/pulse/context.c
index d9bdf064..1eb8258d 100644
--- a/src/pulse/context.c
+++ b/src/pulse/context.c
@@ -1365,7 +1365,7 @@ pa_tagstruct *pa_tagstruct_command(pa_context *c, uint32_t command, uint32_t *ta
return t;
}
-uint32_t pa_context_get_index(pa_context *c) {
+uint32_t pa_context_get_index(const pa_context *c) {
pa_assert(c);
pa_assert(PA_REFCNT_VALUE(c) >= 1);
diff --git a/src/pulse/context.h b/src/pulse/context.h
index 1f87fa9e..d306b63c 100644
--- a/src/pulse/context.h
+++ b/src/pulse/context.h
@@ -256,7 +256,7 @@ pa_operation *pa_context_proplist_remove(pa_context *c, const char *const keys[]
* identified in the server with. This is useful for usage with the
* introspection functions, such as pa_context_get_client_info().
* Returns PA_INVALID_INDEX on error. \since 0.9.11 */
-uint32_t pa_context_get_index(pa_context *s);
+uint32_t pa_context_get_index(const pa_context *s);
/** Create a new timer event source for the specified time (wrapper
* for mainloop->time_new). \since 0.9.16 */
From e1b6fc0431fe2fd7af14e63ad33e34169dc18259 Mon Sep 17 00:00:00 2001
From: Lyndon Brown <[email protected]>
Date: Thu, 7 Jun 2018 04:00:42 +0100
Subject: context: pa_context_rttime_new: constify context pointer
diff --git a/src/pulse/context.c b/src/pulse/context.c
index 1eb8258d..13012eed 100644
--- a/src/pulse/context.c
+++ b/src/pulse/context.c
@@ -1595,7 +1595,7 @@ finish:
pa_proplist_free(pl);
}
-pa_time_event* pa_context_rttime_new(pa_context *c, pa_usec_t usec, pa_time_event_cb_t cb, void *userdata) {
+pa_time_event* pa_context_rttime_new(const pa_context *c, pa_usec_t usec, pa_time_event_cb_t cb, void *userdata) {
struct timeval tv;
pa_assert(c);
diff --git a/src/pulse/context.h b/src/pulse/context.h
index d306b63c..e9d3ca0e 100644
--- a/src/pulse/context.h
+++ b/src/pulse/context.h
@@ -260,7 +260,7 @@ uint32_t pa_context_get_index(const pa_context *s);
/** Create a new timer event source for the specified time (wrapper
* for mainloop->time_new). \since 0.9.16 */
-pa_time_event* pa_context_rttime_new(pa_context *c, pa_usec_t usec, pa_time_event_cb_t cb, void *userdata);
+pa_time_event* pa_context_rttime_new(const pa_context *c, pa_usec_t usec, pa_time_event_cb_t cb, void *userdata);
/** Restart a running or expired timer event source (wrapper for
* mainloop->time_restart). \since 0.9.16 */
From 923e8eaa6ab4764b7c811473e272cddbe84b57a9 Mon Sep 17 00:00:00 2001
From: Lyndon Brown <[email protected]>
Date: Thu, 7 Jun 2018 04:02:16 +0100
Subject: context: pa_context_rttime_restart: constify context pointer
diff --git a/src/pulse/context.c b/src/pulse/context.c
index 13012eed..97ee2ba8 100644
--- a/src/pulse/context.c
+++ b/src/pulse/context.c
@@ -1610,7 +1610,7 @@ pa_time_event* pa_context_rttime_new(const pa_context *c, pa_usec_t usec, pa_tim
return c->mainloop->time_new(c->mainloop, &tv, cb, userdata);
}
-void pa_context_rttime_restart(pa_context *c, pa_time_event *e, pa_usec_t usec) {
+void pa_context_rttime_restart(const pa_context *c, pa_time_event *e, pa_usec_t usec) {
struct timeval tv;
pa_assert(c);
diff --git a/src/pulse/context.h b/src/pulse/context.h
index e9d3ca0e..dd90c21e 100644
--- a/src/pulse/context.h
+++ b/src/pulse/context.h
@@ -264,7 +264,7 @@ pa_time_event* pa_context_rttime_new(const pa_context *c, pa_usec_t usec, pa_tim
/** Restart a running or expired timer event source (wrapper for
* mainloop->time_restart). \since 0.9.16 */
-void pa_context_rttime_restart(pa_context *c, pa_time_event *e, pa_usec_t usec);
+void pa_context_rttime_restart(const pa_context *c, pa_time_event *e, pa_usec_t usec);
/** Return the optimal block size for passing around audio buffers. It
* is recommended to allocate buffers of the size returned here when
From c9cbc1e8c8ee0175114c54d620078f4845d9e575 Mon Sep 17 00:00:00 2001
From: Lyndon Brown <[email protected]>
Date: Thu, 7 Jun 2018 04:03:45 +0100
Subject: context: pa_context_get_tile_size: constify
diff --git a/src/pulse/context.c b/src/pulse/context.c
index 97ee2ba8..535ed7db 100644
--- a/src/pulse/context.c
+++ b/src/pulse/context.c
@@ -1625,7 +1625,7 @@ void pa_context_rttime_restart(const pa_context *c, pa_time_event *e, pa_usec_t
}
}
-size_t pa_context_get_tile_size(pa_context *c, const pa_sample_spec *ss) {
+size_t pa_context_get_tile_size(const pa_context *c, const pa_sample_spec *ss) {
size_t fs, mbs;
pa_assert(c);
diff --git a/src/pulse/context.h b/src/pulse/context.h
index dd90c21e..38656f8a 100644
--- a/src/pulse/context.h
+++ b/src/pulse/context.h
@@ -280,7 +280,7 @@ void pa_context_rttime_restart(const pa_context *c, pa_time_event *e, pa_usec_t
* supposed to be used in a construct such as
* pa_context_get_tile_size(pa_stream_get_context(s),
* pa_stream_get_sample_spec(ss)); \since 0.9.20 */
-size_t pa_context_get_tile_size(pa_context *c, const pa_sample_spec *ss);
+size_t pa_context_get_tile_size(const pa_context *c, const pa_sample_spec *ss);
/** Load the authentication cookie from a file. This function is primarily
* meant for PulseAudio's own tunnel modules, which need to load the cookie
_______________________________________________
pulseaudio-discuss mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/pulseaudio-discuss