diff mbox

[05/16] rbd: define dup_token()

Message ID 4FFD8733.9090305@inktank.com (mailing list archive)
State New, archived
Headers show

Commit Message

Alex Elder July 11, 2012, 2:01 p.m. UTC
Define a new function dup_token(), to be used during argument
parsing for making dynamically-allocated copies of tokens being
parsed.

For now, no gfp_flags parameter is defined (GFP_KERNEL is used) but
it could be easily be added if needed.

Signed-off-by: Alex Elder <elder@inktank.com>
---
 drivers/block/rbd.c |   36 ++++++++++++++++++++++++++++++++++++
 1 files changed, 36 insertions(+), 0 deletions(-)

Comments

Yehuda Sadeh July 11, 2012, 5:48 p.m. UTC | #1
On Wed, Jul 11, 2012 at 7:01 AM, Alex Elder <elder@inktank.com> wrote:
> Define a new function dup_token(), to be used during argument
> parsing for making dynamically-allocated copies of tokens being
> parsed.
>
> For now, no gfp_flags parameter is defined (GFP_KERNEL is used) but
> it could be easily be added if needed.

I assume this is specialized enough so that there's no risk in reusing
it in a different context, so for this one we can keep it this way.

Reviewed-by: Yehuda Sadeh <yehuda@inktank.com>


>
> Signed-off-by: Alex Elder <elder@inktank.com>
> ---
>  drivers/block/rbd.c |   36 ++++++++++++++++++++++++++++++++++++
>  1 files changed, 36 insertions(+), 0 deletions(-)
>
> diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
> index 2ae3bb0..63c132f 100644
> --- a/drivers/block/rbd.c
> +++ b/drivers/block/rbd.c
> @@ -2281,6 +2281,42 @@ static inline size_t copy_token(const char **buf,
>  }
>
>  /*
> + * Finds the next token in *buf, dynamically allocates a buffer big
> + * enough to hold a copy of it, and copies the token into the new
> + * buffer.  The copy is guaranteed to be terminated with '\0'.  Note
> + * that a duplicate buffer is created even for a zero-length token.
> + *
> + * Returns a pointer to the newly-allocated duplicate, or a null
> + * pointer if memory for the duplicate was not available.  If
> + * the lenp argument is a non-null pointer, the length of the token
> + * (not including the '\0') is returned in *lenp.
> + *
> + * If successful, the *buf pointer will be updated to point beyond
> + * the end of the found token.
> + *
> + * Note:  For now, the memory is allocated using GFP_KERNEL.
> + */
> +static inline char *dup_token(const char **buf, size_t *lenp)
> +{
> +       char *dup;
> +       size_t len;
> +
> +       len = next_token(buf);
> +       dup = kmalloc(len + 1, GFP_KERNEL);
> +       if (!dup)
> +               return NULL;
> +
> +       memcpy(dup, *buf, len);
> +       *(dup + len) = '\0';
> +       *buf += len;
> +
> +       if (lenp)
> +               *lenp = len;
> +
> +       return dup;
> +}
> +
> +/*
>   * This fills in the pool_name, obj, obj_len, snap_name, obj_len,
>   * rbd_dev, rbd_md_name, and name fields of the given rbd_dev, based
>   * on the list of monitor addresses and other options provided via
> --
> 1.7.5.4
>
> --
> To unsubscribe from this list: send the line "unsubscribe ceph-devel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Josh Durgin July 11, 2012, 6:50 p.m. UTC | #2
Reviewed-by: Josh Durgin <josh.durgin@inktank.com>

On 07/11/2012 07:01 AM, Alex Elder wrote:
> Define a new function dup_token(), to be used during argument
> parsing for making dynamically-allocated copies of tokens being
> parsed.
>
> For now, no gfp_flags parameter is defined (GFP_KERNEL is used) but
> it could be easily be added if needed.
>
> Signed-off-by: Alex Elder<elder@inktank.com>
> ---
>   drivers/block/rbd.c |   36 ++++++++++++++++++++++++++++++++++++
>   1 files changed, 36 insertions(+), 0 deletions(-)
>
> diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
> index 2ae3bb0..63c132f 100644
> --- a/drivers/block/rbd.c
> +++ b/drivers/block/rbd.c
> @@ -2281,6 +2281,42 @@ static inline size_t copy_token(const char **buf,
>   }
>
>   /*
> + * Finds the next token in *buf, dynamically allocates a buffer big
> + * enough to hold a copy of it, and copies the token into the new
> + * buffer.  The copy is guaranteed to be terminated with '\0'.  Note
> + * that a duplicate buffer is created even for a zero-length token.
> + *
> + * Returns a pointer to the newly-allocated duplicate, or a null
> + * pointer if memory for the duplicate was not available.  If
> + * the lenp argument is a non-null pointer, the length of the token
> + * (not including the '\0') is returned in *lenp.
> + *
> + * If successful, the *buf pointer will be updated to point beyond
> + * the end of the found token.
> + *
> + * Note:  For now, the memory is allocated using GFP_KERNEL.
> + */
> +static inline char *dup_token(const char **buf, size_t *lenp)
> +{
> +	char *dup;
> +	size_t len;
> +
> +	len = next_token(buf);
> +	dup = kmalloc(len + 1, GFP_KERNEL);
> +	if (!dup)
> +		return NULL;
> +
> +	memcpy(dup, *buf, len);
> +	*(dup + len) = '\0';
> +	*buf += len;
> +
> +	if (lenp)
> +		*lenp = len;
> +
> +	return dup;
> +}
> +
> +/*
>    * This fills in the pool_name, obj, obj_len, snap_name, obj_len,
>    * rbd_dev, rbd_md_name, and name fields of the given rbd_dev, based
>    * on the list of monitor addresses and other options provided via

--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Alex Elder July 11, 2012, 9:50 p.m. UTC | #3
On 07/11/2012 12:48 PM, Yehuda Sadeh wrote:
> I assume this is specialized enough so that there's no risk in reusing
> it in a different context, so for this one we can keep it this way.

I agree on this one.  It's only  used in argument parsing right now.
If we find we want to use it in a place that needs better control it's
easy enough to add the gfp argument.

					-Alex
--
To unsubscribe from this list: send the line "unsubscribe ceph-devel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/drivers/block/rbd.c b/drivers/block/rbd.c
index 2ae3bb0..63c132f 100644
--- a/drivers/block/rbd.c
+++ b/drivers/block/rbd.c
@@ -2281,6 +2281,42 @@  static inline size_t copy_token(const char **buf,
 }

 /*
+ * Finds the next token in *buf, dynamically allocates a buffer big
+ * enough to hold a copy of it, and copies the token into the new
+ * buffer.  The copy is guaranteed to be terminated with '\0'.  Note
+ * that a duplicate buffer is created even for a zero-length token.
+ *
+ * Returns a pointer to the newly-allocated duplicate, or a null
+ * pointer if memory for the duplicate was not available.  If
+ * the lenp argument is a non-null pointer, the length of the token
+ * (not including the '\0') is returned in *lenp.
+ *
+ * If successful, the *buf pointer will be updated to point beyond
+ * the end of the found token.
+ *
+ * Note:  For now, the memory is allocated using GFP_KERNEL.
+ */
+static inline char *dup_token(const char **buf, size_t *lenp)
+{
+	char *dup;
+	size_t len;
+
+	len = next_token(buf);
+	dup = kmalloc(len + 1, GFP_KERNEL);
+	if (!dup)
+		return NULL;
+
+	memcpy(dup, *buf, len);
+	*(dup + len) = '\0';
+	*buf += len;
+
+	if (lenp)
+		*lenp = len;
+
+	return dup;
+}
+
+/*
  * This fills in the pool_name, obj, obj_len, snap_name, obj_len,
  * rbd_dev, rbd_md_name, and name fields of the given rbd_dev, based
  * on the list of monitor addresses and other options provided via