Skip to content

Commit dc192a5

Browse files
committed
Convert earthdistance's only C function to v1 call convention,
to future-proof it against pass-by-value float8.
1 parent 7c81d79 commit dc192a5

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

contrib/earthdistance/earthdistance.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/* $PostgreSQL: pgsql/contrib/earthdistance/earthdistance.c,v 1.13 2006/10/19 20:08:03 tgl Exp $ */
1+
/* $PostgreSQL: pgsql/contrib/earthdistance/earthdistance.c,v 1.14 2008/04/20 01:05:52 tgl Exp $ */
22

33
#include "postgres.h"
44

55
#include <math.h>
66

7-
#include "utils/geo_decls.h" /* for Pt */
7+
#include "utils/geo_decls.h" /* for Point */
88

99
#ifndef M_PI
1010
#define M_PI 3.14159265358979323846
@@ -14,10 +14,12 @@
1414
PG_MODULE_MAGIC;
1515

1616
/* Earth's radius is in statute miles. */
17-
const double EARTH_RADIUS = 3958.747716;
18-
const double TWO_PI = 2.0 * M_PI;
17+
static const double EARTH_RADIUS = 3958.747716;
18+
static const double TWO_PI = 2.0 * M_PI;
1919

20-
double *geo_distance(Point *pt1, Point *pt2);
20+
PG_FUNCTION_INFO_V1(geo_distance);
21+
22+
Datum geo_distance(PG_FUNCTION_ARGS);
2123

2224

2325
/******************************************************
@@ -45,21 +47,22 @@ degtorad(double degrees)
4547
* x-coordinate is longitude in degrees west of Greenwich
4648
* y-coordinate is latitude in degrees above equator
4749
*
48-
* returns: double
50+
* returns: float8
4951
* distance between the points in miles on earth's surface
5052
******************************************************/
5153

52-
double *
53-
geo_distance(Point *pt1, Point *pt2)
54+
Datum
55+
geo_distance(PG_FUNCTION_ARGS)
5456
{
55-
57+
Point *pt1 = PG_GETARG_POINT_P(0);
58+
Point *pt2 = PG_GETARG_POINT_P(1);
59+
float8 result;
5660
double long1,
5761
lat1,
5862
long2,
5963
lat2;
6064
double longdiff;
6165
double sino;
62-
double *resultp = palloc(sizeof(double));
6366

6467
/* convert degrees to radians */
6568

@@ -78,7 +81,7 @@ geo_distance(Point *pt1, Point *pt2)
7881
cos(lat1) * cos(lat2) * sin(longdiff / 2.) * sin(longdiff / 2.));
7982
if (sino > 1.)
8083
sino = 1.;
81-
*resultp = 2. * EARTH_RADIUS * asin(sino);
84+
result = 2. * EARTH_RADIUS * asin(sino);
8285

83-
return resultp;
86+
PG_RETURN_FLOAT8(result);
8487
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy