qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH 0/4] docs: add docs about use of automatic cleanup functions
@ 2019-08-23 16:39 Daniel P. Berrangé
  2019-08-23 16:39 ` [Qemu-devel] [PATCH 1/4] docs: convert CODING_STYLE and HACKING to markdown syntax Daniel P. Berrangé
                   ` (4 more replies)
  0 siblings, 5 replies; 18+ messages in thread
From: Daniel P. Berrangé @ 2019-08-23 16:39 UTC (permalink / raw)
  To: qemu-devel; +Cc: Alex Bennée, Daniel P. Berrangé

This is ostensibly about adding docs for the g_autofree/g_autoptr
macros. As part of doing that, however, the existing HACKING doc
is merged into the CODING_STYLE doc and the text is converted to
markdown with a table of contents.

Daniel P. Berrangé (4):
  docs: convert CODING_STYLE and HACKING to markdown syntax
  docs: merge HACKING.md contents into CODING_STYLE.md
  docs: document use of automatic cleanup functions in glib
  docs: add table of contents to CODING_STYLE.md

 CODING_STYLE    | 216 -----------------
 CODING_STYLE.md | 613 ++++++++++++++++++++++++++++++++++++++++++++++++
 HACKING         | 257 --------------------
 README          |   2 +-
 4 files changed, 614 insertions(+), 474 deletions(-)
 delete mode 100644 CODING_STYLE
 create mode 100644 CODING_STYLE.md
 delete mode 100644 HACKING

-- 
2.21.0



^ permalink raw reply	[flat|nested] 18+ messages in thread

* [Qemu-devel] [PATCH 1/4] docs: convert CODING_STYLE and HACKING to markdown syntax
  2019-08-23 16:39 [Qemu-devel] [PATCH 0/4] docs: add docs about use of automatic cleanup functions Daniel P. Berrangé
@ 2019-08-23 16:39 ` Daniel P. Berrangé
  2019-08-28 12:25   ` Alex Bennée
  2019-08-23 16:39 ` [Qemu-devel] [PATCH 2/4] docs: merge HACKING.md contents into CODING_STYLE.md Daniel P. Berrangé
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 18+ messages in thread
From: Daniel P. Berrangé @ 2019-08-23 16:39 UTC (permalink / raw)
  To: qemu-devel; +Cc: Alex Bennée, Daniel P. Berrangé

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 CODING_STYLE => CODING_STYLE.md | 37 +++++++++---------
 HACKING => HACKING.md           | 68 ++++++++++++++++++---------------
 README                          |  2 +-
 3 files changed, 58 insertions(+), 49 deletions(-)
 rename CODING_STYLE => CODING_STYLE.md (92%)
 rename HACKING => HACKING.md (88%)

diff --git a/CODING_STYLE b/CODING_STYLE.md
similarity index 92%
rename from CODING_STYLE
rename to CODING_STYLE.md
index cb8edcbb36..056eda7739 100644
--- a/CODING_STYLE
+++ b/CODING_STYLE.md
@@ -4,7 +4,7 @@ QEMU Coding Style
 Please use the script checkpatch.pl in the scripts directory to check
 patches before submitting.
 
-1. Whitespace
+## Whitespace
 
 Of course, the most important aspect in any coding style is whitespace.
 Crusty old coders who have trouble spotting the glasses on their noses
@@ -29,7 +29,7 @@ Spaces of course are superior to tabs because:
 
 Do not leave whitespace dangling off the ends of lines.
 
-1.1 Multiline Indent
+### Multiline Indent
 
 There are several places where indent is necessary:
 
@@ -53,9 +53,8 @@ For example:
 
 In case of function, there are several variants:
 
-    * 4 spaces indent from the beginning
-    * align the secondary lines just after the opening parenthesis of the
-      first
+ * 4 spaces indent from the beginning
+ * align the secondary lines just after the opening parenthesis of the first
 
 For example:
 
@@ -68,7 +67,7 @@ For example:
     do_something(x, do_another(y,
                                z));
 
-2. Line width
+## Line width
 
 Lines should be 80 characters; try not to make them longer.
 
@@ -77,6 +76,7 @@ that use long function or symbol names.  Even in that case, do not make
 lines much longer than 80 characters.
 
 Rationale:
+
  - Some people like to tile their 24" screens with a 6x4 matrix of 80x24
    xterms and use vi in all of them.  The best way to punish them is to
    let them keep doing it.
@@ -86,7 +86,7 @@ Rationale:
    at all that white space on the left!") moot.
  - It is the QEMU coding style.
 
-3. Naming
+## Naming
 
 Variables are lower_case_with_underscores; easy to type and read.  Structured
 type names are in CamelCase; harder to type but standing out.  Enum type
@@ -98,7 +98,7 @@ and is therefore likely to be changed.
 When wrapping standard library functions, use the prefix qemu_ to alert
 readers that they are seeing a wrapped version; otherwise avoid this prefix.
 
-4. Block structure
+## Block structure
 
 Every indented statement is braced; even if the block contains just one
 statement.  The opening brace is on the line that contains the control
@@ -130,7 +130,7 @@ Rationale: a consistent (except for functions...) bracing style reduces
 ambiguity and avoids needless churn when lines are added or removed.
 Furthermore, it is the QEMU coding style.
 
-5. Declarations
+## Declarations
 
 Mixed declarations (interleaving statements and declarations within
 blocks) are generally not allowed; declarations should be at the beginning
@@ -142,7 +142,7 @@ be placed at the top of the block even if there are statements above.
 On the other hand, however, it's often best to move that #ifdef/#ifndef
 block to a separate function altogether.
 
-6. Conditional statements
+## Conditional statements
 
 When comparing a variable for (in)equality with a constant, list the
 constant on the right, as in:
@@ -156,7 +156,7 @@ Rationale: Yoda conditions (as in 'if (1 == a)') are awkward to read.
 Besides, good compilers already warn users when '==' is mis-typed as '=',
 even when the constant is on the right.
 
-7. Comment style
+## Comment style
 
 We use traditional C-style /* */ comments and avoid // comments.
 
@@ -165,10 +165,12 @@ consistency of style. The checkpatch script will warn you about this.
 
 Multiline comment blocks should have a row of stars on the left,
 and the initial /* and terminating */ both on their own lines:
+
     /*
      * like
      * this
      */
+
 This is the same format required by the Linux kernel coding style.
 
 (Some of the existing comments in the codebase use the GNU Coding
@@ -180,24 +182,24 @@ comment anyway.)
 Rationale: Consistency, and ease of visually picking out a multiline
 comment from the surrounding code.
 
-8. trace-events style
+## trace-events style
 
-8.1 0x prefix
+### 0x prefix
 
 In trace-events files, use a '0x' prefix to specify hex numbers, as in:
 
-some_trace(unsigned x, uint64_t y) "x 0x%x y 0x" PRIx64
+    some_trace(unsigned x, uint64_t y) "x 0x%x y 0x" PRIx64
 
 An exception is made for groups of numbers that are hexadecimal by
 convention and separated by the symbols '.', '/', ':', or ' ' (such as
 PCI bus id):
 
-another_trace(int cssid, int ssid, int dev_num) "bus id: %x.%x.%04x"
+    another_trace(int cssid, int ssid, int dev_num) "bus id: %x.%x.%04x"
 
 However, you can use '0x' for such groups if you want. Anyway, be sure that
 it is obvious that numbers are in hex, ex.:
 
-data_dump(uint8_t c1, uint8_t c2, uint8_t c3) "bytes (in hex): %02x %02x %02x"
+    data_dump(uint8_t c1, uint8_t c2, uint8_t c3) "bytes (in hex): %02x %02x %02x"
 
 Rationale: hex numbers are hard to read in logs when there is no 0x prefix,
 especially when (occasionally) the representation doesn't contain any letters
@@ -205,12 +207,13 @@ and especially in one line with other decimal numbers. Number groups are allowed
 to not use '0x' because for some things notations like %x.%x.%x are used not
 only in Qemu. Also dumping raw data bytes with '0x' is less readable.
 
-8.2 '#' printf flag
+### '#' printf flag
 
 Do not use printf flag '#', like '%#x'.
 
 Rationale: there are two ways to add a '0x' prefix to printed number: '0x%...'
 and '%#...'. For consistency the only one way should be used. Arguments for
 '0x%' are:
+
  - it is more popular
  - '%#' omits the 0x for the value 0 which makes output inconsistent
diff --git a/HACKING b/HACKING.md
similarity index 88%
rename from HACKING
rename to HACKING.md
index 097d482603..f2f85be40f 100644
--- a/HACKING
+++ b/HACKING.md
@@ -1,19 +1,22 @@
-1. Preprocessor
+QEMU Hacking
+============
 
-1.1. Variadic macros
+## Preprocessor
+
+### Variadic macros
 
 For variadic macros, stick with this C99-like syntax:
 
-#define DPRINTF(fmt, ...)                                       \
-    do { printf("IRQ: " fmt, ## __VA_ARGS__); } while (0)
+    #define DPRINTF(fmt, ...)                                       \
+        do { printf("IRQ: " fmt, ## __VA_ARGS__); } while (0)
 
-1.2. Include directives
+### Include directives
 
 Order include directives as follows:
 
-#include "qemu/osdep.h"  /* Always first... */
-#include <...>           /* then system headers... */
-#include "..."           /* and finally QEMU headers. */
+    #include "qemu/osdep.h"  /* Always first... */
+    #include <...>           /* then system headers... */
+    #include "..."           /* and finally QEMU headers. */
 
 The "qemu/osdep.h" header contains preprocessor macros that affect the behavior
 of core system headers like <stdint.h>.  It must be the first include so that
@@ -23,12 +26,12 @@ that QEMU depends on.
 Do not include "qemu/osdep.h" from header files since the .c file will have
 already included it.
 
-2. C types
+## C types
 
 It should be common sense to use the right type, but we have collected
 a few useful guidelines here.
 
-2.1. Scalars
+### Scalars
 
 If you're using "int" or "long", odds are good that there's a better type.
 If a variable is counting something, it should be declared with an
@@ -89,7 +92,7 @@ Finally, while using descriptive types is important, be careful not to
 go overboard.  If whatever you're doing causes warnings, or requires
 casts, then reconsider or ask for help.
 
-2.2. Pointers
+### Pointers
 
 Ensure that all of your pointers are "const-correct".
 Unless a pointer is used to modify the pointed-to storage,
@@ -99,7 +102,7 @@ importantly, if we're diligent about this, when you see a non-const
 pointer, you're guaranteed that it is used to modify the storage
 it points to, or it is aliased to another pointer that is.
 
-2.3. Typedefs
+### Typedefs
 
 Typedefs are used to eliminate the redundant 'struct' keyword, since type
 names have a different style than other identifiers ("CamelCase" versus
@@ -114,11 +117,11 @@ definitions instead of typedefs in headers and function prototypes; this
 avoids problems with duplicated typedefs and reduces the need to include
 headers from other headers.
 
-2.4. Reserved namespaces in C and POSIX
+### Reserved namespaces in C and POSIX
 Underscore capital, double underscore, and underscore 't' suffixes should be
 avoided.
 
-3. Low level memory management
+## Low level memory management
 
 Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign
 APIs is not allowed in the QEMU codebase. Instead of these routines,
@@ -133,16 +136,15 @@ Calling g_malloc with a zero size is valid and will return NULL.
 Prefer g_new(T, n) instead of g_malloc(sizeof(T) * n) for the following
 reasons:
 
-  a. It catches multiplication overflowing size_t;
-  b. It returns T * instead of void *, letting compiler catch more type
-     errors.
+ * It catches multiplication overflowing size_t;
+ * It returns T * instead of void *, letting compiler catch more type errors.
 
 Declarations like T *v = g_malloc(sizeof(*v)) are acceptable, though.
 
 Memory allocated by qemu_memalign or qemu_blockalign must be freed with
 qemu_vfree, since breaking this will cause problems on Win32.
 
-4. String manipulation
+## String manipulation
 
 Do not use the strncpy function.  As mentioned in the man page, it does *not*
 guarantee a NULL-terminated buffer, which makes it extremely dangerous to use.
@@ -151,15 +153,17 @@ use this similar function when possible, but note its different signature:
 void pstrcpy(char *dest, int dest_buf_size, const char *src)
 
 Don't use strcat because it can't check for buffer overflows, but:
-char *pstrcat(char *buf, int buf_size, const char *s)
+
+    char *pstrcat(char *buf, int buf_size, const char *s)
 
 The same limitation exists with sprintf and vsprintf, so use snprintf and
 vsnprintf.
 
 QEMU provides other useful string functions:
-int strstart(const char *str, const char *val, const char **ptr)
-int stristart(const char *str, const char *val, const char **ptr)
-int qemu_strnlen(const char *s, int max_len)
+
+    int strstart(const char *str, const char *val, const char **ptr)
+    int stristart(const char *str, const char *val, const char **ptr)
+    int qemu_strnlen(const char *s, int max_len)
 
 There are also replacement character processing macros for isxyz and toxyz,
 so instead of e.g. isalnum you should use qemu_isalnum.
@@ -167,7 +171,7 @@ so instead of e.g. isalnum you should use qemu_isalnum.
 Because of the memory management rules, you must use g_strdup/g_strndup
 instead of plain strdup/strndup.
 
-5. Printf-style functions
+## Printf-style functions
 
 Whenever you add a new printf-style function, i.e., one with a format
 string argument and following "..." in its prototype, be sure to use
@@ -177,12 +181,13 @@ This makes it so gcc's -Wformat and -Wformat-security options can do
 their jobs and cross-check format strings with the number and types
 of arguments.
 
-6. C standard, implementation defined and undefined behaviors
+## C standard, implementation defined and undefined behaviors
 
 C code in QEMU should be written to the C99 language specification. A copy
 of the final version of the C99 standard with corrigenda TC1, TC2, and TC3
 included, formatted as a draft, can be downloaded from:
- http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf
+
+    http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf
 
 The C language specification defines regions of undefined behavior and
 implementation defined behavior (to give compiler authors enough leeway to
@@ -193,17 +198,18 @@ argument...) However there are a few areas where we allow ourselves to
 assume certain behaviors because in practice all the platforms we care about
 behave in the same way and writing strictly conformant code would be
 painful. These are:
- * you may assume that integers are 2s complement representation
- * you may assume that right shift of a signed integer duplicates
+
+ - you may assume that integers are 2s complement representation
+ - you may assume that right shift of a signed integer duplicates
    the sign bit (ie it is an arithmetic shift, not a logical shift)
 
 In addition, QEMU assumes that the compiler does not use the latitude
 given in C99 and C11 to treat aspects of signed '<<' as undefined, as
 documented in the GNU Compiler Collection manual starting at version 4.0.
 
-7. Error handling and reporting
+## Error handling and reporting
 
-7.1 Reporting errors to the human user
+### Reporting errors to the human user
 
 Do not use printf(), fprintf() or monitor_printf().  Instead, use
 error_report() or error_vreport() from error-report.h.  This ensures the
@@ -217,7 +223,7 @@ like command line parsing, the current location is tracked
 automatically.  To manipulate it manually, use the loc_*() from
 error-report.h.
 
-7.2 Propagating errors
+### Propagating errors
 
 An error can't always be reported to the user right where it's detected,
 but often needs to be propagated up the call chain to a place that can
@@ -242,7 +248,7 @@ Do not report an error to the user when you're also returning an error
 for somebody else to handle.  Leave the reporting to the place that
 consumes the error returned.
 
-7.3 Handling errors
+### Handling errors
 
 Calling exit() is fine when handling configuration errors during
 startup.  It's problematic during normal operation.  In particular,
diff --git a/README b/README
index 441c33eb2f..374b8f1486 100644
--- a/README
+++ b/README
@@ -60,7 +60,7 @@ When submitting patches, one common approach is to use 'git
 format-patch' and/or 'git send-email' to format & send the mail to the
 qemu-devel@nongnu.org mailing list. All patches submitted must contain
 a 'Signed-off-by' line from the author. Patches should follow the
-guidelines set out in the HACKING and CODING_STYLE files.
+guidelines set out in the HACKING.md and CODING_STYLE.md files.
 
 Additional information on submitting patches can be found online via
 the QEMU website
-- 
2.21.0



^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [Qemu-devel] [PATCH 2/4] docs: merge HACKING.md contents into CODING_STYLE.md
  2019-08-23 16:39 [Qemu-devel] [PATCH 0/4] docs: add docs about use of automatic cleanup functions Daniel P. Berrangé
  2019-08-23 16:39 ` [Qemu-devel] [PATCH 1/4] docs: convert CODING_STYLE and HACKING to markdown syntax Daniel P. Berrangé
