Skip to content

Commit 234a02b

Browse files
committed
Replace direct assignments to VARATT_SIZEP(x) with SET_VARSIZE(x, len).
Get rid of VARATT_SIZE and VARATT_DATA, which were simply redundant with VARSIZE and VARDATA, and as a consequence almost no code was using the longer names. Rename the length fields of struct varlena and various derived structures to catch anyplace that was accessing them directly; and clean up various places so caught. In itself this patch doesn't change any behavior at all, but it is necessary infrastructure if we hope to play any games with the representation of varlena headers. Greg Stark and Tom Lane
1 parent 0459b59 commit 234a02b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+483
-498
lines changed

contrib/btree_gist/btree_bit.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ gbt_bit_xfrm(bytea *leaf)
7070
int s = INTALIGN(VARBITBYTES(leaf) + VARHDRSZ);
7171

7272
out = palloc(s);
73-
VARATT_SIZEP(out) = s;
73+
SET_VARSIZE(out, s);
7474
memcpy((void *) VARDATA(out), (void *) VARBITS(leaf), VARBITBYTES(leaf));
7575
return out;
7676
}

contrib/btree_gist/btree_utils_var.c

+15-20
Original file line numberDiff line numberDiff line change
@@ -51,42 +51,34 @@ gbt_var_key_readable(const GBT_VARKEY * k)
5151
GBT_VARKEY *
5252
gbt_var_key_copy(const GBT_VARKEY_R * u, bool force_node)
5353
{
54-
5554
GBT_VARKEY *r = NULL;
5655

5756
if (u->lower == u->upper && !force_node)
5857
{ /* leaf key mode */
59-
6058
r = (GBT_VARKEY *) palloc(VARSIZE(u->lower) + VARHDRSZ);
61-
memcpy((void *) VARDATA(r), (void *) u->lower, VARSIZE(u->lower));
62-
r->vl_len = VARSIZE(u->lower) + VARHDRSZ;
63-
59+
memcpy(VARDATA(r), u->lower, VARSIZE(u->lower));
60+
SET_VARSIZE(r, VARSIZE(u->lower) + VARHDRSZ);
6461
}
6562
else
6663
{ /* node key mode */
67-
6864
r = (GBT_VARKEY *) palloc(INTALIGN(VARSIZE(u->lower)) + VARSIZE(u->upper) + VARHDRSZ);
69-
memcpy((void *) VARDATA(r), (void *) u->lower, VARSIZE(u->lower));
70-
memcpy((void *) &(((char *) r)[VARHDRSZ + INTALIGN(VARSIZE(u->lower))]), (void *) u->upper, VARSIZE(u->upper));
71-
r->vl_len = INTALIGN(VARSIZE(u->lower)) + VARSIZE(u->upper) + VARHDRSZ;
72-
65+
memcpy(VARDATA(r), u->lower, VARSIZE(u->lower));
66+
memcpy(VARDATA(r) + INTALIGN(VARSIZE(u->lower)), u->upper, VARSIZE(u->upper));
67+
SET_VARSIZE(r, INTALIGN(VARSIZE(u->lower)) + VARSIZE(u->upper) + VARHDRSZ);
7368
}
7469
return r;
75-
7670
}
7771

7872

7973
static GBT_VARKEY *
8074
gbt_var_leaf2node(GBT_VARKEY * leaf, const gbtree_vinfo * tinfo)
8175
{
82-
8376
GBT_VARKEY *out = leaf;
8477

8578
if (tinfo->f_l2n)
8679
out = (*tinfo->f_l2n) (leaf);
8780

8881
return out;
89-
9082
}
9183

9284

