[Asterisk-code-review] astobj2: Reduce memory overhead. (asterisk[master])

Jenkins2 asteriskteam at digium.com
Mon Oct 1 09:02:38 CDT 2018


Jenkins2 has submitted this change and it was merged. ( https://gerrit.asterisk.org/10294 )

Change subject: astobj2: Reduce memory overhead.
......................................................................

astobj2: Reduce memory overhead.

Reduce options to 2-bit field, magic to 30 bit field.  Move ref_counter
next to options and explicitly use int32_t so the fields will pack.

This reduces memory overhead for every ao2 object by 8 bytes on x86_64.

Change-Id: Idc1baabb35ec3b3d8de463c4fa3011eaf7fcafb5
---
M main/astobj2.c
1 file changed, 13 insertions(+), 12 deletions(-)

Approvals:
  Richard Mudgett: Looks good to me, but someone else must approve
  Kevin Harwell: Looks good to me, but someone else must approve
  Joshua Colp: Looks good to me, approved
  Jenkins2: Approved for Submit



diff --git a/main/astobj2.c b/main/astobj2.c
index 63058e1..ea4ad02 100644
--- a/main/astobj2.c
+++ b/main/astobj2.c
@@ -48,7 +48,6 @@
  * The magic number is used for consistency check.
  */
 struct __priv_data {
-	int ref_counter;
 	ao2_destructor_fn destructor_fn;
 	/*! This field is used for astobj2 and ao2_weakproxy objects to reference each other */
 	void *weakptr;
@@ -56,15 +55,17 @@
 	/*! User data size for stats */
 	size_t data_size;
 #endif
+	/*! Number of references held for this object */
+	int32_t ref_counter;
 	/*! The ao2 object option flags */
-	uint32_t options;
+	uint32_t options:2;
 	/*! magic number.  This is used to verify that a pointer passed in is a
 	 *  valid astobj2 or ao2_weak reference */
-	uint32_t magic;
+	uint32_t magic:30;
 };
 
-#define	AO2_MAGIC	0xa570b123
-#define	AO2_WEAK	0xa570b122
+#define	AO2_MAGIC	0x3a70b123
+#define	AO2_WEAK	0x3a70b122
 #define IS_AO2_MAGIC_BAD(p) (AO2_MAGIC != (p->priv_data.magic | 1))
 
 /*!
@@ -465,8 +466,8 @@
 	struct astobj2_lock *obj_mutex;
 	struct astobj2_rwlock *obj_rwlock;
 	struct astobj2_lockobj *obj_lockobj;
-	int current_value;
-	int ret;
+	int32_t current_value;
+	int32_t ret;
 	struct ao2_weakproxy *weakproxy = NULL;
 
 	if (obj == NULL) {
@@ -488,7 +489,7 @@
 	}
 
 	/* we modify with an atomic operation the reference counter */
-	ret = ast_atomic_fetchadd_int(&obj->priv_data.ref_counter, delta);
+	ret = ast_atomic_fetch_add(&obj->priv_data.ref_counter, delta, __ATOMIC_RELAXED);
 	current_value = ret + delta;
 
 #ifdef AO2_DEBUG
@@ -541,7 +542,7 @@
 			/* We just reached or went over the excessive ref count trigger */
 			snprintf(excessive_ref_buf, sizeof(excessive_ref_buf),
 				"Excessive refcount %d reached on ao2 object %p",
-				current_value, user_data);
+				(int)current_value, user_data);
 			ast_log(__LOG_ERROR, file, line, func, "%s\n", excessive_ref_buf);
 
 			__ast_assert_failed(0, excessive_ref_buf, file, line, func);
@@ -550,7 +551,7 @@
 		if (ref_log && tag) {
 			fprintf(ref_log, "%p,%s%d,%d,%s,%d,%s,%d,%s\n", user_data,
 				(delta < 0 ? "" : "+"), delta, ast_get_tid(),
-				file, line, func, ret, tag);
+				file, line, func, (int)ret, tag);
 			fflush(ref_log);
 		}
 		return ret;
@@ -559,7 +560,7 @@
 	/* this case must never happen */
 	if (current_value < 0) {
 		ast_log(__LOG_ERROR, file, line, func,
-			"Invalid refcount %d on ao2 object %p\n", current_value, user_data);
+			"Invalid refcount %d on ao2 object %p\n", (int)current_value, user_data);
 		if (ref_log) {
 			/* Log to ref_log invalid even if (tag == NULL) */
 			fprintf(ref_log, "%p,%d,%d,%s,%d,%s,**invalid**,%s\n",
@@ -692,8 +693,8 @@
 	}
 
 	/* Initialize common ao2 values. */
-	obj->priv_data.ref_counter = 1;
 	obj->priv_data.destructor_fn = destructor_fn;	/* can be NULL */
+	obj->priv_data.ref_counter = 1;
 	obj->priv_data.options = options;
 	obj->priv_data.magic = AO2_MAGIC;
 

-- 
To view, visit https://gerrit.asterisk.org/10294
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-MessageType: merged
Gerrit-Change-Id: Idc1baabb35ec3b3d8de463c4fa3011eaf7fcafb5
Gerrit-Change-Number: 10294
Gerrit-PatchSet: 2
Gerrit-Owner: Corey Farrell <git at cfware.com>
Gerrit-Reviewer: Corey Farrell <git at cfware.com>
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Jenkins2 (1000185)
Gerrit-Reviewer: Joshua Colp <jcolp at digium.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-Reviewer: Richard Mudgett <rmudgett at digium.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20181001/7f61c92e/attachment.html>


More information about the asterisk-code-review mailing list