diff options
Diffstat (limited to 'src/backend/utils')
-rw-r--r-- | src/backend/utils/adt/datum.c | 135 | ||||
-rw-r--r-- | src/backend/utils/adt/varlena.c | 108 | ||||
-rw-r--r-- | src/backend/utils/cache/fcache.c | 15 | ||||
-rw-r--r-- | src/backend/utils/mmgr/aset.c | 13 |
4 files changed, 141 insertions, 130 deletions
diff --git a/src/backend/utils/adt/datum.c b/src/backend/utils/adt/datum.c index a86ec87067b..7f590e06e46 100644 --- a/src/backend/utils/adt/datum.c +++ b/src/backend/utils/adt/datum.c @@ -1,13 +1,14 @@ /*------------------------------------------------------------------------- * * datum.c + * POSTGRES Datum (abstract data type) manipulation routines. * * Portions Copyright (c) 1996-2000, PostgreSQL, Inc * Portions Copyright (c) 1994, Regents of the University of California * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/datum.c,v 1.17 2000/01/26 05:57:13 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/datum.c,v 1.18 2000/07/12 02:37:19 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -16,7 +17,7 @@ * * A) if a type is "byVal" then all the information is stored in the * Datum itself (i.e. no pointers involved!). In this case the - * length of the type is always greater than zero and less than + * length of the type is always greater than zero and not more than * "sizeof(Datum)" * B) if a type is not "byVal" and it has a fixed length, then * the "Datum" always contain a pointer to a stream of bytes. @@ -27,15 +28,19 @@ * This varlena structure has information about the actual length of this * particular instance of the type and about its value. * + * Note that we do not treat "toasted" datums specially; therefore what + * will be copied or compared is the compressed data or toast reference. */ + #include "postgres.h" + #include "utils/datum.h" /*------------------------------------------------------------------------- * datumGetSize * * Find the "real" size of a datum, given the datum value, - * its type, whether it is a "by value", and its length. + * whether it is a "by value", and its length. * * To cut a long story short, usually the real size is equal to the * type length, with the exception of variable length types which have @@ -45,47 +50,31 @@ *------------------------------------------------------------------------- */ Size -datumGetSize(Datum value, Oid type, bool byVal, Size len) +datumGetSize(Datum value, bool typByVal, int typLen) { + Size size; - struct varlena *s; - Size size = 0; - - if (byVal) + if (typByVal) { - if (len <= sizeof(Datum)) - size = len; - else - { - elog(ERROR, - "datumGetSize: Error: type=%ld, byVaL with len=%d", - (long) type, len); - } + /* Pass-by-value types are always fixed-length */ + Assert(typLen > 0 && typLen <= sizeof(Datum)); + size = (Size) typLen; } else - { /* not byValue */ - if (len == -1) + { + if (typLen == -1) { + /* Assume it is a varlena datatype */ + struct varlena *s = (struct varlena *) DatumGetPointer(value); - /* - * variable length type Look at the varlena struct for its - * real length... - */ - s = (struct varlena *) DatumGetPointer(value); if (!PointerIsValid(s)) - { - elog(ERROR, - "datumGetSize: Invalid Datum Pointer"); - } + elog(ERROR, "datumGetSize: Invalid Datum Pointer"); size = (Size) VARSIZE(s); } else { - - /* - * fixed length type - */ - size = len; + /* Fixed-length pass-by-ref type */ + size = (Size) typLen; } } @@ -97,37 +86,29 @@ datumGetSize(Datum value, Oid type, bool byVal, Size len) * * make a copy of a datum * - * If the type of the datum is not passed by value (i.e. "byVal=false") - * then we assume that the datum contains a pointer and we copy all the - * bytes pointed by this pointer + * If the datatype is pass-by-reference, memory is obtained with palloc(). *------------------------------------------------------------------------- */ Datum -datumCopy(Datum value, Oid type, bool byVal, Size len) +datumCopy(Datum value, bool typByVal, int typLen) { - - Size realSize; Datum res; - char *s; - - if (byVal) + if (typByVal) res = value; else { - if (value == 0) - return (Datum) NULL; - realSize = datumGetSize(value, type, byVal, len); + Size realSize; + char *s; + + if (DatumGetPointer(value) == NULL) + return PointerGetDatum(NULL); + + realSize = datumGetSize(value, typByVal, typLen); - /* - * the value is a pointer. Allocate enough space and copy the - * pointed data. - */ s = (char *) palloc(realSize); - if (s == NULL) - elog(ERROR, "datumCopy: out of memory\n"); - memmove(s, DatumGetPointer(value), realSize); - res = (Datum) s; + memcpy(s, DatumGetPointer(value), realSize); + res = PointerGetDatum(s); } return res; } @@ -143,21 +124,12 @@ datumCopy(Datum value, Oid type, bool byVal, Size len) */ #ifdef NOT_USED void -datumFree(Datum value, Oid type, bool byVal, Size len) +datumFree(Datum value, bool typByVal, int typLen) { - - Size realSize; - Pointer s; - - realSize = datumGetSize(value, type, byVal, len); - - if (!byVal) + if (!typByVal) { + Pointer s = DatumGetPointer(value); - /* - * free the space palloced by "datumCopy()" - */ - s = DatumGetPointer(value); pfree(s); } } @@ -174,46 +146,41 @@ datumFree(Datum value, Oid type, bool byVal, Size len) * This routine will return false if there are 2 different * representations of the same value (something along the lines * of say the representation of zero in one's complement arithmetic). - * + * Also, it will probably not give the answer you want if either + * datum has been "toasted". *------------------------------------------------------------------------- */ bool -datumIsEqual(Datum value1, Datum value2, Oid type, bool byVal, Size len) +datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen) { - Size size1, - size2; - char *s1, - *s2; + bool res; - if (byVal) + if (typByVal) { - /* * just compare the two datums. NOTE: just comparing "len" bytes * will not do the work, because we do not know how these bytes * are aligned inside the "Datum". */ - if (value1 == value2) - return true; - else - return false; + res = (value1 == value2); } else { + Size size1, + size2; + char *s1, + *s2; /* - * byVal = false Compare the bytes pointed by the pointers stored - * in the datums. + * Compare the bytes pointed by the pointers stored in the datums. */ - size1 = datumGetSize(value1, type, byVal, len); - size2 = datumGetSize(value2, type, byVal, len); + size1 = datumGetSize(value1, typByVal, typLen); + size2 = datumGetSize(value2, typByVal, typLen); if (size1 != size2) return false; s1 = (char *) DatumGetPointer(value1); s2 = (char *) DatumGetPointer(value2); - if (!memcmp(s1, s2, size1)) - return true; - else - return false; + res = (memcmp(s1, s2, size1) == 0); } + return res; } diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 8247e16812d..b5380a8c52a 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.63 2000/07/06 05:48:11 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/adt/varlena.c,v 1.64 2000/07/12 02:37:19 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -436,25 +436,38 @@ textpos(PG_FUNCTION_ARGS) /* * texteq - returns true iff arguments are equal * textne - returns true iff arguments are not equal + * + * Note: btree indexes need these routines not to leak memory; therefore, + * be careful to free working copies of toasted datums. Most places don't + * need to be so careful. */ Datum texteq(PG_FUNCTION_ARGS) { text *arg1 = PG_GETARG_TEXT_P(0); text *arg2 = PG_GETARG_TEXT_P(1); - int len; - char *a1p, - *a2p; + bool result; if (VARSIZE(arg1) != VARSIZE(arg2)) - PG_RETURN_BOOL(false); + result = false; + else + { + int len; + char *a1p, + *a2p; - len = VARSIZE(arg1) - VARHDRSZ; + len = VARSIZE(arg1) - VARHDRSZ; - a1p = VARDATA(arg1); - a2p = VARDATA(arg2); + a1p = VARDATA(arg1); + a2p = VARDATA(arg2); - PG_RETURN_BOOL(memcmp(a1p, a2p, len) == 0); + result = (memcmp(a1p, a2p, len) == 0); + } + + PG_FREE_IF_COPY(arg1, 0); + PG_FREE_IF_COPY(arg2, 1); + + PG_RETURN_BOOL(result); } Datum @@ -462,19 +475,28 @@ textne(PG_FUNCTION_ARGS) { text *arg1 = PG_GETARG_TEXT_P(0); text *arg2 = PG_GETARG_TEXT_P(1); - int len; - char *a1p, - *a2p; + bool result; if (VARSIZE(arg1) != VARSIZE(arg2)) - PG_RETURN_BOOL(true); + result = true; + else + { + int len; + char *a1p, + *a2p; - len = VARSIZE(arg1) - VARHDRSZ; + len = VARSIZE(arg1) - VARHDRSZ; - a1p = VARDATA(arg1); - a2p = VARDATA(arg2); + a1p = VARDATA(arg1); + a2p = VARDATA(arg2); + + result = (memcmp(a1p, a2p, len) != 0); + } - PG_RETURN_BOOL(memcmp(a1p, a2p, len) != 0); + PG_FREE_IF_COPY(arg1, 0); + PG_FREE_IF_COPY(arg2, 1); + + PG_RETURN_BOOL(result); } /* varstr_cmp() @@ -545,6 +567,10 @@ text_cmp(text *arg1, text *arg2) /* * Comparison functions for text strings. + * + * Note: btree indexes need these routines not to leak memory; therefore, + * be careful to free working copies of toasted datums. Most places don't + * need to be so careful. */ Datum @@ -552,8 +578,14 @@ text_lt(PG_FUNCTION_ARGS) { text *arg1 = PG_GETARG_TEXT_P(0); text *arg2 = PG_GETARG_TEXT_P(1); + bool result; - PG_RETURN_BOOL(text_cmp(arg1, arg2) < 0); + result = (text_cmp(arg1, arg2) < 0); + + PG_FREE_IF_COPY(arg1, 0); + PG_FREE_IF_COPY(arg2, 1); + + PG_RETURN_BOOL(result); } Datum @@ -561,8 +593,14 @@ text_le(PG_FUNCTION_ARGS) { text *arg1 = PG_GETARG_TEXT_P(0); text *arg2 = PG_GETARG_TEXT_P(1); + bool result; + + result = (text_cmp(arg1, arg2) <= 0); + + PG_FREE_IF_COPY(arg1, 0); + PG_FREE_IF_COPY(arg2, 1); - PG_RETURN_BOOL(text_cmp(arg1, arg2) <= 0); + PG_RETURN_BOOL(result); } Datum @@ -570,8 +608,14 @@ text_gt(PG_FUNCTION_ARGS) { text *arg1 = PG_GETARG_TEXT_P(0); text *arg2 = PG_GETARG_TEXT_P(1); + bool result; - PG_RETURN_BOOL(text_cmp(arg1, arg2) > 0); + result = (text_cmp(arg1, arg2) > 0); + + PG_FREE_IF_COPY(arg1, 0); + PG_FREE_IF_COPY(arg2, 1); + + PG_RETURN_BOOL(result); } Datum @@ -579,8 +623,14 @@ text_ge(PG_FUNCTION_ARGS) { text *arg1 = PG_GETARG_TEXT_P(0); text *arg2 = PG_GETARG_TEXT_P(1); + bool result; + + result = (text_cmp(arg1, arg2) >= 0); + + PG_FREE_IF_COPY(arg1, 0); + PG_FREE_IF_COPY(arg2, 1); - PG_RETURN_BOOL(text_cmp(arg1, arg2) >= 0); + PG_RETURN_BOOL(result); } Datum @@ -589,14 +639,8 @@ text_larger(PG_FUNCTION_ARGS) text *arg1 = PG_GETARG_TEXT_P(0); text *arg2 = PG_GETARG_TEXT_P(1); text *result; - text *temp; - temp = ((text_cmp(arg1, arg2) > 0) ? arg1 : arg2); - - /* Make a copy --- temporary hack until nodeAgg.c is smarter */ - - result = (text *) palloc(VARSIZE(temp)); - memcpy((char *) result, (char *) temp, VARSIZE(temp)); + result = ((text_cmp(arg1, arg2) > 0) ? arg1 : arg2); PG_RETURN_TEXT_P(result); } @@ -607,14 +651,8 @@ text_smaller(PG_FUNCTION_ARGS) text *arg1 = PG_GETARG_TEXT_P(0); text *arg2 = PG_GETARG_TEXT_P(1); text *result; - text *temp; - - temp = ((text_cmp(arg1, arg2) < 0) ? arg1 : arg2); - - /* Make a copy --- temporary hack until nodeAgg.c is smarter */ - result = (text *) palloc(VARSIZE(temp)); - memcpy((char *) result, (char *) temp, VARSIZE(temp)); + result = ((text_cmp(arg1, arg2) < 0) ? arg1 : arg2); PG_RETURN_TEXT_P(result); } diff --git a/src/backend/utils/cache/fcache.c b/src/backend/utils/cache/fcache.c index 7e9d18c7e27..ba34dfd03dc 100644 --- a/src/backend/utils/cache/fcache.c +++ b/src/backend/utils/cache/fcache.c @@ -8,7 +8,7 @@ * * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/cache/Attic/fcache.c,v 1.33 2000/07/05 23:11:39 tgl Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/cache/Attic/fcache.c,v 1.34 2000/07/12 02:37:20 tgl Exp $ * *------------------------------------------------------------------------- */ @@ -75,6 +75,7 @@ init_fcache(Oid foid, retval = (FunctionCachePtr) palloc(sizeof(FunctionCache)); MemSet(retval, 0, sizeof(FunctionCache)); + retval->fcacheCxt = CurrentMemoryContext; /* ---------------- * get the procedure tuple corresponding to the given functionOid @@ -256,22 +257,26 @@ init_fcache(Oid foid, void setFcache(Node *node, Oid foid, List *argList, ExprContext *econtext) { - Func *fnode; - Oper *onode; + MemoryContext oldcontext; FunctionCachePtr fcache; + /* Switch to a context long-lived enough for the fcache entry */ + oldcontext = MemoryContextSwitchTo(econtext->ecxt_per_query_memory); + fcache = init_fcache(foid, argList, econtext); if (IsA(node, Oper)) { - onode = (Oper *) node; + Oper *onode = (Oper *) node; onode->op_fcache = fcache; } else if (IsA(node, Func)) { - fnode = (Func *) node; + Func *fnode = (Func *) node; fnode->func_fcache = fcache; } else elog(ERROR, "init_fcache: node must be Oper or Func!"); + + MemoryContextSwitchTo(oldcontext); } diff --git a/src/backend/utils/mmgr/aset.c b/src/backend/utils/mmgr/aset.c index bf212ff7bb5..6c6f58b0222 100644 --- a/src/backend/utils/mmgr/aset.c +++ b/src/backend/utils/mmgr/aset.c @@ -11,7 +11,7 @@ * Portions Copyright (c) 1994, Regents of the University of California * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/backend/utils/mmgr/aset.c,v 1.29 2000/07/11 14:30:28 momjian Exp $ + * $Header: /cvsroot/pgsql/src/backend/utils/mmgr/aset.c,v 1.30 2000/07/12 02:37:23 tgl Exp $ * * NOTE: * This is a new (Feb. 05, 1999) implementation of the allocation set @@ -349,20 +349,21 @@ AllocSetReset(MemoryContext context) if (block == set->keeper) { /* Reset the block, but don't return it to malloc */ - block->next = NULL; - block->freeptr = ((char *) block) + ALLOC_BLOCKHDRSZ; + char *datastart = ((char *) block) + ALLOC_BLOCKHDRSZ; + #ifdef CLOBBER_FREED_MEMORY /* Wipe freed memory for debugging purposes */ - memset(block->freeptr, 0x7F, - ((char *) block->endptr) - ((char *) block->freeptr)); + memset(datastart, 0x7F, ((char *) block->freeptr) - datastart); #endif + block->freeptr = datastart; + block->next = NULL; } else { /* Normal case, release the block */ #ifdef CLOBBER_FREED_MEMORY /* Wipe freed memory for debugging purposes */ - memset(block, 0x7F, ((char *) block->endptr) - ((char *) block)); + memset(block, 0x7F, ((char *) block->freeptr) - ((char *) block)); #endif free(block); } |