Skip to contentSkip to conversation

xen: Swap order of actions in the FREE*() macros

PATCH In review Andrew Cooper 3 participants 3 messages Feb 2, 2024 – Feb 8, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen: Swap order of actions in the FREE*() macros

Wherever possible, it is a good idea to NULL out the visible reference to an object prior to freeing it. The FREE*() macros already collect together both parts, making it easy to adjust.

This has a marginal code generation improvement, as some of the calls to the free() function can be tailcall optimised.

No functional change.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>

---
 xen/include/xen/mm.h      | 3 ++-
 xen/include/xen/xmalloc.h | 7 ++++---
 2 files changed, 6 insertions(+), 4 deletions(-)
xen/include/xen/mm.h +2 -1
index 3d9b2d05a5c8..044f3f3b19c8 100644
@@ -92,8 +92,9 @@ bool scrub_free_pages(void);
 
 /* Free an allocation, and zero the pointer to it. */
 #define FREE_XENHEAP_PAGES(p, o) do { \
-    free_xenheap_pages(p, o);         \
+    void *_ptr_ = (p);                \
     (p) = NULL;                       \
+    free_xenheap_pages(_ptr_, o);     \
 } while ( false )
 #define FREE_XENHEAP_PAGE(p) FREE_XENHEAP_PAGES(p, 0)
 
xen/include/xen/xmalloc.h +4 -3
index 9ecddbff5e00..1b88a83be879 100644
@@ -66,9 +66,10 @@
 extern void xfree(void *p);
 
 /* Free an allocation, and zero the pointer to it. */
-#define XFREE(p) do { \
-    xfree(p);         \
-    (p) = NULL;       \
+#define XFREE(p) do {                       \
+    void *_ptr_ = (p);                      \
+    (p) = NULL;                             \
+    xfree(_ptr_);                           \
 } while ( false )
 
 /* Underlying functions */

base-commit: 3f819af8a796c0e2f798dd301ec8c3f8cccbc9fc

Signature · 1 line
2.30.2
Acked-by Reviewed-by on v1 1/1
#1 lore raw
Quoted 10 lines · Andrew Cooper

Wherever possible, it is a good idea to NULL out the visible reference to an object prior to freeing it. The FREE*() macros already collect together both parts, making it easy to adjust.

This has a marginal code generation improvement, as some of the calls to the free() function can be tailcall optimised.

No functional change.

However, ...

xen/include/xen/mm.h bool scrub_free_pages(void); · line 95

Quoted 8 lines
xen/include/xen/mm.h +1 -1
@@ -92,8 +92,9 @@ bool scrub_free_pages(void);
 
 /* Free an allocation, and zero the pointer to it. */
 #define FREE_XENHEAP_PAGES(p, o) do { \
-    free_xenheap_pages(p, o);         \
+    void *_ptr_ = (p);                \

... why a trailing _and_ a leading underscore? Sooner or later we'll need to get rid of the leading ones anyway aiui (for Misra), here and elsewhere. With it omitted right away (also below):

Jan

#2 lore raw
In reply to Andrew Cooper

Hi Andrew,

Quoted 9 lines · Andrew Cooper

Wherever possible, it is a good idea to NULL out the visible reference to an object prior to freeing it. The FREE*() macros already collect together both parts, making it easy to adjust.

This has a marginal code generation improvement, as some of the calls to the free() function can be tailcall optimised.

Could you improve a bit the commit message and explain a bit why it is a good idea and also how the code might be improved because i do not get it ?

Cheers
Bertrand

Quoted 53 lines

No functional change.

---
CC: George Dunlap <[email protected]>
CC: Jan Beulich <[email protected]>
CC: Stefano Stabellini <[email protected]>
CC: Wei Liu <[email protected]>
CC: Julien Grall <[email protected]>
---
xen/include/xen/mm.h | 3 ++-
xen/include/xen/xmalloc.h | 7 ++++---

2 files changed, 6 insertions(+), 4 deletions(-)
xen/include/xen/mm.h +2 -1
index 3d9b2d05a5c8..044f3f3b19c8 100644
@@ -92,8 +92,9 @@ bool scrub_free_pages(void);
 
 /* Free an allocation, and zero the pointer to it. */
 #define FREE_XENHEAP_PAGES(p, o) do { \
-    free_xenheap_pages(p, o);         \
+    void *_ptr_ = (p);                \
    (p) = NULL;                       \
+    free_xenheap_pages(_ptr_, o);     \
 } while ( false )
 #define FREE_XENHEAP_PAGE(p) FREE_XENHEAP_PAGES(p, 0)
 
xen/include/xen/xmalloc.h +4 -3
index 9ecddbff5e00..1b88a83be879 100644
@@ -66,9 +66,10 @@
 extern void xfree(void *p);
 
 /* Free an allocation, and zero the pointer to it. */
-#define XFREE(p) do { \
-    xfree(p);         \
-    (p) = NULL;       \
+#define XFREE(p) do {                       \
+    void *_ptr_ = (p);                      \
+    (p) = NULL;                             \
+    xfree(_ptr_);                           \
 } while ( false )
 
 /* Underlying functions */

base-commit: 3f819af8a796c0e2f798dd301ec8c3f8cccbc9fc

Signature · 1 line
2.30.2