@ 2019-08-23 16:39 ` Daniel P. Berrangé
  2019-08-23 19:35   ` Eric Blake
  2019-08-23 16:39 ` [Qemu-devel] [PATCH 3/4] docs: document use of automatic cleanup functions in glib Daniel P. Berrangé
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 18+ messages in thread
From: Daniel P. Berrangé @ 2019-08-23 16:39 UTC (permalink / raw)
  To: qemu-devel; +Cc: Alex Bennée, Daniel P. Berrangé

The split of information between the two docs is rather arbitary and
unclear. It is simpler for contributors if all the information is in
one file.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 CODING_STYLE.md | 262 +++++++++++++++++++++++++++++++++++++++++++++++
 HACKING.md      | 263 ------------------------------------------------
 README          |   2 +-
 3 files changed, 263 insertions(+), 264 deletions(-)
 delete mode 100644 HACKING.md

diff --git a/CODING_STYLE.md b/CODING_STYLE.md
index 056eda7739..9f4fc9dc77 100644
--- a/CODING_STYLE.md
+++ b/CODING_STYLE.md
@@ -217,3 +217,265 @@ and '%#...'. For consistency the only one way should be used. Arguments for
 
  - it is more popular
  - '%#' omits the 0x for the value 0 which makes output inconsistent
+
+
+## Preprocessor
+
+### Variadic macros
+
+For variadic macros, stick with this C99-like syntax:
+
+    #define DPRINTF(fmt, ...)                                       \
+        do { printf("IRQ: " fmt, ## __VA_ARGS__); } while (0)
+
+### Include directives
+
+Order include directives as follows:
+
+    #include "qemu/osdep.h"  /* Always first... */
+    #include <...>           /* then system headers... */
+    #include "..."           /* and finally QEMU headers. */
+
+The "qemu/osdep.h" header contains preprocessor macros that affect the behavior
+of core system headers like <stdint.h>.  It must be the first include so that
+core system headers included by external libraries get the preprocessor macros
+that QEMU depends on.
+
+Do not include "qemu/osdep.h" from header files since the .c file will have
+already included it.
+
+## C types
+
+It should be common sense to use the right type, but we have collected
+a few useful guidelines here.
+
+### Scalars
+
+If you're using "int" or "long", odds are good that there's a better type.
+If a variable is counting something, it should be declared with an
+unsigned type.
+
+If it's host memory-size related, size_t should be a good choice (use
+ssize_t only if required). Guest RAM memory offsets must use ram_addr_t,
+but only for RAM, it may not cover whole guest address space.
+
+If it's file-size related, use off_t.
+If it's file-offset related (i.e., signed), use off_t.
+If it's just counting small numbers use "unsigned int";
+(on all but oddball embedded systems, you can assume that that
+type is at least four bytes wide).
+
+In the event that you require a specific width, use a standard type
+like int32_t, uint32_t, uint64_t, etc.  The specific types are
+mandatory for VMState fields.
+
+Don't use Linux kernel internal types like u32, __u32 or __le32.
+
+Use hwaddr for guest physical addresses except pcibus_t
+for PCI addresses.  In addition, ram_addr_t is a QEMU internal address
+space that maps guest RAM physical addresses into an intermediate
+address space that can map to host virtual address spaces.  Generally
+speaking, the size of guest memory can always fit into ram_addr_t but
+it would not be correct to store an actual guest physical address in a
+ram_addr_t.
+
+For CPU virtual addresses there are several possible types.
+vaddr is the best type to use to hold a CPU virtual address in
+target-independent code. It is guaranteed to be large enough to hold a
+virtual address for any target, and it does not change size from target
+to target. It is always unsigned.
+target_ulong is a type the size of a virtual address on the CPU; this means
+it may be 32 or 64 bits depending on which target is being built. It should
+therefore be used only in target-specific code, and in some
+performance-critical built-per-target core code such as the TLB code.
+There is also a signed version, target_long.
+abi_ulong is for the *-user targets, and represents a type the size of
+'void *' in that target's ABI. (This may not be the same as the size of a
+full CPU virtual address in the case of target ABIs which use 32 bit pointers
+on 64 bit CPUs, like sparc32plus.) Definitions of structures that must match
+the target's ABI must use this type for anything that on the target is defined
+to be an 'unsigned long' or a pointer type.
+There is also a signed version, abi_long.
+
+Of course, take all of the above with a grain of salt.  If you're about
+to use some system interface that requires a type like size_t, pid_t or
+off_t, use matching types for any corresponding variables.
+
+Also, if you try to use e.g., "unsigned int" as a type, and that
+conflicts with the signedness of a related variable, sometimes
+it's best just to use the *wrong* type, if "pulling the thread"
+and fixing all related variables would be too invasive.
+
+Finally, while using descriptive types is important, be careful not to
+go overboard.  If whatever you're doing causes warnings, or requires
+casts, then reconsider or ask for help.
+
+### Pointers
+
+Ensure that all of your pointers are "const-correct".
+Unless a pointer is used to modify the pointed-to storage,
+give it the "const" attribute.  That way, the reader knows
+up-front that this is a read-only pointer.  Perhaps more
+importantly, if we're diligent about this, when you see a non-const
+pointer, you're guaranteed that it is used to modify the storage
+it points to, or it is aliased to another pointer that is.
+
+### Typedefs
+
+Typedefs are used to eliminate the redundant 'struct' keyword, since type
+names have a different style than other identifiers ("CamelCase" versus
+"snake_case").  Each named struct type should have a CamelCase name and a
+corresponding typedef.
+
+Since certain C compilers choke on duplicated typedefs, you should avoid
+them and declare a typedef only in one header file.  For common types,
+you can use "include/qemu/typedefs.h" for example.  However, as a matter
+of convenience it is also perfectly fine to use forward struct
+definitions instead of typedefs in headers and function prototypes; this
+avoids problems with duplicated typedefs and reduces the need to include
+headers from other headers.
+
+### Reserved namespaces in C and POSIX
+Underscore capital, double underscore, and underscore 't' suffixes should be
+avoided.
+
+## Low level memory management
+
+Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign
+APIs is not allowed in the QEMU codebase. Instead of these routines,
+use the GLib memory allocation routines g_malloc/g_malloc0/g_new/
+g_new0/g_realloc/g_free or QEMU's qemu_memalign/qemu_blockalign/qemu_vfree
+APIs.
+
+Please note that g_malloc will exit on allocation failure, so there
+is no need to test for failure (as you would have to with malloc).
+Calling g_malloc with a zero size is valid and will return NULL.
+
+Prefer g_new(T, n) instead of g_malloc(sizeof(T) * n) for the following
+reasons:
+
+ * It catches multiplication overflowing size_t;
+ * It returns T * instead of void *, letting compiler catch more type errors.
+
+Declarations like T *v = g_malloc(sizeof(*v)) are acceptable, though.
+
+Memory allocated by qemu_memalign or qemu_blockalign must be freed with
+qemu_vfree, since breaking this will cause problems on Win32.
+
+## String manipulation
+
+Do not use the strncpy function.  As mentioned in the man page, it does *not*
+guarantee a NULL-terminated buffer, which makes it extremely dangerous to use.
+It also zeros trailing destination bytes out to the specified length.  Instead,
+use this similar function when possible, but note its different signature:
+void pstrcpy(char *dest, int dest_buf_size, const char *src)
+
+Don't use strcat because it can't check for buffer overflows, but:
+
+    char *pstrcat(char *buf, int buf_size, const char *s)
+
+The same limitation exists with sprintf and vsprintf, so use snprintf and
+vsnprintf.
+
+QEMU provides other useful string functions:
+
+    int strstart(const char *str, const char *val, const char **ptr)
+    int stristart(const char *str, const char *val, const char **ptr)
+    int qemu_strnlen(const char *s, int max_len)
+
+There are also replacement character processing macros for isxyz and toxyz,
+so instead of e.g. isalnum you should use qemu_isalnum.
+
+Because of the memory management rules, you must use g_strdup/g_strndup
+instead of plain strdup/strndup.
+
+## Printf-style functions
+
+Whenever you add a new printf-style function, i.e., one with a format
+string argument and following "..." in its prototype, be sure to use
+gcc's printf attribute directive in the prototype.
+
+This makes it so gcc's -Wformat and -Wformat-security options can do
+their jobs and cross-check format strings with the number and types
+of arguments.
+
+## C standard, implementation defined and undefined behaviors
+
+C code in QEMU should be written to the C99 language specification. A copy
+of the final version of the C99 standard with corrigenda TC1, TC2, and TC3
+included, formatted as a draft, can be downloaded from:
+
+    http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf
+
+The C language specification defines regions of undefined behavior and
+implementation defined behavior (to give compiler authors enough leeway to
+produce better code).  In general, code in QEMU should follow the language
+specification and avoid both undefined and implementation defined
+constructs. ("It works fine on the gcc I tested it with" is not a valid
+argument...) However there are a few areas where we allow ourselves to
+assume certain behaviors because in practice all the platforms we care about
+behave in the same way and writing strictly conformant code would be
+painful. These are:
+
+ - you may assume that integers are 2s complement representation
+ - you may assume that right shift of a signed integer duplicates
+   the sign bit (ie it is an arithmetic shift, not a logical shift)
+
+In addition, QEMU assumes that the compiler does not use the latitude
+given in C99 and C11 to treat aspects of signed '<<' as undefined, as
+documented in the GNU Compiler Collection manual starting at version 4.0.
+
+## Error handling and reporting
+
+### Reporting errors to the human user
+
+Do not use printf(), fprintf() or monitor_printf().  Instead, use
+error_report() or error_vreport() from error-report.h.  This ensures the
+error is reported in the right place (current monitor or stderr), and in
+a uniform format.
+
+Use error_printf() & friends to print additional information.
+
+error_report() prints the current location.  In certain common cases
+like command line parsing, the current location is tracked
+automatically.  To manipulate it manually, use the loc_*() from
+error-report.h.
+
+### Propagating errors
+
+An error can't always be reported to the user right where it's detected,
+but often needs to be propagated up the call chain to a place that can
+handle it.  This can be done in various ways.
+
+The most flexible one is Error objects.  See error.h for usage
+information.
+
+Use the simplest suitable method to communicate success / failure to
+callers.  Stick to common methods: non-negative on success / -1 on
+error, non-negative / -errno, non-null / null, or Error objects.
+
+Example: when a function returns a non-null pointer on success, and it
+can fail only in one way (as far as the caller is concerned), returning
+null on failure is just fine, and certainly simpler and a lot easier on
+the eyes than propagating an Error object through an Error ** parameter.
+
+Example: when a function's callers need to report details on failure
+only the function really knows, use Error **, and set suitable errors.
+
+Do not report an error to the user when you're also returning an error
+for somebody else to handle.  Leave the reporting to the place that
+consumes the error returned.
+
+### Handling errors
+
+Calling exit() is fine when handling configuration errors during
+startup.  It's problematic during normal operation.  In particular,
+monitor commands should never exit().
+
+Do not call exit() or abort() to handle an error that can be triggered
+by the guest (e.g., some unimplemented corner case in guest code
+translation or device emulation).  Guests should not be able to
+terminate QEMU.
+
+Note that &error_fatal is just another way to exit(1), and &error_abort
+is just another way to abort().
diff --git a/HACKING.md b/HACKING.md
deleted file mode 100644
index f2f85be40f..0000000000
--- a/HACKING.md
+++ /dev/null
@@ -1,263 +0,0 @@
-QEMU Hacking
-============
-
-## Preprocessor
-
-### Variadic macros
-
-For variadic macros, stick with this C99-like syntax:
-
-    #define DPRINTF(fmt, ...)                                       \
-        do { printf("IRQ: " fmt, ## __VA_ARGS__); } while (0)
-
-### Include directives
-
-Order include directives as follows:
-
-    #include "qemu/osdep.h"  /* Always first... */
-    #include <...>           /* then system headers... */
-    #include "..."           /* and finally QEMU headers. */
-
-The "qemu/osdep.h" header contains preprocessor macros that affect the behavior
-of core system headers like <stdint.h>.  It must be the first include so that
-core system headers included by external libraries get the preprocessor macros
-that QEMU depends on.
-
-Do not include "qemu/osdep.h" from header files since the .c file will have
-already included it.
-
-## C types
-
-It should be common sense to use the right type, but we have collected
-a few useful guidelines here.
-
-### Scalars
-
-If you're using "int" or "long", odds are good that there's a better type.
-If a variable is counting something, it should be declared with an
-unsigned type.
-
-If it's host memory-size related, size_t should be a good choice (use
-ssize_t only if required). Guest RAM memory offsets must use ram_addr_t,
-but only for RAM, it may not cover whole guest address space.
-
-If it's file-size related, use off_t.
-If it's file-offset related (i.e., signed), use off_t.
-If it's just counting small numbers use "unsigned int";
-(on all but oddball embedded systems, you can assume that that
-type is at least four bytes wide).
-
-In the event that you require a specific width, use a standard type
-like int32_t, uint32_t, uint64_t, etc.  The specific types are
-mandatory for VMState fields.
-
-Don't use Linux kernel internal types like u32, __u32 or __le32.
-
-Use hwaddr for guest physical addresses except pcibus_t
-for PCI addresses.  In addition, ram_addr_t is a QEMU internal address
-space that maps guest RAM physical addresses into an intermediate
-address space that can map to host virtual address spaces.  Generally
-speaking, the size of guest memory can always fit into ram_addr_t but
-it would not be correct to store an actual guest physical address in a
-ram_addr_t.
-
-For CPU virtual addresses there are several possible types.
-vaddr is the best type to use to hold a CPU virtual address in
-target-independent code. It is guaranteed to be large enough to hold a
-virtual address for any target, and it does not change size from target
-to target. It is always unsigned.
-target_ulong is a type the size of a virtual address on the CPU; this means
-it may be 32 or 64 bits depending on which target is being built. It should
-therefore be used only in target-specific code, and in some
-performance-critical built-per-target core code such as the TLB code.
-There is also a signed version, target_long.
-abi_ulong is for the *-user targets, and represents a type the size of
-'void *' in that target's ABI. (This may not be the same as the size of a
-full CPU virtual address in the case of target ABIs which use 32 bit pointers
-on 64 bit CPUs, like sparc32plus.) Definitions of structures that must match
-the target's ABI must use this type for anything that on the target is defined
-to be an 'unsigned long' or a pointer type.
-There is also a signed version, abi_long.
-
-Of course, take all of the above with a grain of salt.  If you're about
-to use some system interface that requires a type like size_t, pid_t or
-off_t, use matching types for any corresponding variables.
-
-Also, if you try to use e.g., "unsigned int" as a type, and that
-conflicts with the signedness of a related variable, sometimes
-it's best just to use the *wrong* type, if "pulling the thread"
-and fixing all related variables would be too invasive.
-
-Finally, while using descriptive types is important, be careful not to
-go overboard.  If whatever you're doing causes warnings, or requires
-casts, then reconsider or ask for help.
-
-### Pointers
-
-Ensure that all of your pointers are "const-correct".
-Unless a pointer is used to modify the pointed-to storage,
-give it the "const" attribute.  That way, the reader knows
-up-front that this is a read-only pointer.  Perhaps more
-importantly, if we're diligent about this, when you see a non-const
-pointer, you're guaranteed that it is used to modify the storage
-it points to, or it is aliased to another pointer that is.
-
-### Typedefs
-
-Typedefs are used to eliminate the redundant 'struct' keyword, since type
-names have a different style than other identifiers ("CamelCase" versus
-"snake_case").  Each named struct type should have a CamelCase name and a
-corresponding typedef.
-
-Since certain C compilers choke on duplicated typedefs, you should avoid
-them and declare a typedef only in one header file.  For common types,
-you can use "include/qemu/typedefs.h" for example.  However, as a matter
-of convenience it is also perfectly fine to use forward struct
-definitions instead of typedefs in headers and function prototypes; this
-avoids problems with duplicated typedefs and reduces the need to include
-headers from other headers.
-
-### Reserved namespaces in C and POSIX
-Underscore capital, double underscore, and underscore 't' suffixes should be
-avoided.
-
-## Low level memory management
-
-Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign
-APIs is not allowed in the QEMU codebase. Instead of these routines,
-use the GLib memory allocation routines g_malloc/g_malloc0/g_new/
-g_new0/g_realloc/g_free or QEMU's qemu_memalign/qemu_blockalign/qemu_vfree
-APIs.
-
-Please note that g_malloc will exit on allocation failure, so there
-is no need to test for failure (as you would have to with malloc).
-Calling g_malloc with a zero size is valid and will return NULL.
-
-Prefer g_new(T, n) instead of g_malloc(sizeof(T) * n) for the following
-reasons:
-
- * It catches multiplication overflowing size_t;
- * It returns T * instead of void *, letting compiler catch more type errors.
-
-Declarations like T *v = g_malloc(sizeof(*v)) are acceptable, though.
-
-Memory allocated by qemu_memalign or qemu_blockalign must be freed with
-qemu_vfree, since breaking this will cause problems on Win32.
-
-## String manipulation
-
-Do not use the strncpy function.  As mentioned in the man page, it does *not*
-guarantee a NULL-terminated buffer, which makes it extremely dangerous to use.
-It also zeros trailing destination bytes out to the specified length.  Instead,
-use this similar function when possible, but note its different signature:
-void pstrcpy(char *dest, int dest_buf_size, const char *src)
-
-Don't use strcat because it can't check for buffer overflows, but:
-
-    char *pstrcat(char *buf, int buf_size, const char *s)
-
-The same limitation exists with sprintf and vsprintf, so use snprintf and
-vsnprintf.
-
-QEMU provides other useful string functions:
-
-    int strstart(const char *str, const char *val, const char **ptr)
-    int stristart(const char *str, const char *val, const char **ptr)
-    int qemu_strnlen(const char *s, int max_len)
-
-There are also replacement character processing macros for isxyz and toxyz,
-so instead of e.g. isalnum you should use qemu_isalnum.
-
-Because of the memory management rules, you must use g_strdup/g_strndup
-instead of plain strdup/strndup.
-
-## Printf-style functions
-
-Whenever you add a new printf-style function, i.e., one with a format
-string argument and following "..." in its prototype, be sure to use
-gcc's printf attribute directive in the prototype.
-
-This makes it so gcc's -Wformat and -Wformat-security options can do
-their jobs and cross-check format strings with the number and types
-of arguments.
-
-## C standard, implementation defined and undefined behaviors
-
-C code in QEMU should be written to the C99 language specification. A copy
-of the final version of the C99 standard with corrigenda TC1, TC2, and TC3
-included, formatted as a draft, can be downloaded from:
-
-    http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf
-
-The C language specification defines regions of undefined behavior and
-implementation defined behavior (to give compiler authors enough leeway to
-produce better code).  In general, code in QEMU should follow the language
-specification and avoid both undefined and implementation defined
-constructs. ("It works fine on the gcc I tested it with" is not a valid
-argument...) However there are a few areas where we allow ourselves to
-assume certain behaviors because in practice all the platforms we care about
-behave in the same way and writing strictly conformant code would be
-painful. These are:
-
- - you may assume that integers are 2s complement representation
- - you may assume that right shift of a signed integer duplicates
-   the sign bit (ie it is an arithmetic shift, not a logical shift)
-
-In addition, QEMU assumes that the compiler does not use the latitude
-given in C99 and C11 to treat aspects of signed '<<' as undefined, as
-documented in the GNU Compiler Collection manual starting at version 4.0.
-
-## Error handling and reporting
-
-### Reporting errors to the human user
-
-Do not use printf(), fprintf() or monitor_printf().  Instead, use
-error_report() or error_vreport() from error-report.h.  This ensures the
-error is reported in the right place (current monitor or stderr), and in
-a uniform format.
-
-Use error_printf() & friends to print additional information.
-
-error_report() prints the current location.  In certain common cases
-like command line parsing, the current location is tracked
-automatically.  To manipulate it manually, use the loc_*() from
-error-report.h.
-
-### Propagating errors
-
-An error can't always be reported to the user right where it's detected,
-but often needs to be propagated up the call chain to a place that can
-handle it.  This can be done in various ways.
-
-The most flexible one is Error objects.  See error.h for usage
-information.
-
-Use the simplest suitable method to communicate success / failure to
-callers.  Stick to common methods: non-negative on success / -1 on
-error, non-negative / -errno, non-null / null, or Error objects.
-
-Example: when a function returns a non-null pointer on success, and it
-can fail only in one way (as far as the caller is concerned), returning
-null on failure is just fine, and certainly simpler and a lot easier on
-the eyes than propagating an Error object through an Error ** parameter.
-
-Example: when a function's callers need to report details on failure
-only the function really knows, use Error **, and set suitable errors.
-
-Do not report an error to the user when you're also returning an error
-for somebody else to handle.  Leave the reporting to the place that
-consumes the error returned.
-
-### Handling errors
-
-Calling exit() is fine when handling configuration errors during
-startup.  It's problematic during normal operation.  In particular,
-monitor commands should never exit().
-
-Do not call exit() or abort() to handle an error that can be triggered
-by the guest (e.g., some unimplemented corner case in guest code
-translation or device emulation).  Guests should not be able to
-terminate QEMU.
-
-Note that &error_fatal is just another way to exit(1), and &error_abort
-is just another way to abort().
diff --git a/README b/README
index 374b8f1486..9d2c2688ad 100644
--- a/README
+++ b/README
@@ -60,7 +60,7 @@ When submitting patches, one common approach is to use 'git
 format-patch' and/or 'git send-email' to format & send the mail to the
 qemu-devel@nongnu.org mailing list. All patches submitted must contain
 a 'Signed-off-by' line from the author. Patches should follow the
-guidelines set out in the HACKING.md and CODING_STYLE.md files.
+guidelines set out in the CODING_STYLE.md file.
 
 Additional information on submitting patches can be found online via
 the QEMU website
-- 
2.21.0



^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [Qemu-devel] [PATCH 3/4] docs: document use of automatic cleanup functions in glib
  2019-08-23 16:39 [Qemu-devel] [PATCH 0/4] docs: add docs about use of automatic cleanup functions Daniel P. Berrangé
  2019-08-23 16:39 ` [Qemu-devel] [PATCH 1/4] docs: convert CODING_STYLE and HACKING to markdown syntax Daniel P. Berrangé
  2019-08-23 16:39 ` [Qemu-devel] [PATCH 2/4] docs: merge HACKING.md contents into CODING_STYLE.md Daniel P. Berrangé
@ 2019-08-23 16:39 ` Daniel P. Berrangé
  2019-08-23 19:53   ` Eric Blake
                     ` (2 more replies)
  2019-08-23 16:39 ` [Qemu-devel] [PATCH 4/4] docs: add table of contents to CODING_STYLE.md Daniel P. Berrangé
  2019-08-23 21:48 ` [Qemu-devel] [PATCH 0/4] docs: add docs about use of automatic cleanup functions Marc-André Lureau
  4 siblings, 3 replies; 18+ messages in thread
From: Daniel P. Berrangé @ 2019-08-23 16:39 UTC (permalink / raw)
  To: qemu-devel; +Cc: Alex Bennée, Daniel P. Berrangé

Document the use of g_autofree and g_autoptr in glib for automatic
freeing of memory, or other resource cleanup (eg mutex unlocking).

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 CODING_STYLE.md | 101 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 101 insertions(+)

diff --git a/CODING_STYLE.md b/CODING_STYLE.md
index 9f4fc9dc77..f37b6c2d01 100644
--- a/CODING_STYLE.md
+++ b/CODING_STYLE.md
@@ -479,3 +479,104 @@ terminate QEMU.
 
 Note that &error_fatal is just another way to exit(1), and &error_abort
 is just another way to abort().
+
+
+## Automatic memory deallocation
+
+QEMU has a mandatory dependency either the GCC or CLang compiler. As
+such it has the freedom to make use of a C language extension for
+automatically running a cleanup function when a stack variable goes
+out of scope. This can be used to simplify function cleanup paths,
+often allowing many goto jumps to be eliminated, through automatic
+free'ing of memory.
+
+The GLib2 library provides a number of functions/macros for enabling
+automatic cleanup:
+
+ https://developer.gnome.org/glib/stable/glib-Miscellaneous-Macros.html
+
+Most notably:
+
+ - g_autofree - will invoke g_free() on the variable going out of scope
+
+ - g_autoptr - for structs / objects, will invoke the cleanup func created
+               by a previous use of G_DEFINE_AUTOPTR_CLEANUP_FUNC. This is
+               supported for most GLib data types and GObjects
+
+For example, instead of
+
+    int somefunc(void) {
+        int ret = -1;
+        char *foo = g_strdup_printf("foo%", "wibble");
+        GList *bar = .....
+
+        if (eek) {
+           goto cleanup;
+        }
+
+        ret = 0;
+
+      cleanup:
+        g_free(foo);
+        g_list_free(bar);
+        return ret;
+    }
+
+Using g_autofree/g_autoptr enables the code to be written as:
+
+    int somefunc(void) {
+        g_autofree char *foo = g_strdup_printf("foo%", "wibble");
+        g_autoptr (GList) bar = .....
+
+        if (eek) {
+           return -1;
+        }
+
+        return 0;
+    }
+
+While this generally results in simpler, less leak-prone code, there
+are still some caveats to beware of
+
+ * Variables declared with g_auto* MUST always be initialized,
+   otherwise the cleanup function will use uninitialized stack memory
+
+ * If a variable declared with g_auto* holds a value which must
+   live beyond the life of the function, that value must be saved
+   and the original variable NULL'd out. This can be simpler using
+   g_steal_pointer
+
+
+    char *somefunc(void) {
+        g_autofree char *foo = g_strdup_printf("foo%", "wibble");
+        g_autoptr (GList) bar = .....
+
+        if (eek) {
+           return NULL;
+        }
+
+        return g_steal_pointer(&foo);
+    }
+
+The cleanup functions are not restricted to simply free'ing memory. The
+GMutexLocker class is a variant of GMutex that has automatic locking and
+unlocking at start and end of the enclosing scope
+
+In the following example, the `lock` in `MyObj` will be held for the
+precise duration of the `somefunc` function
+
+    typedef struct {
+        GMutex lock;
+    } MyObj;
+
+    char *somefunc(MyObj *obj) {
+        g_autofree GMutexLocker *locker = g_mutex_locker_new(&obj->lock)
+        g_autofree char *foo = g_strdup_printf("foo%", "wibble");
+        g_autoptr (GList) bar = .....
+
+        if (eek) {
+           return NULL;
+        }
+
+        return g_steal_pointer(&foo);
+    }
-- 
2.21.0