@@ -212,19 +204,22 @@ gbt_var_node_truncate(const GBT_VARKEY * node, int32 cpf_length, const gbtree_vi
212204
GBT_VARKEY_R r = gbt_var_key_readable(node);
213205
int32 len1 = VARSIZE(r.lower) - VARHDRSZ;
214206
int32 len2 = VARSIZE(r.upper) - VARHDRSZ;
215-
int32 si = 0;
207+
int32 si;
208+
char *out2;
216209

217210
len1 = Min(len1, (cpf_length + 1));
218211
len2 = Min(len2, (cpf_length + 1));
219212

220-
si = 2 * VARHDRSZ + INTALIGN(VARHDRSZ + len1) + len2;
213+
si = 2 * VARHDRSZ + INTALIGN(len1 + VARHDRSZ) + len2;
221214
out = (GBT_VARKEY *) palloc(si);
222-
out->vl_len = si;
223-
memcpy((void *) &(((char *) out)[VARHDRSZ]), (void *) r.lower, len1 + VARHDRSZ);
224-
memcpy((void *) &(((char *) out)[VARHDRSZ + INTALIGN(VARHDRSZ + len1)]), (void *) r.upper, len2 + VARHDRSZ);
215+
SET_VARSIZE(out, si);
216+
217+
memcpy(VARDATA(out), r.lower, len1 + VARHDRSZ);
218+
SET_VARSIZE(VARDATA(out), len1 + VARHDRSZ);
225219

226-
*((int32 *) &(((char *) out)[VARHDRSZ])) = len1 + VARHDRSZ;
227-
*((int32 *) &(((char *) out)[VARHDRSZ + INTALIGN(VARHDRSZ + len1)])) = len2 + VARHDRSZ;
220+
out2 = VARDATA(out) + INTALIGN(len1 + VARHDRSZ);
221+
memcpy(out2, r.upper, len2 + VARHDRSZ);
222+
SET_VARSIZE(out2, len2 + VARHDRSZ);
228223

229224
return out;
230225
}

contrib/chkpass/chkpass.c

+14-12
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* darcy@druid.net
55
* http://www.druid.net/darcy/
66
*
7-
* $PostgreSQL: pgsql/contrib/chkpass/chkpass.c,v 1.18 2007/02/07 00:52:35 petere Exp $
7+
* $PostgreSQL: pgsql/contrib/chkpass/chkpass.c,v 1.19 2007/02/27 23:48:05 tgl Exp $
88
* best viewed with tabs set to 4
99
*/
1010

@@ -125,10 +125,12 @@ chkpass_rout(PG_FUNCTION_ARGS)
125125
{
126126
chkpass *password = (chkpass *) PG_GETARG_POINTER(0);
127127
text *result;
128+
int slen;
128129

129-
result = (text *) palloc(VARHDRSZ + 16);
130-
result->vl_len = VARHDRSZ + strlen(password->password);
131-
memcpy(result->vl_dat, password->password, strlen(password->password));
130+
slen = strlen(password->password);
131+
result = (text *) palloc(VARHDRSZ + slen);
132+
SET_VARSIZE(result, VARHDRSZ + slen);
133+
memcpy(VARDATA(result), password->password, slen);
132134

133135
PG_RETURN_TEXT_P(result);
134136
}
@@ -145,11 +147,11 @@ chkpass_eq(PG_FUNCTION_ARGS)
145147
chkpass *a1 = (chkpass *) PG_GETARG_POINTER(0);
146148
text *a2 = (text *) PG_GETARG_TEXT_P(1);
147149
char str[10];
148-
int sz = 8;
150+
int sz;
149151

150-
if (a2->vl_len < 12)
151-
sz = a2->vl_len - 4;
152-
strlcpy(str, a2->vl_dat, sz + 1);
152+
sz = Min(VARSIZE(a2) - VARHDRSZ, 8);
153+
memcpy(str, VARDATA(a2), sz);
154+
str[sz] = '\0';
153155
PG_RETURN_BOOL(strcmp(a1->password, crypt(str, a1->password)) == 0);
154156
}
155157

@@ -160,10 +162,10 @@ chkpass_ne(PG_FUNCTION_ARGS)
160162
chkpass *a1 = (chkpass *) PG_GETARG_POINTER(0);
161163
text *a2 = (text *) PG_GETARG_TEXT_P(1);
162164
char str[10];
163-
int sz = 8;
165+
int sz;
164166

