libmultipath: fix memory API logic error
PATCH 1/1 libmultipath: fix memory API logic error
From: "tang.junhui" <[email protected]>
Memroy API use mem_allocated to record the total size of used memory, however, it's wrong to use size(p) as the length of freed memory in xfree(), and memory may also be allocated by STRDUP() or REALLOC(), which is not calculated into mem_allocated, so the total size of used memory is not correctly. For these reasons, we removed these incorrectly code to keep the code clean.
Signed-off-by tang.junhui <[email protected]>
--- libmpathpersist/mpath_updatepr.c | 1 - libmultipath/memory.c | 31 ------------------------------- libmultipath/memory.h | 13 ++----------- 3 files changed, 2 insertions(+), 43 deletions(-)
libmpathpersist/mpath_updatepr.c +0 -1
@@ -16,7 +16,6 @@#include "uxsock.h" #include "memory.h" -unsigned long mem_allocated;/* Total memory used in Bytes */ int update_prflag (char * arg1,char * arg2,int noisy) {
libmultipath/memory.c +0 -31
@@ -22,37 +22,6 @@#include "memory.h" -/* Global var */ -unsigned long mem_allocated;/* Total memory used in Bytes */ - -void * -xalloc (unsigned long size) -{ -void *mem; -if ((mem =malloc (size))) - mem_allocated += size; -return mem; -} - -void * -zalloc (unsigned long size) -{ -void *mem; -if ((mem =malloc (size))) { -memset (mem,0 , size); - mem_allocated += size; - } -return mem; -} - -void -xfree (void *p) -{ - mem_allocated -=sizeof (p); -free (p); - p =NULL ; -} -/* * Memory management. in debug mode, * help finding eventual memory leak.
libmultipath/memory.h +2 -11
@@ -29,15 +29,6 @@#include <string.h> #include <assert.h> -/* extern types */ -extern unsigned long mem_allocated; -extern void *xalloc (unsigned long size); -extern void *zalloc (unsigned long size); -extern void xfree (void *p); - -/* Global alloc macro */ -#define ALLOC (n) (xalloc(n)) -/* Local defines */ #ifdef _DEBUG_
@@ -63,8 +54,8 @@ extern void dbg_free_final(char *);#else -#define MALLOC (n) (zalloc(n)) -#define FREE (p) (xfree(p)) +#define MALLOC (n) (calloc(1,(n))) +#define FREE (p) do { free(p); p = NULL; } while(0)#define REALLOC (p,n) (realloc((p),(n)))#define STRDUP (n) (strdup(n))
Signature · 1 line
2.8.1.windows.1