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

Commit6edd2b4

Browse files
committed
Switch over to using our own qsort() all the time, as has been proposed
repeatedly. Now that we don't have to worry about memory leaks fromglibc's qsort, we can safely put CHECK_FOR_INTERRUPTS into the tuplesortcomparators, as was requested a couple months ago. Also, get rid ofnon-reentrancy and an extra level of function call in tuplesort.c byproviding a variant qsort_arg() API that passes an extra void * argumentthrough to the comparison routine. (We might want to use that in otherplaces too, I didn't look yet.)
1 parented80f57 commit6edd2b4

File tree

7 files changed

+248
-72
lines changed

7 files changed

+248
-72
lines changed

‎configure

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14951,21 +14951,6 @@ case $host_os in bsdi*|netbsd*)
1495114951
ac_cv_func_fseeko=yes
1495214952
esac
1495314953

14954-
# Solaris has a very slow qsort in certain cases, so we replace it:
14955-
# http://forum.sun.com/thread.jspa?forumID=4&threadID=7231
14956-
# Supposedly it is fixed in Solaris, but not sure which version, and
14957-
# no confirmed testing. 2005-12-16
14958-
if test "$PORTNAME" = "solaris"; then
14959-
case $LIBOBJS in
14960-
"qsort.$ac_objext" | \
14961-
*" qsort.$ac_objext" | \
14962-
"qsort.$ac_objext "* | \
14963-
*" qsort.$ac_objext "* ) ;;
14964-
*) LIBOBJS="$LIBOBJS qsort.$ac_objext" ;;
14965-
esac
14966-
14967-
fi
14968-
1496914954
# Win32 support
1497014955
if test "$PORTNAME" = "win32"; then
1497114956
case $LIBOBJS in

‎configure.in

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
dnl Process this file with autoconf to produce a configure script.
2-
dnl $PostgreSQL: pgsql/configure.in,v 1.478 2006/10/02 00:06:18 tgl Exp $
2+
dnl $PostgreSQL: pgsql/configure.in,v 1.479 2006/10/03 22:18:22 tgl Exp $
33
dnl
44
dnl Developers, please strive to achieve this order:
55
dnl
@@ -986,14 +986,6 @@ case $host_os in bsdi*|netbsd*)
986986
ac_cv_func_fseeko=yes
987987
esac
988988

989-
# Solaris has a very slow qsort in certain cases, so we replace it:
990-
# http://forum.sun.com/thread.jspa?forumID=4&threadID=7231
991-
# Supposedly it is fixed in Solaris, but not sure which version, and
992-
# no confirmed testing. 2005-12-16
993-
if test "$PORTNAME" = "solaris"; then
994-
AC_LIBOBJ(qsort)
995-
fi
996-
997989
# Win32 support
998990
if test "$PORTNAME" = "win32"; then
999991
AC_LIBOBJ(gettimeofday)

‎src/Makefile.global.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*-makefile-*-
2-
# $PostgreSQL: pgsql/src/Makefile.global.in,v 1.230 2006/09/19 15:36:07 tgl Exp $
2+
# $PostgreSQL: pgsql/src/Makefile.global.in,v 1.231 2006/10/03 22:18:22 tgl Exp $
33

44
#------------------------------------------------------------------------------
55
# All PostgreSQL makefiles include this file and use the variables it sets,
@@ -413,7 +413,7 @@ endif
413413
#
414414
# substitute implementations of C library routines (see src/port/)
415415

416-
LIBOBJS = @LIBOBJS@ copydir.o dirmod.o exec.o noblock.o path.o pipe.o pgsleep.o pgstrcasecmp.o sprompt.o thread.o
416+
LIBOBJS = @LIBOBJS@ copydir.o dirmod.o exec.o noblock.o path.o pipe.o pgsleep.o pgstrcasecmp.oqsort.o qsort_arg.osprompt.o thread.o
417417

418418
LIBS := -lpgport$(LIBS)
419419
# add location of libpgport.a to LDFLAGS

‎src/backend/utils/sort/tuplesort.c

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
* Portions Copyright (c) 1994, Regents of the University of California
9292
*
9393
* IDENTIFICATION
94-
* $PostgreSQL: pgsql/src/backend/utils/sort/tuplesort.c,v 1.68 2006/07/14 14:52:25 momjian Exp $
94+
* $PostgreSQL: pgsql/src/backend/utils/sort/tuplesort.c,v 1.69 2006/10/03 22:18:23 tgl Exp $
9595
*
9696
*-------------------------------------------------------------------------
9797
*/
@@ -201,11 +201,11 @@ struct Tuplesortstate
201201
* They are set up by the tuplesort_begin_xxx routines.
202202
*
203203
* Function to compare two tuples; result is per qsort() convention, ie:
204-
*
205-
*<0, 0, >0 according as a<b, a=b, a>b.
204+
* <0, 0, >0 according as a<b, a=b, a>b. The API must match
205+
*qsort_arg_comparator.
206206
*/
207-
int(*comparetup) (Tuplesortstate*state,
208-
constSortTuple*a,constSortTuple*b);
207+
int(*comparetup) (constSortTuple*a,constSortTuple*b,
208+
Tuplesortstate*state);
209209

