This is an automated email from the ASF dual-hosted git repository.

jdanek pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/qpid-dispatch.git


The following commit(s) were added to refs/heads/main by this push:
     new 3d6d3b9  DISPATCH-2277 Aligned memory allocation and freeing (#1429)
3d6d3b9 is described below

commit 3d6d3b9140f669644f810a6d13e5779a87e25d85
Author: Jiri DanÄ›k <[email protected]>
AuthorDate: Sat Nov 13 21:53:35 2021 +0100

    DISPATCH-2277 Aligned memory allocation and freeing (#1429)
---
 include/qpid/dispatch/ctools.h |  7 ++++++-
 src/alloc_pool.c               | 16 ++++++++--------
 src/posix/threading.c          |  2 +-
 3 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/include/qpid/dispatch/ctools.h b/include/qpid/dispatch/ctools.h
index d3569c9..9d38dbc 100644
--- a/include/qpid/dispatch/ctools.h
+++ b/include/qpid/dispatch/ctools.h
@@ -41,7 +41,7 @@
 //
 
 #ifdef __sun
-#define NEW_CACHE_ALIGNED(t,p)                  \
+#define NEW_CACHE_ALIGNED(t,p) \
 do { \
     p = memalign(64, sizeof(t) + (sizeof(t) % 64 ? 64 - (sizeof(t) % 64) : 
0)); \
 } while (0)
@@ -64,6 +64,11 @@ do { \
 } while (0)
 #endif
 
+#define FREE_CACHE_ALIGNED(p) \
+do { \
+    free(p); \
+} while (0)
+
 #define ZERO(p) memset(p, 0, sizeof(*p))
 
 #define DEQ_DECLARE(i,d) typedef struct { \
diff --git a/src/alloc_pool.c b/src/alloc_pool.c
index 4548a2e..971a2df 100644
--- a/src/alloc_pool.c
+++ b/src/alloc_pool.c
@@ -395,7 +395,7 @@ void *qd_alloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t 
**tpool)
             DEQ_ITEM_INIT(item);
 #endif
             if (!push_stack(&pool->free_list, item)) {
-                free(item);
+                FREE_CACHE_ALIGNED(item);
                 break;
             }
             item->sequence = 0;
@@ -457,7 +457,7 @@ void qd_dealloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t 
**tpool, char *p)
     // thread-local pool for this type.
     //
     if (*tpool == 0) {
-        *tpool = NEW(qd_alloc_pool_t);
+        NEW_CACHE_ALIGNED(qd_alloc_pool_t, *tpool);
         DEQ_ITEM_INIT(*tpool);
         init_stack(&(*tpool)->free_list);
         sys_mutex_lock(desc->lock);
@@ -469,7 +469,7 @@ void qd_dealloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t 
**tpool, char *p)
 
     item->sequence++;
     if (!push_stack(&pool->free_list, item)) {
-        free(item);
+        FREE_CACHE_ALIGNED(item);
     }
 
     if (DEQ_SIZE(pool->free_list) < desc->config->local_free_list_max)
@@ -492,7 +492,7 @@ void qd_dealloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t 
**tpool, char *p)
     if (desc->config->global_free_list_max != 0) {
         while (DEQ_SIZE(desc->global_pool->free_list) > 
desc->config->global_free_list_max) {
             item = pop_stack(&desc->global_pool->free_list);
-            free(item);
+            FREE_CACHE_ALIGNED(item);
             desc->stats->total_free_to_heap++;
         }
     }
@@ -564,7 +564,7 @@ void qd_alloc_finalize(void)
         //
         item = pop_stack(&desc->global_pool->free_list);
         while (item) {
-            free(item);
+            FREE_CACHE_ALIGNED(item);
             desc->stats->total_free_to_heap++;
             item = pop_stack(&desc->global_pool->free_list);
         }
@@ -580,13 +580,13 @@ void qd_alloc_finalize(void)
         while (tpool) {
             item = pop_stack(&tpool->free_list);
             while (item) {
-                free(item);
+                FREE_CACHE_ALIGNED(item);
                 desc->stats->total_free_to_heap++;
                 item = pop_stack(&tpool->free_list);
             }
             DEQ_REMOVE_HEAD(desc->tpool_list);
             free_stack_chunks(&tpool->free_list);
-            free(tpool);
+            FREE_CACHE_ALIGNED(tpool);
             tpool = DEQ_HEAD(desc->tpool_list);
         }
 
@@ -632,7 +632,7 @@ void qd_alloc_finalize(void)
                 // Since this is a custom heap ASAN will dump the first
                 // malloc() of the object - not the last time it was allocated
                 // from the pool.
-                free(item);
+                FREE_CACHE_ALIGNED(item);
                 item = DEQ_HEAD(qtype->allocated);
             }
 #endif
diff --git a/src/posix/threading.c b/src/posix/threading.c
index 9ba15e9..7913db4 100644
--- a/src/posix/threading.c
+++ b/src/posix/threading.c
@@ -48,7 +48,7 @@ sys_mutex_t *sys_mutex(void)
 void sys_mutex_free(sys_mutex_t *mutex)
 {
     pthread_mutex_destroy(&(mutex->mutex));
-    free(mutex);
+    FREE_CACHE_ALIGNED(mutex);
 }
 
 

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to