Blob Blame History Raw
From 32fe4086fe865247e69a6a94d2e94ed45463c2e9 Mon Sep 17 00:00:00 2001
From: Niels de Vos <ndevos@redhat.com>
Date: Fri, 18 Nov 2016 14:37:41 +0100
Subject: [PATCH 184/206] libglusterfs: add gf_get_mem_type()

gfapi needs to provide a function towards applications to free memory
that it allocated. Depending on how the application is compiled/linked,
it could use a different memory allocator than Gluster itself. Therefore
it is not safe for gfapi to request applications to free memory with
'standard' free().

Examples for this are Gluster allocated structures with GF_CALLOC() when
memory accounting is enabled (the default). Some gfapi functions use
malloc() to allocate memory as a workaround, but the free() from the
jemalloc implementation should not be combined with the malloc() from
glibc.

Cherry picked from commit db4e26ed71a01e5f760fbc3c7051962426f102c9:
> Change-Id: I626cd1a60abf8965f9263290f4045d1f69fc2093
> BUG: 1344714
> Signed-off-by: Niels de Vos <ndevos@redhat.com>
> Reviewed-on: http://review.gluster.org/15108
> Smoke: Gluster Build System <jenkins@build.gluster.org>
> Reviewed-by: soumya k <skoduri@redhat.com>
> Reviewed-by: jiffin tony Thottan <jthottan@redhat.com>
> NetBSD-regression: NetBSD Build System <jenkins@build.gluster.org>
> CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
> Reviewed-by: Shyamsundar Ranganathan <srangana@redhat.com>

This also includes commit da2aabe3 which fixes an unused variable
warning.

Cherry picked from commit 8d1b94f6e43175df60ca9067db1811997d3ef4fc:
> Change-Id: I626cd1a60abf8965f9263290f4045d1f69fc2093
> BUG: 1347717
> Signed-off-by: Niels de Vos <ndevos@redhat.com>
> Reviewed-on: http://review.gluster.org/15639
> Smoke: Gluster Build System <jenkins@build.gluster.org>
> NetBSD-regression: NetBSD Build System <jenkins@build.gluster.org>
> CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
> Reviewed-by: jiffin tony Thottan <jthottan@redhat.com>

Change-Id: I626cd1a60abf8965f9263290f4045d1f69fc2093
BUG: 1386635
Signed-off-by: Niels de Vos <ndevos@redhat.com>
Reviewed-on: https://code.engineering.redhat.com/gerrit/90577
Reviewed-by: Soumya Koduri <skoduri@redhat.com>
Tested-by: Soumya Koduri <skoduri@redhat.com>
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
---
 libglusterfs/src/mem-pool.c | 20 ++++++++++++++++++++
 libglusterfs/src/mem-pool.h |  2 ++
 2 files changed, 22 insertions(+)

diff --git a/libglusterfs/src/mem-pool.c b/libglusterfs/src/mem-pool.c
index 93a1415..78fecec 100644
--- a/libglusterfs/src/mem-pool.c
+++ b/libglusterfs/src/mem-pool.c
@@ -338,6 +338,26 @@ free:
 }
 
 
+/* Based on the mem-type that is used for the allocation, GF_FREE can be
+ * called, or something more intelligent for the structure can be done.
+ */
+int
+gf_get_mem_type (void *ptr)
+{
+        struct mem_header *header = NULL;
+
+        if (!ptr || !THIS->ctx->mem_acct_enable)
+                return 0;
+
+        header = (struct mem_header *) (ptr - GF_MEM_HEADER_SIZE);
+
+        /* Possible corruption, assert here */
+        GF_ASSERT (GF_MEM_HEADER_MAGIC == header->magic);
+
+        return header->type;
+}
+
+
 
 struct mem_pool *
 mem_pool_new_fn (unsigned long sizeof_type,
diff --git a/libglusterfs/src/mem-pool.h b/libglusterfs/src/mem-pool.h
index 5115cef..6cff7be 100644
--- a/libglusterfs/src/mem-pool.h
+++ b/libglusterfs/src/mem-pool.h
@@ -97,6 +97,8 @@ gf_asprintf (char **string_ptr, const char *format, ...);
 void
 __gf_free (void *ptr);
 
+int
+gf_get_mem_type (void *ptr);
 
 static inline
 void* __gf_default_malloc (size_t size)
-- 
2.9.3