210210
/*
211211
* Function to copy a supplied input tuple into palloc'd space and set up
@@ -345,7 +345,7 @@ struct Tuplesortstate
345345
#endif
346346
};
347347

348-
#defineCOMPARETUP(state,a,b)((*(state)->comparetup) (state,a, b))
348+
#defineCOMPARETUP(state,a,b)((*(state)->comparetup) (a, b, state))
349349
#defineCOPYTUP(state,stup,tup)((*(state)->copytup) (state, stup, tup))
350350
#defineWRITETUP(state,tape,stup)((*(state)->writetup) (state, tape, stup))
351351
#defineREADTUP(state,stup,tape,len) ((*(state)->readtup) (state, stup, tape, len))
@@ -410,37 +410,28 @@ static void tuplesort_heap_insert(Tuplesortstate *state, SortTuple *tuple,
410410
staticvoidtuplesort_heap_siftup(Tuplesortstate*state,boolcheckIndex);
411411
staticunsignedintgetlen(Tuplesortstate*state,inttapenum,booleofOK);
412412
staticvoidmarkrunend(Tuplesortstate*state,inttapenum);
413-
staticintqsort_comparetup(constvoid*a,constvoid*b);
414-
staticintcomparetup_heap(Tuplesortstate*state,
415-
constSortTuple*a,constSortTuple*b);
413+
staticintcomparetup_heap(constSortTuple*a,constSortTuple*b,
414+
Tuplesortstate*state);
416415
staticvoidcopytup_heap(Tuplesortstate*state,SortTuple*stup,void*tup);
417416
staticvoidwritetup_heap(Tuplesortstate*state,inttapenum,
418417
SortTuple*stup);
419418
staticvoidreadtup_heap(Tuplesortstate*state,SortTuple*stup,
420419
inttapenum,unsignedintlen);
421-
staticintcomparetup_index(Tuplesortstate*state,
422-
constSortTuple*a,constSortTuple*b);
420+
staticintcomparetup_index(constSortTuple*a,constSortTuple*b,
421+
Tuplesortstate*state);
423422
staticvoidcopytup_index(Tuplesortstate*state,SortTuple*stup,void*tup);
424423
staticvoidwritetup_index(Tuplesortstate*state,inttapenum,
425424
SortTuple*stup);
426425
staticvoidreadtup_index(Tuplesortstate*state,SortTuple*stup,
427426
inttapenum,unsignedintlen);
428-
staticintcomparetup_datum(Tuplesortstate*state,
429-
constSortTuple*a,constSortTuple*b);
427+
staticintcomparetup_datum(constSortTuple*a,constSortTuple*b,
428+
Tuplesortstate*state);
430429
staticvoidcopytup_datum(Tuplesortstate*state,SortTuple*stup,void*tup);
431430
staticvoidwritetup_datum(Tuplesortstate*state,inttapenum,
432431
SortTuple*stup);
433432
staticvoidreadtup_datum(Tuplesortstate*state,SortTuple*stup,
434433
inttapenum,unsignedintlen);
435434

436-
/*
437-
* Since qsort(3) will not pass any context info to qsort_comparetup(),
438-
* we have to use this ugly static variable. It is set to point to the
439-
* active Tuplesortstate object just before calling qsort.It should
440-
* not be used directly by anything except qsort_comparetup().
441-
*/
442-
staticTuplesortstate*qsort_tuplesortstate;
443-
444435

445436
/*
446437
*tuplesort_begin_xxx
@@ -930,11 +921,11 @@ tuplesort_performsort(Tuplesortstate *state)
930921
* amount of memory. Just qsort 'em and we're done.
931922
*/
932923
if (state->memtupcount>1)
933-
{
934-
qsort_tuplesortstate=state;
935-
qsort((void*)state->memtuples,state->memtupcount,
936-
sizeof(SortTuple),qsort_comparetup);
937-
}
924+
qsort_arg((void*)state->memtuples,
925+
state->memtupcount,
926+
sizeof(SortTuple),
927+
(qsort_arg_comparator)state->comparetup,
928+
(void*)state);
938929
state->current=0;
939930
state->eof_reached= false;
940931
state->markpos_offset=0;
@@ -1587,7 +1578,6 @@ mergeonerun(Tuplesortstate *state)
15871578
*/
15881579
while (state->memtupcount>0)
15891580
{
1590-
CHECK_FOR_INTERRUPTS();
15911581
/* write the tuple to destTape */
15921582
priorAvail=state->availMem;
15931583
srcTape=state->memtuples[0].tupindex;
@@ -2091,20 +2081,6 @@ markrunend(Tuplesortstate *state, int tapenum)
20912081
}
20922082

20932083