165-
if (a2->vl_len < 12)
166-
sz = a2->vl_len - 4;
167-
strlcpy(str, a2->vl_dat, sz + 1);
167+
sz = Min(VARSIZE(a2) - VARHDRSZ, 8);
168+
memcpy(str, VARDATA(a2), sz);
169+
str[sz] = '\0';
168170
PG_RETURN_BOOL(strcmp(a1->password, crypt(str, a1->password)) != 0);
169171
}

contrib/cube/cube.c

+27-39
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/******************************************************************************
2-
$PostgreSQL: pgsql/contrib/cube/cube.c,v 1.30 2006/10/04 00:29:44 momjian Exp $
2+
$PostgreSQL: pgsql/contrib/cube/cube.c,v 1.31 2007/02/27 23:48:05 tgl Exp $
33
44
This file contains routines that can be bound to a Postgres backend and
55
called by the backend in the process of processing queries. The calling
@@ -223,9 +223,8 @@ cube_a_f8_f8(PG_FUNCTION_ARGS)
223223
dll = ARRPTR(ll);
224224

225225
size = offsetof(NDBOX, x[0]) + sizeof(double) * 2 * dim;
226-
result = (NDBOX *) palloc(size);
227-
memset(result, 0, size);
228-
result->size = size;
226+
result = (NDBOX *) palloc0(size);
227+
SET_VARSIZE(result, size);
229228
result->dim = dim;
230229

231230
for (i = 0; i < dim; i++)
@@ -264,9 +263,8 @@ cube_a_f8(PG_FUNCTION_ARGS)
264263
dur = ARRPTR(ur);
265264

266265
size = offsetof(NDBOX, x[0]) + sizeof(double) * 2 * dim;
267-
result = (NDBOX *) palloc(size);
268-
memset(result, 0, size);
269-
result->size = size;
266+
result = (NDBOX *) palloc0(size);
267+
SET_VARSIZE(result, size);
270268
result->dim = dim;
271269

272270
for (i = 0; i < dim; i++)
@@ -303,9 +301,8 @@ cube_subset(PG_FUNCTION_ARGS)
303301

304302
dim = ARRNELEMS(idx);
305303
size = offsetof(NDBOX, x[0]) + sizeof(double) * 2 * dim;
306-
result = (NDBOX *) palloc(size);
307-
memset(result, 0, size);
308-
result->size = size;
304+
result = (NDBOX *) palloc0(size);
305+
SET_VARSIZE(result, size);
309306
result->dim = dim;
310307

311308
for (i = 0; i < dim; i++)
@@ -432,7 +429,7 @@ g_cube_union(PG_FUNCTION_ARGS)
432429
/*
433430
* sizep = sizeof(NDBOX); -- NDBOX has variable size
434431
*/
435-
*sizep = tmp->size;
432+
*sizep = VARSIZE(tmp);
436433

437434
for (i = 1; i < entryvec->n; i++)
438435
{
@@ -744,7 +741,7 @@ g_cube_binary_union(NDBOX * r1, NDBOX * r2, int *sizep)
744741
NDBOX *retval;
745742

746743
retval = cube_union_v0(r1, r2);
747-
*sizep = retval->size;
744+
*sizep = VARSIZE(retval);
748745

749746
return (retval);
750747
}
@@ -759,16 +756,14 @@ cube_union_v0(NDBOX * a, NDBOX * b)
759756

760757
if (a->dim >= b->dim)
761758
{
762-
result = palloc(a->size);
763-
memset(result, 0, a->size);
764-
result->size = a->size;
759+
result = palloc0(VARSIZE(a));
760+
SET_VARSIZE(result, VARSIZE(a));
765761
result->dim = a->dim;
766762
}
767763
else
768764
{
769-
result = palloc(b->size);
770-
memset(result, 0, b->size);
771-
result->size = b->size;
765+
result = palloc0(VARSIZE(b));
766+
SET_VARSIZE(result, VARSIZE(b));
772767
result->dim = b->dim;
773768
}
774769

