[ofw] [PATCH] libs: convert libraries to use private heaps

Sean Hefty sean.hefty at intel.com
Thu Sep 3 11:30:39 PDT 2009


Convert winverbs, dapl, dat, libibverbs, and librdmacm to use private heaps.

This allows for better support of memory registration caching by upper
level libaries (MPI) that use SecureMemoryCacheCallback.

It also makes it easier to debug heap corruption issues.

Signed-off-by: Sean Hefty <sean.hefty at intel.com>
---
This should be added to winof 2.1

Index: core/winverbs/user/wv_main.cpp
===================================================================
--- core/winverbs/user/wv_main.cpp	(revision 2342)
+++ core/winverbs/user/wv_main.cpp	(working copy)
@@ -32,13 +32,27 @@
 #include "wv_base.h"
 
 volatile LONG WvRef;
+HANDLE heap;
 
 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
 {
 	UNREFERENCED_PARAMETER(hInstance);
-	UNREFERENCED_PARAMETER(dwReason);
 	UNREFERENCED_PARAMETER(lpReserved);
 
+	switch (dwReason) {
+	case DLL_PROCESS_ATTACH:
+		heap = HeapCreate(0, 0, 0);
+		if (heap == NULL) {
+			return FALSE;
+		}
+		break;
+	case DLL_PROCESS_DETACH:
+		HeapDestroy(heap);
+		break;
+	default:
+		break;
+	}
+
 	return TRUE;
 }
 
Index: core/winverbs/user/wv_memory.h
===================================================================
--- core/winverbs/user/wv_memory.h	(revision 2342)
+++ core/winverbs/user/wv_memory.h	(working copy)
@@ -34,14 +34,16 @@
 
 #include <windows.h>
 
+extern HANDLE heap;
+
 __inline void* __cdecl operator new(size_t size)
 {
-	return HeapAlloc(GetProcessHeap(), 0, size);
+	return HeapAlloc(heap, 0, size);
 }
 
 __inline void __cdecl operator delete(void *pObj)
 {
-	HeapFree(GetProcessHeap(), 0, pObj);
+	HeapFree(heap, 0, pObj);
 }
 
 const int WvDefaultBufferSize = 128;
@@ -70,4 +72,4 @@
 	void *m_pBuf;
 };
 
-#endif // _WV_MEMORY_H_
\ No newline at end of file
+#endif // _WV_MEMORY_H_
Index: ulp/dapl2/dapl/openib_scm/cm.c
===================================================================
--- ulp/dapl2/dapl/openib_scm/cm.c	(revision 2405)
+++ ulp/dapl2/dapl/openib_scm/cm.c	(working copy)
@@ -1830,7 +1830,7 @@
 	}
 
 	dapl_os_unlock(&hca_ptr->ib_trans.lock);
-	free(set);
+	dapl_os_free(set, sizeof(struct dapl_fd_set));
       out:
 	hca_ptr->ib_trans.cr_state = IB_THREAD_EXIT;
 	dapl_dbg_log(DAPL_DBG_TYPE_UTIL, " cr_thread(hca %p) exit\n", hca_ptr);
Index: ulp/dapl2/dapl/udapl/windows/dapl_osd.c
===================================================================
--- ulp/dapl2/dapl/udapl/windows/dapl_osd.c	(revision 2342)
+++ ulp/dapl2/dapl/udapl/windows/dapl_osd.c	(working copy)
@@ -48,8 +48,8 @@
 #include <sys/timeb.h>
 #include <stdlib.h>			/* needed for getenv() */
 
+HANDLE heap;
 
-
 /*
  * DllMain
  *
@@ -75,6 +75,10 @@
     switch( fdwReason ) 
     { 
         case DLL_PROCESS_ATTACH:
+		heap = HeapCreate(0, 0, 0);
+		if (heap == NULL) {
+			return FALSE;
+		}
 	    /*
 	     * We don't attach/detach threads that need any sort
 	     * of initialization, so disable this ability to optimize
@@ -112,6 +116,7 @@
              */
             dapl_fini ();
 #endif
+			HeapDestroy(heap);
             break;
     }
     return TRUE;  
Index: ulp/dapl2/dapl/udapl/windows/dapl_osd.h
===================================================================
--- ulp/dapl2/dapl/udapl/windows/dapl_osd.h	(revision 2342)
+++ ulp/dapl2/dapl/udapl/windows/dapl_osd.h	(working copy)
@@ -350,6 +350,8 @@
  * Memory Functions
  */
 
+extern HANDLE heap;
+
 /* function prototypes */
 STATIC __inline void *dapl_os_alloc (int size);
 
@@ -389,19 +391,18 @@
 
 STATIC __inline void *dapl_os_alloc (int size)
 {
-    return malloc (size);
+	return HeapAlloc(heap, 0, size);
 }
 
 STATIC __inline void *dapl_os_realloc (void *ptr, int size)
 {
-    return realloc(ptr, size);
+    return HeapReAlloc(heap, 0, ptr, size);
 }
 
 STATIC __inline void dapl_os_free (void *ptr, int size)
 {
-    size = size;
-    free (ptr);
-    ptr = NULL;
+	UNREFERENCED_PARAMETER(size);
+	HeapFree(heap, 0, ptr);
 }
 
 STATIC __inline void * dapl_os_memzero (void *loc, int size)
