Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbebe174

Browse files
committed
PL/Perl portability fix: avoid including XSUB.h in plperl.c.
In Perl builds that define PERL_IMPLICIT_SYS, XSUB.h defines macrosthat replace a whole lot of basic libc functions with Perl functions.We can't tolerate that in plperl.c; it breaks at least PG_TRY andprobably other stuff. The core idea of this patch is to include XSUB.honly in the .xs files where it's really needed, and to move any codebroken by PERL_IMPLICIT_SYS out of the .xs files and into plperl.c.The reason this hasn't been a problem before is that our build techniquesdid not result in PERL_IMPLICIT_SYS appearing as a #define in PL/Perl,even on some platforms where Perl thinks it is defined. That's about tochange in order to fix a nasty portability issue, so we need this work tomake the code safe for that.Rather unaccountably, the Perl people chose XSUB.h as the place to providethe versions of the aTHX/aTHX_ macros that are needed by code that's notexplicitly aware of the MULTIPLICITY API conventions. Hence, just removingXSUB.h from plperl.c fails miserably. But we can work around that bydefining PERL_NO_GET_CONTEXT (which would make the relevant stanza ofXSUB.h a no-op anyway). As explained in perlguts.pod, that means we needto add a "dTHX" macro call in every C function that calls a Perl APIfunction. In most of them we just add this at the top; but since themacro fetches the current Perl interpreter pointer, more care is neededin functions that switch the active interpreter. Lack of the macro iseasily recognized since it results in bleats about "my_perl" not beingdefined.(A nice side benefit of this is that it significantly reduces the numberof fetches of the current interpreter pointer. On my machine, plperl.sogets more than 10% smaller, and there's probably some performance win too.We could reduce the number of fetches still more by decorating the codewith pTHX_/aTHX_ macros to pass the interpreter pointer around, asexplained by perlguts.pod; but that's a task for another day.)Formatting note: pgindent seems happy to treat "dTHX;" as a declarationso long as it's the first thing after the left brace, as we'd alreadyobserved with respect to the similar macro "dSP;". If you try to putit later in a set of declarations, pgindent puts ugly extra spacearound it.Having removed XSUB.h from plperl.c, we need only move the supportfunctions for spi_return_next and util_elog (both of which use PG_TRY)out of the .xs files and into plperl.c. This seems sufficient toavoid the known problems caused by PERL_IMPLICIT_SYS, although wecould move more code if additional issues emerge.This will need to be back-patched, but first let's see what thebuildfarm makes of it.Patch by me, with some help from Ashutosh SharmaDiscussion:https://postgr.es/m/CANFyU97OVQ3+Mzfmt3MhuUm5NwPU=-FtbNH5Eb7nZL9ua8=rcA@mail.gmail.com
1 parent8d30407 commitbebe174

File tree

6 files changed

+210
-137
lines changed

6 files changed

+210
-137
lines changed

