+static
+int optional_uint_from_buffer(
+ const struct lttng_buffer_view *view,
+ size_t inner_value_size,
+ size_t *offset,
+ optional_uint64 *value)
+{
+ int ret;
+
+ /*
+ * Those cases are identical except for the optional's inner type width.
+ */
+ switch (inner_value_size) {
+ case sizeof(uint8_t):
+ {
+ LTTNG_OPTIONAL_COMM(uint8_t) *value_comm;
+ const struct lttng_buffer_view optional_uint_view =
+ lttng_buffer_view_from_view(view, *offset,
+ sizeof(*value_comm));
+
+ if (!lttng_buffer_view_is_valid(&optional_uint_view)) {
+ ret = -1;
+ goto end;
+ }
+
+ value_comm = (typeof(value_comm)) optional_uint_view.data;
+ *value = (typeof(*value)) {
+ .is_set = value_comm->is_set,
+ .value = (uint64_t) value_comm->value,
+ };
+
+ *offset += sizeof(*value_comm);
+ break;
+ }
+ case sizeof(uint16_t):
+ {
+ LTTNG_OPTIONAL_COMM(uint16_t) *value_comm;
+ const struct lttng_buffer_view optional_uint_view =
+ lttng_buffer_view_from_view(view, *offset,
+ sizeof(*value_comm));
+
+ if (!lttng_buffer_view_is_valid(&optional_uint_view)) {
+ ret = -1;
+ goto end;
+ }
+
+ value_comm = (typeof(value_comm)) optional_uint_view.data;
+ *value = (typeof(*value)) {
+ .is_set = value_comm->is_set,
+ .value = (uint64_t) value_comm->value,
+ };
+
+ *offset += sizeof(*value_comm);
+ break;
+ }
+ case sizeof(uint32_t):
+ {
+ LTTNG_OPTIONAL_COMM(uint32_t) *value_comm;
+ const struct lttng_buffer_view optional_uint_view =
+ lttng_buffer_view_from_view(view, *offset,
+ sizeof(*value_comm));
+
+ if (!lttng_buffer_view_is_valid(&optional_uint_view)) {
+ ret = -1;
+ goto end;
+ }
+
+ value_comm = (typeof(value_comm)) optional_uint_view.data;
+ *value = (typeof(*value)) {
+ .is_set = value_comm->is_set,
+ .value = (uint64_t) value_comm->value,
+ };
+
+ *offset += sizeof(*value_comm);
+ break;
+ }
+ case sizeof(uint64_t):
+ {
+ LTTNG_OPTIONAL_COMM(uint64_t) *value_comm;
+ const struct lttng_buffer_view optional_uint_view =
+ lttng_buffer_view_from_view(view, *offset,
+ sizeof(*value_comm));
+
+ if (!lttng_buffer_view_is_valid(&optional_uint_view)) {
+ ret = -1;
+ goto end;
+ }
+
+ value_comm = (typeof(value_comm)) optional_uint_view.data;
+ *value = (typeof(*value)) {
+ .is_set = value_comm->is_set,
+ .value = (uint64_t) value_comm->value,
+ };
+
+ *offset += sizeof(*value_comm);
+ break;
+ }
+ default:
+ abort();
+ }
+
+ ret = 0;
+end:
+ return ret;
+}
+