^ permalink raw reply related	[flat|nested] 18+ messages in thread

* [Qemu-devel] [PATCH 4/4] docs: add table of contents to CODING_STYLE.md
  2019-08-23 16:39 [Qemu-devel] [PATCH 0/4] docs: add docs about use of automatic cleanup functions Daniel P. Berrangé
                   ` (2 preceding siblings ...)
  2019-08-23 16:39 ` [Qemu-devel] [PATCH 3/4] docs: document use of automatic cleanup functions in glib Daniel P. Berrangé
@ 2019-08-23 16:39 ` Daniel P. Berrangé
  2019-08-23 21:48 ` [Qemu-devel] [PATCH 0/4] docs: add docs about use of automatic cleanup functions Marc-André Lureau
  4 siblings, 0 replies; 18+ messages in thread
From: Daniel P. Berrangé @ 2019-08-23 16:39 UTC (permalink / raw)
  To: qemu-devel; +Cc: Alex Bennée, Daniel P. Berrangé

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---
 CODING_STYLE.md | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/CODING_STYLE.md b/CODING_STYLE.md
index f37b6c2d01..0841edb2f7 100644
--- a/CODING_STYLE.md
+++ b/CODING_STYLE.md
@@ -1,6 +1,37 @@
 QEMU Coding Style
 =================
 
