pod: Improve type-safety in SPA POD code

Use direct field access when the type is known, instead of a macro that
includes a cast.
This commit is contained in:
Demi Marie Obenour 2025-06-07 15:21:37 -04:00 committed by Wim Taymans
parent aa2289a25b
commit e4fcbef89a
4 changed files with 17 additions and 19 deletions

View file

@ -186,9 +186,7 @@ SPA_API_DEBUG_POD int spa_debugc_pod(struct spa_debug_context *ctx, int indent,
const struct spa_type_info *info, const struct spa_pod *pod) const struct spa_type_info *info, const struct spa_pod *pod)
{ {
return spa_debugc_pod_value(ctx, indent, info ? info : SPA_TYPE_ROOT, return spa_debugc_pod_value(ctx, indent, info ? info : SPA_TYPE_ROOT,
SPA_POD_TYPE(pod), pod->type, SPA_POD_BODY(pod), pod->size);
SPA_POD_BODY(pod),
SPA_POD_BODY_SIZE(pod));
} }
SPA_API_DEBUG_POD int SPA_API_DEBUG_POD int

View file

@ -94,10 +94,10 @@ SPA_API_POD_COMPARE int spa_pod_compare(const struct spa_pod *pod1,
if (n_vals1 != n_vals2) if (n_vals1 != n_vals2)
return -EINVAL; return -EINVAL;
if (SPA_POD_TYPE(pod1) != SPA_POD_TYPE(pod2)) if (pod1->type != pod2->type)
return -EINVAL; return -EINVAL;
switch (SPA_POD_TYPE(pod1)) { switch (pod1->type) {
case SPA_TYPE_Struct: case SPA_TYPE_Struct:
{ {
const struct spa_pod *p1, *p2; const struct spa_pod *p1, *p2;
@ -145,17 +145,17 @@ SPA_API_POD_COMPARE int spa_pod_compare(const struct spa_pod *pod1,
} }
case SPA_TYPE_Array: case SPA_TYPE_Array:
{ {
if (SPA_POD_BODY_SIZE(pod1) != SPA_POD_BODY_SIZE(pod2)) if (pod1->size != pod2->size)
return -EINVAL; return -EINVAL;
res = memcmp(SPA_POD_BODY(pod1), SPA_POD_BODY(pod2), SPA_POD_BODY_SIZE(pod2)); res = memcmp(SPA_POD_BODY(pod1), SPA_POD_BODY(pod2), pod2->size);
break; break;
} }
default: default:
if (SPA_POD_BODY_SIZE(pod1) != SPA_POD_BODY_SIZE(pod2)) if (pod1->size != pod2->size)
return -EINVAL; return -EINVAL;
res = spa_pod_compare_value(SPA_POD_TYPE(pod1), res = spa_pod_compare_value(pod1->type,
SPA_POD_BODY(pod1), SPA_POD_BODY(pod2), SPA_POD_BODY(pod1), SPA_POD_BODY(pod2),
SPA_POD_BODY_SIZE(pod1)); pod1->size);
break; break;
} }
return res; return res;

View file

@ -262,14 +262,14 @@ SPA_API_POD_FILTER int spa_pod_filter_part(struct spa_pod_builder *b,
uint32_t filter_offset = 0; uint32_t filter_offset = 0;
struct spa_pod_frame f; struct spa_pod_frame f;
switch (SPA_POD_TYPE(pp)) { switch (pp->type) {
case SPA_TYPE_Object: case SPA_TYPE_Object:
if (pf != NULL) { if (pf != NULL) {
struct spa_pod_object *op = (struct spa_pod_object *) pp; struct spa_pod_object *op = (struct spa_pod_object *) pp;
struct spa_pod_object *of = (struct spa_pod_object *) pf; struct spa_pod_object *of = (struct spa_pod_object *) pf;
const struct spa_pod_prop *p1, *p2; const struct spa_pod_prop *p1, *p2;
if (SPA_POD_TYPE(pf) != SPA_POD_TYPE(pp)) if (pf->type != pp->type)
return -EINVAL; return -EINVAL;
spa_pod_builder_push_object(b, &f, op->body.type, op->body.id); spa_pod_builder_push_object(b, &f, op->body.type, op->body.id);
@ -307,7 +307,7 @@ SPA_API_POD_FILTER int spa_pod_filter_part(struct spa_pod_builder *b,
case SPA_TYPE_Struct: case SPA_TYPE_Struct:
if (pf != NULL) { if (pf != NULL) {
if (SPA_POD_TYPE(pf) != SPA_POD_TYPE(pp)) if (pf->type != pp->type)
return -EINVAL; return -EINVAL;
filter_offset = sizeof(struct spa_pod_struct); filter_offset = sizeof(struct spa_pod_struct);
@ -326,9 +326,9 @@ SPA_API_POD_FILTER int spa_pod_filter_part(struct spa_pod_builder *b,
default: default:
if (pf != NULL) { if (pf != NULL) {
if (SPA_POD_SIZE(pp) != SPA_POD_SIZE(pf)) if (pp->size != pf->size)
return -EINVAL; return -EINVAL;
if (memcmp(pp, pf, SPA_POD_SIZE(pp)) != 0) if (memcmp(pp, pf, pp->size) != 0)
return -EINVAL; return -EINVAL;
do_advance = true; do_advance = true;
} }

View file

@ -96,8 +96,8 @@ struct spa_pod_bitmap {
}; };
#define SPA_POD_ARRAY_CHILD(arr) (&((struct spa_pod_array*)(arr))->body.child) #define SPA_POD_ARRAY_CHILD(arr) (&((struct spa_pod_array*)(arr))->body.child)
#define SPA_POD_ARRAY_VALUE_TYPE(arr) (SPA_POD_TYPE(SPA_POD_ARRAY_CHILD(arr))) #define SPA_POD_ARRAY_VALUE_TYPE(arr) (SPA_POD_ARRAY_CHILD(arr)->type)
#define SPA_POD_ARRAY_VALUE_SIZE(arr) (SPA_POD_BODY_SIZE(SPA_POD_ARRAY_CHILD(arr))) #define SPA_POD_ARRAY_VALUE_SIZE(arr) (SPA_POD_ARRAY_CHILD(arr)->size)
#define SPA_POD_ARRAY_N_VALUES(arr) (SPA_POD_ARRAY_VALUE_SIZE(arr) ? ((SPA_POD_BODY_SIZE(arr) - sizeof(struct spa_pod_array_body)) / SPA_POD_ARRAY_VALUE_SIZE(arr)) : 0) #define SPA_POD_ARRAY_N_VALUES(arr) (SPA_POD_ARRAY_VALUE_SIZE(arr) ? ((SPA_POD_BODY_SIZE(arr) - sizeof(struct spa_pod_array_body)) / SPA_POD_ARRAY_VALUE_SIZE(arr)) : 0)
#define SPA_POD_ARRAY_VALUES(arr) SPA_POD_CONTENTS(struct spa_pod_array, arr) #define SPA_POD_ARRAY_VALUES(arr) SPA_POD_CONTENTS(struct spa_pod_array, arr)
@ -114,8 +114,8 @@ struct spa_pod_array {
#define SPA_POD_CHOICE_CHILD(choice) (&((struct spa_pod_choice*)(choice))->body.child) #define SPA_POD_CHOICE_CHILD(choice) (&((struct spa_pod_choice*)(choice))->body.child)
#define SPA_POD_CHOICE_TYPE(choice) (((struct spa_pod_choice*)(choice))->body.type) #define SPA_POD_CHOICE_TYPE(choice) (((struct spa_pod_choice*)(choice))->body.type)
#define SPA_POD_CHOICE_FLAGS(choice) (((struct spa_pod_choice*)(choice))->body.flags) #define SPA_POD_CHOICE_FLAGS(choice) (((struct spa_pod_choice*)(choice))->body.flags)
#define SPA_POD_CHOICE_VALUE_TYPE(choice) (SPA_POD_TYPE(SPA_POD_CHOICE_CHILD(choice))) #define SPA_POD_CHOICE_VALUE_TYPE(choice) (SPA_POD_CHOICE_CHILD(choice)->type)
#define SPA_POD_CHOICE_VALUE_SIZE(choice) (SPA_POD_BODY_SIZE(SPA_POD_CHOICE_CHILD(choice))) #define SPA_POD_CHOICE_VALUE_SIZE(choice) (SPA_POD_CHOICE_CHILD(choice)->size)
#define SPA_POD_CHOICE_N_VALUES(choice) (SPA_POD_CHOICE_VALUE_SIZE(choice) ? ((SPA_POD_BODY_SIZE(choice) - sizeof(struct spa_pod_choice_body)) / SPA_POD_CHOICE_VALUE_SIZE(choice)) : 0) #define SPA_POD_CHOICE_N_VALUES(choice) (SPA_POD_CHOICE_VALUE_SIZE(choice) ? ((SPA_POD_BODY_SIZE(choice) - sizeof(struct spa_pod_choice_body)) / SPA_POD_CHOICE_VALUE_SIZE(choice)) : 0)
#define SPA_POD_CHOICE_VALUES(choice) (SPA_POD_CONTENTS(struct spa_pod_choice, choice)) #define SPA_POD_CHOICE_VALUES(choice) (SPA_POD_CONTENTS(struct spa_pod_choice, choice))