@@ -834,16 +829,14 @@ cube_inter(PG_FUNCTION_ARGS)
834829

835830
if (a->dim >= b->dim)
836831
{
837-
result = palloc(a->size);
838-
memset(result, 0, a->size);
839-
result->size = a->size;
832+
result = palloc0(VARSIZE(a));
833+
SET_VARSIZE(result, VARSIZE(a));
840834
result->dim = a->dim;
841835
}
842836
else
843837
{
844-
result = palloc(b->size);
845-
memset(result, 0, b->size);
846-
result->size = b->size;
838+
result = palloc0(VARSIZE(b));
839+
SET_VARSIZE(result, VARSIZE(b));
847840
result->dim = b->dim;
848841
}
849842

@@ -1371,9 +1364,8 @@ cube_enlarge(PG_FUNCTION_ARGS)
13711364
if (a->dim > dim)
13721365
dim = a->dim;
13731366
size = offsetof(NDBOX, x[0]) + sizeof(double) * dim * 2;
1374-
result = (NDBOX *) palloc(size);
1375-
memset(result, 0, size);
1376-
result->size = size;
1367+
result = (NDBOX *) palloc0(size);
1368+
SET_VARSIZE(result, size);
13771369
result->dim = dim;
13781370
for (i = 0, j = dim, k = a->dim; i < a->dim; i++, j++, k++)
13791371
{
@@ -1411,9 +1403,8 @@ cube_f8(PG_FUNCTION_ARGS)
14111403
int size;
14121404

14131405
size = offsetof(NDBOX, x[0]) + sizeof(double) * 2;
1414-
result = (NDBOX *) palloc(size);
1415-
memset(result, 0, size);
1416-
result->size = size;
1406+
result = (NDBOX *) palloc0(size);
1407+
SET_VARSIZE(result, size);
14171408
result->dim = 1;
14181409
result->x[0] = PG_GETARG_FLOAT8(0);
14191410
result->x[1] = result->x[0];
@@ -1429,9 +1420,8 @@ cube_f8_f8(PG_FUNCTION_ARGS)
14291420
int size;
14301421

14311422
size = offsetof(NDBOX, x[0]) + sizeof(double) * 2;
1432-
result = (NDBOX *) palloc(size);
1433-
memset(result, 0, size);
1434-
result->size = size;
1423+
result = (NDBOX *) palloc0(size);
1424+
SET_VARSIZE(result, size);
14351425
result->dim = 1;
14361426
result->x[0] = PG_GETARG_FLOAT8(0);
14371427
result->x[1] = PG_GETARG_FLOAT8(1);
@@ -1454,9 +1444,8 @@ cube_c_f8(PG_FUNCTION_ARGS)
14541444
x = PG_GETARG_FLOAT8(1);
14551445

14561446
size = offsetof(NDBOX, x[0]) + sizeof(double) * (c->dim + 1) *2;
1457-
result = (NDBOX *) palloc(size);
1458-
memset(result, 0, size);
1459-
result->size = size;
1447+
result = (NDBOX *) palloc0(size);
1448+
SET_VARSIZE(result, size);
14601449
result->dim = c->dim + 1;
14611450
for (i = 0; i < c->dim; i++)
14621451
{
@@ -1485,9 +1474,8 @@ cube_c_f8_f8(PG_FUNCTION_ARGS)
14851474
x2 = PG_GETARG_FLOAT8(2);
14861475

14871476
size = offsetof(NDBOX, x[0]) + sizeof(double) * (c->dim + 1) *2;
1488-
result = (NDBOX *) palloc(size);
1489-
memset(result, 0, size);
1490-
result->size = size;
1477+
result = (NDBOX *) palloc0(size);
1478+
SET_VARSIZE(result, size);
14911479
result->dim = c->dim + 1;
14921480
for (i = 0; i < c->dim; i++)
14931481
{

contrib/cube/cubedata.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
/* $PostgreSQL: pgsql/contrib/cube/cubedata.h,v 1.7 2006/03/11 04:38:28 momjian Exp $ */
1+
/* $PostgreSQL: pgsql/contrib/cube/cubedata.h,v 1.8 2007/02/27 23:48:05 tgl Exp $ */
22

33
#define CUBE_MAX_DIM (100)
4+
45
typedef struct NDBOX
56
{
6-
unsigned int size; /* required to be a Postgres varlena type */
7+
int32 vl_len_; /* varlena header (do not touch directly!) */
78
unsigned int dim;
89
double x[1];
910
} NDBOX;

contrib/cube/cubeparse.y

+5-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
/* NdBox = [(lowerleft),(upperright)] */
33
/* [(xLL(1)...xLL(N)),(xUR(1)...xUR(n))] */
44

5-
/* $PostgreSQL: pgsql/contrib/cube/cubeparse.y,v 1.16 2006/03/11 04:38:28 momjian Exp $ */
5+
/* $PostgreSQL: pgsql/contrib/cube/cubeparse.y,v 1.17 2007/02/27 23:48:05 tgl Exp $ */
66

77
#define YYPARSE_PARAM result /* need this to pass a pointer (void *) to yyparse */
88
#define YYSTYPE char *
@@ -165,9 +165,8 @@ write_box(unsigned int dim, char *str1, char *str2)
165165
int i;
166166
int size = offsetof(NDBOX, x[0]) + sizeof(double) * dim * 2;
167167

168-
bp = palloc(size);
169-
memset(bp, 0, size);
170-
bp->size = size;
168+
bp = palloc0(size);
169+
SET_VARSIZE(bp, size);
171170
bp->dim = dim;
172171

173172
s = str1;
@@ -198,9 +197,8 @@ write_point_as_box(char *str, int dim)
198197

199198
size = offsetof(NDBOX, x[0]) + sizeof(double) * dim * 2;
200199

201-
bp = palloc(size);
202-
memset(bp, 0, size);
203-
bp->size = size;
200+
bp = palloc0(size);
201+
SET_VARSIZE(bp, size);
204202
bp->dim = dim;
205203

206204
i = 0;

contrib/fuzzystrmatch/dmetaphone.c

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* This is a port of the Double Metaphone algorithm for use in PostgreSQL.
33
*
4-
* $PostgreSQL: pgsql/contrib/fuzzystrmatch/dmetaphone.c,v 1.10 2006/09/22 21:39:56 tgl Exp $
4+
* $PostgreSQL: pgsql/contrib/fuzzystrmatch/dmetaphone.c,v 1.11 2007/02/27 23:48:05 tgl Exp $
55
*
66
* Double Metaphone computes 2 "sounds like" strings - a primary and an
77
* alternate. In most cases they are the same, but for foreign names
@@ -165,10 +165,9 @@ dmetaphone(PG_FUNCTION_ARGS)
165165
code = "";
166166
rsize = VARHDRSZ + strlen(code);
167167
result = (text *) palloc(rsize);
168-
memset(result, 0, rsize);
169168
rptr = VARDATA(result);
170-
memcpy(rptr, code, strlen(code));
171-
VARATT_SIZEP(result) = rsize;
169+
memcpy(rptr, code, rsize - VARHDRSZ);
170+
SET_VARSIZE(result, rsize);
172171
PG_RETURN_TEXT_P(result);
173172
}
174173

@@ -206,10 +205,9 @@ dmetaphone_alt(PG_FUNCTION_ARGS)
206205
code = "";
207206
rsize = VARHDRSZ + strlen(code);
208207
result = (text *) palloc(rsize);
209-
memset(result, 0, rsize);
210208
rptr = VARDATA(result);
211-
memcpy(rptr, code, strlen(code));
212-
VARATT_SIZEP(result) = rsize;
209+
memcpy(rptr, code, rsize - VARHDRSZ);
210+
SET_VARSIZE(result, rsize);
213211
PG_RETURN_TEXT_P(result);
214212
}
215213

0 commit comments

Comments
 (0)