LCOV - code coverage report
Current view: top level - monetdb5/mal - mal_atom.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 29 112 25.9 %
Date: 2021-09-14 22:17:06 Functions: 4 5 80.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             :  * (author) M.L.Kersten
      11             :  * Every MAL command introduced in an atom module should be checked
      12             :  * to detect overloading of a predefined function.
      13             :  * Subsequently, we update the GDK atom structure.
      14             :  * The function signatures should be parameter-less, which
      15             :  * enables additional functions with the same name to appear
      16             :  * as ordinary mal operators.
      17             :  *
      18             :  * A few fields are set only once, at creation time.
      19             :  * They should be implemented with parameter-less functions.
      20             :  */
      21             : #include "monetdb_config.h"
      22             : #include "mal_instruction.h"
      23             : #include "mal_atom.h"
      24             : #include "mal_namespace.h"
      25             : #include "mal_exception.h"
      26             : #include "mal_private.h"
      27             : 
      28           0 : static void setAtomName(InstrPtr pci)
      29             : {
      30             :         char buf[FILENAME_MAX];
      31           0 :         snprintf(buf, FILENAME_MAX, "#%s", getFunctionId(pci));
      32           0 :         setFunctionId(pci, putName(buf));
      33           0 : }
      34             : 
      35             : str
      36           3 : malAtomProperty(MalBlkPtr mb, InstrPtr pci)
      37             : {
      38             :         const char *name;
      39             :         int tpe;
      40             :         (void)mb;  /* fool compilers */
      41           3 :         assert(pci != 0);
      42           3 :         name = getFunctionId(pci);
      43           3 :         tpe = getAtomIndex(getModuleId(pci), strlen(getModuleId(pci)), TYPE_any);
      44           3 :         if (tpe < 0 || tpe >= GDKatomcnt || tpe >= MAXATOMS)
      45             :                 return MAL_SUCCEED;
      46           3 :         assert(pci->fcn != NULL);
      47           3 :         switch (name[0]) {
      48           0 :         case 'd':
      49           0 :                 if (idcmp("del", name) == 0 && pci->argc == 1) {
      50           0 :                         BATatoms[tpe].atomDel = (void (*)(Heap *, var_t *))pci->fcn;
      51           0 :                         setAtomName(pci);
      52           0 :                         return MAL_SUCCEED;
      53             :                 }
      54             :                 break;
      55           0 :         case 'c':
      56           0 :                 if (idcmp("cmp", name) == 0 && pci->argc == 1) {
      57           0 :                         BATatoms[tpe].atomCmp = (int (*)(const void *, const void *))pci->fcn;
      58           0 :                         BATatoms[tpe].linear = true;
      59           0 :                         setAtomName(pci);
      60           0 :                         return MAL_SUCCEED;
      61             :                 }
      62             :                 break;
      63           3 :         case 'f':
      64           3 :                 if (idcmp("fromstr", name) == 0 && pci->argc == 1) {
      65           0 :                         BATatoms[tpe].atomFromStr = (ssize_t (*)(const char *, size_t *, ptr *, bool))pci->fcn;
      66           0 :                         setAtomName(pci);
      67           0 :                         return MAL_SUCCEED;
      68             :                 }
      69           3 :                 if (idcmp("fix", name) == 0 && pci->argc == 1) {
      70           0 :                         BATatoms[tpe].atomFix = (gdk_return (*)(const void *))pci->fcn;
      71           0 :                         setAtomName(pci);
      72           0 :                         return MAL_SUCCEED;
      73             :                 }
      74             :                 break;
      75           0 :         case 'h':
      76           0 :                 if (idcmp("heap", name) == 0 && pci->argc == 1) {
      77             :                         /* heap function makes an atom varsized */
      78           0 :                         BATatoms[tpe].size = sizeof(var_t);
      79           0 :                         assert_shift_width(ATOMelmshift(ATOMsize(tpe)), ATOMsize(tpe));
      80           0 :                         BATatoms[tpe].atomHeap = (gdk_return (*)(Heap *, size_t))pci->fcn;
      81           0 :                         setAtomName(pci);
      82           0 :                         return MAL_SUCCEED;
      83             :                 }
      84           0 :                 if (idcmp("hash", name) == 0 && pci->argc == 1) {
      85           0 :                         BATatoms[tpe].atomHash = (BUN (*)(const void *))pci->fcn;
      86           0 :                         setAtomName(pci);
      87           0 :                         return MAL_SUCCEED;
      88             :                 }
      89             :                 break;
      90           0 :         case 'l':
      91           0 :                 if (idcmp("length", name) == 0 && pci->argc == 1) {
      92           0 :                         BATatoms[tpe].atomLen = (size_t (*)(const void *))pci->fcn;
      93           0 :                         setAtomName(pci);
      94           0 :                         return MAL_SUCCEED;
      95             :                 }
      96             :                 break;
      97           0 :         case 'n':
      98           0 :                 if (idcmp("null", name) == 0 && pci->argc == 1) {
      99           0 :                         const void *atmnull = ((const void *(*)(void))pci->fcn)();
     100             : 
     101           0 :                         BATatoms[tpe].atomNull = atmnull;
     102           0 :                         setAtomName(pci);
     103           0 :                         return MAL_SUCCEED;
     104             :                 }
     105           0 :                 if (idcmp("nequal", name) == 0 && pci->argc == 1) {
     106           0 :                         BATatoms[tpe].atomCmp = (int (*)(const void *, const void *))pci->fcn;
     107           0 :                         setAtomName(pci);
     108           0 :                         return MAL_SUCCEED;
     109             :                 }
     110             :                 break;
     111           0 :         case 'p':
     112           0 :                 if (idcmp("put", name) == 0 && pci->argc == 1) {
     113           0 :                         BATatoms[tpe].atomPut = (var_t (*)(BAT *, var_t *, const void *))pci->fcn;
     114           0 :                         setAtomName(pci);
     115           0 :                         return MAL_SUCCEED;
     116             :                 }
     117             :                 break;
     118           0 :         case 's':
     119           0 :                 if (idcmp("storage", name) == 0 && pci->argc == 1) {
     120           0 :                         BATatoms[tpe].storage = (*(int (*)(void))pci->fcn)();
     121           0 :                         setAtomName(pci);
     122           0 :                         return MAL_SUCCEED;
     123             :                 }
     124             :                 break;
     125           0 :         case 't':
     126           0 :                 if (idcmp("tostr", name) == 0 && pci->argc == 1) {
     127           0 :                         BATatoms[tpe].atomToStr = (ssize_t (*)(str *, size_t *, const void *, bool))pci->fcn;
     128           0 :                         setAtomName(pci);
     129           0 :                         return MAL_SUCCEED;
     130             :                 }
     131             :                 break;
     132           0 :         case 'u':
     133           0 :                 if (idcmp("unfix", name) == 0 && pci->argc == 1) {
     134           0 :                         BATatoms[tpe].atomUnfix = (gdk_return (*)(const void *))pci->fcn;
     135           0 :                         setAtomName(pci);
     136           0 :                         return MAL_SUCCEED;
     137             :                 }
     138             :                 break;
     139           0 :         case 'r':
     140           0 :                 if (idcmp("read", name) == 0 && pci->argc == 1) {
     141           0 :                         BATatoms[tpe].atomRead = (void *(*)(void *, size_t *, stream *, size_t))pci->fcn;
     142           0 :                         setAtomName(pci);
     143           0 :                         return MAL_SUCCEED;
     144             :                 }
     145             :                 break;
     146           0 :         case 'w':
     147           0 :                 if (idcmp("write", name) == 0 && pci->argc == 1) {
     148           0 :                         BATatoms[tpe].atomWrite = (gdk_return (*)(const void *, stream *, size_t))pci->fcn;
     149           0 :                         setAtomName(pci);
     150           0 :                         return MAL_SUCCEED;
     151             :                 }
     152             :                 break;
     153             :         }
     154             :         return MAL_SUCCEED;
     155             : }
     156             : /*
     157             :  * Atoms are constructed incrementally in the kernel using the
     158             :  * ATOMallocate function. It takes an existing type as a base
     159             :  * to derive a new one.
     160             :  * The most tedisous work is to check the signature types of the functions
     161             :  * acceptable for the kernel.
     162             :  */
     163             : 
     164             : str
     165           4 : malAtomDefinition(const char *name, int tpe)
     166             : {
     167             :         int i;
     168             : 
     169           4 :         if (strlen(name) >= IDLENGTH) {
     170           0 :                 throw (SYNTAX, "atomDefinition", "Atom name '%s' too long", name);
     171             :         }
     172           4 :         if (ATOMindex(name) >= 0) {
     173             :                 return MAL_SUCCEED;
     174             :         }
     175           4 :         if (tpe < 0 || tpe >= GDKatomcnt) {
     176           0 :                 throw(TYPE, "atomDefinition", "Undefined atom inheritance '%s'", name);
     177             :         }
     178           4 :         if (strlen(name) >= sizeof(BATatoms[0].name))
     179           0 :                 throw(TYPE, "atomDefinition", "Atom name too long '%s'", name);
     180             : 
     181           4 :         i = ATOMallocate(name);
     182           4 :         if (is_int_nil(i))
     183           0 :                 throw(TYPE,"atomDefinition", SQLSTATE(HY013) MAL_MALLOC_FAIL);
     184             :         /* overload atom ? */
     185           4 :         if (tpe) {
     186           4 :                 BATatoms[i] = BATatoms[tpe];
     187           4 :                 strcpy_len(BATatoms[i].name, name, sizeof(BATatoms[i].name));
     188           4 :                 BATatoms[i].storage = ATOMstorage(tpe);
     189             :         } else { /* cannot overload void atoms */
     190           0 :                 BATatoms[i].storage = i;
     191           0 :                 BATatoms[i].linear = false;
     192             :         }
     193             :         return MAL_SUCCEED;
     194             : }
     195             : /*
     196             :  * User defined modules may introduce fixed sized types
     197             :  * to store information in BATs.
     198             :  */
     199         254 : int malAtomSize(int size, const char *name)
     200             : {
     201             :         int i = 0;
     202             : 
     203         254 :         i = ATOMindex(name);
     204         254 :         BATatoms[i].storage = i;
     205         254 :         BATatoms[i].size = size;
     206         254 :         assert_shift_width(ATOMelmshift(ATOMsize(i)), ATOMsize(i));
     207         254 :         return i;
     208             : }
     209             : 
     210             : void
     211         264 : mal_atom_reset(void)
     212             : {
     213             :         int i;
     214             :         for( i = 0; i < GDKatomcnt; i++)
     215             :         if( BATatoms[i].atomNull){
     216             :                 // TBD
     217             :         }
     218         264 : }

Generated by: LCOV version 1.14