2094-
/*
2095-
* qsort interface
2096-
*/
2097-
2098-
staticint
2099-
qsort_comparetup(constvoid*a,constvoid*b)
2100-
{
2101-
/* The passed pointers are pointers to SortTuple ... */
2102-
returnCOMPARETUP(qsort_tuplesortstate,
2103-
(constSortTuple*)a,
2104-
(constSortTuple*)b);
2105-
}
2106-
2107-
21082084
/*
21092085
* This routine selects an appropriate sorting function to implement
21102086
* a sort operator as efficiently as possible.The straightforward
@@ -2319,7 +2295,7 @@ ApplySortFunction(FmgrInfo *sortFunction, SortFunctionKind kind,
23192295
*/
23202296

23212297
staticint
2322-
comparetup_heap(Tuplesortstate*state,constSortTuple*a,constSortTuple*b)
2298+
comparetup_heap(constSortTuple*a,constSortTuple*b,Tuplesortstate*state)
23232299
{
23242300
ScanKeyscanKey=state->scanKeys;
23252301
HeapTupleDataltup;
@@ -2328,6 +2304,9 @@ comparetup_heap(Tuplesortstate *state, const SortTuple *a, const SortTuple *b)
23282304
intnkey;
23292305
int32compare;
23302306

2307+
/* Allow interrupting long sorts */
2308+
CHECK_FOR_INTERRUPTS();
2309+
23312310
/* Compare the leading sort key */
23322311
compare=inlineApplySortFunction(&scanKey->sk_func,
23332312
state->sortFnKinds[0],
@@ -2449,7 +2428,7 @@ readtup_heap(Tuplesortstate *state, SortTuple *stup,
24492428
*/
24502429

24512430
staticint
2452-
comparetup_index(Tuplesortstate*state,constSortTuple*a,constSortTuple*b)
2431+
comparetup_index(constSortTuple*a,constSortTuple*b,Tuplesortstate*state)
24532432
{
24542433
/*
24552434
* This is similar to _bt_tuplecompare(), but we have already done the
@@ -2466,6 +2445,9 @@ comparetup_index(Tuplesortstate *state, const SortTuple *a, const SortTuple *b)
24662445
intnkey;
24672446
int32compare;
24682447

2448+
/* Allow interrupting long sorts */
2449+
CHECK_FOR_INTERRUPTS();
2450+
24692451
/* Compare the leading sort key */
24702452
compare=inlineApplySortFunction(&scanKey->sk_func,
24712453
SORTFUNC_CMP,
@@ -2622,8 +2604,11 @@ readtup_index(Tuplesortstate *state, SortTuple *stup,
26222604
*/
26232605

26242606
staticint
2625-
comparetup_datum(Tuplesortstate*state,constSortTuple*a,constSortTuple*b)
2607+
comparetup_datum(constSortTuple*a,constSortTuple*b,Tuplesortstate*state)
26262608
{
2609+
/* Allow interrupting long sorts */
2610+
CHECK_FOR_INTERRUPTS();
2611+
26272612
returninlineApplySortFunction(&state->sortOpFn,state->sortFnKind,
26282613
a->datum1,a->isnull1,
26292614
b->datum1,b->isnull1);

‎src/include/port.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
* Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $PostgreSQL: pgsql/src/include/port.h,v 1.102 2006/10/02 00:06:18 tgl Exp $
9+
* $PostgreSQL: pgsql/src/include/port.h,v 1.103 2006/10/03 22:18:23 tgl Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
13+
#ifndefPG_PORT_H
14+
#definePG_PORT_H
1315

1416
#include<ctype.h>
1517
#include<netdb.h>
@@ -361,3 +363,10 @@ extern int pqGethostbyname(const char *name,
361363
char*buffer,size_tbuflen,
362364
structhostent**result,
363365
int*herrno);
366+
367+
typedefint (*qsort_arg_comparator) (constvoid*a,constvoid*b,void*arg);
368+
369+
externvoidqsort_arg(void*base,size_tnel,size_telsize,
370+
qsort_arg_comparatorcmp,void*arg);
371+
372+
#endif/* PG_PORT_H */

‎src/port/qsort.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
/*
2+
*qsort.c: standard quicksort algorithm
3+
*
24
*Modifications from vanilla NetBSD source:
35
* Add do ... while() macro fix
46
* Remove __inline, _DIAGASSERTs, __P
57
* Remove ill-considered "swap_cnt" switch to insertion sort,
68
* in favor of a simple check for presorted input.
79
*
8-
*$PostgreSQL: pgsql/src/port/qsort.c,v 1.9 2006/03/21 19:49:15 tgl Exp $
10+
*CAUTION: if you change this file, see also qsort_arg.c
11+
*
12+
*$PostgreSQL: pgsql/src/port/qsort.c,v 1.10 2006/10/03 22:18:23 tgl Exp $
913
*/
1014

1115
/*$NetBSD: qsort.c,v 1.13 2003/08/07 16:43:42 agc Exp $*/

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp