File 1384-erts-Make-DbTerm-tpl-into-flexible-array.patch of Package erlang

From 780808c9ee9bb47309489c5d466b2e1ecf97ecd3 Mon Sep 17 00:00:00 2001
From: Sverker Eriksson <sverker@erlang.org>
Date: Tue, 27 Jan 2026 18:32:44 +0100
Subject: [PATCH 4/6] erts: Make DbTerm::tpl[] into flexible array

Purpose: Avoid warnings when compiler can figure out index > 0

Effect: sizeof(DbTerm) decreases with sizeof(Eterm)
---
 erts/emulator/beam/erl_db.c      |  4 ++--
 erts/emulator/beam/erl_db_util.c | 16 +++++++---------
 erts/emulator/beam/erl_db_util.h |  4 +++-
 3 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/erts/emulator/beam/erl_db.c b/erts/emulator/beam/erl_db.c
index d0434f5063..3c1bb3a100 100644
--- a/erts/emulator/beam/erl_db.c
+++ b/erts/emulator/beam/erl_db.c
@@ -5337,7 +5337,7 @@ static void set_heir(Process* me, DbTable* tb, Eterm heir, Eterm heir_data)
 	wrap_tpl = TUPLE1(tmp,heir_data);
 	size = size_object(wrap_tpl);
 	dbterm = erts_db_alloc(ERTS_ALC_T_DB_HEIR_DATA, (DbTable *)tb,
-			       (sizeof(DbTerm) + sizeof(Eterm)*(size-1)));
+                               ERTS_SIZEOF_DBTERM(size));
 	dbterm->size = size;
 	top = dbterm->tpl;
 	tmp_offheap.first  = NULL;
@@ -5357,7 +5357,7 @@ static void free_heir_data(DbTable* tb)
 	DbTerm* p = (DbTerm*) tb->common.heir_data;
 	db_cleanup_offheap_comp(p);
 	erts_db_free(ERTS_ALC_T_DB_HEIR_DATA, tb, (void *)p,
-		     sizeof(DbTerm) + (p->size-1)*sizeof(Eterm));
+                     ERTS_SIZEOF_DBTERM(p->size));
     }
     #ifdef DEBUG
     tb->common.heir_data = am_undefined;
diff --git a/erts/emulator/beam/erl_db_util.c b/erts/emulator/beam/erl_db_util.c
index 1ffe59aa5e..e9dfea2c3e 100644
--- a/erts/emulator/beam/erl_db_util.c
+++ b/erts/emulator/beam/erl_db_util.c
@@ -3401,9 +3401,8 @@ static Uint db_size_dbterm_comp(int keypos, Eterm obj)
 {
     Eterm* tpl = tuple_val(obj);
     int i;
-    Uint size = sizeof(DbTerm)
-	+ arityval(*tpl) * sizeof(Eterm)
-        + sizeof(Uint); /* "alloc_size" */
+    Uint size = (ERTS_SIZEOF_DBTERM(1 + arityval(*tpl))
+                 + sizeof(Uint)); /* "alloc_size" */
 
     for (i = arityval(*tpl); i>0; i--) {
 	if (i != keypos && is_not_immed(tpl[i])) {
@@ -3549,8 +3548,8 @@ void* db_store_term(DbTableCommon *tb, DbTerm* old, Uint offset, Eterm obj)
 	    newp = old;
 	}
 	else {
-	    Uint new_sz = offset + sizeof(DbTerm) + sizeof(Eterm)*(size-1);
-	    Uint old_sz = offset + sizeof(DbTerm) + sizeof(Eterm)*(old->size-1);
+	    Uint new_sz = offset + ERTS_SIZEOF_DBTERM(size);
+	    Uint old_sz = offset + ERTS_SIZEOF_DBTERM(old->size);
 
 	    basep = db_realloc_term(tb, basep, old_sz, new_sz, offset);
 	    newp = (DbTerm*) (basep + offset);
@@ -3558,7 +3557,7 @@ void* db_store_term(DbTableCommon *tb, DbTerm* old, Uint offset, Eterm obj)
     }
     else {
 	basep = erts_db_alloc(ERTS_ALC_T_DB_TERM, (DbTable *)tb,
-			      (offset + sizeof(DbTerm) + sizeof(Eterm)*(size-1)));
+			      (offset + ERTS_SIZEOF_DBTERM(size)));
 	newp = (DbTerm*) (basep + offset);
     }
     newp->size = size;
@@ -3632,7 +3631,7 @@ void db_finalize_resize(DbUpdateHandle* handle, Uint offset)
     Uint alloc_sz = offset +
 	(tbl->common.compress ?
 	 db_size_dbterm_comp(tbl->common.keypos, make_tuple(handle->dbterm->tpl)) :
-	 sizeof(DbTerm)+sizeof(Eterm)*(handle->new_size-1));
+	 ERTS_SIZEOF_DBTERM(handle->new_size));
     byte* newp = erts_db_alloc(ERTS_ALC_T_DB_TERM, tbl, alloc_sz);
     byte* oldp = *(handle->bp);
 
@@ -6134,8 +6133,7 @@ static Eterm seq_trace_fake(Process *p, Eterm arg1)
 DbTerm* db_alloc_tmp_uncompressed(DbTableCommon* tb, DbTerm* org)
 {
     ErlOffHeap tmp_offheap;
-    DbTerm* res = erts_alloc(ERTS_ALC_T_TMP,
-			     sizeof(DbTerm) + org->size*sizeof(Eterm));
+    DbTerm* res = erts_alloc(ERTS_ALC_T_TMP, ERTS_SIZEOF_DBTERM(org->size));
     Eterm* hp = res->tpl;
     tmp_offheap.first = NULL;
     db_copy_from_comp(tb, org, &hp, &tmp_offheap);
diff --git a/erts/emulator/beam/erl_db_util.h b/erts/emulator/beam/erl_db_util.h
index 9606e31c23..67839f48cf 100644
--- a/erts/emulator/beam/erl_db_util.h
+++ b/erts/emulator/beam/erl_db_util.h
@@ -71,7 +71,7 @@ typedef struct db_term {
 #ifdef DEBUG_CLONE
     Eterm* debug_clone;    /* An uncompressed copy */
 #endif
-    Eterm tpl[1];          /* Term data. Top tuple always first */
+    Eterm tpl[];           /* Term data. Top tuple always first */
 
     /* Compression: is_immed and key element are uncompressed.
        Compressed elements are stored in external format after each other
@@ -81,6 +81,8 @@ typedef struct db_term {
      */
 } DbTerm;
 
+#define ERTS_SIZEOF_DBTERM(WORDS) (offsetof(DbTerm,tpl) + sizeof(Eterm)*(WORDS))
+
 #define DB_MUST_RESIZE 1
 #define DB_NEW_OBJECT 2
 #define DB_INC_TRY_GROW 4
-- 
2.51.0

openSUSE Build Service is sponsored by