Skip to content

Commit c602f0b

Browse files
committed
Add mimalloc submodule and update build script
1 parent 4e1c125 commit c602f0b

File tree

6 files changed

+88
-18
lines changed

6 files changed

+88
-18
lines changed

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[submodule "libs/mimalloc"]
2+
path = libs/mimalloc
3+
url = https://github.com/microsoft/mimalloc
4+
branch = master

inouealc.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#undef _MEMORY_DEBUG_
22
#include "psqlodbc.h"
33

4+
#ifndef _MIMALLOC_
45
#ifdef WIN32
56
#ifdef _DEBUG
67
/* #include <stdlib.h> */
@@ -10,6 +11,7 @@
1011
#include <malloc.h>
1112
#endif /* _DEBUG */
1213
#endif /* WIN32 */
14+
#endif /* _MIMALLOC_ */
1315
#include <string.h>
1416

1517
#include "misc.h"
@@ -26,20 +28,20 @@ CSTR ALCERR = "alcerr";
2628
void * pgdebug_alloc(size_t size)
2729
{
2830
void * alloced;
29-
alloced = malloc(size);
31+
alloced = pg_malloc(size);
3032
MYLOG(2, " alloced=%p(" FORMAT_SIZE_T ")\n", alloced, size);
3133
if (alloced)
3234
{
3335
if (!alsize)
3436
{
3537
alsize = 100;
36-
altbl = (ALADR *) malloc(alsize * sizeof(ALADR));
38+
altbl = (ALADR *) pg_malloc(alsize * sizeof(ALADR));
3739
}
3840
else if (tbsize >= alsize)
3941
{
4042
ALADR *al;
4143
alsize *= 2;
42-
if (al = (ALADR *) realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
44+
if (al = (ALADR *) pg_realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
4345
return alloced;
4446
altbl = al;
4547
}
@@ -53,20 +55,20 @@ MYLOG(2, " alloced=%p(" FORMAT_SIZE_T ")\n", alloced, size);
5355
}
5456
void * pgdebug_calloc(size_t n, size_t size)
5557
{
56-
void * alloced = calloc(n, size);
58+
void * alloced = pg_calloc(n, size);
5759

5860
if (alloced)
5961
{
6062
if (!alsize)
6163
{
6264
alsize = 100;
63-
altbl = (ALADR *) malloc(alsize * sizeof(ALADR));
65+
altbl = (ALADR *) pg_malloc(alsize * sizeof(ALADR));
6466
}
6567
else if (tbsize >= alsize)
6668
{
6769
ALADR *al;
6870
alsize *= 2;
69-
if (al = (ALADR *) realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
71+
if (al = (ALADR *) pg_realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
7072
return alloced;
7173
altbl = al;
7274
}
@@ -85,7 +87,7 @@ void * pgdebug_realloc(void * ptr, size_t size)
8587

8688
if (!ptr)
8789
return pgdebug_alloc(size);
88-
alloced = realloc(ptr, size);
90+
alloced = pg_realloc(ptr, size);
8991
if (!alloced)
9092
{
9193
MYLOG(0, "%s %p error\n", ALCERR, ptr);
@@ -109,7 +111,7 @@ void * pgdebug_realloc(void * ptr, size_t size)
109111
}
110112
char * pgdebug_strdup(const char * ptr)
111113
{
112-
char * alloced = strdup(ptr);
114+
char * alloced = pg_strdup(ptr);
113115
if (!alloced)
114116
{
115117
MYLOG(0, "%s %p error\n", ALCERR, ptr);
@@ -119,13 +121,13 @@ char * pgdebug_strdup(const char * ptr)
119121
if (!alsize)
120122
{
121123
alsize = 100;
122-
altbl = (ALADR *) malloc(alsize * sizeof(ALADR));
124+
altbl = (ALADR *) pg_malloc(alsize * sizeof(ALADR));
123125
}
124126
else if (tbsize >= alsize)
125127
{
126128
ALADR *al;
127129
alsize *= 2;
128-
if (al = (ALADR *) realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
130+
if (al = (ALADR *) pg_realloc(altbl, alsize * sizeof(ALADR)), NULL == al)
129131
return alloced;
130132
altbl = al;
131133
}
@@ -168,7 +170,7 @@ void pgdebug_free(void * ptr)
168170
}
169171
else
170172
MYLOG(2, "ptr=%p\n", ptr);
171-
free(ptr);
173+
pg_free(ptr);
172174
}
173175

174176
static BOOL out_check(void *out, size_t len, const char *name)
@@ -253,7 +255,7 @@ void debug_memory_check(void)
253255
if (0 == tbsize)
254256
{
255257
MYLOG(0, "no memry leak found and max count allocated so far is %d\n", alsize);
256-
free(altbl);
258+
pg_free(altbl);
257259
alsize = 0;
258260
}
259261
else

libs/mimalloc

Submodule mimalloc added at 4e50d67

psqlodbc.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#endif
2525
#include "version.h"
2626

27+
#ifdef _MIMALLOC_
28+
#include <stdlib.h>
29+
#else /* _MIMALLOC_ */
2730
#ifdef WIN32
2831
#ifdef _DEBUG
2932
#ifndef _MEMORY_DEBUG_
@@ -41,6 +44,11 @@
4144
#else /* WIN32 */
4245
#include <stdlib.h>
4346
#endif /* WIN32 */
47+
#endif /* _MIMALLOC_ */
48+
49+
#ifdef WIN32
50+
#include <stdbool.h>
51+
#endif /* WIN32 */
4452

4553
#ifdef __INCLUDE_POSTGRES_FE_H__ /* currently not defined */
4654
/*
@@ -61,6 +69,21 @@
6169
#endif /* __GNUC__ || __IBMC__ */
6270
#endif /* __INCLUDE_POSTGRES_FE_H__ */
6371

72+
#ifdef _MIMALLOC_
73+
#include <mimalloc.h>
74+
#define pg_malloc mi_malloc
75+
#define pg_realloc mi_realloc
76+
#define pg_calloc mi_calloc
77+
#define pg_strdup mi_strdup
78+
#define pg_free mi_free
79+
#else /* _MIMALLOC_ */
80+
#define pg_malloc malloc
81+
#define pg_realloc realloc
82+
#define pg_calloc calloc
83+
#define pg_strdup _strdup
84+
#define pg_free free
85+
#endif /* _MIMALLOC_ */
86+
6487
#ifdef _MEMORY_DEBUG_
6588
void *pgdebug_alloc(size_t);
6689
void *pgdebug_calloc(size_t, size_t);
@@ -87,6 +110,15 @@ void debug_memory_check(void);
87110
/* #define strncpy_null pgdebug_strncpy_null */
88111
#define memcpy pgdebug_memcpy
89112
#define memset pgdebug_memset
113+
#else /* _MEMORY_DEBUG_ */
114+
#ifdef WIN32
115+
#undef strdup
116+
#endif /* WIN32 */
117+
#define malloc pg_malloc
118+
#define realloc pg_realloc
119+
#define calloc pg_calloc
120+
#define strdup pg_strdup
121+
#define free pg_free
90122
#endif /* _MEMORY_DEBUG_ */
91123

92124
#ifdef WIN32

winbuild/BuildAll.ps1

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
Specify the configuration xml file name if you want to use
3131
the configuration file other than standard one.
3232
The relative path is relative to the current directory.
33+
.PARAMETER UseMimalloc
34+
Whether to use the mimalloc allocator for improved performance.
35+
Requires a toolset of v141, v142 or later. Specify "yes" or
36+
"no"(default).
3337
.EXAMPLE
3438
> .\BuildAll
3539
Build with default or automatically selected parameters.
@@ -65,7 +69,9 @@ Param(
6569
[ValidateSet("Debug", "Release")]
6670
[String]$Configuration="Release",
6771
[string]$BuildConfigPath,
68-
[switch]$AlongWithInstallers
72+
[switch]$AlongWithInstallers,
73+
[ValidateSet("yes", "no")]
74+
[string]$UseMimalloc="no"
6975
)
7076

7177
function buildPlatform([xml]$configInfo, [string]$Platform)
@@ -100,7 +106,25 @@ function buildPlatform([xml]$configInfo, [string]$Platform)
100106
$BUILD_MACROS = $BUILD_MACROS -replace '"', '`"'
101107
$macroList = iex "write-output $BUILD_MACROS"
102108
}
103-
& ${msbuildexe} ./platformbuild.vcxproj /tv:$MSToolsV "/p:Platform=$Platform;Configuration=$Configuration;PlatformToolset=${Toolset}" /t:$target /p:VisualStudioVersion=${VCVersion} /p:DRIVERVERSION=$DRIVERVERSION /p:PG_INC=$PG_INC /p:PG_LIB=$PG_LIB /p:PG_BIN=$PG_BIN $macroList
109+
110+
if ($UseMimalloc -eq "yes") {
111+
switch ($VCVersion) {
112+
"10.0" { $mimallocIdeDir = "vs2017" }
113+
"11.0" { $mimallocIdeDir = "vs2017" }
114+
"12.0" { $mimallocIdeDir = "vs2017" }
115+
"14.0" { $mimallocIdeDir = "vs2017" }
116+
"15.0" { $mimallocIdeDir = "vs2017" }
117+
"16.0" { $mimallocIdeDir = "vs2019" }
118+
"17.0" { $mimallocIdeDir = "vs2022" }
119+
default { throw "Unable to resolve mimalloc IDE directory for VC ${VCVersion}."}
120+
}
121+
122+
# build mimalloc dependency
123+
& ${msbuildexe} ..\libs\mimalloc\ide\$mimallocIdeDir\mimalloc.vcxproj /tv:$MSToolsV "/p:Platform=$Platform;Configuration=$Configuration;PlatformToolset=${Toolset}" /t:$target /p:VisualStudioVersion=${VCVersion}
124+
}
125+
126+
# build psqlodbc
127+
& ${msbuildexe} ./platformbuild.vcxproj /tv:$MSToolsV "/p:Platform=$Platform;Configuration=$Configuration;PlatformToolset=${Toolset}" /t:$target /p:VisualStudioVersion=${VCVersion} /p:DRIVERVERSION=$DRIVERVERSION /p:PG_INC=$PG_INC /p:PG_LIB=$PG_LIB /p:PG_BIN=$PG_BIN /p:MIMALLOC=$UseMimalloc $macroList
104128
}
105129

106130
$scriptPath = (Split-Path $MyInvocation.MyCommand.Path)

winbuild/psqlodbc.vcxproj

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@
131131
<PropertyGroup Condition="'$(MEMORY_DEBUG)'=='yes'" >
132132
<ADD_DEFINES>$(ADD_DEFINES);_MEMORY_DEBUG_</ADD_DEFINES>
133133
</PropertyGroup>
134+
<!-- MIMALLOC -->
135+
<PropertyGroup Condition="'$(MIMALLOC)'=='yes'" >
136+
<ADD_DEFINES>$(ADD_DEFINES);_MIMALLOC_</ADD_DEFINES>
137+
<ADD_INC>$(ADD_INC);..\libs\mimalloc\include</ADD_INC>
138+
<ADD_LIBPATH>$(ADD_LIBPATH);..\libs\mimalloc\out\msvc-$(TARGET_CPU)\$(Configuration)</ADD_LIBPATH>
139+
<CALL_LIB>$(CALL_LIB);mimalloc-static.lib</CALL_LIB>
140+
</PropertyGroup>
134141

135142
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
136143
<LinkIncremental>true</LinkIncremental>
@@ -158,7 +165,7 @@
158165
<Link>
159166
<DelayLoadDLLs>$(DELAY_LOAD_DLLS);%(DelayLoadDLLs)</DelayLoadDLLs>
160167
<AdditionalLibraryDirectories>$(ADD_LIBPATH);$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
161-
<AdditionalDependencies>libpq.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;wsock32.lib;ws2_32.lib;secur32.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
168+
<AdditionalDependencies>$(CALL_LIB);libpq.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;wsock32.lib;ws2_32.lib;secur32.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
162169
<ModuleDefinitionFile>$(MAINDEF)</ModuleDefinitionFile>
163170
<SupportUnloadOfDelayLoadedDLL>true</SupportUnloadOfDelayLoadedDLL>
164171
</Link>
@@ -177,7 +184,7 @@
177184
<Link>
178185
<DelayLoadDLLs>$(DELAY_LOAD_DLLS);%(DelayLoadDLLs)</DelayLoadDLLs>
179186
<AdditionalLibraryDirectories>$(ADD_LIBPATH);$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
180-
<AdditionalDependencies>libpq.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;wsock32.lib;ws2_32.lib;secur32.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
187+
<AdditionalDependencies>$(CALL_LIB);libpq.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;wsock32.lib;ws2_32.lib;secur32.lib;winmm.lib;%(AdditionalDependencies)</AdditionalDependencies>
181188
<ModuleDefinitionFile>$(MAINDEF)</ModuleDefinitionFile>
182189
<SupportUnloadOfDelayLoadedDLL>true</SupportUnloadOfDelayLoadedDLL>
183190
</Link>
@@ -196,7 +203,7 @@
196203
<Link>
197204
<DelayLoadDLLs>$(DELAY_LOAD_DLLS);%(DelayLoadDLLs)</DelayLoadDLLs>
198205
<AdditionalLibraryDirectories>$(ADD_LIBPATH);$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
199-
<AdditionalDependencies>libpq.lib;winmm.lib;wsock32.lib;ws2_32.lib;secur32.lib;%(AdditionalDependencies)</AdditionalDependencies>
206+
<AdditionalDependencies>$(CALL_LIB);libpq.lib;winmm.lib;wsock32.lib;ws2_32.lib;secur32.lib;%(AdditionalDependencies)</AdditionalDependencies>
200207
<ModuleDefinitionFile>$(MAINDEF)</ModuleDefinitionFile>
201208
<SupportUnloadOfDelayLoadedDLL>true</SupportUnloadOfDelayLoadedDLL>
202209
</Link>
@@ -215,7 +222,7 @@
215222
<Link>
216223
<DelayLoadDLLs>$(DELAY_LOAD_DLLS);%(DelayLoadDLLs)</DelayLoadDLLs>
217224
<AdditionalLibraryDirectories>$(ADD_LIBPATH);$(OutDir);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
218-
<AdditionalDependencies>libpq.lib;winmm.lib;wsock32.lib;ws2_32.lib;secur32.lib;%(AdditionalDependencies)</AdditionalDependencies>
225+
<AdditionalDependencies>$(CALL_LIB);libpq.lib;winmm.lib;wsock32.lib;ws2_32.lib;secur32.lib;%(AdditionalDependencies)</AdditionalDependencies>
219226
<ModuleDefinitionFile>$(MAINDEF)</ModuleDefinitionFile>
220227
<SupportUnloadOfDelayLoadedDLL>true</SupportUnloadOfDelayLoadedDLL>
221228
</Link>

0 commit comments

Comments
 (0)