LCOV - code coverage report
Current view: top level - gdk - gdk_value.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 165 189 87.3 %
Date: 2021-10-13 02:24:04 Functions: 10 10 100.0 %

          Line data    Source code
       1             : /*
       2             :  * This Source Code Form is subject to the terms of the Mozilla Public
       3             :  * License, v. 2.0.  If a copy of the MPL was not distributed with this
       4             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       5             :  *
       6             :  * Copyright 1997 - July 2008 CWI, August 2008 - 2021 MonetDB B.V.
       7             :  */
       8             : 
       9             : /*
      10             :  * @a Martin L. Kersten & Peter Boncz
      11             :  * @v 2.0
      12             :  * @+ Value representation
      13             :  *
      14             :  *
      15             :  * When manipulating values, MonetDB puts them into value records.
      16             :  * The built-in types have a direct entry in the union. Others should
      17             :  * be represented as a pointer of memory in pval or as a string, which
      18             :  * is basically the same. In such cases the len field indicates the
      19             :  * size of this piece of memory.
      20             :  *
      21             :  * MonetDB extenders will use value records for passing parameters to
      22             :  * their new operators. MonetDB algebraic commands receive an (argc,
      23             :  * argv) combination, where argc is an integer indicating the size of
      24             :  * the the argv array of value records. On call, the first record,
      25             :  * argv[0], is always empty. The routine must place its return value -
      26             :  * if any - there. The other values are the parameters.
      27             :  *
      28             :  * Actually, the gdk value type defined here should become a built-in
      29             :  * type in the kernel. Next step will be to define the corresponding
      30             :  * extension module.
      31             :  *
      32             :  * @+ Value operations
      33             :  * The following primitives are required to manipulate value records.
      34             :  * Note that binding a BAT requires upgrading its reference count.
      35             :  * The receiver of the value should have been cleared or represent
      36             :  * free space.
      37             :  */
      38             : #include "monetdb_config.h"
      39             : #include "gdk.h"
      40             : #include "gdk_private.h"
      41             : 
      42             : /* Set V to the type/value combination in T/P.  Also see VALinit.  In
      43             :  * this version, if P refers to an external type, no new memory is
      44             :  * allocated, but instead the pointer P is given to V. */
      45             : ValPtr
      46     4290169 : VALset(ValPtr v, int t, ptr p)
      47             : {
      48     4290169 :         switch (ATOMstorage(v->vtype = t)) {
      49       17172 :         case TYPE_void:
      50       17172 :                 v->val.oval = *(oid *) p;
      51       17172 :                 break;
      52           0 :         case TYPE_msk:
      53           0 :                 v->val.mval = *(msk *) p;
      54           0 :                 break;
      55       10548 :         case TYPE_bte:
      56       10548 :                 v->val.btval = *(bte *) p;
      57       10548 :                 break;
      58        9533 :         case TYPE_sht:
      59        9533 :                 v->val.shval = *(sht *) p;
      60        9533 :                 break;
      61       60817 :         case TYPE_int:
      62       60817 :                 v->val.ival = *(int *) p;
      63       60817 :                 break;
      64        2789 :         case TYPE_flt:
      65        2789 :                 v->val.fval = *(flt *) p;
      66        2789 :                 break;
      67        2804 :         case TYPE_dbl:
      68        2804 :                 v->val.dval = *(dbl *) p;
      69        2804 :                 break;
      70       97350 :         case TYPE_lng:
      71       97350 :                 v->val.lval = *(lng *) p;
      72       97350 :                 break;
      73             : #ifdef HAVE_HGE
      74         467 :         case TYPE_hge:
      75         467 :                 v->val.hval = *(hge *) p;
      76         467 :                 break;
      77             : #endif
      78          98 :         case TYPE_uuid:
      79          98 :                 v->val.uval = *(uuid *) p;
      80          98 :                 break;
      81     3884112 :         case TYPE_str:
      82     3884112 :                 v->val.sval = (str) p;
      83     3884112 :                 break;
      84      204167 :         case TYPE_ptr:
      85      204167 :                 v->val.pval = *(ptr *) p;
      86      204167 :                 break;
      87         312 :         default:
      88         312 :                 v->val.pval = p;
      89         312 :                 break;
      90             :         }
      91     4290169 :         v->len = ATOMlen(v->vtype, VALptr(v));
      92     4290170 :         return v;
      93             : }
      94             : 
      95             : /* Return a pointer to the value contained in V.  Also see VALptr
      96             :  * which returns a const void *. */
      97             : void *
      98    34247011 : VALget(ValPtr v)
      99             : {
     100    34247011 :         switch (ATOMstorage(v->vtype)) {
     101           3 :         case TYPE_void: return (void *) &v->val.oval;
     102           0 :         case TYPE_msk: return (void *) &v->val.mval;
     103     2114882 :         case TYPE_bte: return (void *) &v->val.btval;
     104       27332 :         case TYPE_sht: return (void *) &v->val.shval;
     105    29993386 :         case TYPE_int: return (void *) &v->val.ival;
     106         759 :         case TYPE_flt: return (void *) &v->val.fval;
     107       18498 :         case TYPE_dbl: return (void *) &v->val.dval;
     108     1135164 :         case TYPE_lng: return (void *) &v->val.lval;
     109             : #ifdef HAVE_HGE
     110        2899 :         case TYPE_hge: return (void *) &v->val.hval;
     111             : #endif
     112          63 :         case TYPE_uuid: return (void *) &v->val.uval;
     113           1 :         case TYPE_ptr: return (void *) &v->val.pval;
     114      953917 :         case TYPE_str: return (void *) v->val.sval;
     115         107 :         default:       return (void *) v->val.pval;
     116             :         }
     117             : }
     118             : 
     119             : /* Clear V to an empty value (type void, value nil), freeing any
     120             :  * memory allocated for external types.  See VALempty for when V does
     121             :  * not yet contain a value. */
     122             : void
     123    33750988 : VALclear(ValPtr v)
     124             : {
     125    33750988 :         if (ATOMextern(v->vtype)) {
     126     4739000 :                 if (v->val.pval && v->val.pval != ATOMnilptr(v->vtype))
     127     4712339 :                         GDKfree(v->val.pval);
     128             :         }
     129    33750988 :         VALempty(v);
     130    33750989 : }
     131             : 
     132             : /* Initialize V to an empty value (type void, value nil).  See
     133             :  * VALclear for when V already contains a value. */
     134             : void
     135    48057551 : VALempty(ValPtr v)
     136             : {
     137    48057551 :         v->len = 0;
     138    48057551 :         v->val.oval = oid_nil;
     139    48057551 :         v->vtype = TYPE_void;
     140    48057551 : }
     141             : 
     142             : /* Create a copy of S into D, allocating space for external values
     143             :  * (non-fixed sized values).  See VALinit for a version where the
     144             :  * source is not in a VALRecord.
     145             :  *
     146             :  * Returns NULL In case of (malloc) failure. */
     147             : ValPtr
     148    26639052 : VALcopy(ValPtr d, const ValRecord *s)
     149             : {
     150    26639052 :         if (!ATOMextern(s->vtype)) {
     151    17522563 :                 *d = *s;
     152     9116489 :         } else if (s->val.pval == NULL) {
     153           0 :                 return VALinit(d, s->vtype, ATOMnilptr(s->vtype));
     154     9116489 :         } else if (s->vtype == TYPE_str) {
     155             :                 const char *p = s->val.sval;
     156     9112337 :                 d->vtype = TYPE_str;
     157     9112337 :                 d->len = strLen(p);
     158     9112337 :                 d->val.sval = GDKmalloc(d->len);
     159     9112343 :                 if (d->val.sval == NULL)
     160             :                         return NULL;
     161     9112343 :                 memcpy(d->val.sval, p, d->len);
     162             :         } else {
     163             :                 const void *p = s->val.pval;
     164        4152 :                 d->vtype = s->vtype;
     165        4152 :                 d->len = ATOMlen(d->vtype, p);
     166        4152 :                 d->val.pval = GDKmalloc(d->len);
     167        4152 :                 if (d->val.pval == NULL)
     168             :                         return NULL;
     169        4152 :                 memcpy(d->val.pval, p, d->len);
     170             :         }
     171             :         return d;
     172             : }
     173             : 
     174             : /* Create a copy of the type value combination in TPE/S, allocating
     175             :  * space for external values (non-fixed sized values).  See VALcopy
     176             :  * for a version where the source is in a ValRecord, and see VALset
     177             :  * for a version where ownership of the source is transferred.
     178             :  *
     179             :  * Returns NULL in case of (malloc) failure. */
     180             : ValPtr
     181     3435419 : VALinit(ValPtr d, int tpe, const void *s)
     182             : {
     183     3435419 :         switch (ATOMstorage(d->vtype = tpe)) {
     184           0 :         case TYPE_void:
     185           0 :                 d->val.oval = *(const oid *) s;
     186           0 :                 break;
     187           0 :         case TYPE_msk:
     188           0 :                 d->val.mval = *(const msk *) s;
     189           0 :                 break;
     190       21702 :         case TYPE_bte:
     191       21702 :                 d->val.btval = *(const bte *) s;
     192       21702 :                 break;
     193       35367 :         case TYPE_sht:
     194       35367 :                 d->val.shval = *(const sht *) s;
     195       35367 :                 break;
     196      187991 :         case TYPE_int:
     197      187991 :                 d->val.ival = *(const int *) s;
     198      187991 :                 break;
     199        3238 :         case TYPE_flt:
     200        3238 :                 d->val.fval = *(const flt *) s;
     201        3238 :                 break;
     202        6107 :         case TYPE_dbl:
     203        6107 :                 d->val.dval = *(const dbl *) s;
     204        6107 :                 break;
     205      240299 :         case TYPE_lng:
     206      240299 :                 d->val.lval = *(const lng *) s;
     207      240299 :                 break;
     208             : #ifdef HAVE_HGE
     209        1374 :         case TYPE_hge:
     210        1374 :                 d->val.hval = *(const hge *) s;
     211        1374 :                 break;
     212             : #endif
     213         103 :         case TYPE_uuid:
     214         103 :                 d->val.uval = *(const uuid *) s;
     215         103 :                 break;
     216     2939051 :         case TYPE_str:
     217     2939051 :                 d->len = strLen(s);
     218     2939051 :                 d->val.sval = GDKmalloc(d->len);
     219     2939051 :                 if (d->val.sval == NULL)
     220             :                         return NULL;
     221     2939051 :                 memcpy(d->val.sval, s, d->len);
     222     2939051 :                 return d;
     223           0 :         case TYPE_ptr:
     224           0 :                 d->val.pval = *(const ptr *) s;
     225           0 :                 d->len = ATOMlen(tpe, *(const ptr *) s);
     226           0 :                 return d;
     227         187 :         default:
     228         187 :                 assert(ATOMextern(ATOMstorage(tpe)));
     229         187 :                 d->len = ATOMlen(tpe, s);
     230         187 :                 d->val.pval = GDKmalloc(d->len);
     231         187 :                 if (d->val.pval == NULL)
     232             :                         return NULL;
     233         187 :                 memcpy(d->val.pval, s, d->len);
     234         187 :                 return d;
     235             :         }
     236      496181 :         d->len = ATOMsize(d->vtype);
     237      496181 :         return d;
     238             : }
     239             : 
     240             : /* Format the value in RES in the standard way for the type of RES
     241             :  * into a newly allocated buffer.  Also see ATOMformat. */
     242             : char *
     243       18701 : VALformat(const ValRecord *res)
     244             : {
     245       18701 :         return ATOMformat(res->vtype, VALptr(res));
     246             : }
     247             : 
     248             : /* Convert (cast) the value in T to the type TYP, do this in place.
     249             :  * Return a pointer to the converted value, or NULL if the conversion
     250             :  * didn't succeed.  If the conversion didn't succeed, the original
     251             :  * value is not modified.  Also see VARconvert. */
     252             : ptr
     253      340321 : VALconvert(int typ, ValPtr t)
     254             : {
     255      340321 :         int src_tpe = t->vtype;
     256             :         ValRecord dst;
     257             : 
     258      340321 :         dst.vtype = typ;
     259             : 
     260             :         /* first convert into a new location */
     261      340321 :         if (VARconvert(&dst, t, 0, 0, 0, 0) != GDK_SUCCEED)
     262             :                 return NULL;
     263             : 
     264             :         /* then maybe free the old */
     265      340320 :         if (src_tpe != dst.vtype &&
     266      340310 :             t->vtype != typ &&
     267      340310 :             dst.vtype != TYPE_void &&
     268      340135 :             (src_tpe >= TYPE_str || dst.vtype >= TYPE_str))
     269      113370 :                 VALclear(t);
     270             :         /* and finally copy the result */
     271      340320 :         *t = dst;
     272             :         /* make sure we return the correct type (not the storage type) */
     273      340320 :         t->vtype = typ;
     274      340320 :         return VALget(t);
     275             : }
     276             : 
     277             : /* Compare two values in P and Q and return -1/0/1 depending on
     278             :  * whether P is less than, equal to, or larger than Q. Also return -1
     279             :  * if P or Q is NULL or NIL, or if the types of P and Q are not
     280             :  * equal. */
     281             : int
     282       43056 : VALcmp(const ValRecord *p, const ValRecord *q)
     283             : {
     284             : 
     285             :         int (*cmp)(const void *, const void *);
     286             :         int tpe;
     287             :         const void *nilptr, *pp, *pq;
     288             : 
     289       43056 :         if (p == 0 || q == 0)
     290             :                 return -1;
     291       43056 :         if ((tpe = p->vtype) != q->vtype)
     292             :                 return -1;
     293             : 
     294       43056 :         if (tpe == TYPE_ptr)
     295             :                 return 0;       /* ignore comparing C pointers */
     296       43056 :         cmp = ATOMcompare(tpe);
     297       43056 :         nilptr = ATOMnilptr(tpe);
     298       43056 :         pp = VALptr(p);
     299       43056 :         pq = VALptr(q);
     300       43056 :         if ((*cmp)(pp, nilptr) == 0 && (*cmp)(pq, nilptr) == 0)
     301             :                 return 0;       /* eq nil val */
     302       43056 :         if ((*cmp)(pp, nilptr) == 0 || (*cmp)(pq, nilptr) == 0)
     303           0 :                 return -1;
     304       43056 :         return (*cmp)(pp, pq);
     305             : 
     306             : }
     307             : 
     308             : /* Return TRUE if the value in V is NIL. */
     309             : bool
     310      607303 : VALisnil(const ValRecord *v)
     311             : {
     312      607303 :         switch (v->vtype) {
     313             :         case TYPE_void:
     314             :                 return true;
     315           0 :         case TYPE_msk:
     316           0 :                 return false;
     317        1893 :         case TYPE_bte:
     318        1893 :                 return is_bte_nil(v->val.btval);
     319         771 :         case TYPE_sht:
     320         771 :                 return is_sht_nil(v->val.shval);
     321       17074 :         case TYPE_int:
     322       17074 :                 return is_int_nil(v->val.ival);
     323       26585 :         case TYPE_lng:
     324       26585 :                 return is_lng_nil(v->val.lval);
     325             : #ifdef HAVE_HGE
     326          15 :         case TYPE_hge:
     327          15 :                 return is_hge_nil(v->val.hval);
     328             : #endif
     329           2 :         case TYPE_uuid:
     330           2 :                 return is_uuid_nil(v->val.uval);
     331           5 :         case TYPE_flt:
     332           5 :                 return is_flt_nil(v->val.fval);
     333          66 :         case TYPE_dbl:
     334          66 :                 return is_dbl_nil(v->val.dval);
     335           0 :         case TYPE_oid:
     336           0 :                 return is_oid_nil(v->val.oval);
     337           0 :         case TYPE_ptr:
     338           0 :                 return v->val.pval == NULL;
     339           0 :         case TYPE_bat:
     340           0 :                 return is_bat_nil(v->val.bval);
     341             :         default:
     342             :                 break;
     343             :         }
     344      560884 :         return (*ATOMcompare(v->vtype))(VALptr(v), ATOMnilptr(v->vtype)) == 0;
     345             : }

Generated by: LCOV version 1.14