Skip to contentSkip to conversation

lib/igt_debugfs: Update documentation and clenup

PATCH In review Arkadiusz Hiler 3 participants 3 messages Jul 17, 2017 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/igt_debugfs: Update documentation and clenup

The documentation was lying. The igt_crc_to_string() is threadsafe and does not return a pointer to an internal buffer.

Actually the caller is responsible for the memory that is allocated (and they are for all the current cases), so let's put that in the doc too.

While I was at it I got rid of strdup() in favor of an early allocation.

Cc Martin Peres <[email protected]>
Cc Liviu Dudau <[email protected]>
---
 lib/igt_debugfs.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
lib/igt_debugfs.c +4 -5
index 80f25c6..5e4d72c 100644
@@ -304,21 +304,20 @@ void igt_assert_crc_equal(const igt_crc_t *a, const igt_crc_t *b)
  * igt_crc_to_string:
  * @crc: pipe CRC value to print
  *
- * This formats @crc into a string buffer which is owned by igt_crc_to_string().
- * The next call will override the buffer again, which makes this multithreading
- * unsafe.
+ * This formats @crc into a string. Function allocates memory - the caller is
+ * in charge of freeing it.
  *
  * This should only ever be used for diagnostic debug output.
  */
 char *igt_crc_to_string(igt_crc_t *crc)
 {
 	int i;
-	char buf[128] = { 0 };
+	char *buf = calloc(128, sizeof(char));
 
 	for (i = 0; i < crc->n_words; i++)
 		sprintf(buf + strlen(buf), "%08x ", crc->crc[i]);
 
-	return strdup(buf);
+	return buf;
 }
 
 #define MAX_CRC_ENTRIES 10
Signature · 6 lines
2.9.4

_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Arkadiusz Hiler

The documentation was lying. The igt_crc_to_string() is threadsafe and does not return a pointer to an internal buffer.

Actually the caller is responsible for the memory that is allocated (and they are for all the current cases), so let's put that in the doc too.

While I was at it I got rid of strdup() in favor of an early allocation.

Cc Martin Peres <[email protected]>
Cc Liviu Dudau <[email protected]>
Quoted 38 lines
---
 lib/igt_debugfs.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
lib/igt_debugfs.c +4 -5
index 80f25c6..5e4d72c 100644
@@ -304,21 +304,20 @@ void igt_assert_crc_equal(const igt_crc_t *a, const igt_crc_t *b)
  * igt_crc_to_string:
  * @crc: pipe CRC value to print
  *
- * This formats @crc into a string buffer which is owned by igt_crc_to_string().
- * The next call will override the buffer again, which makes this multithreading
- * unsafe.
+ * This formats @crc into a string. Function allocates memory - the caller is
+ * in charge of freeing it.
  *
  * This should only ever be used for diagnostic debug output.
  */
 char *igt_crc_to_string(igt_crc_t *crc)
 {
 	int i;
-	char buf[128] = { 0 };
+	char *buf = calloc(128, sizeof(char));
 
 	for (i = 0; i < crc->n_words; i++)
 		sprintf(buf + strlen(buf), "%08x ", crc->crc[i]);
 
-	return strdup(buf);
+	return buf;
 }
 
 #define MAX_CRC_ENTRIES 10
Signature · 1 line
2.9.4
Signature · 12 lines
====================
| I would like to |
| fix the world,  |
| but they're not |
| giving me the   |
 \ source code!  /
  ---------------
    ¯\_(ツ)_/¯
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx
Reviewed-by
#2 lore raw

clenup -> cleanup

Quoted 19 lines · Liviu Dudau
Quoted 10 lines · Arkadiusz Hiler

The documentation was lying. The igt_crc_to_string() is threadsafe and does not return a pointer to an internal buffer.

Actually the caller is responsible for the memory that is allocated (and they are for all the current cases), so let's put that in the doc too.

While I was at it I got rid of strdup() in favor of an early allocation.

Cc Martin Peres <[email protected]>
Cc Liviu Dudau <[email protected]>
Quoted 5 lines
---
  lib/igt_debugfs.c | 9 ++++-----
  1 file changed, 4 insertions(+), 5 deletions(-)
Quoted 13 lines

lib/igt_debugfs.c void igt_assert_crc_equal(const igt_crc_t *a, const igt_crc_t *b) · line 308

Quoted 13 lines
lib/igt_debugfs.c +2 -3
index 80f25c6..5e4d72c 100644
@@ -304,21 +304,20 @@ void igt_assert_crc_equal(const igt_crc_t *a, const igt_crc_t *b)
   * igt_crc_to_string:
   * @crc: pipe CRC value to print
   *
- * This formats @crc into a string buffer which is owned by igt_crc_to_string().
- * The next call will override the buffer again, which makes this multithreading
- * unsafe.
+ * This formats @crc into a string. Function allocates memory - the caller is
+ * in charge of freeing it.

These sentences are a little strange from a grammatical point of view. How about:

This function allocates a string and formats @crc into it. The caller is responsible for freeing the string.

Quoted 8 lines

lib/igt_debugfs.c void igt_assert_crc_equal(const igt_crc_t *a, const igt_crc_t *b) · line 315

Quoted 8 lines
lib/igt_debugfs.c +1 -1
   *
   * This should only ever be used for diagnostic debug output.
   */
  char *igt_crc_to_string(igt_crc_t *crc)
  {
  	int i;
-	char buf[128] = { 0 };
+	char *buf = calloc(128, sizeof(char));

To mimic the previous code's behaviour, please add the following:

if (!buf)

    return NULL;
Quoted 4 lines
Quoted 4 lines
lib/igt_debugfs.c +0 -0
  	for (i = 0; i < crc->n_words; i++)
  		sprintf(buf + strlen(buf), "%08x ", crc->crc[i]);
 
Quoted 9 lines
Quoted 8 lines
lib/igt_debugfs.c +1 -1
-	return strdup(buf);
+	return buf;
  }
 
  #define MAX_CRC_ENTRIES 10
Signature · 1 line
2.9.4

With at least the title and code fixed, this is:

Thanks for doing this :)
Martin
---------------------------------------------------------------------
Intel Finland Oy
Registered Address: PL 281, 00181 Helsinki Business Identity Code: 0357606 - 4 Domiciled in Helsinki

This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies.
_______________________________________________
Intel-gfx mailing list
[email protected]
https://lists.freedesktop.org/mailman/listinfo/intel-gfx