Skip to contentSkip to conversation

util: Add L_AUTO_FREE_OBJ and l_steal_ptr macros

PATCH In review Andrew Zaborowski 3 participants 7 messages Mar 4, 2021 – Mar 8, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 util: Add L_AUTO_FREE_OBJ and l_steal_ptr macros

L_AUTO_FREE_OBJ can be used for ell object types that define a destructor named l_<type>_free such as l_settings_free(). The "FREE" in the name hopefully is enough to communicate this.

Some of our classes use l_<type>_destroy and similar names and some of those functions, like l_queue_destroy, take additional parameters so for those classes if a similar "AUTO" macros is wanted, it may be better defined directly in that class's header.

l_steal_ptr is similar to g_steal_pointer except for being a macro. Both macros can be used like in this example:

struct l_settings *load_settings(const char *path)

{
	L_AUTO_FREE_OBJ(l_settings, s) = l_settings_new();

	if (!l_settings_load_from_file(s, path))
		return NULL;

	return l_steal_ptr(s);
}

---
This is an RFC. L_AUTO_FREE_OBJ is based on James's idea.

 ell/util.h | 8 ++++++++
 1 file changed, 8 insertions(+)
ell/util.h +8 -0
index b112878..94a4178 100644
@@ -235,6 +235,11 @@ static inline void l_put_be64(uint64_t val, void *ptr)
 #define L_AUTO_FREE_VAR(vartype,varname) \
 	vartype varname __attribute__((cleanup(auto_free)))
 
+#define L_AUTO_FREE_OBJ(l_type,varname) \
+	void varname ## _auto_free(void *_ptr)		\
+		{ l_type ## _free(*(void **) _ptr); }	\
+	L_AUTO_CLEANUP_VAR(struct l_type *, varname, varname ## _auto_free)
+
 #define L_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 
 void *l_malloc(size_t size) __attribute__ ((warn_unused_result, malloc));
@@ -276,6 +281,9 @@ static inline size_t minsize(size_t a, size_t b)
 		__p;				\
 	}))
 
+#define l_steal_ptr(ptr)	\
+	(__extension__ ({ typeof(ptr) *_tmp = (ptr); (ptr) = NULL; _tmp; }))
+
 char *l_strdup(const char *str);
 char *l_strndup(const char *str, size_t max);
 char *l_strdup_printf(const char *format, ...)
Signature · 1 line
2.27.0
#1 lore raw

Hi Andrew,

Quoted 9 lines · Andrew Zaborowski

L_AUTO_FREE_OBJ can be used for ell object types that define a destructor named l_<type>_free such as l_settings_free(). The "FREE" in the name hopefully is enough to communicate this.

Some of our classes use l_<type>_destroy and similar names and some of those functions, like l_queue_destroy, take additional parameters so for those classes if a similar "AUTO" macros is wanted, it may be better defined directly in that class's header.

I'd rather have the caller provide the destructor method itself rather than the class. That becomes a bit more generic as well.

Quoted 3 lines

l_steal_ptr is similar to g_steal_pointer except for being a macro. Both macros can be used like in this example:

No objections from me

Quoted 20 lines

struct l_settings *load_settings(const char *path)

{
	L_AUTO_FREE_OBJ(l_settings, s) = l_settings_new();

	if (!l_settings_load_from_file(s, path))
		return NULL;

	return l_steal_ptr(s);
}

---
This is an RFC. L_AUTO_FREE_OBJ is based on James's idea.

  ell/util.h | 8 ++++++++
  1 file changed, 8 insertions(+)
ell/util.h +0 -0
index b112878..94a4178 100644

ell/util.h static inline void l_put_be64(uint64_t val, void *ptr) · line 242

Quoted 8 lines
ell/util.h +5 -0
@@ -235,6 +235,11 @@ static inline void l_put_be64(uint64_t val, void *ptr)
  #define L_AUTO_FREE_VAR(vartype,varname) \
  	vartype varname __attribute__((cleanup(auto_free)))
 
