Skip to content

Commit ed9ca68

Browse files
committed
Convert inet-related functions to new fmgr style. I have also taken it
on myself to do something about the non-self-consistency of the inet comparison functions. The results are probably still semantically wrong (inet and cidr should have different comparison semantics, I think) but at least the boolean operators now agree with each other and with the sort order of indexes on inet/cidr.
1 parent 61aca81 commit ed9ca68

File tree

6 files changed

+292
-281
lines changed

6 files changed

+292
-281
lines changed

src/backend/utils/adt/mac.c

Lines changed: 76 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
/*
22
* PostgreSQL type definitions for MAC addresses.
33
*
4-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/mac.c,v 1.16 2000/07/06 05:48:11 tgl Exp $
4+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/mac.c,v 1.17 2000/08/03 23:07:46 tgl Exp $
55
*/
66

77
#include "postgres.h"
88

99
#include "utils/builtins.h"
10-
10+
#include "utils/inet.h"
1111

1212
/*
13-
* macaddr is a pass-by-reference datatype.
13+
* XXX this table of manufacturers is long out of date, and should never
14+
* have been wired into the code in the first place.
1415
*/
15-
#define PG_GETARG_MACADDR_P(n) ((macaddr *) PG_GETARG_POINTER(n))
16-
#define PG_RETURN_MACADDR_P(x) return PointerGetDatum(x)
17-
1816

1917
typedef struct manufacturer
2018
{
@@ -145,18 +143,18 @@ static manufacturer manufacturers[] = {
145143
*/
146144

147145
#define hibits(addr) \
148-
((unsigned long)((addr->a<<16)|(addr->b<<8)|(addr->c)))
146+
((unsigned long)(((addr)->a<<16)|((addr)->b<<8)|((addr)->c)))
149147

150148
#define lobits(addr) \
151-
((unsigned long)((addr->d<<16)|(addr->e<<8)|(addr->f)))
149+
((unsigned long)(((addr)->d<<16)|((addr)->e<<8)|((addr)->f)))
152150

153151
/*
154152
* MAC address reader. Accepts several common notations.
155153
*/
156-
157-
macaddr *
158-
macaddr_in(char *str)
154+
Datum
155+
macaddr_in(PG_FUNCTION_ARGS)
159156
{
157+
char *str = PG_GETARG_CSTRING(0);
160158
int a,
161159
b,
162160
c,
@@ -201,21 +199,18 @@ macaddr_in(char *str)
201199
result->e = e;
202200
result->f = f;
203201

204-
return (result);
202+
PG_RETURN_MACADDR_P(result);
205203
}
206204

207205
/*
208206
* MAC address output function. Fixed format.
209207
*/
210-
211-
char *
212-
macaddr_out(macaddr *addr)
208+
Datum
209+
macaddr_out(PG_FUNCTION_ARGS)
213210
{
211+
macaddr *addr = PG_GETARG_MACADDR_P(0);
214212
char *result;
215213

216-
if (addr == NULL)
217-
return (NULL);
218-
219214
result = (char *) palloc(32);
220215

221216
if ((hibits(addr) > 0) || (lobits(addr) > 0))
@@ -225,84 +220,95 @@ macaddr_out(macaddr *addr)
225220
}
226221
else
227222
{
228-
result[0] = 0; /* special case for missing address */
223+
result[0] = '\0'; /* special case for missing address */
229224
}
230-
return (result);
225+
226+
PG_RETURN_CSTRING(result);
231227
}
232228

233229
/*
234-
* Boolean tests.
230+
* Comparison function for sorting:
235231
*/
236232

237-
bool
238-
macaddr_lt(macaddr *a1, macaddr *a2)
233+
static int32
234+
macaddr_cmp_internal(macaddr *a1, macaddr *a2)
239235
{
240-
if (!PointerIsValid(a1) || !PointerIsValid(a2))
241-
return FALSE;
242-
return ((hibits(a1) < hibits(a2)) ||
243-
((hibits(a1) == hibits(a2)) && lobits(a1) < lobits(a2)));
236+
if (hibits(a1) < hibits(a2))
237+
return -1;
238+
else if (hibits(a1) > hibits(a2))
239+
return 1;
240+
else if (lobits(a1) < lobits(a2))
241+
return -1;
242+
else if (lobits(a1) > lobits(a2))
243+
return 1;
244+
else
245+
return 0;
244246
}
245247

246-
bool
247-
macaddr_le(macaddr *a1, macaddr *a2)
248+
Datum
249+
macaddr_cmp(PG_FUNCTION_ARGS)
248250
{
249-
if (!PointerIsValid(a1) || !PointerIsValid(a2))
250-
return FALSE;
251-
return ((hibits(a1) < hibits(a2)) ||
252-
((hibits(a1) == hibits(a2)) && lobits(a1) <= lobits(a2)));
251+
macaddr *a1 = PG_GETARG_MACADDR_P(0);
252+
macaddr *a2 = PG_GETARG_MACADDR_P(1);
253+
254+
PG_RETURN_INT32(macaddr_cmp_internal(a1, a2));
253255
}
254256

255-
bool
256-
macaddr_eq(macaddr *a1, macaddr *a2)
257+
/*
258+
* Boolean comparisons.
259+
*/
260+
Datum
261+
macaddr_lt(PG_FUNCTION_ARGS)
257262
{
258-
if (!PointerIsValid(a1) || !PointerIsValid(a2))
259-
return FALSE;
260-
return ((hibits(a1) == hibits(a2)) && (lobits(a1) == lobits(a2)));
263+
macaddr *a1 = PG_GETARG_MACADDR_P(0);
264+
macaddr *a2 = PG_GETARG_MACADDR_P(1);
265+
266+
PG_RETURN_BOOL(macaddr_cmp_internal(a1, a2) < 0);
261267
}
262268

263-
bool
264-
macaddr_ge(macaddr *a1, macaddr *a2)
269+
Datum
270+
macaddr_le(PG_FUNCTION_ARGS)
265271
{
266-
if (!PointerIsValid(a1) || !PointerIsValid(a2))
267-
return FALSE;
268-
return ((hibits(a1) > hibits(a2)) ||
269-
((hibits(a1) == hibits(a2)) && lobits(a1) >= lobits(a2)));
272+
macaddr *a1 = PG_GETARG_MACADDR_P(0);
273+
macaddr *a2 = PG_GETARG_MACADDR_P(1);
274+
275+
PG_RETURN_BOOL(macaddr_cmp_internal(a1, a2) <= 0);
270276
}
271277

272-
bool
273-
macaddr_gt(macaddr *a1, macaddr *a2)
278+
Datum
279+
macaddr_eq(PG_FUNCTION_ARGS)
274280
{
275-
if (!PointerIsValid(a1) || !PointerIsValid(a2))
276-
return FALSE;
277-
return ((hibits(a1) > hibits(a2)) ||
278-
((hibits(a1) == hibits(a2)) && lobits(a1) > lobits(a2)));
281+
macaddr *a1 = PG_GETARG_MACADDR_P(0);
282+
macaddr *a2 = PG_GETARG_MACADDR_P(1);
283+
284+
PG_RETURN_BOOL(macaddr_cmp_internal(a1, a2) == 0);
279285
}
280286

281-
bool
282-
macaddr_ne(macaddr *a1, macaddr *a2)
287+
Datum
288+
macaddr_ge(PG_FUNCTION_ARGS)
283289
{
284-
if (!PointerIsValid(a1) || !PointerIsValid(a2))
285-
return FALSE;
286-
return ((hibits(a1) != hibits(a2)) || (lobits(a1) != lobits(a2)));
290+
macaddr *a1 = PG_GETARG_MACADDR_P(0);
291+
macaddr *a2 = PG_GETARG_MACADDR_P(1);
292+
293+
PG_RETURN_BOOL(macaddr_cmp_internal(a1, a2) >= 0);
287294
}
288295

289-
/*
290-
* Comparison function for sorting:
291-
*/
296+
Datum
297+
macaddr_gt(PG_FUNCTION_ARGS)
298+
{
299+
macaddr *a1 = PG_GETARG_MACADDR_P(0);
300+
macaddr *a2 = PG_GETARG_MACADDR_P(1);
301+
302+
PG_RETURN_BOOL(macaddr_cmp_internal(a1, a2) > 0);
303+
}
292304

293-
int4
294-
macaddr_cmp(macaddr *a1, macaddr *a2)
305+
Datum
306+
macaddr_ne(PG_FUNCTION_ARGS)
295307
{
296-
if (hibits(a1) < hibits(a2))
297-
return -1;
298-
else if (hibits(a1) > hibits(a2))
299-
return 1;
300-
else if (lobits(a1) < lobits(a2))
301-
return -1;
302-
else if (lobits(a1) > lobits(a2))
303-
return 1;
304-
else
305-
return 0;
308+
macaddr *a1 = PG_GETARG_MACADDR_P(0);
309+
macaddr *a2 = PG_GETARG_MACADDR_P(1);
310+
311+
PG_RETURN_BOOL(macaddr_cmp_internal(a1, a2) != 0);
306312
}
307313

308314
/*

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