ALSA: pcm: Mostly constify constraints
PATCH 1/1 ALSA: pcm: Mostly constify constraints
There is no need for the constraints to be modified while being applied so they can be declared const, allowing drivers to declare constraint lists const if they wish.
--- include/sound/pcm.h | 6 +++--- sound/core/pcm_lib.c | 10 +++++----- sound/core/pcm_native.c | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-)
include/sound/pcm.h +3 -3
@@ -217,7 +217,7 @@ struct snd_pcm_hw_rule {snd_pcm_hw_rule_func_t func;int var;int deps[4 ]; -void *private; +const void *private; };struct snd_pcm_hw_constraints {
@@ -810,7 +810,7 @@ int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_paint snd_pcm_hw_constraint_list (struct snd_pcm_runtime *runtime,unsigned int cond,snd_pcm_hw_param_t var, -struct snd_pcm_hw_constraint_list *l); +const struct snd_pcm_hw_constraint_list *l); int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime, unsigned int cond, snd_pcm_hw_param_t var,
@@ -835,7 +835,7 @@ int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,int snd_pcm_hw_rule_add (struct snd_pcm_runtime *runtime,unsigned int cond,int var, -snd_pcm_hw_rule_func_t func,void *private, +snd_pcm_hw_rule_func_t func,const void *private,int dep, ...);int snd_pcm_format_signed (snd_pcm_format_t format);
sound/core/pcm_lib.c +5 -5
@@ -1104,7 +1104,7 @@ static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned*/ int snd_pcm_hw_rule_add (struct snd_pcm_runtime *runtime,unsigned int cond,int var, -snd_pcm_hw_rule_func_t func,void *private, +snd_pcm_hw_rule_func_t func,const void *private,int dep, ...) {struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;
@@ -1233,7 +1233,7 @@ EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);static int snd_pcm_hw_rule_list (struct snd_pcm_hw_params *params,struct snd_pcm_hw_rule *rule) { -struct snd_pcm_hw_constraint_list *list = rule->private; +const struct snd_pcm_hw_constraint_list *list = rule->private;return snd_interval_list (hw_param_interval (params, rule->var), list->count, list->list, list->mask); }
@@ -1250,7 +1250,7 @@ static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,int snd_pcm_hw_constraint_list (struct snd_pcm_runtime *runtime,unsigned int cond,snd_pcm_hw_param_t var, -struct snd_pcm_hw_constraint_list *l) +const struct snd_pcm_hw_constraint_list *l) { return snd_pcm_hw_rule_add(runtime, cond, var, snd_pcm_hw_rule_list, l,
@@ -1262,7 +1262,7 @@ EXPORT_SYMBOL(snd_pcm_hw_constraint_list);static int snd_pcm_hw_rule_ratnums (struct snd_pcm_hw_params *params,struct snd_pcm_hw_rule *rule) { -struct snd_pcm_hw_constraint_ratnums *r = rule->private; +const struct snd_pcm_hw_constraint_ratnums *r = rule->private;unsigned int num =0 , den =0 ;int err; err = snd_interval_ratnum(hw_param_interval(params, rule->var),
@@ -1296,7 +1296,7 @@ EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);static int snd_pcm_hw_rule_ratdens (struct snd_pcm_hw_params *params,struct snd_pcm_hw_rule *rule) { -struct snd_pcm_hw_constraint_ratdens *r = rule->private; +const struct snd_pcm_hw_constraint_ratdens *r = rule->private;unsigned int num =0 , den =0 ;int err =snd_interval_ratden (hw_param_interval (params, rule->var), r->nrats, r->rats, &num, &den);
sound/core/pcm_native.c +2 -2
@@ -1775,7 +1775,7 @@ const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {static int snd_pcm_hw_rule_rate (struct snd_pcm_hw_params *params,struct snd_pcm_hw_rule *rule) { -struct snd_pcm_hardware *hw = rule->private; +const struct snd_pcm_hardware *hw = rule->private;return snd_interval_list (hw_param_interval (params, rule->var), snd_pcm_known_rates.count, snd_pcm_known_rates.list, hw->rates);
@@ -1785,7 +1785,7 @@ static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) {struct snd_interval t; -struct snd_pcm_substream *substream = rule->private; +const struct snd_pcm_substream *substream = rule->private; t.min =0 ; t.max = substream->buffer_bytes_max; t.openmin =0 ;
Signature · 1 line
1.7.10
At Wed, 4 Jul 2012 18:35:12 +0100,
Quoted 3 lines · Mark Brown
There is no need for the constraints to be modified while being applied
Many drivers pass the local instance to rule->private, and it's not always guaranteed to be const. For example, you can imagine some state modified kept in the struct while it's modified via rules.
In short: you'd have seen many compile warnings for non-ASoC drivers if you run make once with this patch ;)
As a safe side fix, how about just adding const to snd_pcm_hw_constraint_list() & co, and expclitly cast to non-const later?
Takashi
Quoted 116 lines
so
they can be declared const, allowing drivers to declare constraint lists const if they wish.Signed-off-by Mark Brown <[email protected]>--- include/sound/pcm.h | 6 +++--- sound/core/pcm_lib.c | 10 +++++----- sound/core/pcm_native.c | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-)include/sound/pcm.h +3
-3@@ -217,7 +217,7 @@ struct snd_pcm_hw_rule {snd_pcm_hw_rule_func_t func;int var;int deps[4 ]; -void *private; +const void *private; };struct snd_pcm_hw_constraints {@@ -810,7 +810,7 @@ int snd_pcm_hw_constraint_integer(struct snd_pcm_runtime *runtime, snd_pcm_hw_paint snd_pcm_hw_constraint_list (struct snd_pcm_runtime *runtime,unsigned int cond,snd_pcm_hw_param_t var, -struct snd_pcm_hw_constraint_list *l); +const struct snd_pcm_hw_constraint_list *l); int snd_pcm_hw_constraint_ratnums(struct snd_pcm_runtime *runtime, unsigned int cond, snd_pcm_hw_param_t var,@@ -835,7 +835,7 @@ int snd_pcm_hw_rule_noresample(struct snd_pcm_runtime *runtime,int snd_pcm_hw_rule_add (struct snd_pcm_runtime *runtime,unsigned int cond,int var, -snd_pcm_hw_rule_func_t func,void *private, +snd_pcm_hw_rule_func_t func,const void *private,int dep, ...);int snd_pcm_format_signed (snd_pcm_format_t format);sound/core/pcm_lib.c +5
-5@@ -1104,7 +1104,7 @@ static int snd_interval_step(struct snd_interval *i, unsigned int min, unsigned*/ int snd_pcm_hw_rule_add (struct snd_pcm_runtime *runtime,unsigned int cond,int var, -snd_pcm_hw_rule_func_t func,void *private, +snd_pcm_hw_rule_func_t func,const void *private,int dep, ...) {struct snd_pcm_hw_constraints *constrs = &runtime->hw_constraints;@@ -1233,7 +1233,7 @@ EXPORT_SYMBOL(snd_pcm_hw_constraint_minmax);static int snd_pcm_hw_rule_list (struct snd_pcm_hw_params *params,struct snd_pcm_hw_rule *rule) { -struct snd_pcm_hw_constraint_list *list = rule->private; +const struct snd_pcm_hw_constraint_list *list = rule->private;return snd_interval_list (hw_param_interval (params, rule->var), list->count, list->list, list->mask); }@@ -1250,7 +1250,7 @@ static int snd_pcm_hw_rule_list(struct snd_pcm_hw_params *params,int snd_pcm_hw_constraint_list (struct snd_pcm_runtime *runtime,unsigned int cond,snd_pcm_hw_param_t var, -struct snd_pcm_hw_constraint_list *l) +const struct snd_pcm_hw_constraint_list *l) { return snd_pcm_hw_rule_add(runtime, cond, var, snd_pcm_hw_rule_list, l,@@ -1262,7 +1262,7 @@ EXPORT_SYMBOL(snd_pcm_hw_constraint_list);static int snd_pcm_hw_rule_ratnums (struct snd_pcm_hw_params *params,struct snd_pcm_hw_rule *rule) { -struct snd_pcm_hw_constraint_ratnums *r = rule->private; +const struct snd_pcm_hw_constraint_ratnums *r = rule->private;unsigned int num =0 , den =0 ;int err; err = snd_interval_ratnum(hw_param_interval(params, rule->var),@@ -1296,7 +1296,7 @@ EXPORT_SYMBOL(snd_pcm_hw_constraint_ratnums);static int snd_pcm_hw_rule_ratdens (struct snd_pcm_hw_params *params,struct snd_pcm_hw_rule *rule) { -struct snd_pcm_hw_constraint_ratdens *r = rule->private; +const struct snd_pcm_hw_constraint_ratdens *r = rule->private;unsigned int num =0 , den =0 ;int err =snd_interval_ratden (hw_param_interval (params, rule->var), r->nrats, r->rats, &num, &den);sound/core/pcm_native.c +2
-2@@ -1775,7 +1775,7 @@ const struct snd_pcm_hw_constraint_list snd_pcm_known_rates = {static int snd_pcm_hw_rule_rate (struct snd_pcm_hw_params *params,struct snd_pcm_hw_rule *rule) { -struct snd_pcm_hardware *hw = rule->private; +const struct snd_pcm_hardware *hw = rule->private;return snd_interval_list (hw_param_interval (params, rule->var), snd_pcm_known_rates.count, snd_pcm_known_rates.list, hw->rates);@@ -1785,7 +1785,7 @@ static int snd_pcm_hw_rule_buffer_bytes_max(struct snd_pcm_hw_params *params, struct snd_pcm_hw_rule *rule) {struct snd_interval t; -struct snd_pcm_substream *substream = rule->private; +const struct snd_pcm_substream *substream = rule->private; t.min =0 ; t.max = substream->buffer_bytes_max; t.openmin =0 ;Signature · 1 line
1.7.10
Quoted 11 lines · Takashi Iwai
Quoted 2 lines · Mark Brown
There is no need for the constraints to be modified while being applied
Many drivers pass the local instance to
rule->private, and it's not always guaranteed to be const. For example, you can imagine some state modified kept in the struct while it's modified via rules.In short: you'd have seen many compile warnings for non-ASoC drivers if you run make once with this patch ;)
Well, that's trivial to fix.
Quoted 3 lines
As a safe side fix, how about just adding const to
snd_pcm_hw_constraint_list()& co, and expclitly cast to non-const later?
My inclination for something like this is to put the casts with the users since that way we get the type safety through most of the code and we can see that dropping the const is safe since we're just getting back our own data. If we can see the APIs immediately dropping the const I'd expect we'd get people spending time on code review trying to figure out if it's safe.
At Thu, 5 Jul 2012 10:28:42 +0100,
Quoted 20 lines · Mark Brown
Quoted 11 lines · Takashi Iwai
Quoted 2 lines · Mark Brown
There is no need for the constraints to be modified while being applied
Many drivers pass the local instance to
rule->private, and it's not always guaranteed to be const. For example, you can imagine some state modified kept in the struct while it's modified via rules.In short: you'd have seen many compile warnings for non-ASoC drivers if you run make once with this patch ;)
Well, that's trivial to fix.
Quoted 3 lines
As a safe side fix, how about just adding const to
snd_pcm_hw_constraint_list()& co, and expclitly cast to non-const later?
Quoted 6 lines
My inclination for something like this is to put the casts with the users since that way we get the type safety through most of the code and we can see that dropping the const is safe since we're just getting back our own data. If we can see the APIs immediately dropping the const I'd expect we'd get people spending time on code review trying to figure out if it's safe.
The rule->private pointer isn't designed to be always const. The data purely depends on each rule function, thus whether it can be const or not also depends on the rule, too. For simple rules like list take indeed the constant list. But more complex rules may take a struct containing variables changed on the fly (e.g. the max rate set by user via control API).
Takashi
Quoted 6 lines · Takashi Iwai
The
rule->privatepointer isn't designed to be always const. The data purely depends on each rule function, thus whether it can be const or not also depends on the rule, too. For simple rules like list take indeed the constant list. But more complex rules may take a struct containing variables changed on the fly (e.g. the max rate set by user via control API).
Well, it's mostly const - it's const unless the rule itself starts fiddling with stuff which I'd have expected to be the exception rather than the rule.
At Thu, 5 Jul 2012 11:35:28 +0100,
Quoted 10 lines · Mark Brown
Quoted 6 lines · Takashi Iwai
The
rule->privatepointer isn't designed to be always const. The data purely depends on each rule function, thus whether it can be const or not also depends on the rule, too. For simple rules like list take indeed the constant list. But more complex rules may take a struct containing variables changed on the fly (e.g. the max rate set by user via control API).
Quoted 3 lines
Well, it's mostly const - it's const unless the rule itself starts fiddling with stuff which I'd have expected to be the exception rather than the rule.
Well, the only rule is that rule (function) rules the meaning of the private pointer :)
Takashi
Quoted 8 lines · Takashi Iwai
Quoted 3 lines · Mark Brown
Well, it's mostly const - it's const unless the rule itself starts fiddling with stuff which I'd have expected to be the exception rather than the rule.
Well, the only rule is that rule (function) rules the meaning of the private pointer :)
Ugh, that's pretty vile really. But anyway...