Skip to content

Commit f1792b9

Browse files
committed
Use PQfreemem() consistently, and document its use for Notify.
Keep PQfreeNotify() around for binary compatibility.
1 parent 6a19c6d commit f1792b9

File tree

8 files changed

+47
-35
lines changed

8 files changed

+47
-35
lines changed

doc/src/sgml/libpq.sgml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.115 2003/03/24 18:33:52 momjian Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/libpq.sgml,v 1.116 2003/03/25 02:44:36 momjian Exp $
33
-->
44

55
<chapter id="libpq">
@@ -1049,8 +1049,7 @@ characters that are otherwise interpreted specially by the SQL parser.
10491049
<function>PQescapeString</> performs this operation.
10501050
</para>
10511051
<para>
1052-
The
1053-
parameter <parameter>from</> points to the first character of the string that
1052+
The parameter <parameter>from</> points to the first character of the string that
10541053
is to be escaped, and the <parameter>length</> parameter counts the
10551054
number of characters in this string. (A terminating zero byte is
10561055
neither necessary nor counted.) <parameter>to</> shall point to a
@@ -1117,7 +1116,9 @@ unsigned char *PQescapeBytea(const unsigned char *from,
11171116

11181117
<para>
11191118
<function>PQescapeBytea</> returns an escaped version of the
1120-
<parameter>from</parameter> parameter binary string in memory allocated with <function>malloc()</>.
1119+
<parameter>from</parameter> parameter binary string in memory
1120+
allocated with <function>malloc()</>, and must be freed using
1121+
<function>PQfreemem()</>.
11211122
The return string has all special characters replaced
11221123
so that they can be properly processed by the PostgreSQL string literal
11231124
parser, and the <type>bytea</type> input function. A terminating zero
@@ -1143,8 +1144,11 @@ unsigned char *PQunescapeBytea(const unsigned char *from, size_t *to_length);
11431144
such as might be returned by <function>PQgetvalue</function> when applied to a
11441145
<type>bytea</type> column. <function>PQunescapeBytea</function> converts
11451146
this string representation into its binary representation.
1146-
It returns a pointer to a buffer allocated with <function>malloc()</function>, or null on error, and puts the size
1147-
of the buffer in <parameter>to_length</parameter>.
1147+
It returns a pointer to a buffer allocated with
1148+
<function>malloc()</function>, or null on error, and puts the size of
1149+
the buffer in <parameter>to_length</parameter>. The memory must be
1150+
freed using <function>PQfreemem()</>.
1151+
11481152
</para>
11491153
</listitem>
11501154
</varlistentry>
@@ -1161,7 +1165,9 @@ void PQfreemem(void *ptr);
11611165

11621166
<para>
11631167
Frees memory allocated by <application>libpq</>, particularly
1164-
<function>PQescapeBytea</function> and <function>PQunescapeBytea</function>.
1168+
<function>PQescapeBytea</function>,
1169+
<function>PQunescapeBytea</function>,
1170+
and <function>PQnotifies</function>.
11651171
It is needed by Win32, which can not free memory across
11661172
DLL's, unless multithreaded DLL's (/MD in VC6) are used.
11671173
</para>
@@ -1926,7 +1932,7 @@ typedef struct pgNotify {
19261932
} PGnotify;
19271933
</synopsis>
19281934
After processing a <structname>PGnotify</structname> object returned by <function>PQnotifies</function>,
1929-
be sure to free it with <function>free()</function> to avoid a memory leak.
1935+
be sure to free it with <function>PQfreemem()</function>.
19301936
</para>
19311937

19321938
<note>
@@ -2867,7 +2873,7 @@ main()
28672873
fprintf(stderr,
28682874
"ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
28692875
notify-&gt;relname, notify-&gt;be_pid);
2870-
free(notify);
2876+
PQfreemem(notify);
28712877
}
28722878
}
28732879

src/bin/psql/common.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.61 2003/03/20 15:39:52 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.62 2003/03/25 02:44:36 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -441,7 +441,7 @@ PrintNotifications(void)
441441
{
442442
fprintf(pset.queryFout, gettext("Asynchronous NOTIFY '%s' from backend with pid %d received.\n"),
443443
notify->relname, notify->be_pid);
444-
free(notify);
444+
PQfreemem(notify);
445445
fflush(pset.queryFout);
446446
}
447447
}

src/interfaces/ecpg/ecpglib/execute.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.4 2003/03/20 15:56:50 meskes Exp $ */
1+
/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/execute.c,v 1.5 2003/03/25 02:44:36 momjian Exp $ */
22

