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.
#define __HSTORE_H__
#include "postgres.h"
+
#include "funcapi.h"
#include "access/gist.h"
#include "access/itup.h"
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 ) )
#include "access/gist.h"
#include "access/itup.h"
-/*#include "access/rtree.h"*/
#include "crc32.h"
/* bigint defines */
typedef struct
{
- int4 len;
+ int32 vl_len_; /* varlena header (do not touch directly!) */
int4 flag;
char data[1];
} GISTTYPE;
#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 ) )
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)
{
);
res = (GISTTYPE *) palloc(CALCGTSIZE(ALLISTRUE));
- res->len = CALCGTSIZE(ALLISTRUE);
+ SET_VARSIZE(res, CALCGTSIZE(ALLISTRUE));
res->flag = ALLISTRUE;
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
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);
}
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))
;
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));
}
freeHSParse(&state);
len = CALCDATASIZE(0, 0);
out = palloc(len);
- out->len = len;
+ SET_VARSIZE(out, len);
out->size = 0;
PG_RETURN_POINTER(out);
}
len = CALCDATASIZE(state.pcur, buflen);
out = palloc(len);
- out->len = len;
+ SET_VARSIZE(out, len);
out->size = state.pcur;
entries = ARRPTR(out);
}
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++)
{
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);
out->size = ed - ARRPTR(out);
memmove(STRPTR(out), ptrd, buflen);
- out->len = CALCDATASIZE(out->size, buflen);
+ SET_VARSIZE(out, CALCDATASIZE(out->size, buflen));
}
{
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;
*es2,
*ed;
- out->len = s1->len + s2->len;
+ SET_VARSIZE(out, VARSIZE(s1) + VARSIZE(s2));
out->size = s1->size + s2->size;
ps1 = STRPTR(s1);
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);
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;
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);
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");
*/
typedef struct
{
- int4 len;
+ int32 vl_len_; /* varlena header (do not touch directly!) */
int4 flag;
char data[1];
} GISTTYPE;
#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 ) )
typedef struct
{
- int4 len;
+ int32 vl_len_; /* varlena header (do not touch directly!) */
int4 size;
char data[1];
} QUERYTYPE;
-#define HDRSIZEQT ( 2*sizeof(int4) )
+#define HDRSIZEQT (VARHDRSZ + sizeof(int4))
#define COMPUTESIZE(size) ( HDRSIZEQT + size * sizeof(ITEM) )
#define GETQUERY(x) (ITEM*)( (char*)(x)+HDRSIZEQT )
commonlen = COMPUTESIZE(state.num);
query = (QUERYTYPE *) palloc(commonlen);
- query->len = commonlen;
+ SET_VARSIZE(query, commonlen);
query->size = state.num;
ptr = GETQUERY(query);
ArrayType *in = (ArrayType *) PG_DETOAST_DATUM(entry->key);
int4 *ptr;
int num;
- GISTTYPE *res = (GISTTYPE *) palloc(CALCGTSIZE(0));
+ GISTTYPE *res = (GISTTYPE *) palloc0(CALCGTSIZE(0));
CHECKARRVALID(in);
if (ARRISVOID(in))
ptr = ARRPTR(in);
num = ARRNELEMS(in);
}
- memset(res, 0, CALCGTSIZE(0));
- res->len = CALCGTSIZE(0);
+ SET_VARSIZE(res, CALCGTSIZE(0));
while (num--)
{
);
res = (GISTTYPE *) palloc(CALCGTSIZE(ALLISTRUE));
- res->len = CALCGTSIZE(ALLISTRUE);
+ SET_VARSIZE(res, CALCGTSIZE(ALLISTRUE));
res->flag = ALLISTRUE;
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
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);
}
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));
}
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));
}
errmsg("array must not contain nulls")));
key = (ltree_gist *) palloc(len);
- key->len = len;
+ SET_VARSIZE(key, len);
key->flag = 0;
MemSet(LTG_SIGN(key), 0, ASIGLEN);
);
len = LTG_HDRSIZE;
key = (ltree_gist *) palloc(len);
- key->len = len;
+ SET_VARSIZE(key, len);
key->flag = LTG_ALLTRUE;
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
len = LTG_HDRSIZE + ((flag & LTG_ALLTRUE) ? 0 : ASIGLEN);
result = (ltree_gist *) palloc(len);
- *size = result->len = len;
+ SET_VARSIZE(result, len);
result->flag = flag;
if (!LTG_ISALLTRUE(result))
memcpy((void *) LTG_SIGN(result), (void *) base, sizeof(ABITVEC));
+ *size = len;
PG_RETURN_POINTER(result);
}
if (LTG_ISALLTRUE(GETENTRY(entryvec, seed_1)))
{
datum_l = (ltree_gist *) palloc(LTG_HDRSIZE);
- datum_l->len = LTG_HDRSIZE;
+ SET_VARSIZE(datum_l, LTG_HDRSIZE);
datum_l->flag = LTG_ALLTRUE;
}
else
{
datum_l = (ltree_gist *) palloc(LTG_HDRSIZE + ASIGLEN);
- datum_l->len = LTG_HDRSIZE + ASIGLEN;
+ SET_VARSIZE(datum_l, LTG_HDRSIZE + ASIGLEN);
datum_l->flag = 0;
memcpy((void *) LTG_SIGN(datum_l), (void *) LTG_SIGN(GETENTRY(entryvec, seed_1)), sizeof(ABITVEC));
}
if (LTG_ISALLTRUE(GETENTRY(entryvec, seed_2)))
{
datum_r = (ltree_gist *) palloc(LTG_HDRSIZE);
- datum_r->len = LTG_HDRSIZE;
+ SET_VARSIZE(datum_r, LTG_HDRSIZE);
datum_r->flag = LTG_ALLTRUE;
}
else
{
datum_r = (ltree_gist *) palloc(LTG_HDRSIZE + ASIGLEN);
- datum_r->len = LTG_HDRSIZE + ASIGLEN;
+ SET_VARSIZE(datum_r, LTG_HDRSIZE + ASIGLEN);
datum_r->flag = 0;
memcpy((void *) LTG_SIGN(datum_r), (void *) LTG_SIGN(GETENTRY(entryvec, seed_2)), sizeof(ABITVEC));
}
PG_RETURN_NULL();
}
- item = (ltree *) palloc(found->len);
- memcpy(item, found, found->len);
+ item = (ltree *) palloc(VARSIZE(found));
+ memcpy(item, found, VARSIZE(found));
PG_FREE_IF_COPY(la, 0);
PG_FREE_IF_COPY(query, 1);
PG_RETURN_NULL();
}
- item = (ltree *) palloc(found->len);
- memcpy(item, found, found->len);
+ item = (ltree *) palloc(VARSIZE(found));
+ memcpy(item, found, VARSIZE(found));
PG_FREE_IF_COPY(la, 0);
PG_FREE_IF_COPY(query, 1);
PG_RETURN_NULL();
}
- item = (ltree *) palloc(found->len);
- memcpy(item, found, found->len);
+ item = (ltree *) palloc(VARSIZE(found));
+ memcpy(item, found, VARSIZE(found));
PG_FREE_IF_COPY(la, 0);
PG_FREE_IF_COPY(query, 1);
PG_RETURN_NULL();
}
- item = (ltree *) palloc(found->len);
- memcpy(item, found, found->len);
+ item = (ltree *) palloc(VARSIZE(found));
+ memcpy(item, found, VARSIZE(found));
PG_FREE_IF_COPY(la, 0);
PG_FREE_IF_COPY(query, 1);
-/* $PostgreSQL: pgsql/contrib/ltree/ltree.h,v 1.17 2006/10/04 00:29:45 momjian Exp $ */
+/* $PostgreSQL: pgsql/contrib/ltree/ltree.h,v 1.18 2007/02/28 22:44:38 tgl Exp $ */
#ifndef __LTREE_H__
#define __LTREE_H__
typedef struct
{
- int32 len;
+ int32 vl_len_; /* varlena header (do not touch directly!) */
uint16 numlevel;
char data[1];
} ltree;
-#define LTREE_HDRSIZE MAXALIGN( sizeof(int32) + sizeof(uint16) )
+#define LTREE_HDRSIZE MAXALIGN(VARHDRSZ + sizeof(uint16))
#define LTREE_FIRST(x) ( (ltree_level*)( ((char*)(x))+LTREE_HDRSIZE ) )
typedef struct
{
- int32 len;
+ int32 vl_len_; /* varlena header (do not touch directly!) */
uint16 numlevel;
uint16 firstgood;
uint16 flag;
char data[1];
} lquery;
-#define LQUERY_HDRSIZE MAXALIGN( sizeof(int32) + 3*sizeof(uint16) )
+#define LQUERY_HDRSIZE MAXALIGN(VARHDRSZ + 3*sizeof(uint16))
#define LQUERY_FIRST(x) ( (lquery_level*)( ((char*)(x))+LQUERY_HDRSIZE ) )
#define LQUERY_HASNOT 0x01
*/
typedef struct
{
- int4 len;
+ int32 vl_len_; /* varlena header (do not touch directly!) */
int4 size;
char data[1];
} ltxtquery;
-#define HDRSIZEQT MAXALIGN( 2*sizeof(int4) )
+#define HDRSIZEQT MAXALIGN(VARHDRSZ + sizeof(int4))
#define COMPUTESIZE(size,lenofoperand) ( HDRSIZEQT + (size) * sizeof(ITEM) + (lenofoperand) )
#define GETQUERY(x) (ITEM*)( (char*)(x)+HDRSIZEQT )
#define GETOPERAND(x) ( (char*)GETQUERY(x) + ((ltxtquery*)x)->size * sizeof(ITEM) )
typedef struct
{
- int4 len;
+ int32 vl_len_; /* varlena header (do not touch directly!) */
uint32 flag;
char data[1];
} ltree_gist;
#define LTG_ALLTRUE 0x02
#define LTG_NORIGHT 0x04
-#define LTG_HDRSIZE MAXALIGN( sizeof(int4) + sizeof(uint32) )
+#define LTG_HDRSIZE MAXALIGN(VARHDRSZ + sizeof(uint32))
#define LTG_SIGN(x) ( (BITVECP)( ((char*)(x))+LTG_HDRSIZE ) )
#define LTG_NODE(x) ( (ltree*)( ((char*)(x))+LTG_HDRSIZE ) )
#define LTG_ISONENODE(x) ( ((ltree_gist*)(x))->flag & LTG_ONENODE )
#define LTG_ISALLTRUE(x) ( ((ltree_gist*)(x))->flag & LTG_ALLTRUE )
#define LTG_ISNORIGHT(x) ( ((ltree_gist*)(x))->flag & LTG_NORIGHT )
#define LTG_LNODE(x) ( (ltree*)( ( ((char*)(x))+LTG_HDRSIZE ) + ( LTG_ISALLTRUE(x) ? 0 : SIGLEN ) ) )
-#define LTG_RENODE(x) ( (ltree*)( ((char*)LTG_LNODE(x)) + LTG_LNODE(x)->len) )
+#define LTG_RENODE(x) ( (ltree*)( ((char*)LTG_LNODE(x)) + VARSIZE(LTG_LNODE(x))) )
#define LTG_RNODE(x) ( LTG_ISNORIGHT(x) ? LTG_LNODE(x) : LTG_RENODE(x) )
#define LTG_GETLNODE(x) ( LTG_ISONENODE(x) ? LTG_NODE(x) : LTG_LNODE(x) )
/*
* GiST support for ltree
* Teodor Sigaev <teodor@stack.net>
- * $PostgreSQL: pgsql/contrib/ltree/ltree_gist.c,v 1.19 2006/10/04 00:29:45 momjian Exp $
+ * $PostgreSQL: pgsql/contrib/ltree/ltree_gist.c,v 1.20 2007/02/28 22:44:38 tgl Exp $
*/
#include "ltree.h"
{ /* ltree */
ltree_gist *key;
ltree *val = (ltree *) DatumGetPointer(PG_DETOAST_DATUM(entry->key));
- int4 len = LTG_HDRSIZE + val->len;
+ int4 len = LTG_HDRSIZE + VARSIZE(val);
key = (ltree_gist *) palloc(len);
- key->len = len;
+ SET_VARSIZE(key, len);
key->flag = LTG_ONENODE;
- memcpy((void *) LTG_NODE(key), (void *) val, val->len);
+ memcpy((void *) LTG_NODE(key), (void *) val, VARSIZE(val));
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
gistentryinit(*retval, PointerGetDatum(key),
}
isleqr = (left == right || ISEQ(left, right)) ? true : false;
- *size = LTG_HDRSIZE + ((isalltrue) ? 0 : SIGLEN) + left->len + ((isleqr) ? 0 : right->len);
+ *size = LTG_HDRSIZE + ((isalltrue) ? 0 : SIGLEN) + VARSIZE(left) + ((isleqr) ? 0 : VARSIZE(right));
result = (ltree_gist *) palloc(*size);
- result->len = *size;
+ SET_VARSIZE(result, *size);
result->flag = 0;
if (isalltrue)
else
memcpy((void *) LTG_SIGN(result), base, SIGLEN);
- memcpy((void *) LTG_LNODE(result), (void *) left, left->len);
+ memcpy((void *) LTG_LNODE(result), (void *) left, VARSIZE(left));
if (isleqr)
result->flag |= LTG_NORIGHT;
else
- memcpy((void *) LTG_RNODE(result), (void *) right, right->len);
+ memcpy((void *) LTG_RNODE(result), (void *) right, VARSIZE(right));
PG_RETURN_POINTER(result);
}
lu_l = LTG_GETLNODE(GETENTRY(entryvec, array[FirstOffsetNumber].index));
isleqr = (lu_l == lu_r || ISEQ(lu_l, lu_r)) ? true : false;
- size = LTG_HDRSIZE + ((lisat) ? 0 : SIGLEN) + lu_l->len + ((isleqr) ? 0 : lu_r->len);
+ size = LTG_HDRSIZE + ((lisat) ? 0 : SIGLEN) + VARSIZE(lu_l) + ((isleqr) ? 0 : VARSIZE(lu_r));
lu = (ltree_gist *) palloc(size);
- lu->len = size;
+ SET_VARSIZE(lu, size);
lu->flag = 0;
if (lisat)
lu->flag |= LTG_ALLTRUE;
else
memcpy((void *) LTG_SIGN(lu), ls, SIGLEN);
- memcpy((void *) LTG_LNODE(lu), (void *) lu_l, lu_l->len);
+ memcpy((void *) LTG_LNODE(lu), (void *) lu_l, VARSIZE(lu_l));
if (isleqr)
lu->flag |= LTG_NORIGHT;
else
- memcpy((void *) LTG_RNODE(lu), (void *) lu_r, lu_r->len);
+ memcpy((void *) LTG_RNODE(lu), (void *) lu_r, VARSIZE(lu_r));
ru_l = LTG_GETLNODE(GETENTRY(entryvec, array[1 + ((maxoff - FirstOffsetNumber + 1) / 2)].index));
isleqr = (ru_l == ru_r || ISEQ(ru_l, ru_r)) ? true : false;
- size = LTG_HDRSIZE + ((risat) ? 0 : SIGLEN) + ru_l->len + ((isleqr) ? 0 : ru_r->len);
+ size = LTG_HDRSIZE + ((risat) ? 0 : SIGLEN) + VARSIZE(ru_l) + ((isleqr) ? 0 : VARSIZE(ru_r));
ru = (ltree_gist *) palloc(size);
- ru->len = size;
+ SET_VARSIZE(ru, size);
ru->flag = 0;
if (risat)
ru->flag |= LTG_ALLTRUE;
else
memcpy((void *) LTG_SIGN(ru), rs, SIGLEN);
- memcpy((void *) LTG_LNODE(ru), (void *) ru_l, ru_l->len);
+ memcpy((void *) LTG_LNODE(ru), (void *) ru_l, VARSIZE(ru_l));
if (isleqr)
ru->flag |= LTG_NORIGHT;
else
- memcpy((void *) LTG_RNODE(ru), (void *) ru_r, ru_r->len);
+ memcpy((void *) LTG_RNODE(ru), (void *) ru_r, VARSIZE(ru_r));
v->spl_ldatum = PointerGetDatum(lu);
v->spl_rdatum = PointerGetDatum(ru);
static ltree *
copy_ltree(ltree * src)
{
- ltree *dst = (ltree *) palloc(src->len);
+ ltree *dst = (ltree *) palloc(VARSIZE(src));
- memcpy(dst, src, src->len);
+ memcpy(dst, src, VARSIZE(src));
return dst;
}
/*
* in/out function for ltree and lquery
* Teodor Sigaev <teodor@stack.net>
- * $PostgreSQL: pgsql/contrib/ltree/ltree_io.c,v 1.13 2006/09/22 21:39:57 tgl Exp $
+ * $PostgreSQL: pgsql/contrib/ltree/ltree_io.c,v 1.14 2007/02/28 22:44:38 tgl Exp $
*/
#include "ltree.h"
errdetail("Unexpected end of line.")));
result = (ltree *) palloc(LTREE_HDRSIZE + totallen);
- result->len = LTREE_HDRSIZE + totallen;
+ SET_VARSIZE(result, LTREE_HDRSIZE + totallen);
result->numlevel = lptr - list;
curlevel = LTREE_FIRST(result);
lptr = list;
int i;
ltree_level *curlevel;
- ptr = buf = (char *) palloc(in->len);
+ ptr = buf = (char *) palloc(VARSIZE(in));
curlevel = LTREE_FIRST(in);
for (i = 0; i < in->numlevel; i++)
{
}
result = (lquery *) palloc(totallen);
- result->len = totallen;
+ SET_VARSIZE(result, totallen);
result->numlevel = num;
result->firstgood = 0;
result->flag = 0;
/*
* op function for ltree
* Teodor Sigaev <teodor@stack.net>
- * $PostgreSQL: pgsql/contrib/ltree/ltree_op.c,v 1.15 2007/02/27 23:48:06 tgl Exp $
+ * $PostgreSQL: pgsql/contrib/ltree/ltree_op.c,v 1.16 2007/02/28 22:44:38 tgl Exp $
*/
#include "ltree.h"
}
res = (ltree *) palloc(LTREE_HDRSIZE + (end - start));
- res->len = LTREE_HDRSIZE + (end - start);
+ SET_VARSIZE(res, LTREE_HDRSIZE + (end - start));
res->numlevel = endpos - startpos;
memcpy(LTREE_FIRST(res), start, end - start);
{
ltree *r;
- r = (ltree *) palloc(a->len + b->len - LTREE_HDRSIZE);
- r->len = a->len + b->len - LTREE_HDRSIZE;
+ r = (ltree *) palloc(VARSIZE(a) + VARSIZE(b) - LTREE_HDRSIZE);
+ SET_VARSIZE(r, VARSIZE(a) + VARSIZE(b) - LTREE_HDRSIZE);
r->numlevel = a->numlevel + b->numlevel;
- memcpy(LTREE_FIRST(r), LTREE_FIRST(a), a->len - LTREE_HDRSIZE);
- memcpy(((char *) LTREE_FIRST(r)) + a->len - LTREE_HDRSIZE, LTREE_FIRST(b), b->len -
- LTREE_HDRSIZE);
+ memcpy(LTREE_FIRST(r), LTREE_FIRST(a), VARSIZE(a) - LTREE_HDRSIZE);
+ memcpy(((char *) LTREE_FIRST(r)) + VARSIZE(a) - LTREE_HDRSIZE,
+ LTREE_FIRST(b),
+ VARSIZE(b) - LTREE_HDRSIZE);
return r;
}
}
res = (ltree *) palloc(reslen);
- res->len = reslen;
+ SET_VARSIZE(res, reslen);
res->numlevel = num;
l1 = LTREE_FIRST(*a);
ltree_level *curlevel;
text *out;
- out = (text *) palloc(in->len + VARHDRSZ);
+ out = (text *) palloc(VARSIZE(in) + VARHDRSZ);
ptr = VARDATA(out);
curlevel = LTREE_FIRST(in);
for (i = 0; i < in->numlevel; i++)
/*
* txtquery io
* Teodor Sigaev <teodor@stack.net>
- * $PostgreSQL: pgsql/contrib/ltree/ltxtquery_io.c,v 1.12 2006/09/22 21:39:57 tgl Exp $
+ * $PostgreSQL: pgsql/contrib/ltree/ltxtquery_io.c,v 1.13 2007/02/28 22:44:38 tgl Exp $
*/
#include "ltree.h"
/* make finish struct */
commonlen = COMPUTESIZE(state.num, state.sumlen);
query = (ltxtquery *) palloc(commonlen);
- query->len = commonlen;
+ SET_VARSIZE(query, commonlen);
query->size = state.num;
ptr = GETQUERY(query);
typedef struct
{
- int4 len;
+ int32 vl_len_; /* varlena header (do not touch directly!) */
uint8 flag;
char data[1];
} TRGM;
-#define TRGMHRDSIZE (sizeof(int4)+sizeof(uint8))
+#define TRGMHDRSIZE (VARHDRSZ + sizeof(uint8))
/* gist */
#define BITBYTE 8
#define ISSIGNKEY(x) ( ((TRGM*)x)->flag & SIGNKEY )
#define ISALLTRUE(x) ( ((TRGM*)x)->flag & ALLISTRUE )
-#define CALCGTSIZE(flag, len) ( TRGMHRDSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(trgm)) : (((flag) & ALLISTRUE) ? 0 : SIGLEN) ) )
-#define GETSIGN(x) ( (BITVECP)( (char*)x+TRGMHRDSIZE ) )
-#define GETARR(x) ( (trgm*)( (char*)x+TRGMHRDSIZE ) )
-#define ARRNELEM(x) ( ( ((TRGM*)x)->len - TRGMHRDSIZE )/sizeof(trgm) )
+#define CALCGTSIZE(flag, len) ( TRGMHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(trgm)) : (((flag) & ALLISTRUE) ? 0 : SIGLEN) ) )
+#define GETSIGN(x) ( (BITVECP)( (char*)x+TRGMHDRSIZE ) )
+#define GETARR(x) ( (trgm*)( (char*)x+TRGMHDRSIZE ) )
+#define ARRNELEM(x) ( ( VARSIZE(x) - TRGMHDRSIZE )/sizeof(trgm) )
extern float4 trgm_limit;
+
TRGM *generate_trgm(char *str, int slen);
float4 cnt_sml(TRGM * trg1, TRGM * trg2);
len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
res = (TRGM *) palloc(len);
- res->len = len;
+ SET_VARSIZE(res, len);
res->flag = SIGNKEY | ALLISTRUE;
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
flag |= SIGNKEY;
len = CALCGTSIZE(flag, 0);
result = (TRGM *) 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);
}
if (cache[seed_1].allistrue)
{
datum_l = (TRGM *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
- datum_l->len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
+ SET_VARSIZE(datum_l, CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
datum_l->flag = SIGNKEY | ALLISTRUE;
}
else
{
datum_l = (TRGM *) palloc(CALCGTSIZE(SIGNKEY, 0));
- datum_l->len = CALCGTSIZE(SIGNKEY, 0);
+ SET_VARSIZE(datum_l, CALCGTSIZE(SIGNKEY, 0));
datum_l->flag = SIGNKEY;
memcpy((void *) GETSIGN(datum_l), (void *) cache[seed_1].sign, sizeof(BITVEC));
}
if (cache[seed_2].allistrue)
{
datum_r = (TRGM *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
- datum_r->len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
+ SET_VARSIZE(datum_r, CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
datum_r->flag = SIGNKEY | ALLISTRUE;
}
else
{
datum_r = (TRGM *) palloc(CALCGTSIZE(SIGNKEY, 0));
- datum_r->len = CALCGTSIZE(SIGNKEY, 0);
+ SET_VARSIZE(datum_r, CALCGTSIZE(SIGNKEY, 0));
datum_r->flag = SIGNKEY;
memcpy((void *) GETSIGN(datum_r), (void *) cache[seed_2].sign, sizeof(BITVEC));
}
int wl,
len;
- trg = (TRGM *) palloc(TRGMHRDSIZE + sizeof(trgm) * (slen / 2 + 1) * 3);
+ trg = (TRGM *) palloc(TRGMHDRSIZE + sizeof(trgm) * (slen / 2 + 1) * 3);
trg->flag = ARRKEY;
- trg->len = TRGMHRDSIZE;
+ SET_VARSIZE(trg, TRGMHDRSIZE);
if (slen + LPADDING + RPADDING < 3 || slen == 0)
return trg;
len = unique_array(GETARR(trg), len);
}
- trg->len = CALCGTSIZE(ARRKEY, len);
+ SET_VARSIZE(trg, CALCGTSIZE(ARRKEY, len));
return trg;
}
-/* $PostgreSQL: pgsql/contrib/tsearch2/gistidx.c,v 1.15 2006/10/04 00:29:46 momjian Exp $ */
+/* $PostgreSQL: pgsql/contrib/tsearch2/gistidx.c,v 1.16 2007/02/28 22:44:38 tgl Exp $ */
#include "postgres.h"
len = CALCGTSIZE(ARRKEY, val->size);
res = (GISTTYPE *) palloc(len);
- res->len = len;
+ SET_VARSIZE(res, len);
res->flag = ARRKEY;
arr = GETARR(res);
len = val->size;
*/
len = CALCGTSIZE(ARRKEY, len);
res = (GISTTYPE *) repalloc((void *) res, len);
- res->len = len;
+ SET_VARSIZE(res, len);
}
/* make signature, if array is too long */
- if (res->len > TOAST_INDEX_TARGET)
+ if (VARSIZE(res) > TOAST_INDEX_TARGET)
{
GISTTYPE *ressign;
len = CALCGTSIZE(SIGNKEY, 0);
ressign = (GISTTYPE *) palloc(len);
- ressign->len = len;
+ SET_VARSIZE(ressign, len);
ressign->flag = SIGNKEY;
makesign(GETSIGN(ressign), res);
res = ressign;
len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
res = (GISTTYPE *) palloc(len);
- res->len = len;
+ SET_VARSIZE(res, len);
res->flag = SIGNKEY | ALLISTRUE;
retval = (GISTENTRY *) palloc(sizeof(GISTENTRY));
flag |= SIGNKEY;
len = CALCGTSIZE(flag, 0);
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);
}
if (cache[seed_1].allistrue)
{
datum_l = (GISTTYPE *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
- datum_l->len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
+ SET_VARSIZE(datum_l, CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
datum_l->flag = SIGNKEY | ALLISTRUE;
}
else
{
datum_l = (GISTTYPE *) palloc(CALCGTSIZE(SIGNKEY, 0));
- datum_l->len = CALCGTSIZE(SIGNKEY, 0);
+ SET_VARSIZE(datum_l, CALCGTSIZE(SIGNKEY, 0));
datum_l->flag = SIGNKEY;
memcpy((void *) GETSIGN(datum_l), (void *) cache[seed_1].sign, sizeof(BITVEC));
}
if (cache[seed_2].allistrue)
{
datum_r = (GISTTYPE *) palloc(CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
- datum_r->len = CALCGTSIZE(SIGNKEY | ALLISTRUE, 0);
+ SET_VARSIZE(datum_r, CALCGTSIZE(SIGNKEY | ALLISTRUE, 0));
datum_r->flag = SIGNKEY | ALLISTRUE;
}
else
{
datum_r = (GISTTYPE *) palloc(CALCGTSIZE(SIGNKEY, 0));
- datum_r->len = CALCGTSIZE(SIGNKEY, 0);
+ SET_VARSIZE(datum_r, CALCGTSIZE(SIGNKEY, 0));
datum_r->flag = SIGNKEY;
memcpy((void *) GETSIGN(datum_r), (void *) cache[seed_2].sign, sizeof(BITVEC));
}
-/* $PostgreSQL: pgsql/contrib/tsearch2/gistidx.h,v 1.7 2006/03/11 04:38:30 momjian Exp $ */
+/* $PostgreSQL: pgsql/contrib/tsearch2/gistidx.h,v 1.8 2007/02/28 22:44:38 tgl Exp $ */
#ifndef __GISTIDX_H__
#define __GISTIDX_H__
*/
typedef struct
{
- int4 len;
+ int32 vl_len_; /* varlena header (do not touch directly!) */
int4 flag;
char data[1];
} GISTTYPE;
#define ISSIGNKEY(x) ( ((GISTTYPE*)(x))->flag & SIGNKEY )
#define ISALLTRUE(x) ( ((GISTTYPE*)(x))->flag & ALLISTRUE )
-#define GTHDRSIZE ( sizeof(int4) * 2 )
+#define GTHDRSIZE ( VARHDRSZ + sizeof(int4) )
#define CALCGTSIZE(flag, len) ( GTHDRSIZE + ( ( (flag) & ARRKEY ) ? ((len)*sizeof(int4)) : (((flag) & ALLISTRUE) ? 0 : SIGLEN) ) )
#define GETSIGN(x) ( (BITVECP)( (char*)(x)+GTHDRSIZE ) )
#define GETARR(x) ( (int4*)( (char*)(x)+GTHDRSIZE ) )
-#define ARRNELEM(x) ( ( ((GISTTYPE*)(x))->len - GTHDRSIZE )/sizeof(int4) )
+#define ARRNELEM(x) ( ( VARSIZE(x) - GTHDRSIZE )/sizeof(int4) )
#endif
(errmsg("tsearch query doesn't contain lexeme(s): \"%s\"",
state.buffer)));
query = (QUERYTYPE *) palloc(HDRSIZEQT);
- query->len = HDRSIZEQT;
+ SET_VARSIZE(query, HDRSIZEQT);
query->size = 0;
return query;
}
/* make finish struct */
commonlen = COMPUTESIZE(state.num, state.sumlen);
query = (QUERYTYPE *) palloc(commonlen);
- query->len = commonlen;
+ SET_VARSIZE(query, commonlen);
query->size = state.num;
ptr = GETQUERY(query);
res = clean_fakeval_v2(GETQUERY(query), &len);
if (!res)
{
- query->len = HDRSIZEQT;
+ SET_VARSIZE(query, HDRSIZEQT);
query->size = 0;
PG_RETURN_POINTER(query);
}
res = clean_fakeval_v2(GETQUERY(query), &len);
if (!res)
{
- query->len = HDRSIZEQT;
+ SET_VARSIZE(query, HDRSIZEQT);
query->size = 0;
PG_RETURN_POINTER(query);
}
*/
typedef struct
{
- int4 len;
+ int32 vl_len_; /* varlena header (do not touch directly!) */
int4 size;
char data[1];
} QUERYTYPE;
-#define HDRSIZEQT ( 2 * sizeof(int4) )
+#define HDRSIZEQT ( VARHDRSZ + sizeof(int4) )
#define COMPUTESIZE(size,lenofoperand) ( HDRSIZEQT + (size) * sizeof(ITEM) + (lenofoperand) )
#define GETQUERY(x) (ITEM*)( (char*)(x)+HDRSIZEQT )
#define GETOPERAND(x) ( (char*)GETQUERY(x) + ((QUERYTYPE*)(x))->size * sizeof(ITEM) )
if (acc == NULL || PG_ARGISNULL(0))
{
acc = (QUERYTYPE *) MEMALLOC(AggMemory, sizeof(QUERYTYPE));
- acc->len = HDRSIZEQT;
+ SET_VARSIZE(acc, HDRSIZEQT);
acc->size = 0;
}
if (!acc->size)
{
- if (acc->len > HDRSIZEQT)
+ if (VARSIZE(acc) > HDRSIZEQT)
{
pfree(elemsp);
PG_RETURN_POINTER(acc);
else
{
acc = (QUERYTYPE *) MEMALLOC(AggMemory, HDRSIZEQT * 2);
- acc->len = HDRSIZEQT * 2;
+ SET_VARSIZE(acc, HDRSIZEQT * 2);
acc->size = 0;
}
}
if (acc == NULL || PG_ARGISNULL(0) || acc->size == 0)
{
acc = (QUERYTYPE *) palloc(sizeof(QUERYTYPE));
- acc->len = HDRSIZEQT;
+ SET_VARSIZE(acc, HDRSIZEQT);
acc->size = 0;
}
- rewrited = (QUERYTYPE *) palloc(acc->len);
- memcpy(rewrited, acc, acc->len);
+ rewrited = (QUERYTYPE *) palloc(VARSIZE(acc));
+ memcpy(rewrited, acc, VARSIZE(acc));
pfree(acc);
PG_RETURN_POINTER(rewrited);
}
else
{
- rewrited->len = HDRSIZEQT;
+ SET_VARSIZE(rewrited, HDRSIZEQT);
rewrited->size = 0;
}
if (!tree)
{
- rewrited->len = HDRSIZEQT;
+ SET_VARSIZE(rewrited, HDRSIZEQT);
rewrited->size = 0;
PG_FREE_IF_COPY(ex, 1);
PG_FREE_IF_COPY(subst, 2);
{
return (a->size < b->size) ? -1 : 1;
}
- else if (a->len != b->len)
+ else if (VARSIZE(a) != VARSIZE(b))
{
- return (a->len < b->len) ? -1 : 1;
+ return (VARSIZE(a) < VARSIZE(b)) ? -1 : 1;
}
else
{
len = COMPUTESIZE(nnode, sumlen);
out = (QUERYTYPE *) MEMALLOC(memtype, len);
- out->len = len;
+ SET_VARSIZE(out, len);
out->size = nnode;
state.curitem = GETQUERY(out);