diff options
| author | Pali Rohár <pali.rohar@gmail.com> | 2024-03-22 01:53:18 +0100 |
|---|---|---|
| committer | Silvio <silvio.traversaro@iit.it> | 2025-04-28 14:49:51 +0200 |
| commit | f87cc876be9b450d3491a174b8c673d918ec5daf (patch) | |
| tree | 6d4b7772976585710d0cdb145e7cf4e43f7300ad /src | |
| parent | 8c28ff3b85f87ae9cb645fed9ec2d146b3059e7a (diff) | |
| download | dlfcn-win32-f87cc876be9b450d3491a174b8c673d918ec5daf.tar.gz dlfcn-win32-f87cc876be9b450d3491a174b8c673d918ec5daf.tar.bz2 dlfcn-win32-f87cc876be9b450d3491a174b8c673d918ec5daf.zip | |
Replace CRT's malloc() and free() by WinAPI's LocalAlloc() and LocalFree() in release mode
In debug mode is still used malloc() and free() for as part of memory leak
testing done by _CRTDBG_MAP_ALLOC and _CrtDumpMemoryLeaks() in test.c.
This change allows to wrap CRT's malloc() via dlsym's RTLD_NEXT.
See #112
Diffstat (limited to 'src')
| -rw-r--r-- | src/dlfcn.c | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/src/dlfcn.c b/src/dlfcn.c index d52d736..be66cac 100644 --- a/src/dlfcn.c +++ b/src/dlfcn.c | |||
| @@ -30,7 +30,6 @@ | |||
| 30 | #include <crtdbg.h> | 30 | #include <crtdbg.h> |
| 31 | #endif | 31 | #endif |
| 32 | #include <windows.h> | 32 | #include <windows.h> |
| 33 | #include <stdlib.h> /* malloc() and free() */ | ||
| 34 | #include <string.h> /* strlen() and memcpy() */ | 33 | #include <string.h> /* strlen() and memcpy() */ |
| 35 | 34 | ||
| 36 | /* Older versions do not have this type */ | 35 | /* Older versions do not have this type */ |
| @@ -105,6 +104,24 @@ __declspec( naked ) static void *_ReturnAddress( void ) { __asm mov eax, [ebp+4] | |||
| 105 | #define DLFCN_NOINLINE | 104 | #define DLFCN_NOINLINE |
| 106 | #endif | 105 | #endif |
| 107 | 106 | ||
| 107 | static void *MyAlloc( size_t size ) | ||
| 108 | { | ||
| 109 | #ifdef _DEBUG | ||
| 110 | return malloc( size ); | ||
| 111 | #else | ||
| 112 | return LocalAlloc( LPTR, size ); | ||
| 113 | #endif | ||
| 114 | } | ||
| 115 | |||
| 116 | static void MyFree( void *ptr ) | ||
| 117 | { | ||
| 118 | #ifdef _DEBUG | ||
| 119 | free( ptr ); | ||
| 120 | #else | ||
| 121 | LocalFree( ptr ); | ||
| 122 | #endif | ||
| 123 | } | ||
| 124 | |||
| 108 | /* Note: | 125 | /* Note: |
| 109 | * MSDN says these functions are not thread-safe. We make no efforts to have | 126 | * MSDN says these functions are not thread-safe. We make no efforts to have |
| 110 | * any kind of thread safety. | 127 | * any kind of thread safety. |
| @@ -149,7 +166,7 @@ static BOOL local_add( HMODULE hModule ) | |||
| 149 | 166 | ||
| 150 | for( pobject = &first_object; pobject->next; pobject = pobject->next ); | 167 | for( pobject = &first_object; pobject->next; pobject = pobject->next ); |
| 151 | 168 | ||
| 152 | nobject = (local_object *) malloc( sizeof( local_object ) ); | 169 | nobject = (local_object *) MyAlloc( sizeof( local_object ) ); |
| 153 | 170 | ||
| 154 | if( !nobject ) | 171 | if( !nobject ) |
| 155 | return FALSE; | 172 | return FALSE; |
| @@ -179,7 +196,7 @@ static void local_rem( HMODULE hModule ) | |||
| 179 | if( pobject->previous ) | 196 | if( pobject->previous ) |
| 180 | pobject->previous->next = pobject->next; | 197 | pobject->previous->next = pobject->next; |
| 181 | 198 | ||
| 182 | free( pobject ); | 199 | MyFree( pobject ); |
| 183 | } | 200 | } |
| 184 | 201 | ||
| 185 | /* POSIX says dlerror( ) doesn't have to be thread-safe, so we use one | 202 | /* POSIX says dlerror( ) doesn't have to be thread-safe, so we use one |
| @@ -568,7 +585,7 @@ void *dlsym( void *handle, const char *name ) | |||
| 568 | */ | 585 | */ |
| 569 | if( MyEnumProcessModules( hCurrentProc, NULL, 0, &dwSize ) != 0 ) | 586 | if( MyEnumProcessModules( hCurrentProc, NULL, 0, &dwSize ) != 0 ) |
| 570 | { | 587 | { |
| 571 | modules = (HMODULE *) malloc( dwSize ); | 588 | modules = (HMODULE *) MyAlloc( dwSize ); |
| 572 | if( modules ) | 589 | if( modules ) |
| 573 | { | 590 | { |
| 574 | if( MyEnumProcessModules( hCurrentProc, modules, dwSize, &cbNeeded ) != 0 && dwSize == cbNeeded ) | 591 | if( MyEnumProcessModules( hCurrentProc, modules, dwSize, &cbNeeded ) != 0 && dwSize == cbNeeded ) |
| @@ -587,13 +604,13 @@ void *dlsym( void *handle, const char *name ) | |||
| 587 | symbol = GetProcAddress( modules[i], name ); | 604 | symbol = GetProcAddress( modules[i], name ); |
| 588 | if( symbol != NULL ) | 605 | if( symbol != NULL ) |
| 589 | { | 606 | { |
| 590 | free( modules ); | 607 | MyFree( modules ); |
| 591 | goto end; | 608 | goto end; |
| 592 | } | 609 | } |
| 593 | } | 610 | } |
| 594 | 611 | ||
| 595 | } | 612 | } |
| 596 | free( modules ); | 613 | MyFree( modules ); |
| 597 | } | 614 | } |
| 598 | else | 615 | else |
| 599 | { | 616 | { |
