Skip to contentSkip to conversation

doc: Prefer l_new instead of g_try*

PATCH Applied Denis Kenzior 2 participants 3 messages Feb 1, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 doc: Prefer l_new instead of g_try*

Using l_new and g_new* variants for small allocations has been the preferred approach for quite some time. Update the coding style document to reflect that.

---
 doc/coding-style.txt | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)
doc/coding-style.txt +6 -9
index c27a2f275565..0ec1cd97e8ca 100644
@@ -155,15 +155,12 @@ for (i = 0; i < 3; i++) {
 }
 
 
-M8: Use g_try_malloc instead of g_malloc
-========================================
-When g_malloc fails, the whole program would exit. Most of time, this is not
-the expected behavior, and you may want to use g_try_malloc instead.
-
-Example:
-additional = g_try_malloc(len - 1);  // correct
-if (additional == NULL)
-	return FALSE;
+M8: Prefer l_new when allocating small structures
+=================================================
+Small allocations (less than a page) in userspace Linux applications typically
+do not fail, and often there's nothing meaningful a program can do to recover.
+For small allocations, prefer using l_new (or the GLib equivalent g_new0)
+instead of g_try* functions.
 
 
 M9: Follow the order of include header elements
Signature · 1 line
2.43.0
#1 lore raw

PATCH 2/2 doc: Remove rule 13 in favor of O3

Since oFono is slowly migrating to ell, remove rule M13 and indicate that rule O3 style is preferred to match the prevalent coding style in ell. Many of the newer code additions have already been silently following these recommendations. Make them explicit.

---
 doc/coding-style.txt | 38 ++++++++++++++++----------------------
 1 file changed, 16 insertions(+), 22 deletions(-)
doc/coding-style.txt +16 -22
index 0ec1cd97e8ca..96837f7687eb 100644
@@ -241,28 +241,6 @@ However if the enum comes from an external header file outside ofono
 we cannot make any assumption of how the enum is defined and this
 rule might not apply.
 
-M13: Check for pointer being NULL
-=================================
-
-When checking if a pointer or a return value is NULL, explicitly compare to
-NULL rather than use the shorter check with "!" operator.
-
-Example:
-1)
-array = g_try_new0(int, 20);
-if (!array)	// Wrong
-	return;
-
-2)
-if (!g_at_chat_get_slave(chat))	// Wrong
-	return -EINVAL;
-
-3)
-array = g_try_new0(int, 20);
-if (array == NULL)	// Correct
-	return;
-
-
 M14: Always use parenthesis with sizeof
 =======================================
 The expression argument to the sizeof operator should always be in
@@ -347,3 +325,19 @@ v = voicecall_create(vc, call);
 v->detect_time = time(NULL);
 DBG("Registering new call: %d", call->id);
 voicecall_dbus_register(v);
+
+O3: Prefer !foo when checking pointers for NULL or boolean values
+=================================================================
+
+When checking if a pointer or a return value is NULL, prefer the negation
+operator form.
+
+Example:
+1)
+array = g_try_new0(int, 2000000);
+if (!array)
+	return -ENOMEM;
+
+2)
+if (!g_at_chat_get_slave(chat))
+	return -EINVAL;
Signature · 1 line
2.43.0
#2 lore raw
In reply to Denis Kenzior
This series was applied to ofono.git (master) by Denis Kenzior <[email protected]>:

Hello:

This series was applied to ofono.git (master)
by Denis Kenzior <[email protected]>:

Quoted 6 lines · On Thu, 1 Feb 2024 14:43:59 -0600 you wrote:

Using l_new and g_new* variants for small allocations has been the preferred approach for quite some time. Update the coding style document to reflect that.

---
 doc/coding-style.txt | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

Here is the summary with links:
- [1/2] doc: Prefer l_new instead of g_try*

    https://git.kernel.org/pub/scm/network/ofono/ofono.git/?id=b461139f484e
  - [2/2] doc: Remove rule 13 in favor of O3
    https://git.kernel.org/pub/scm/network/ofono/ofono.git/?id=feb3e44bb6b4

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html