Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4c8156d

Browse files
committed
Add PGTYPESchar_free() to avoid cross-module problems on Windows.
On Windows, it is sometimes important for corresponding malloc() and free() calls to be made from the same DLL, since some build options can result in multiple allocators being active at the same time. For that reason we already provided PQfreemem(). This commit adds a similar function for freeing string results allocated by the pgtypes library. Author: Takayuki Tsunakawa Reviewed-by: Kyotaro Horiguchi Discussion: https://postgr.es/m/0A3221C70F24FB45833433255569204D1F8AD5D6%40G01JPEXMBYT05
1 parent 70b4f82 commit 4c8156d

20 files changed

+189
-136
lines changed

doc/src/sgml/ecpg.sgml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1951,11 +1951,23 @@ EXEC SQL SELECT started, duration INTO :ts1, :iv1 FROM datetbl WHERE d=:date1;
19511951
PGTYPEStimestamp_add_interval(&ts1, &iv1, &tsout);
19521952
out = PGTYPEStimestamp_to_asc(&tsout);
19531953
printf("Started + duration: %s\n", out);
1954-
free(out);
1954+
PGTYPESchar_free(out);
19551955
]]>
19561956
</programlisting>
19571957
</para>
19581958

1959+
<sect2 id="ecpg-pgtypes-cstrings">
1960+
<title>Character Strings</title>
1961+
<para>
1962+
Some functions such as <function>PGTYPESnumeric_to_asc</function> return
1963+
a pointer to a freshly allocated character string. These results should be
1964+
freed with <function>PGTYPESchar_free</function> instead of
1965+
<function>free</function>. (This is important only on Windows, where
1966+
memory allocation and release sometimes need to be done by the same
1967+
library.)
1968+
</para>
1969+
</sect2>
1970+
19591971
<sect2 id="ecpg-pgtypes-numeric">
19601972
<title>The numeric Type</title>
19611973
<para>
@@ -2029,6 +2041,7 @@ char *PGTYPESnumeric_to_asc(numeric *num, int dscale);
20292041
</synopsis>
20302042
The numeric value will be printed with <literal>dscale</literal> decimal
20312043
digits, with rounding applied if necessary.
2044+
The result must be freed with <function>PGTYPESchar_free()</function>.
20322045
</para>
20332046
</listitem>
20342047
</varlistentry>
@@ -2419,6 +2432,7 @@ char *PGTYPESdate_to_asc(date dDate);
24192432
The function receives the date <literal>dDate</literal> as its only parameter.
24202433
It will output the date in the form <literal>1999-01-18</literal>, i.e., in the
24212434
<literal>YYYY-MM-DD</literal> format.
2435+
The result must be freed with <function>PGTYPESchar_free()</function>.
24222436
</para>
24232437
</listitem>
24242438
</varlistentry>
@@ -2841,6 +2855,7 @@ char *PGTYPEStimestamp_to_asc(timestamp tstamp);
28412855
The function receives the timestamp <literal>tstamp</literal> as
28422856
its only argument and returns an allocated string that contains the
28432857
textual representation of the timestamp.
2858+
The result must be freed with <function>PGTYPESchar_free()</function>.
28442859
</para>
28452860
</listitem>
28462861
</varlistentry>
@@ -3349,6 +3364,7 @@ char *PGTYPESinterval_to_asc(interval *span);
33493364
The function converts the interval variable that <literal>span</literal>
33503365
points to into a C char*. The output looks like this example:
33513366
<literal>@ 1 day 12 hours 59 mins 10 secs</literal>.
3367+
The result must be freed with <function>PGTYPESchar_free()</function>.
33523368
</para>
33533369
</listitem>
33543370
</varlistentry>

src/interfaces/ecpg/include/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ install: all installdirs install-headers
1414

1515
.PHONY: install-headers
1616
ecpg_headers = ecpgerrno.h ecpglib.h ecpgtype.h sqlca.h sql3types.h ecpg_informix.h \
17-
pgtypes_error.h pgtypes_numeric.h pgtypes_timestamp.h pgtypes_date.h pgtypes_interval.h \
17+
pgtypes_error.h pgtypes_numeric.h pgtypes_timestamp.h pgtypes_date.h pgtypes_interval.h pgtypes.h \
1818
sqlda.h sqlda-compat.h sqlda-native.h
1919
informix_headers = datetime.h decimal.h sqltypes.h
2020

src/interfaces/ecpg/include/pgtypes.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* src/interfaces/ecpg/include/pgtypes.h */
2+
3+
#ifndef PGTYPES_H
4+
#define PGTYPES_H
5+
6+
#ifdef __cplusplus
7+
extern "C"
8+
{
9+
#endif
10+
11+
extern void PGTYPESchar_free(char *ptr);
12+
13+
#ifdef __cplusplus
14+
}
15+
#endif
16+
17+
#endif /* PGTYPES_H */

src/interfaces/ecpg/include/pgtypes_date.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#ifndef PGTYPES_DATETIME
44
#define PGTYPES_DATETIME
55

6+
#include <pgtypes.h>
67
#include <pgtypes_timestamp.h>
78

89
typedef long date;

src/interfaces/ecpg/include/pgtypes_interval.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define PGTYPES_INTERVAL
55

66
#include <ecpg_config.h>
7+
#include <pgtypes.h>
78

89
#ifndef C_H
910

src/interfaces/ecpg/include/pgtypes_numeric.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef PGTYPES_NUMERIC
22
#define PGTYPES_NUMERIC
33

4+
#include <pgtypes.h>
5+
46
#define NUMERIC_POS 0x0000
57
#define NUMERIC_NEG 0x4000
68
#define NUMERIC_NAN 0xC000

src/interfaces/ecpg/include/pgtypes_timestamp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#ifndef PGTYPES_TIMESTAMP
44
#define PGTYPES_TIMESTAMP
55

6+
#include <pgtypes.h>
67
/* pgtypes_interval.h includes ecpg_config.h */
78
#include <pgtypes_interval.h>
89

src/interfaces/ecpg/pgtypeslib/common.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "postgres_fe.h"
44

55
#include "extern.h"
6+
#include "pgtypes.h"
67

78
/* Return value is zero-filled. */
89
char *
@@ -136,3 +137,12 @@ pgtypes_fmt_replace(union un_fmt_comb replace_val, int replace_type, char **outp
136137
}
137138
return 0;
138139
}
140+
141+
/* Functions declared in pgtypes.h. */
142+
143+
/* Just frees memory (mostly needed for Windows) */
144+
void
145+
PGTYPESchar_free(char *ptr)
146+
{
147+
free(ptr);
148+
}

src/interfaces/ecpg/pgtypeslib/exports.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,4 @@ PGTYPEStimestamp_from_asc 42
4545
PGTYPEStimestamp_sub 43
4646
PGTYPEStimestamp_sub_interval 44
4747
PGTYPEStimestamp_to_asc 45
48+
PGTYPESchar_free 46

0 commit comments

Comments
 (0)