[PATCH 2/2] xkb: strdup the values returned by XkbGetRulesDflts

Dan Nicholson dbn.lists at gmail.com
Thu Apr 16 19:03:47 PDT 2009


On Thu, Apr 16, 2009 at 5:01 PM, Peter Hutterer
<peter.hutterer at who-t.net> wrote:
> On Wed, Apr 15, 2009 at 10:17:57PM -0700, Dan Nicholson wrote:
>> >  static Bool
>> > @@ -586,6 +596,17 @@ InitKeyboardDeviceStruct(DeviceIntPtr dev, XkbRMLVOSet *rmlvo,
>> >     XkbSetRulesDflts(rmlvo);
>> >     XkbSetRulesUsed(rmlvo);
>> >
>> > +    if (rmlvo_dflts.rules)
>> > +        xfree(rmlvo_dflts.rules);
>> > +    if (rmlvo_dflts.model)
>> > +        xfree(rmlvo_dflts.model);
>> > +    if (rmlvo_dflts.layout)
>> > +        xfree(rmlvo_dflts.layout);
>> > +    if (rmlvo_dflts.variant)
>> > +        xfree(rmlvo_dflts.variant);
>> > +    if (rmlvo_dflts.options)
>> > +        xfree(rmlvo_dflts.options);
>>
>> Can we get a helper for this to make it easier on callers? Something like:
>>
>> XkbFreeRMLVO(XkbRMLVOSet *rmlvo, Bool freeRules)
>> {
>>     if (!rmlvo)
>>         return;
>>
>>     xfree(rmlvo->rules);
>>     xfree(rmlvo->model);
>>     xfree(rmlvo->layout);
>>     xfree(rmlvo->variant);
>>     xfree(rmlvo->options);
>>
>>     if (freeRules)
>>         xfree(rmlvo);
>> }
>>
>> I think xfree/Xfree already does the right thing with NULL arguments.
>
> How about this?
>
> From 404137d5d5c8f2ffaccd06b37308259dd5fa82ed Mon Sep 17 00:00:00 2001
> From: Peter Hutterer <peter.hutterer at who-t.net>
> Date: Thu, 16 Apr 2009 17:06:33 +1000
> Subject: [PATCH] xkb: Add XkbFreeRMLVOSet helper function.
>
> Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
> ---
>  include/xkbsrv.h |    5 +++++
>  xkb/xkbInit.c    |   30 +++++++++++++++++++-----------
>  2 files changed, 24 insertions(+), 11 deletions(-)
>
> diff --git a/include/xkbsrv.h b/include/xkbsrv.h
> index 8a81431..df33085 100644
> --- a/include/xkbsrv.h
> +++ b/include/xkbsrv.h
> @@ -858,6 +858,11 @@ extern _X_EXPORT void      XkbGetRulesDflts(
>         XkbRMLVOSet *           /* rmlvo */
>  );
>
> +extern _X_EXPORT void   XkbFreeRMLVOSet(
> +        XkbRMLVOSet *           /* rmlvo */,
> +        Bool                    /* freeRMLVO */
> +);
> +
>  extern _X_EXPORT void  XkbSetRulesDflts(
>         XkbRMLVOSet *           /* rmlvo */
>  );
> diff --git a/xkb/xkbInit.c b/xkb/xkbInit.c
> index 9d4d9a2..5ac06fe 100644
> --- a/xkb/xkbInit.c
> +++ b/xkb/xkbInit.c
> @@ -136,6 +136,24 @@ XkbGetRulesDflts(XkbRMLVOSet *rmlvo)
>     rmlvo->options = strdup(rmlvo->options);
>  }
>
> +void
> +XkbFreeRMLVOSet(XkbRMLVOSet *rmlvo, Bool freeRMLVO)
> +{
> +    if (!rmlvo)
> +        return;
> +
> +    xfree(rmlvo->rules);
> +    xfree(rmlvo->model);
> +    xfree(rmlvo->layout);
> +    xfree(rmlvo->variant);
> +    xfree(rmlvo->options);
> +
> +    if (freeRMLVO)
> +        xfree(rmlvo);
> +    else
> +        memset(rmlvo, 0, sizeof(XkbRMLVOSet));
> +}
> +
>  static Bool
>  XkbWriteRulesProp(ClientPtr client, pointer closure)
>  {
> @@ -595,17 +613,7 @@ InitKeyboardDeviceStruct(DeviceIntPtr dev, XkbRMLVOSet *rmlvo,
>
>     XkbSetRulesDflts(rmlvo);
>     XkbSetRulesUsed(rmlvo);
> -
> -    if (rmlvo_dflts.rules)
> -        xfree(rmlvo_dflts.rules);
> -    if (rmlvo_dflts.model)
> -        xfree(rmlvo_dflts.model);
> -    if (rmlvo_dflts.layout)
> -        xfree(rmlvo_dflts.layout);
> -    if (rmlvo_dflts.variant)
> -        xfree(rmlvo_dflts.variant);
> -    if (rmlvo_dflts.options)
> -        xfree(rmlvo_dflts.options);
> +    XkbFreeRMLVOSet(&rmlvo_dflts, FALSE);
>
>     return TRUE;
>
> --
> 1.6.2.2.447.g4afa7

Looks like a winner to me.

Acked-by: Dan Nicholson <dbn.lists at gmail.com>

--
Dan


More information about the xorg-devel mailing list