‎contrib/hstore_plperl/hstore_plperl.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ PG_FUNCTION_INFO_V1(hstore_to_plperl);
6767
Datum
6868
hstore_to_plperl(PG_FUNCTION_ARGS)
6969
{
70+
dTHX;
7071
HStore*in=PG_GETARG_HS(0);
7172
inti;
7273
intcount=HS_COUNT(in);
@@ -99,16 +100,15 @@ PG_FUNCTION_INFO_V1(plperl_to_hstore);
99100
Datum
100101
plperl_to_hstore(PG_FUNCTION_ARGS)
101102
{
102-
HV*hv;
103+
dTHX;
104+
HV*hv= (HV*)SvRV((SV*)PG_GETARG_POINTER(0));
103105
HE*he;
104106
int32buflen;
105107
int32i;
106108
int32pcount;
107109
HStore*out;
108110
Pairs*pairs;
109111

110-
hv= (HV*)SvRV((SV*)PG_GETARG_POINTER(0));
111-
112112
pcount=hv_iterinit(hv);
113113

114114
pairs=palloc(pcount*sizeof(Pairs));

‎src/pl/plperl/SPI.xs

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,44 +9,16 @@
99

1010
/* this must be first: */
1111
#include"postgres.h"
12-
#include"mb/pg_wchar.h"/* for GetDatabaseEncoding */
1312

1413
/* Defined by Perl */
1514
#undef _
1615

1716
/* perl stuff */
17+
#definePG_NEED_PERL_XSUB_H
1818
#include"plperl.h"
1919
#include"plperl_helpers.h"
2020

2121

22-
/*
23-
* Interface routine to catch ereports and punt them to Perl
24-
*/
25-
staticvoid
26-
do_plperl_return_next(SV*sv)
27-
{
28-
MemoryContextoldcontext=CurrentMemoryContext;
29-
30-
PG_TRY();
31-
{
32-
plperl_return_next(sv);
33-
}
34-
PG_CATCH();
35-
{
36-
ErrorData*edata;
37-
38-
/* Must reset elog.c's state */
39-
MemoryContextSwitchTo(oldcontext);
40-
edata=CopyErrorData();
41-
FlushErrorState();
42-
43-
/* Punt the error to Perl */
44-
croak_cstr(edata->message);
45-
}
46-
PG_END_TRY();
47-
}
48-
49-
5022
MODULE=PostgreSQL::InServer::SPIPREFIX=spi_
5123

5224
PROTOTYPES:ENABLE
@@ -76,7 +48,7 @@ void
7648
spi_return_next(rv)
7749
SV*rv;
7850
CODE:
79-
do_plperl_return_next(rv);
51+
plperl_return_next(rv);
8052

8153
SV*
8254
spi_spi_query(sv)

‎src/pl/plperl/Util.xs

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,53 +15,15 @@
1515
#include"fmgr.h"
1616
#include"utils/builtins.h"
1717
#include"utils/bytea.h"/* for byteain & byteaout */
18-
#include"mb/pg_wchar.h"/* for GetDatabaseEncoding */
18+
1919
/* Defined by Perl */
2020
#undef _
2121

2222
/* perl stuff */
23+
#definePG_NEED_PERL_XSUB_H
2324
#include"plperl.h"
2425
#include"plperl_helpers.h"
2526

26-
/*
27-
* Implementation of plperl's elog() function
28-
*
29-
* If the error level is less than ERROR, we'll just emit the message and
30-
* return. When it is ERROR, elog() will longjmp, which we catch and
31-
* turn into a Perl croak(). Note we are assuming that elog() can't have
32-
* any internal failures that are so bad as to require a transaction abort.
33-
*
34-
* This is out-of-line to suppress "might be clobbered by longjmp" warnings.
35-
*/
36-
staticvoid
37-
do_util_elog(intlevel,SV*msg)
38-
{
39-
MemoryContextoldcontext=CurrentMemoryContext;
40-
char*volatilecmsg=NULL;
41-
42-
PG_TRY();
43-
{
44-
cmsg=sv2cstr(msg);
45-
elog(level,"%s",cmsg);
46-
pfree(cmsg);
47-
}
48-
PG_CATCH();
49-
{
50-
ErrorData*edata;
51-
52-
/* Must reset elog.c's state */
53-
MemoryContextSwitchTo(oldcontext);
54-
edata=CopyErrorData();
55-
FlushErrorState();
56-
57-
if (cmsg)
58-
pfree(cmsg);
59-
60-
/* Punt the error to Perl */
61-
croak_cstr(edata->message);
62-
}
63-
PG_END_TRY();
64-
}
6527

6628
statictext*
6729
sv2text(SV*sv)
@@ -105,7 +67,7 @@ util_elog(level, msg)
10567
level=ERROR;
10668
if (level<DEBUG5)
10769
level=DEBUG5;
108-
do_util_elog(level,msg);
70+
plperl_util_elog(level,msg);
10971

11072
SV*
11173
util_quote_literal(sv)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp