From mboxrd@z Thu Jan 1 00:00:00 1970 From: brendanhiggins at google.com (Brendan Higgins) Date: Thu, 2 May 2019 21:37:26 -0700 Subject: [PATCH v2 03/17] kunit: test: add string_stream a std::stream like string builder In-Reply-To: <1befe456-d981-d726-44f9-ebe3702ee51d@kernel.org> References: <20190501230126.229218-1-brendanhiggins@google.com> <20190501230126.229218-4-brendanhiggins@google.com> <1befe456-d981-d726-44f9-ebe3702ee51d@kernel.org> Message-ID: On Thu, May 2, 2019 at 6:26 PM shuah wrote: > > On 5/1/19 5:01 PM, Brendan Higgins wrote: < snip > > > diff --git a/kunit/Makefile b/kunit/Makefile > > index 5efdc4dea2c08..275b565a0e81f 100644 > > --- a/kunit/Makefile > > +++ b/kunit/Makefile > > @@ -1 +1,2 @@ > > -obj-$(CONFIG_KUNIT) += test.o > > +obj-$(CONFIG_KUNIT) += test.o \ > > + string-stream.o > > diff --git a/kunit/string-stream.c b/kunit/string-stream.c > > new file mode 100644 > > index 0000000000000..7018194ecf2fa > > --- /dev/null > > +++ b/kunit/string-stream.c > > @@ -0,0 +1,144 @@ > > +// SPDX-License-Identifier: GPL-2.0 > > +/* > > + * C++ stream style string builder used in KUnit for building messages. > > + * > > + * Copyright (C) 2019, Google LLC. > > + * Author: Brendan Higgins > > + */ > > + > > +#include > > +#include > > +#include > > + > > +int string_stream_vadd(struct string_stream *this, > > + const char *fmt, > > + va_list args) > > +{ > > + struct string_stream_fragment *fragment; > > Since there is field with the same name, please use a different > name. Using the same name for the struct which contains a field > of the same name get very confusing and will hard to maintain > the code. > > > + int len; > > + va_list args_for_counting; > > + unsigned long flags; > > + > > + /* Make a copy because `vsnprintf` could change it */ > > + va_copy(args_for_counting, args); > > + > > + /* Need space for null byte. */ > > + len = vsnprintf(NULL, 0, fmt, args_for_counting) + 1; > > + > > + va_end(args_for_counting); > > + > > + fragment = kmalloc(sizeof(*fragment), GFP_KERNEL); > > + if (!fragment) > > + return -ENOMEM; > > + > > + fragment->fragment = kmalloc(len, GFP_KERNEL); > > This is confusing. See above comment. Good point. Will fix in the next revision. < snip > Thanks! From mboxrd@z Thu Jan 1 00:00:00 1970 From: brendanhiggins@google.com (Brendan Higgins) Date: Thu, 2 May 2019 21:37:26 -0700 Subject: [PATCH v2 03/17] kunit: test: add string_stream a std::stream like string builder In-Reply-To: <1befe456-d981-d726-44f9-ebe3702ee51d@kernel.org> References: <20190501230126.229218-1-brendanhiggins@google.com> <20190501230126.229218-4-brendanhiggins@google.com> <1befe456-d981-d726-44f9-ebe3702ee51d@kernel.org> Message-ID: Content-Type: text/plain; charset="UTF-8" Message-ID: <20190503043726.lC4qn94TiaHqpIEckvkQ4h7HOKcJgJPiINChVa3nbjM@z> On Thu, May 2, 2019@6:26 PM shuah wrote: > > On 5/1/19 5:01 PM, Brendan Higgins wrote: < snip > > > diff --git a/kunit/Makefile b/kunit/Makefile > > index 5efdc4dea2c08..275b565a0e81f 100644 > > --- a/kunit/Makefile > > +++ b/kunit/Makefile > > @@ -1 +1,2 @@ > > -obj-$(CONFIG_KUNIT) += test.o > > +obj-$(CONFIG_KUNIT) += test.o \ > > + string-stream.o > > diff --git a/kunit/string-stream.c b/kunit/string-stream.c > > new file mode 100644 > > index 0000000000000..7018194ecf2fa > > --- /dev/null > > +++ b/kunit/string-stream.c > > @@ -0,0 +1,144 @@ > > +// SPDX-License-Identifier: GPL-2.0 > > +/* > > + * C++ stream style string builder used in KUnit for building messages. > > + * > > + * Copyright (C) 2019, Google LLC. > > + * Author: Brendan Higgins > > + */ > > + > > +#include > > +#include > > +#include > > + > > +int string_stream_vadd(struct string_stream *this, > > + const char *fmt, > > + va_list args) > > +{ > > + struct string_stream_fragment *fragment; > > Since there is field with the same name, please use a different > name. Using the same name for the struct which contains a field > of the same name get very confusing and will hard to maintain > the code. > > > + int len; > > + va_list args_for_counting; > > + unsigned long flags; > > + > > + /* Make a copy because `vsnprintf` could change it */ > > + va_copy(args_for_counting, args); > > + > > + /* Need space for null byte. */ > > + len = vsnprintf(NULL, 0, fmt, args_for_counting) + 1; > > + > > + va_end(args_for_counting); > > + > > + fragment = kmalloc(sizeof(*fragment), GFP_KERNEL); > > + if (!fragment) > > + return -ENOMEM; > > + > > + fragment->fragment = kmalloc(len, GFP_KERNEL); > > This is confusing. See above comment. Good point. Will fix in the next revision. < snip > Thanks!