1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-30 11:03:19 +03:00

Fix up several contrib modules that were using varlena datatypes in not-so-obvious

ways.  I'm not totally sure that I caught everything, but at least now they pass
their regression tests with VARSIZE/SET_VARSIZE defined to reverse byte order.
This commit is contained in:
Tom Lane
2007-02-28 22:44:38 +00:00
parent d1ce4f7396
commit 9f652d430f
24 changed files with 152 additions and 147 deletions

View File

@ -2,6 +2,7 @@
#define __HSTORE_H__
#include "postgres.h"
#include "funcapi.h"
#include "access/gist.h"
#include "access/itup.h"
@ -23,12 +24,12 @@ typedef struct
typedef struct
{
int4 len;
int32 vl_len_; /* varlena header (do not touch directly!) */
int4 size;
char data[1];
} HStore;
#define HSHRDSIZE (2*sizeof(int4))
#define HSHRDSIZE (VARHDRSZ + sizeof(int4))
#define CALCDATASIZE(x, lenstr) ( (x) * sizeof(HEntry) + HSHRDSIZE + (lenstr) )
#define ARRPTR(x) ( (HEntry*) ( (char*)(x) + HSHRDSIZE ) )
#define STRPTR(x) ( (char*)(x) + HSHRDSIZE + ( sizeof(HEntry) * ((HStore*)x)->size ) )

View File

@ -2,7 +2,6 @@
#include "access/gist.h"
#include "access/itup.h"
/*#include "access/rtree.h"*/
#include "crc32.h"
/* bigint defines */
@ -38,7 +37,7 @@ typedef char *BITVECP;
typedef struct
{
int4 len;
int32 vl_len_; /* varlena header (do not touch directly!) */
int4 flag;
char data[1];
} GISTTYPE;
@ -47,7 +46,7 @@ typedef struct
#define ISALLTRUE(x) ( ((GISTTYPE*)x)->flag & ALLISTRUE )
#define GTHDRSIZE ( sizeof(int4)*2 )
#define GTHDRSIZE (VARHDRSZ + sizeof(int4))
#define CALCGTSIZE(flag) ( GTHDRSIZE+(((flag) & ALLISTRUE) ? 0 : SIGLEN) )
#define GETSIGN(x) ( (BITVECP)( (char*)x+GTHDRSIZE ) )
@ -112,14 +111,13 @@ ghstore_compress(PG_FUNCTION_ARGS)
if (entry->leafkey)
{
GISTTYPE *res = (GISTTYPE *) palloc(CALCGTSIZE(0));
GISTTYPE *res = (GISTTYPE *) palloc0(CALCGTSIZE(0));
HStore *toastedval = (HStore *) DatumGetPointer(entry->key);
HStore *val = (HStore *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
HEntry *ptr = ARRPTR(val);
char *words = STRPTR(val);
memset(res, 0, CALCGTSIZE(0));
res->len = CALCGTSIZE(0);
SET_VARSIZE(res, CALCGTSIZE(0));
while (ptr - ARRPTR(val) < val->size)
{
@ -156,7 +154,7 @@ ghstore_compress(PG_FUNCTION_ARGS)
);
res = (GISTTYPE *) palloc(CALCGTSIZE(ALLISTRUE));
res->len = CALCGTSIZE(ALLISTRUE);
SET_VARSIZE(res, CALCGTSIZE(ALLISTRUE));
res->flag = ALLISTRUE;
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
@ -286,10 +284,11 @@ ghstore_union(PG_FUNCTION_ARGS)
len = CALCGTSIZE(flag);
result = (GISTTYPE *) palloc(len);
*size = result->len = len;
SET_VARSIZE(result, len);
result->flag = flag;
if (!ISALLTRUE(result))
memcpy((void *) GETSIGN(result), (void *) base, sizeof(BITVEC));
*size = len;
PG_RETURN_POINTER(result);
}
@ -383,13 +382,13 @@ ghstore_picksplit(PG_FUNCTION_ARGS)
if (ISALLTRUE(GETENTRY(entryvec, seed_1)))
{
datum_l = (GISTTYPE *) palloc(GTHDRSIZE);
datum_l->len = GTHDRSIZE;
SET_VARSIZE(datum_l, GTHDRSIZE);
datum_l->flag = ALLISTRUE;
}
else
{
datum_l = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
datum_l->len = GTHDRSIZE + SIGLEN;
SET_VARSIZE(datum_l, GTHDRSIZE + SIGLEN);
datum_l->flag = 0;
memcpy((void *) GETSIGN(datum_l), (void *) GETSIGN(GETENTRY(entryvec, seed_1)), sizeof(BITVEC))
;
@ -397,13 +396,13 @@ ghstore_picksplit(PG_FUNCTION_ARGS)
if (ISALLTRUE(GETENTRY(entryvec, seed_2)))
{
datum_r = (GISTTYPE *) palloc(GTHDRSIZE);
datum_r->len = GTHDRSIZE;
SET_VARSIZE(datum_r, GTHDRSIZE);
datum_r->flag = ALLISTRUE;
}
else
{
datum_r = (GISTTYPE *) palloc(GTHDRSIZE + SIGLEN);
datum_r->len = GTHDRSIZE + SIGLEN;
SET_VARSIZE(datum_r, GTHDRSIZE + SIGLEN);
datum_r->flag = 0;
memcpy((void *) GETSIGN(datum_r), (void *) GETSIGN(GETENTRY(entryvec, seed_2)), sizeof(BITVEC));
}

View File

@ -363,7 +363,7 @@ hstore_in(PG_FUNCTION_ARGS)
freeHSParse(&state);
len = CALCDATASIZE(0, 0);
out = palloc(len);
out->len = len;
SET_VARSIZE(out, len);
out->size = 0;
PG_RETURN_POINTER(out);
}
@ -372,7 +372,7 @@ hstore_in(PG_FUNCTION_ARGS)
len = CALCDATASIZE(state.pcur, buflen);
out = palloc(len);
out->len = len;
SET_VARSIZE(out, len);
out->size = state.pcur;
entries = ARRPTR(out);
@ -436,7 +436,7 @@ hstore_out(PG_FUNCTION_ARGS)
}
buflen = (4 /* " */ + 2 /* => */ + 2 /* , */ ) * in->size +
2 /* esc */ * (in->len - CALCDATASIZE(in->size, 0));
2 /* esc */ * (VARSIZE(in) - CALCDATASIZE(in->size, 0));
out = ptr = palloc(buflen);
for (i = 0; i < in->size; i++)