@@ -427,7 +428,13 @@
 
 STATIC __inline char * dapl_os_strdup(const char *str)
 {
-    return _strdup(str);
+	char *dup;
+
+	dup = dapl_os_alloc(strlen(str) + 1);
+	if (!dup)
+		return NULL;
+	strcpy(dup, str);
+    return dup;
 }
 
 
Index: ulp/dapl2/dat/udat/windows/dat_osd.c
===================================================================
--- ulp/dapl2/dat/udat/windows/dat_osd.c	(revision 2342)
+++ ulp/dapl2/dat/udat/windows/dat_osd.c	(working copy)
@@ -124,6 +124,7 @@
     }
 }
 
+HANDLE heap;
 
 BOOL APIENTRY
 DllMain(
@@ -138,12 +139,16 @@
 	switch( ul_reason_for_call )
 	{
 	case DLL_PROCESS_ATTACH:
+		heap = HeapCreate(0, 0, 0);
+		if (heap == NULL)
+			return FALSE;
 		DisableThreadLibraryCalls( h_module );
 		udat_check_state();
 		break;
 
 	case DLL_PROCESS_DETACH:
 		dat_fini();
+		HeapDestroy(heap);
 	}
 
 	return TRUE;
Index: ulp/dapl2/dat/udat/windows/dat_osd.h
===================================================================
--- ulp/dapl2/dat/udat/windows/dat_osd.h	(revision 2342)
+++ ulp/dapl2/dat/udat/windows/dat_osd.h	(working copy)
@@ -244,11 +244,13 @@
  *                                                                   *
  *********************************************************************/
 
+extern HANDLE heap;
+
 STATIC INLINE void *
 dat_os_alloc (
     int size)
 {
-    return malloc (size);
+	return HeapAlloc(heap, 0, size);
 }
 
 STATIC INLINE void 
@@ -256,7 +258,8 @@
     void *ptr, 
     int size)
 {
-    free (ptr);
+	UNREFERENCED_PARAMETER(size);
+	HeapFree(heap, 0, ptr);
 }
 
 STATIC INLINE void * 
Index: ulp/libibverbs/src/ibv_main.cpp
===================================================================
--- ulp/libibverbs/src/ibv_main.cpp	(revision 2388)
+++ ulp/libibverbs/src/ibv_main.cpp	(working copy)
@@ -30,6 +30,7 @@
 #include <windows.h>
 
 extern CRITICAL_SECTION lock;
+HANDLE heap;
 
 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
 {
@@ -38,10 +39,15 @@
 
 	switch (dwReason) {
 	case DLL_PROCESS_ATTACH:
+		heap = HeapCreate(0, 0, 0);
+		if (heap == NULL) {
+			return FALSE;
+		}
 		InitializeCriticalSection(&lock);
 		break;
 	case DLL_PROCESS_DETACH:
 		DeleteCriticalSection(&lock);
+		HeapDestroy(heap);
 		break;
 	default:
 		break;
Index: ulp/libibverbs/src/ibverbs.h
===================================================================
--- ulp/libibverbs/src/ibverbs.h	(revision 2342)
+++ ulp/libibverbs/src/ibverbs.h	(working copy)
@@ -31,15 +31,16 @@
 #define IB_VERBS_H
 
 extern COMP_MANAGER comp_mgr;
+extern HANDLE heap;
 
 __inline void* __cdecl operator new(size_t size)
 {
-	return HeapAlloc(GetProcessHeap(), 0, size);
+	return HeapAlloc(heap, 0, size);
 }
 
 __inline void __cdecl operator delete(void *pObj)
 {
-	HeapFree(GetProcessHeap(), 0, pObj);
+	HeapFree(heap, 0, pObj);
 }
 
 #endif /* IB_VERBS_H */
Index: ulp/librdmacm/src/cma.h
===================================================================
--- ulp/librdmacm/src/cma.h	(revision 2342)
+++ ulp/librdmacm/src/cma.h	(working copy)
@@ -32,15 +32,16 @@
 #define CMA_H
 
 extern CRITICAL_SECTION lock;
+extern HANDLE heap;
 
 __inline void* __cdecl operator new(size_t size)
 {
-	return HeapAlloc(GetProcessHeap(), 0, size);
+	return HeapAlloc(heap, 0, size);
 }
 
 __inline void __cdecl operator delete(void *pObj)
 {
-	HeapFree(GetProcessHeap(), 0, pObj);
+	HeapFree(heap, 0, pObj);
 }
 
 #endif /* CMA_H */
Index: ulp/librdmacm/src/cma_main.cpp
===================================================================
--- ulp/librdmacm/src/cma_main.cpp	(revision 2388)
+++ ulp/librdmacm/src/cma_main.cpp	(working copy)
@@ -31,6 +31,7 @@
 #include "cma.h"
 
 CRITICAL_SECTION lock;
+HANDLE heap;
 
 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
 {
@@ -39,10 +40,15 @@
 
 	switch (dwReason) {
 	case DLL_PROCESS_ATTACH:
+		heap = HeapCreate(0, 0, 0);
+		if (heap == NULL) {
+			return FALSE;
+		}
 		InitializeCriticalSection(&lock);
 		break;
 	case DLL_PROCESS_DETACH:
 		DeleteCriticalSection(&lock);
+		HeapDestroy(heap);
 		break;
 	default:
 		break;

-------------- next part --------------
A non-text attachment was scrubbed...
Name: priv-heap.diff
Type: application/octet-stream
Size: 7682 bytes
Desc: not available
URL: <http://lists.openfabrics.org/pipermail/ofw/attachments/20090903/4801df27/attachment.obj>


More information about the ofw mailing list