win_mem_alloc.c

Go to the documentation of this file.
00001 /* Allan CORNET */
00002 /* INRIA 2005 */
00003 #include <stdio.h>
00004 #include <string.h>
00005 #include <memory.h>
00006 #include "../includes/win_mem_alloc.h"
00007 /*-----------------------------------------------------------------------------------*/
00008 /* an interesting article about HeapAlloc,malloc, and OctAlloc */
00009 /* bench show that HeapAlloc is faster than malloc on Windows */
00010 /* http://denisbider.blogspot.com/2007/10/heap-allocation-on-multi-core-systems.html */
00011 /*-----------------------------------------------------------------------------------*/
00012 #define FREE_FLAGS 0
00013 /*-----------------------------------------------------------------------------------*/
00014 IMPORT_EXPORT_MALLOC_DLL LPVOID MyHeapRealloc(LPVOID lpAddress,SIZE_T dwSize,char *fichier,int ligne)
00015 {
00016         LPVOID NewPointer = NULL;
00017         SIZE_T precSize = 0;
00018 
00019         if (lpAddress)
00020         {
00021                 _try
00022                 {
00023                         NewPointer = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,lpAddress,dwSize);
00024                 }
00025                 _except (EXCEPTION_EXECUTE_HANDLER)
00026                 {
00027                 }
00028         }
00029         else
00030         {
00031                 NewPointer = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwSize);
00032 
00033                 if (NewPointer == NULL)
00034                 {
00035                         #ifdef _DEBUG
00036                         char MsgError[1024];
00037                         wsprintf(MsgError,"REALLOC (1) Error File %s Line %d ",fichier,ligne);
00038                         MessageBox(NULL,MsgError,"Error",MB_ICONSTOP | MB_OK);
00039                         #endif
00040                 }
00041         }
00042         return NewPointer;
00043 }
00044 /*-----------------------------------------------------------------------------------*/
00045 IMPORT_EXPORT_MALLOC_DLL LPVOID MyHeapAlloc(SIZE_T dwSize,char *fichier,int ligne)
00046 {
00047         LPVOID NewPointer = NULL;
00048 
00049         if (dwSize>0)
00050         {
00051                 _try
00052                 {
00053                         NewPointer = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwSize);
00054                 }
00055                 _except (EXCEPTION_EXECUTE_HANDLER)
00056                 {
00057                 }
00058 
00059                 if (NewPointer == NULL)
00060                 {
00061                         #ifdef _DEBUG
00062                         char MsgError[1024];
00063                         wsprintf(MsgError,"MALLOC (1) Error File %s Line %d ",fichier,ligne);
00064                         MessageBox(NULL,MsgError,"Error",MB_ICONSTOP | MB_OK);
00065                         #endif
00066                 }
00067         }
00068         else
00069         {
00070                 #ifdef _DEBUG
00071                 char MsgError[1024];
00072                 wsprintf(MsgError,"MALLOC (2) Error File %s Line %d ",fichier,ligne);
00073                 MessageBox(NULL,MsgError,"Error",MB_ICONSTOP | MB_OK);
00074                 #endif
00075                 _try
00076                 {
00077                         NewPointer = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwSize);
00078                 }
00079                 _except (EXCEPTION_EXECUTE_HANDLER)
00080                 {
00081                 }
00082         }
00083         return NewPointer;
00084 }
00085 /*-----------------------------------------------------------------------------------*/
00086 IMPORT_EXPORT_MALLOC_DLL void MyHeapFree(LPVOID lpAddress,char *fichier,int ligne)
00087 {
00088         _try
00089         {
00090                 HeapFree(GetProcessHeap(),FREE_FLAGS,lpAddress);
00091         }
00092         _except (EXCEPTION_EXECUTE_HANDLER)
00093         {
00094                 #ifdef _DEBUG
00095                 char MsgError[1024];
00096                 wsprintf(MsgError,"FREE Error File %s Line %d ",fichier,ligne);
00097                 MessageBox(NULL,MsgError,"Error",MB_ICONSTOP | MB_OK);
00098                 #endif
00099         }
00100 }
00101 /*-----------------------------------------------------------------------------------*/
00102 IMPORT_EXPORT_MALLOC_DLL LPVOID MyVirtualAlloc(SIZE_T dwSize,char *fichier,int ligne)
00103 {
00104         LPVOID NewPointer=NULL;
00105 
00106         if (dwSize>0)
00107         {
00108                 _try
00109                 {
00110                         NewPointer = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwSize);
00111                 }
00112                 _except (EXCEPTION_EXECUTE_HANDLER)
00113                 {
00114                 }
00115 
00116                 if (NewPointer==NULL)
00117                 {
00118                         #ifdef _DEBUG
00119                         char MsgError[1024];
00120                         wsprintf(MsgError,"MALLOC (VirtualAlloc 1) Error File %s Line %d ",fichier,ligne);
00121                         MessageBox(NULL,MsgError,"Error",MB_ICONSTOP | MB_OK);
00122                         #endif
00123                 }
00124         }
00125         else
00126         {
00127                 #ifdef _DEBUG
00128                 char MsgError[1024];
00129                 wsprintf(MsgError,"MALLOC (VirtualAlloc 2) Error File %s Line %d ",fichier,ligne);
00130                 MessageBox(NULL,MsgError,"Error",MB_ICONSTOP | MB_OK);
00131                 #endif
00132 
00133                 _try
00134                 {
00135                         NewPointer = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,dwSize);
00136                 }
00137                 _except (EXCEPTION_EXECUTE_HANDLER)
00138                 {
00139                 }
00140 
00141         }
00142 
00143         return NewPointer;
00144 }
00145 /*-----------------------------------------------------------------------------------*/
00146 IMPORT_EXPORT_MALLOC_DLL void MyVirtualFree(LPVOID lpAddress,char *fichier,int ligne)
00147 {
00148         if (lpAddress) 
00149         {
00150                 _try
00151                 {
00152                         HeapFree(GetProcessHeap(),FREE_FLAGS,lpAddress);
00153                 }
00154                 _except (EXCEPTION_EXECUTE_HANDLER)
00155                 {
00156                 }
00157         }
00158 }
00159 /*-----------------------------------------------------------------------------------*/

Generated on Tue Sep 9 17:48:18 2008 for Scilab [trunk] by  doxygen 1.5.5