View File

@ -105,13 +105,13 @@ delete(PG_FUNCTION_ARGS)
{
HStore *hs = PG_GETARG_HS(0);
text *key = PG_GETARG_TEXT_P(1);
HStore *out = palloc(hs->len);
HStore *out = palloc(VARSIZE(hs));
char *ptrs,
*ptrd;
HEntry *es,
*ed;
out->len = hs->len;
SET_VARSIZE(out, VARSIZE(hs));
out->size = hs->size; /* temporary! */
ptrs = STRPTR(hs);
@ -142,7 +142,7 @@ delete(PG_FUNCTION_ARGS)
out->size = ed - ARRPTR(out);
memmove(STRPTR(out), ptrd, buflen);
out->len = CALCDATASIZE(out->size, buflen);
SET_VARSIZE(out, CALCDATASIZE(out->size, buflen));
}
@ -159,7 +159,7 @@ hs_concat(PG_FUNCTION_ARGS)
{
HStore *s1 = PG_GETARG_HS(0);
HStore *s2 = PG_GETARG_HS(1);
HStore *out = palloc(s1->len + s2->len);
HStore *out = palloc(VARSIZE(s1) + VARSIZE(s2));
char *ps1,
*ps2,
*pd;
@ -167,7 +167,7 @@ hs_concat(PG_FUNCTION_ARGS)
*es2,
*ed;
out->len = s1->len + s2->len;
SET_VARSIZE(out, VARSIZE(s1) + VARSIZE(s2));
out->size = s1->size + s2->size;
ps1 = STRPTR(s1);
@ -256,7 +256,7 @@ hs_concat(PG_FUNCTION_ARGS)
out->size = ed - ARRPTR(out);
memmove(STRPTR(out), pd, buflen);
out->len = CALCDATASIZE(out->size, buflen);
SET_VARSIZE(out, CALCDATASIZE(out->size, buflen));
}
PG_FREE_IF_COPY(s1, 0);
@ -277,7 +277,7 @@ tconvert(PG_FUNCTION_ARGS)
len = CALCDATASIZE(1, VARSIZE(key) + VARSIZE(val) - 2 * VARHDRSZ);
out = palloc(len);
out->len = len;
SET_VARSIZE(out, len);
out->size = 1;
ARRPTR(out)->keylen = VARSIZE(key) - VARHDRSZ;
@ -399,8 +399,8 @@ setup_firstcall(FuncCallContext *funcctx, HStore * hs)
st = (AKStore *) palloc(sizeof(AKStore));
st->i = 0;
st->hs = (HStore *) palloc(hs->len);
memcpy(st->hs, hs, hs->len);
st->hs = (HStore *) palloc(VARSIZE(hs));
memcpy(st->hs, hs, VARSIZE(hs));
funcctx->user_fctx = (void *) st;
MemoryContextSwitchTo(oldcontext);
@ -568,8 +568,8 @@ each(PG_FUNCTION_ARGS)
oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
st = (AKStore *) palloc(sizeof(AKStore));
st->i = 0;
st->hs = (HStore *) palloc(hs->len);
memcpy(st->hs, hs, hs->len);
st->hs = (HStore *) palloc(VARSIZE(hs));
memcpy(st->hs, hs, VARSIZE(hs));
funcctx->user_fctx = (void *) st;
tupdesc = RelationNameGetTupleDesc("hs_each");