33
/*
44
* The aim is to get a simpler inteface to the database routines.
@@ -1197,7 +1197,7 @@ ECPGexecute(struct statement * stmt)
11971197
{
11981198
ECPGlog("ECPGexecute line %d: ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
11991199
stmt->lineno, notify->relname, notify->be_pid);
1200-
ECPGfree(notify);
1200+
PQfreemem(notify);
12011201
}
12021202

12031203
return status;

src/interfaces/libpgtcl/pgtclId.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* Portions Copyright (c) 1994, Regents of the University of California
1414
*
1515
* IDENTIFICATION
16-
* $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclId.c,v 1.40 2003/02/01 00:22:12 tgl Exp $
16+
* $Header: /cvsroot/pgsql/src/interfaces/libpgtcl/Attic/pgtclId.c,v 1.41 2003/03/25 02:44:36 momjian Exp $
1717
*
1818
*-------------------------------------------------------------------------
1919
*/
@@ -554,7 +554,7 @@ Pg_Notify_EventProc(Tcl_Event *evPtr, int flags)
554554
if (event->connid == NULL)
555555
{
556556
if (event->notify)
557-
PQfreeNotify(event->notify);
557+
PQfreemem(event->notify);
558558
return 1;
559559
}
560560

@@ -634,7 +634,7 @@ Pg_Notify_EventProc(Tcl_Event *evPtr, int flags)
634634
Tcl_Release((ClientData) event->connid);
635635

636636
if (event->notify)
637-
PQfreeNotify(event->notify);
637+
PQfreemem(event->notify);
638638

639639
return 1;
640640
}

src/interfaces/libpq/fe-exec.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.127 2003/03/22 03:29:06 momjian Exp $
11+
* $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.128 2003/03/25 02:44:36 momjian Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1577,20 +1577,6 @@ PQnotifies(PGconn *conn)
15771577
return event;
15781578
}
15791579

1580-
/*
1581-
* PQfreeNotify - free's the memory associated with a PGnotify
1582-
*
1583-
* This function is needed on Windows when using libpq.dll and
1584-
* for example libpgtcl.dll: All memory allocated inside a dll
1585-
* should be freed in the context of the same dll.
1586-
*
1587-
*/
1588-
void
1589-
PQfreeNotify(PGnotify *notify)
1590-
{
1591-
free(notify);
1592-
}
1593-
15941580
/*
15951581
* PQgetline - gets a newline-terminated string from the backend.
15961582
*
@@ -2470,3 +2456,22 @@ PQsendSome(PGconn *conn)
24702456
{
24712457
return pqSendSome(conn);
24722458
}
2459+
2460+
/*
2461+
* PQfreeNotify - free's the memory associated with a PGnotify
2462+
*
2463+
* This function is here only for binary backward compatibility.
2464+
* New code should use PQfreemem(). A macro will automatically map
2465+
* calls to PQfreemem. It should be removed in the future. bjm 2003-03-24
2466+
*/
2467+
2468+
#undef PQfreeNotify
2469+
void PQfreeNotify(PGnotify *notify);
2470+
2471+
void
2472+
PQfreeNotify(PGnotify *notify)
2473+
{
2474+
PQfreemem(notify);
2475+
}
2476+
2477+

src/interfaces/libpq/libpq-fe.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: libpq-fe.h,v 1.90 2003/03/22 03:29:06 momjian Exp $
10+
* $Id: libpq-fe.h,v 1.91 2003/03/25 02:44:36 momjian Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -260,7 +260,8 @@ extern void PQfreemem(void *ptr);
260260
/* Simple synchronous query */
261261
extern PGresult *PQexec(PGconn *conn, const char *query);
262262
extern PGnotify *PQnotifies(PGconn *conn);
263-
extern void PQfreeNotify(PGnotify *notify);
263+
/* Exists for backward compatibility. bjm 2003-03-24 */
264+
#define PQfreeNotify(ptr) PQfreemem(ptr)
264265

265266
/* Interface for multiple-result or asynchronous queries */
266267
extern int PQsendQuery(PGconn *conn, const char *query);

src/interfaces/python/pgmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2066,7 +2066,7 @@ pg_getnotify(pgobject * self, PyObject * args)
20662066
}
20672067

20682068
PyTuple_SET_ITEM(notify_result, 1, temp);
2069-
free(notify);
2069+
PQfreemem(notify);
20702070
}
20712071
else
20722072
{

src/test/examples/testlibpq2.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ main()
9898
fprintf(stderr,
9999
"ASYNC NOTIFY of '%s' from backend pid '%d' received\n",
100100
notify->relname, notify->be_pid);
101-
free(notify);
101+
PQfreemem(notify);
102102
break;
103103
}
104104
PQclear(res);

0 commit comments

Comments
 (0)