+#define L_AUTO_FREE_OBJ(l_type,varname) \
+	void varname ## _auto_free(void *_ptr)		\
+		{ l_type ## _free(*(void **) _ptr); }	\
+	L_AUTO_CLEANUP_VAR(struct l_type *, varname, varname ## _auto_free)
+

Nested functions... Very clever!

I ran a few experiments by changing out the L_AUTO_FREE_VAR macro to use the above pattern. Something like:

#define L_AUTO_FREE_VAR(vartype,varname) \
void varname ## _auto_free(void *_ptr) \
{ l_free(*(void **) _ptr); } \
vartype varname __attribute__((cleanup(varname ## _auto_free)))

The resulting iwd code was the same size as the original, which seems to indicate that the nested function is always inlined.

I also made some (somewhat primitive) experiments with writing several versions of a simple function, both using this cleanup pattern and code without any GCC extensions. Both seemed to produce the same code after disassembly.

Curious to see whether you or anyone else has different findings?

Given the above and the fact that L_AUTO_CLEANUP_VAR was never really used (it is used in ell once), my vote would be to simply hijack that macro. Then we can write things like:

L_AUTO_CLEANUP_VAR(struct l_settings *, settings, l_settings_free);

Or perhaps something like:

_cleanup(l_settings_free) struct l_settings *settings;

if we can use a UNIQUE_NAME type macro magic inside.

ell/util.h static inline void l_put_be64(uint64_t val, void *ptr) · line 245

Quoted 3 lines
ell/util.h +0 -0
  #define L_ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 
  void *l_malloc(size_t size) __attribute__ ((warn_unused_result, malloc));

Regards,
-Denis

#2 lore raw

Hi Denis,

Quoted 59 lines · Denis Kenzior
Quoted 9 lines · Andrew Zaborowski

L_AUTO_FREE_OBJ can be used for ell object types that define a destructor named l_<type>_free such as l_settings_free(). The "FREE" in the name hopefully is enough to communicate this.

Some of our classes use l_<type>_destroy and similar names and some of those functions, like l_queue_destroy, take additional parameters so for those classes if a similar "AUTO" macros is wanted, it may be better defined directly in that class's header.

I'd rather have the caller provide the destructor method itself rather than the class. That becomes a bit more generic as well.

Quoted 3 lines

l_steal_ptr is similar to g_steal_pointer except for being a macro. Both macros can be used like in this example:

No objections from me

Quoted 20 lines

struct l_settings *load_settings(const char *path)

{
      L_AUTO_FREE_OBJ(l_settings, s) = l_settings_new();

      if (!l_settings_load_from_file(s, path))
              return NULL;

      return l_steal_ptr(s);
}

---
This is an RFC. L_AUTO_FREE_OBJ is based on James's idea.

  ell/util.h | 8 ++++++++
  1 file changed, 8 insertions(+)
ell/util.h +0 -0
index b112878..94a4178 100644

ell/util.h static inline void l_put_be64(uint64_t val, void *ptr) · line 242

Quoted 8 lines
ell/util.h +5 -0
@@ -235,6 +235,11 @@ static inline void l_put_be64(uint64_t val, void *ptr)
  #define L_AUTO_FREE_VAR(vartype,varname) \
      vartype varname __attribute__((cleanup(auto_free)))
 
+#define L_AUTO_FREE_OBJ(l_type,varname) \
+     void varname ## _auto_free(void *_ptr)          \
+             { l_type ## _free(*(void **) _ptr); }   \
+     L_AUTO_CLEANUP_VAR(struct l_type *, varname, varname ## _auto_free)
+

Nested functions... Very clever!

I ran a few experiments by changing out the L_AUTO_FREE_VAR macro to use the above pattern. Something like:

#define L_AUTO_FREE_VAR(vartype,varname) \
void varname ## _auto_free(void *_ptr) \
{ l_free(*(void **) _ptr); } \
vartype varname __attribute__((cleanup(varname ## _auto_free)))

Quoted 8 lines

The resulting iwd code was the same size as the original, which seems to indicate that the nested function is always inlined.

I also made some (somewhat primitive) experiments with writing several versions of a simple function, both using this cleanup pattern and code without any GCC extensions. Both seemed to produce the same code after disassembly.

Curious to see whether you or anyone else has different findings?

I haven't looked at the code size honestly.

Quoted 6 lines

Given the above and the fact that L_AUTO_CLEANUP_VAR was never really used (it is used in ell once), my vote would be to simply hijack that macro. Then we can write things like:

L_AUTO_CLEANUP_VAR(struct l_settings *, settings, l_settings_free);

Re-using L_AUTO_CLEANUP_VAR makes sense but I think the name and parameters are too long for it to be attractive to use. It would mainly make sense if the individual classes' headers use it to define their own macros. On the other hand in that scenario the headers can define global cleanup functions without using the nested function magic.

I see one of the following forms as most usable:

l_settings_auto_ptr settings;
l_settings_auto_ptr *settings;
l_auto_ptr(l_settings) settings;
l_auto_ptr(l_settings) *settings;

The l_auto_ptr() macro would rely on settings.h defining a cleanup function whose name can be derived by preppending and/or appending something to "l_settings".

Best regards

#3 lore raw

It might be good to avoid any possible confusion with the C++03 auto_ptr, which is deprecated in C++11 favour of unique_ptr.

Quoted 10 lines

I see one of the following forms as most usable:

l_settings_auto_ptr settings;
l_settings_auto_ptr *settings;
l_auto_ptr(l_settings) settings;
l_auto_ptr(l_settings) *settings;

The l_auto_ptr() macro would rely on settings.h defining a cleanup function whose name can be derived by preppending and/or appending something to "l_settings".

#4 lore raw
In reply to Andrew Zaborowski

Hi Andrew,

Quoted 8 lines
Quoted 5 lines

Given the above and the fact that L_AUTO_CLEANUP_VAR was never really used (it is used in ell once), my vote would be to simply hijack that macro. Then we can write things like:

L_AUTO_CLEANUP_VAR(struct l_settings *, settings, l_settings_free);

Re-using L_AUTO_CLEANUP_VAR makes sense but I think the name and parameters are too long for it to be attractive to use. It would

Yes. Ideally we could do _cleanup() instead.

Quoted 4 lines

mainly make sense if the individual classes' headers use it to define their own macros. On the other hand in that scenario the headers can define global cleanup functions without using the nested function magic.

I find this less desirable actually. The nice thing about the nested function approach is that you can easily use it for custom classes without needing to use any extra macros.

Quoted 7 lines

I see one of the following forms as most usable:

l_settings_auto_ptr settings;
l_settings_auto_ptr *settings;
l_auto_ptr(l_settings) settings;
l_auto_ptr(l_settings) *settings;

auto_ptr is some C++-ism though, so that might not be the best candidate.

Quoted 4 lines

The l_auto_ptr() macro would rely on settings.h defining a cleanup function whose name can be derived by preppending and/or appending something to "l_settings".

And this is another down side.

How about something like:

#define __CLEANUP(var, func)                           \
         void cleanup_ ## var(void *ptr)                \
         { func(*(void **) ptr); }                      \
         __attribute((cleanup(cleanup_ ## var)))

#define _CLEANUP(var, func) \
__CLEANUP(var, func)

#define _cleanup(func)                                 \
         _CLEANUP(__COUNTER__, func)

void foo()

{
	_cleanup(destructor) struct something *s =
					constructor();
}

Regards,
-Denis

#5 lore raw
Quoted 9 lines · Denis Kenzior
Quoted 5 lines

mainly make sense if the individual classes' headers use it to define their own macros. On the other hand in that scenario the headers can define global cleanup functions without using the nested function magic.

I find this less desirable actually. The nice thing about the nested function approach is that you can easily use it for custom classes without needing to use any extra macros.

Right, but custom classes are one use case and ell classes are another. We can add both a generic macro and an ell-specific or class-specific (e.g. l_settings-only) extra compact syntax.

Quoted 10 lines
Quoted 8 lines

I see one of the following forms as most usable:

l_settings_auto_ptr settings;
l_settings_auto_ptr *settings;
l_auto_ptr(l_settings) settings;
l_auto_ptr(l_settings) *settings;

auto_ptr is some C++-ism though, so that might not be the best candidate.

Right, I didn't remember that until Phil mentioned it, I take back these name ideas.

Quoted 17 lines
Quoted 5 lines

The l_auto_ptr() macro would rely on settings.h defining a cleanup function whose name can be derived by preppending and/or appending something to "l_settings".

And this is another down side.

How about something like:

#define __CLEANUP(var, func)                           \
         void cleanup_ ## var(void *ptr)                \
         { func(*(void **) ptr); }                      \
         __attribute((cleanup(cleanup_ ## var)))

#define _CLEANUP(var, func) \
__CLEANUP(var, func)

Quoted 8 lines
#define _cleanup(func)                                 \
         _CLEANUP(__COUNTER__, func)

void foo()
{
_cleanup(destructor) struct something *s =
constructor();
}

Make sense, one comment though:

I guess with _cleanup you want to make it shorter by skipping the "l", but for the internal macros there's no reason not to make them more descriptive (e.g. something that hints on how _CLEANUP and __CLEANUP differ from _cleanup and from each other) and perhaps also use the L_ prefix to tidiness. All three are pretty short names and not unlikely for someone to be using already.

Best regards

#6 lore raw

Hi Andrew,

Quoted 8 lines
Quoted 3 lines

I find this less desirable actually. The nice thing about the nested function approach is that you can easily use it for custom classes without needing to use any extra macros.

Right, but custom classes are one use case and ell classes are another. We can add both a generic macro and an ell-specific or class-specific (e.g. l_settings-only) extra compact syntax.

We can. But if we can have a single macro that works for everything, I'd consider that to be a better approach.

Quoted 18 lines
Quoted 9 lines

How about something like:

#define __CLEANUP(var, func)                           \
          void cleanup_ ## var(void *ptr)                \
          { func(*(void **) ptr); }                      \
          __attribute((cleanup(cleanup_ ## var)))

#define _CLEANUP(var, func) \
__CLEANUP(var, func)

Quoted 8 lines
#define _cleanup(func)                                 \
          _CLEANUP(__COUNTER__, func)

void foo()
{
_cleanup(destructor) struct something *s =
constructor();
}

Quoted 3 lines

Make sense, one comment though:

I guess with _cleanup you want to make it shorter by skipping the "l",

Not really. We can make it l_cleanup or l_auto or whichever... I'm not particular to anything. This was mostly a proof of concept

Quoted 5 lines

but for the internal macros there's no reason not to make them more descriptive (e.g. something that hints on how _CLEANUP and __CLEANUP differ from _cleanup and from each other) and perhaps also use the L_ prefix to tidiness. All three are pretty short names and not unlikely for someone to be using already.

Sure.

Regards,
-Denis