+#### Table Of Contents
+
+  * [Whitespace](#whitespace)
+    + [Multiline Indent](#multiline-indent)
+  * [Line width](#line-width)
+  * [Naming](#naming)
+  * [Block structure](#block-structure)
+  * [Declarations](#declarations)
+  * [Conditional statements](#conditional-statements)
+  * [Comment style](#comment-style)
+  * [trace-events style](#trace-events-style)
+    + [0x prefix](#0x-prefix)
+    + ['#' printf flag](#----printf-flag)
+  * [Preprocessor](#preprocessor)
+    + [Variadic macros](#variadic-macros)
+    + [Include directives](#include-directives)
+  * [C types](#c-types)
+    + [Scalars](#scalars)
+    + [Pointers](#pointers)
+    + [Typedefs](#typedefs)
+    + [Reserved namespaces in C and POSIX](#reserved-namespaces-in-c-and-posix)
+  * [Low level memory management](#low-level-memory-management)
+  * [String manipulation](#string-manipulation)
+  * [Printf-style functions](#printf-style-functions)
+  * [C standard, implementation defined and undefined behaviors](#c-standard--implementation-defined-and-undefined-behaviors)
+  * [Error handling and reporting](#error-handling-and-reporting)
+    + [Reporting errors to the human user](#reporting-errors-to-the-human-user)
+    + [Propagating errors](#propagating-errors)
+    + [Handling errors](#handling-errors)
+  * [Automatic memory deallocation](#automatic-memory-deallocation)
+
 Please use the script checkpatch.pl in the scripts directory to check
 patches before submitting.
 
-- 
2.21.0



^ permalink raw reply related	[flat|nested] 18+ messages in thread

* Re: [Qemu-devel] [PATCH 2/4] docs: merge HACKING.md contents into CODING_STYLE.md
  2019-08-23 16:39 ` [Qemu-devel] [PATCH 2/4] docs: merge HACKING.md contents into CODING_STYLE.md Daniel P. Berrangé
@ 2019-08-23 19:35   ` Eric Blake
  2019-08-28 15:06     ` Alex Bennée
  0 siblings, 1 reply; 18+ messages in thread
From: Eric Blake @ 2019-08-23 19:35 UTC (permalink / raw)
  To: Daniel P. Berrangé, qemu-devel; +Cc: Alex Bennée


[-- Attachment #1.1: Type: text/plain, Size: 941 bytes --]

On 8/23/19 11:39 AM, Daniel P. Berrangé wrote:
> The split of information between the two docs is rather arbitary and
> unclear. It is simpler for contributors if all the information is in
> one file.
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  CODING_STYLE.md | 262 +++++++++++++++++++++++++++++++++++++++++++++++
>  HACKING.md      | 263 ------------------------------------------------
>  README          |   2 +-
>  3 files changed, 263 insertions(+), 264 deletions(-)
>  delete mode 100644 HACKING.md

Is it worth trying to group related sections as part of the combination?
 (Your solution of just concatenating at the end is obviously the
fastest, but may result in odd ordering where similar things are
mentioned twice but in different parts of the file).

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [Qemu-devel] [PATCH 3/4] docs: document use of automatic cleanup functions in glib
  2019-08-23 16:39 ` [Qemu-devel] [PATCH 3/4] docs: document use of automatic cleanup functions in glib Daniel P. Berrangé
@ 2019-08-23 19:53   ` Eric Blake
  2019-08-28  9:00   ` Stefan Hajnoczi
  2019-08-28 15:14   ` Alex Bennée
  2 siblings, 0 replies; 18+ messages in thread
From: Eric Blake @ 2019-08-23 19:53 UTC (permalink / raw)
  To: Daniel P. Berrangé, qemu-devel; +Cc: Alex Bennée


[-- Attachment #1.1: Type: text/plain, Size: 1255 bytes --]

On 8/23/19 11:39 AM, Daniel P. Berrangé wrote:
> Document the use of g_autofree and g_autoptr in glib for automatic
> freeing of memory, or other resource cleanup (eg mutex unlocking).
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  CODING_STYLE.md | 101 ++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 101 insertions(+)


> +The cleanup functions are not restricted to simply free'ing memory. The
> +GMutexLocker class is a variant of GMutex that has automatic locking and
> +unlocking at start and end of the enclosing scope
> +
> +In the following example, the `lock` in `MyObj` will be held for the
> +precise duration of the `somefunc` function
> +
> +    typedef struct {
> +        GMutex lock;
> +    } MyObj;
> +
> +    char *somefunc(MyObj *obj) {
> +        g_autofree GMutexLocker *locker = g_mutex_locker_new(&obj->lock)

Wrong example (you don't want to call g_free, and you missed ';'). This
should be

g_autoptr (GMutexLocker) locker = g_mutex_locker_new(&obj->lock);

With that fixed,

Reviewed-by: Eric Blake <eblake@redhat.com>

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [Qemu-devel] [PATCH 0/4] docs: add docs about use of automatic cleanup functions
  2019-08-23 16:39 [Qemu-devel] [PATCH 0/4] docs: add docs about use of automatic cleanup functions Daniel P. Berrangé
                   ` (3 preceding siblings ...)
  2019-08-23 16:39 ` [Qemu-devel] [PATCH 4/4] docs: add table of contents to CODING_STYLE.md Daniel P. Berrangé
@ 2019-08-23 21:48 ` Marc-André Lureau
  2019-08-28 12:30   ` Alex Bennée
  4 siblings, 1 reply; 18+ messages in thread
From: Marc-André Lureau @ 2019-08-23 21:48 UTC (permalink / raw)
  To: Daniel P. Berrangé; +Cc: Alex Bennée, QEMU

Hi

On Fri, Aug 23, 2019 at 8:41 PM Daniel P. Berrangé <berrange@redhat.com> wrote:
>
> This is ostensibly about adding docs for the g_autofree/g_autoptr
> macros. As part of doing that, however, the existing HACKING doc
> is merged into the CODING_STYLE doc and the text is converted to
> markdown with a table of contents.

Why not rst, so it can integrate with the rest of qemu sphinx doc?

(markdown is quite poor when it comes to documenting code or
cross-referencing etc)



>
> Daniel P. Berrangé (4):
>   docs: convert CODING_STYLE and HACKING to markdown syntax
>   docs: merge HACKING.md contents into CODING_STYLE.md
>   docs: document use of automatic cleanup functions in glib
>   docs: add table of contents to CODING_STYLE.md
>
>  CODING_STYLE    | 216 -----------------
>  CODING_STYLE.md | 613 ++++++++++++++++++++++++++++++++++++++++++++++++
>  HACKING         | 257 --------------------
>  README          |   2 +-
>  4 files changed, 614 insertions(+), 474 deletions(-)
>  delete mode 100644 CODING_STYLE
>  create mode 100644 CODING_STYLE.md
>  delete mode 100644 HACKING
>
> --
> 2.21.0
>
>


-- 
Marc-André Lureau


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [Qemu-devel] [PATCH 3/4] docs: document use of automatic cleanup functions in glib
  2019-08-23 16:39 ` [Qemu-devel] [PATCH 3/4] docs: document use of automatic cleanup functions in glib Daniel P. Berrangé
  2019-08-23 19:53   ` Eric Blake
@ 2019-08-28  9:00   ` Stefan Hajnoczi
  2019-08-28 15:14   ` Alex Bennée
  2 siblings, 0 replies; 18+ messages in thread
From: Stefan Hajnoczi @ 2019-08-28  9:00 UTC (permalink / raw)
  To: Daniel P. Berrangé; +Cc: Alex Bennée, qemu-devel

[-- Attachment #1: Type: text/plain, Size: 473 bytes --]

On Fri, Aug 23, 2019 at 05:39:30PM +0100, Daniel P. Berrangé wrote:
> Document the use of g_autofree and g_autoptr in glib for automatic
> freeing of memory, or other resource cleanup (eg mutex unlocking).
> 
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  CODING_STYLE.md | 101 ++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 101 insertions(+)

With Eric's suggestion:

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [Qemu-devel] [PATCH 1/4] docs: convert CODING_STYLE and HACKING to markdown syntax
  2019-08-23 16:39 ` [Qemu-devel] [PATCH 1/4] docs: convert CODING_STYLE and HACKING to markdown syntax Daniel P. Berrangé
@ 2019-08-28 12:25   ` Alex Bennée
  2019-08-28 13:08     ` Daniel P. Berrangé
  0 siblings, 1 reply; 18+ messages in thread
From: Alex Bennée @ 2019-08-28 12:25 UTC (permalink / raw)
  To: Daniel P. Berrangé; +Cc: qemu-devel


Daniel P. Berrangé <berrange@redhat.com> writes:

> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>

> diff --git a/README b/README
> index 441c33eb2f..374b8f1486 100644
> --- a/README
> +++ b/README
> @@ -60,7 +60,7 @@ When submitting patches, one common approach is to use 'git
>  format-patch' and/or 'git send-email' to format & send the mail to the
>  qemu-devel@nongnu.org mailing list. All patches submitted must contain
>  a 'Signed-off-by' line from the author. Patches should follow the
> -guidelines set out in the HACKING and CODING_STYLE files.
> +guidelines set out in the HACKING.md and CODING_STYLE.md files.
>
>  Additional information on submitting patches can be found online via
>  the QEMU website

It's tempting to suggest we go the whole hog and convert the README as
well. We could then add CI buttons which would render nicely on the
github/gitlab mirrors.

--
Alex Bennée


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [Qemu-devel] [PATCH 0/4] docs: add docs about use of automatic cleanup functions
  2019-08-23 21:48 ` [Qemu-devel] [PATCH 0/4] docs: add docs about use of automatic cleanup functions Marc-André Lureau
@ 2019-08-28 12:30   ` Alex Bennée
  2019-08-28 13:07     ` Daniel P. Berrangé
  0 siblings, 1 reply; 18+ messages in thread
From: Alex Bennée @ 2019-08-28 12:30 UTC (permalink / raw)
  To: Marc-André Lureau; +Cc: Daniel P. Berrangé, QEMU


Marc-André Lureau <marcandre.lureau@gmail.com> writes:

> Hi
>
> On Fri, Aug 23, 2019 at 8:41 PM Daniel P. Berrangé <berrange@redhat.com> wrote:
>>
>> This is ostensibly about adding docs for the g_autofree/g_autoptr
>> macros. As part of doing that, however, the existing HACKING doc
>> is merged into the CODING_STYLE doc and the text is converted to
>> markdown with a table of contents.
>
> Why not rst, so it can integrate with the rest of qemu sphinx doc?
>
> (markdown is quite poor when it comes to documenting code or
> cross-referencing etc)

I was going to say markdown does have the advantage of being renderable
on the fly on code hosting sites but it appears it works on
gitlab/github so that is an argument for being consistent with the rest
of the docs.

Does anyone know what is left to get the publishing pipeline up and
running and the results linked to from www.qemu.org?

>
>
>
>>
>> Daniel P. Berrangé (4):
>>   docs: convert CODING_STYLE and HACKING to markdown syntax
>>   docs: merge HACKING.md contents into CODING_STYLE.md
>>   docs: document use of automatic cleanup functions in glib
>>   docs: add table of contents to CODING_STYLE.md
>>
>>  CODING_STYLE    | 216 -----------------
>>  CODING_STYLE.md | 613 ++++++++++++++++++++++++++++++++++++++++++++++++
>>  HACKING         | 257 --------------------
>>  README          |   2 +-
>>  4 files changed, 614 insertions(+), 474 deletions(-)
>>  delete mode 100644 CODING_STYLE
>>  create mode 100644 CODING_STYLE.md
>>  delete mode 100644 HACKING
>>
>> --
>> 2.21.0
>>
>>


--
Alex Bennée


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [Qemu-devel] [PATCH 0/4] docs: add docs about use of automatic cleanup functions
  2019-08-28 12:30   ` Alex Bennée
@ 2019-08-28 13:07     ` Daniel P. Berrangé
  0 siblings, 0 replies; 18+ messages in thread
From: Daniel P. Berrangé @ 2019-08-28 13:07 UTC (permalink / raw)
  To: Alex Bennée; +Cc: Marc-André Lureau, QEMU

On Wed, Aug 28, 2019 at 01:30:15PM +0100, Alex Bennée wrote:
> 
> Marc-André Lureau <marcandre.lureau@gmail.com> writes:
> 
> > Hi
> >
> > On Fri, Aug 23, 2019 at 8:41 PM Daniel P. Berrangé <berrange@redhat.com> wrote:
> >>
> >> This is ostensibly about adding docs for the g_autofree/g_autoptr
> >> macros. As part of doing that, however, the existing HACKING doc
> >> is merged into the CODING_STYLE doc and the text is converted to
> >> markdown with a table of contents.
> >
> > Why not rst, so it can integrate with the rest of qemu sphinx doc?
> >
> > (markdown is quite poor when it comes to documenting code or
> > cross-referencing etc)
> 
> I was going to say markdown does have the advantage of being renderable
> on the fly on code hosting sites but it appears it works on
> gitlab/github so that is an argument for being consistent with the rest
> of the docs.

I picked markdown because most projects seem to use a README.md
and CONTRIBUTING.md file with github/gitlab, but if those sites
cope with .rst for those files too, there's no reason not to use
.rst.

> Does anyone know what is left to get the publishing pipeline up and
> running and the results linked to from www.qemu.org?


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [Qemu-devel] [PATCH 1/4] docs: convert CODING_STYLE and HACKING to markdown syntax
  2019-08-28 12:25   ` Alex Bennée
@ 2019-08-28 13:08     ` Daniel P. Berrangé
  0 siblings, 0 replies; 18+ messages in thread
From: Daniel P. Berrangé @ 2019-08-28 13:08 UTC (permalink / raw)
  To: Alex Bennée; +Cc: qemu-devel

On Wed, Aug 28, 2019 at 01:25:28PM +0100, Alex Bennée wrote:
> 
> Daniel P. Berrangé <berrange@redhat.com> writes:
> 
> > Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> 
> Reviewed-by: Alex Bennée <alex.bennee@linaro.org>
> 
> > diff --git a/README b/README
> > index 441c33eb2f..374b8f1486 100644
> > --- a/README
> > +++ b/README
> > @@ -60,7 +60,7 @@ When submitting patches, one common approach is to use 'git
> >  format-patch' and/or 'git send-email' to format & send the mail to the
> >  qemu-devel@nongnu.org mailing list. All patches submitted must contain
> >  a 'Signed-off-by' line from the author. Patches should follow the
> > -guidelines set out in the HACKING and CODING_STYLE files.
> > +guidelines set out in the HACKING.md and CODING_STYLE.md files.
> >
> >  Additional information on submitting patches can be found online via
> >  the QEMU website
> 
> It's tempting to suggest we go the whole hog and convert the README as
> well. We could then add CI buttons which would render nicely on the
> github/gitlab mirrors.

Yeah, I was actually thinking that too while I did this.


Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [Qemu-devel] [PATCH 2/4] docs: merge HACKING.md contents into CODING_STYLE.md
  2019-08-23 19:35   ` Eric Blake
@ 2019-08-28 15:06     ` Alex Bennée
  2019-08-28 15:10       ` Daniel P. Berrangé
  0 siblings, 1 reply; 18+ messages in thread
From: Alex Bennée @ 2019-08-28 15:06 UTC (permalink / raw)
  To: Eric Blake; +Cc: Daniel P. Berrangé, qemu-devel


Eric Blake <eblake@redhat.com> writes:

> On 8/23/19 11:39 AM, Daniel P. Berrangé wrote:
>> The split of information between the two docs is rather arbitary and
>> unclear. It is simpler for contributors if all the information is in
>> one file.
>>
>> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
>> ---
>>  CODING_STYLE.md | 262 +++++++++++++++++++++++++++++++++++++++++++++++
>>  HACKING.md      | 263 ------------------------------------------------
>>  README          |   2 +-
>>  3 files changed, 263 insertions(+), 264 deletions(-)
>>  delete mode 100644 HACKING.md
>
> Is it worth trying to group related sections as part of the combination?
>  (Your solution of just concatenating at the end is obviously the
> fastest, but may result in odd ordering where similar things are
> mentioned twice but in different parts of the file).

It is a bit all over the place, but just moving trace-events and
automatic memory de-allocation we could group it like this:

Formatting and style:

  * [Whitespace](#whitespace)
    + [Multiline Indent](#multiline-indent)
  * [Line width](#line-width)
  * [Naming](#naming)
  * [Block structure](#block-structure)
  * [Declarations](#declarations)
  * [Conditional statements](#conditional-statements)
  * [Comment style](#comment-style)

Language usage:

  * [Preprocessor](#preprocessor)
    + [Variadic macros](#variadic-macros)
    + [Include directives](#include-directives)
  * [C types](#c-types)
    + [Scalars](#scalars)
    + [Pointers](#pointers)
    + [Typedefs](#typedefs)
    + [Reserved namespaces in C and POSIX](#reserved-namespaces-in-c-and-posix)
  * [Low level memory management](#low-level-memory-management)
  * [String manipulation](#string-manipulation)
  * [Printf-style functions](#printf-style-functions)
  * [C standard, implementation defined and undefined
    behaviors](#c-standard--implementation-defined-and-undefined-behaviors)
  * [Automatic memory deallocation](#automatic-memory-deallocation)

QEMU Specific Idioms

  * [trace-events style](#trace-events-style)
    + [0x prefix](#0x-prefix)
    + ['#' printf flag](#----printf-flag)
  * [Error handling and reporting](#error-handling-and-reporting)
    + [Reporting errors to the human user](#reporting-errors-to-the-human-user)
    + [Propagating errors](#propagating-errors)
    + [Handling errors](#handling-errors)


--
Alex Bennée


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [Qemu-devel] [PATCH 2/4] docs: merge HACKING.md contents into CODING_STYLE.md
  2019-08-28 15:06     ` Alex Bennée
@ 2019-08-28 15:10       ` Daniel P. Berrangé
  0 siblings, 0 replies; 18+ messages in thread
From: Daniel P. Berrangé @ 2019-08-28 15:10 UTC (permalink / raw)
  To: Alex Bennée; +Cc: qemu-devel

On Wed, Aug 28, 2019 at 04:06:20PM +0100, Alex Bennée wrote:
> 
> Eric Blake <eblake@redhat.com> writes:
> 
> > On 8/23/19 11:39 AM, Daniel P. Berrangé wrote:
> >> The split of information between the two docs is rather arbitary and
> >> unclear. It is simpler for contributors if all the information is in
> >> one file.
> >>
> >> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> >> ---
> >>  CODING_STYLE.md | 262 +++++++++++++++++++++++++++++++++++++++++++++++
> >>  HACKING.md      | 263 ------------------------------------------------
> >>  README          |   2 +-
> >>  3 files changed, 263 insertions(+), 264 deletions(-)
> >>  delete mode 100644 HACKING.md
> >
> > Is it worth trying to group related sections as part of the combination?
> >  (Your solution of just concatenating at the end is obviously the
> > fastest, but may result in odd ordering where similar things are
> > mentioned twice but in different parts of the file).
> 
> It is a bit all over the place, but just moving trace-events and
> automatic memory de-allocation we could group it like this:
> 
> Formatting and style:
> 
>   * [Whitespace](#whitespace)
>     + [Multiline Indent](#multiline-indent)
>   * [Line width](#line-width)
>   * [Naming](#naming)
>   * [Block structure](#block-structure)
>   * [Declarations](#declarations)
>   * [Conditional statements](#conditional-statements)
>   * [Comment style](#comment-style)
> 
> Language usage:
> 
>   * [Preprocessor](#preprocessor)
>     + [Variadic macros](#variadic-macros)
>     + [Include directives](#include-directives)
>   * [C types](#c-types)
>     + [Scalars](#scalars)
>     + [Pointers](#pointers)
>     + [Typedefs](#typedefs)
>     + [Reserved namespaces in C and POSIX](#reserved-namespaces-in-c-and-posix)
>   * [Low level memory management](#low-level-memory-management)
>   * [String manipulation](#string-manipulation)
>   * [Printf-style functions](#printf-style-functions)
>   * [C standard, implementation defined and undefined
>     behaviors](#c-standard--implementation-defined-and-undefined-behaviors)
>   * [Automatic memory deallocation](#automatic-memory-deallocation)
> 
> QEMU Specific Idioms
> 
>   * [trace-events style](#trace-events-style)
>     + [0x prefix](#0x-prefix)
>     + ['#' printf flag](#----printf-flag)
>   * [Error handling and reporting](#error-handling-and-reporting)
>     + [Reporting errors to the human user](#reporting-errors-to-the-human-user)
>     + [Propagating errors](#propagating-errors)
>     + [Handling errors](#handling-errors)


Sure, I'm fine doing this.

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [Qemu-devel] [PATCH 3/4] docs: document use of automatic cleanup functions in glib
  2019-08-23 16:39 ` [Qemu-devel] [PATCH 3/4] docs: document use of automatic cleanup functions in glib Daniel P. Berrangé
  2019-08-23 19:53   ` Eric Blake
  2019-08-28  9:00   ` Stefan Hajnoczi
@ 2019-08-28 15:14   ` Alex Bennée
  2019-08-28 15:20     ` Daniel P. Berrangé
  2 siblings, 1 reply; 18+ messages in thread
From: Alex Bennée @ 2019-08-28 15:14 UTC (permalink / raw)
  To: Daniel P. Berrangé; +Cc: qemu-devel


Daniel P. Berrangé <berrange@redhat.com> writes:

> Document the use of g_autofree and g_autoptr in glib for automatic
> freeing of memory, or other resource cleanup (eg mutex unlocking).
>
> Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
> ---
>  CODING_STYLE.md | 101 ++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 101 insertions(+)
>
> diff --git a/CODING_STYLE.md b/CODING_STYLE.md
> index 9f4fc9dc77..f37b6c2d01 100644
> --- a/CODING_STYLE.md
> +++ b/CODING_STYLE.md
> @@ -479,3 +479,104 @@ terminate QEMU.
>
>  Note that &error_fatal is just another way to exit(1), and &error_abort
>  is just another way to abort().
> +
> +
> +## Automatic memory deallocation
> +
> +QEMU has a mandatory dependency either the GCC or CLang compiler. As
> +such it has the freedom to make use of a C language extension for
> +automatically running a cleanup function when a stack variable goes
> +out of scope. This can be used to simplify function cleanup paths,
> +often allowing many goto jumps to be eliminated, through automatic
> +free'ing of memory.
> +
> +The GLib2 library provides a number of functions/macros for enabling
> +automatic cleanup:
> +
> + https://developer.gnome.org/glib/stable/glib-Miscellaneous-Macros.html
> +
> +Most notably:
> +
> + - g_autofree - will invoke g_free() on the variable going out of scope
> +
> + - g_autoptr - for structs / objects, will invoke the cleanup func created
> +               by a previous use of G_DEFINE_AUTOPTR_CLEANUP_FUNC. This is
> +               supported for most GLib data types and GObjects
> +
> +For example, instead of
> +
> +    int somefunc(void) {
> +        int ret = -1;
> +        char *foo = g_strdup_printf("foo%", "wibble");
> +        GList *bar = .....
> +
> +        if (eek) {
> +           goto cleanup;
> +        }
> +
> +        ret = 0;
> +
> +      cleanup:
> +        g_free(foo);
> +        g_list_free(bar);
> +        return ret;
> +    }
> +
> +Using g_autofree/g_autoptr enables the code to be written as:
> +
> +    int somefunc(void) {
> +        g_autofree char *foo = g_strdup_printf("foo%", "wibble");
> +        g_autoptr (GList) bar = .....
> +
> +        if (eek) {
> +           return -1;
> +        }
> +
> +        return 0;
> +    }
> +
> +While this generally results in simpler, less leak-prone code, there
> +are still some caveats to beware of
> +
> + * Variables declared with g_auto* MUST always be initialized,
> +   otherwise the cleanup function will use uninitialized stack memory
> +
> + * If a variable declared with g_auto* holds a value which must
> +   live beyond the life of the function, that value must be saved
> +   and the original variable NULL'd out. This can be simpler using
> +   g_steal_pointer
> +
> +
> +    char *somefunc(void) {
> +        g_autofree char *foo = g_strdup_printf("foo%", "wibble");
> +        g_autoptr (GList) bar = .....
> +
> +        if (eek) {
> +           return NULL;
> +        }
> +
> +        return g_steal_pointer(&foo);
> +    }

All good so far.

> +
> +The cleanup functions are not restricted to simply free'ing memory. The
> +GMutexLocker class is a variant of GMutex that has automatic locking and
> +unlocking at start and end of the enclosing scope
> +
> +In the following example, the `lock` in `MyObj` will be held for the
> +precise duration of the `somefunc` function
> +
> +    typedef struct {
> +        GMutex lock;
> +    } MyObj;
> +
> +    char *somefunc(MyObj *obj) {
> +        g_autofree GMutexLocker *locker = g_mutex_locker_new(&obj->lock)
> +        g_autofree char *foo = g_strdup_printf("foo%", "wibble");
> +        g_autoptr (GList) bar = .....
> +
> +        if (eek) {
> +           return NULL;
> +        }
> +
> +        return g_steal_pointer(&foo);
> +    }

I would personally prefer we get some RFC patches for auto-unlocking under our
belt before we codify it's usage in our developer docs. Locking is a
fickle beast at the best of times and I'd like to see where it benefits
us before there is a rush to covert to the new style.

For one thing the only uses I see of g_mutex_lock is in our tests, the
main code base uses qemu_mutex_lock. How would we go about registering
the clean-up functions for those in the code base?

But apart from the lock stuff:

Reviewed-by: Alex Bennée <alex.bennee@linaro.org>

--
Alex Bennée


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [Qemu-devel] [PATCH 3/4] docs: document use of automatic cleanup functions in glib
  2019-08-28 15:14   ` Alex Bennée
@ 2019-08-28 15:20     ` Daniel P. Berrangé
  2019-08-28 16:04       ` Alex Bennée
  0 siblings, 1 reply; 18+ messages in thread
From: Daniel P. Berrangé @ 2019-08-28 15:20 UTC (permalink / raw)
  To: Alex Bennée; +Cc: qemu-devel

On Wed, Aug 28, 2019 at 04:14:00PM +0100, Alex Bennée wrote:
> > +The cleanup functions are not restricted to simply free'ing memory. The
> > +GMutexLocker class is a variant of GMutex that has automatic locking and
> > +unlocking at start and end of the enclosing scope
> > +
> > +In the following example, the `lock` in `MyObj` will be held for the
> > +precise duration of the `somefunc` function
> > +
> > +    typedef struct {
> > +        GMutex lock;
> > +    } MyObj;
> > +
> > +    char *somefunc(MyObj *obj) {
> > +        g_autofree GMutexLocker *locker = g_mutex_locker_new(&obj->lock)
> > +        g_autofree char *foo = g_strdup_printf("foo%", "wibble");
> > +        g_autoptr (GList) bar = .....
> > +
> > +        if (eek) {
> > +           return NULL;
> > +        }
> > +
> > +        return g_steal_pointer(&foo);
> > +    }
> 
> I would personally prefer we get some RFC patches for auto-unlocking under our
> belt before we codify it's usage in our developer docs. Locking is a
> fickle beast at the best of times and I'd like to see where it benefits
> us before there is a rush to covert to the new style.
> 
> For one thing the only uses I see of g_mutex_lock is in our tests, the
> main code base uses qemu_mutex_lock. How would we go about registering
> the clean-up functions for those in the code base?

Ideally we could just relpace qemu_mutex with g_mutex, but if that's
not possible we would have to create a clone of GMutexLocker as
QemuMutexLocker doing exactly the same thing. It is a shame to reinvent
the wheel with our threading code though.

/me tries to remember what it was that we can do with QEMU's threads
that we can't do with GLib's threads.

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|


^ permalink raw reply	[flat|nested] 18+ messages in thread

* Re: [Qemu-devel] [PATCH 3/4] docs: document use of automatic cleanup functions in glib
  2019-08-28 15:20     ` Daniel P. Berrangé
@ 2019-08-28 16:04       ` Alex Bennée
  0 siblings, 0 replies; 18+ messages in thread
From: Alex Bennée @ 2019-08-28 16:04 UTC (permalink / raw)
  To: Daniel P. Berrangé; +Cc: qemu-devel


Daniel P. Berrangé <berrange@redhat.com> writes:

> On Wed, Aug 28, 2019 at 04:14:00PM +0100, Alex Bennée wrote:
>> > +The cleanup functions are not restricted to simply free'ing memory. The
>> > +GMutexLocker class is a variant of GMutex that has automatic locking and
>> > +unlocking at start and end of the enclosing scope
>> > +
>> > +In the following example, the `lock` in `MyObj` will be held for the
>> > +precise duration of the `somefunc` function
>> > +
>> > +    typedef struct {
>> > +        GMutex lock;
>> > +    } MyObj;
>> > +
>> > +    char *somefunc(MyObj *obj) {
>> > +        g_autofree GMutexLocker *locker = g_mutex_locker_new(&obj->lock)
>> > +        g_autofree char *foo = g_strdup_printf("foo%", "wibble");
>> > +        g_autoptr (GList) bar = .....
>> > +
>> > +        if (eek) {
>> > +           return NULL;
>> > +        }
>> > +
>> > +        return g_steal_pointer(&foo);
>> > +    }
>>
>> I would personally prefer we get some RFC patches for auto-unlocking under our
>> belt before we codify it's usage in our developer docs. Locking is a
>> fickle beast at the best of times and I'd like to see where it benefits
>> us before there is a rush to covert to the new style.
>>
>> For one thing the only uses I see of g_mutex_lock is in our tests, the
>> main code base uses qemu_mutex_lock. How would we go about registering
>> the clean-up functions for those in the code base?
>
> Ideally we could just relpace qemu_mutex with g_mutex, but if that's
> not possible we would have to create a clone of GMutexLocker as
> QemuMutexLocker doing exactly the same thing. It is a shame to reinvent
> the wheel with our threading code though.
>
> /me tries to remember what it was that we can do with QEMU's threads
> that we can't do with GLib's threads.

Apart from having separate POSIX and Win32 implementations we have also
extended the mutex handling to add trace points and also support
profiling of lock latency.

>
> Regards,
> Daniel


--
Alex Bennée


^ permalink raw reply	[flat|nested] 18+ messages in thread

end of thread, other threads:[~2019-08-28 16:06 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-23 16:39 [Qemu-devel] [PATCH 0/4] docs: add docs about use of automatic cleanup functions Daniel P. Berrangé
2019-08-23 16:39 ` [Qemu-devel] [PATCH 1/4] docs: convert CODING_STYLE and HACKING to markdown syntax Daniel P. Berrangé
2019-08-28 12:25   ` Alex Bennée
2019-08-28 13:08     ` Daniel P. Berrangé
2019-08-23 16:39 ` [Qemu-devel] [PATCH 2/4] docs: merge HACKING.md contents into CODING_STYLE.md Daniel P. Berrangé
2019-08-23 19:35   ` Eric Blake
2019-08-28 15:06     ` Alex Bennée
2019-08-28 15:10       ` Daniel P. Berrangé
2019-08-23 16:39 ` [Qemu-devel] [PATCH 3/4] docs: document use of automatic cleanup functions in glib Daniel P. Berrangé
2019-08-23 19:53   ` Eric Blake
2019-08-28  9:00   ` Stefan Hajnoczi
2019-08-28 15:14   ` Alex Bennée
2019-08-28 15:20     ` Daniel P. Berrangé
2019-08-28 16:04       ` Alex Bennée
2019-08-23 16:39 ` [Qemu-devel] [PATCH 4/4] docs: add table of contents to CODING_STYLE.md Daniel P. Berrangé
2019-08-23 21:48 ` [Qemu-devel] [PATCH 0/4] docs: add docs about use of automatic cleanup functions Marc-André Lureau
2019-08-28 12:30   ` Alex Bennée
2019-08-28 13:07     ` Daniel P. Berrangé

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).