home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / m0 / part03 / element.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-06  |  4.4 KB  |  180 lines

  1. /*
  2.     element.h
  3. */
  4. /*  Copyright (c) 1994 Christian F. Tschudin. All rights reserved.
  5.  
  6.     Distributed under the terms of the GNU General Public License
  7.     version 2 of june 1991 as published by the Free Software
  8.     Foundation, Inc.
  9.  
  10.              This file is part of M0.
  11.  
  12. M0 is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY.  No author or distributor accepts responsibility to anyone for
  14. the consequences of using it or for whether it serves any particular
  15. purpose or works at all, unless he says so in writing.  Refer to the GNU
  16. General Public License for full details. 
  17.  
  18. Everyone is granted permission to copy, modify and redistribute M0, but
  19. only under the conditions described in the GNU General Public License. 
  20. A copy of this license is supposed to have been given to you along with
  21. M0 so you can know your rights and responsibilities.  It should be in a
  22. file named LICENSE.  Among other things, the copyright notice and this
  23. notice must be preserved on all copies.  */
  24.  
  25. #ifndef ELEMENT_H
  26. #define ELEMENT_H
  27.  
  28. #include "std.h"
  29.  
  30. #define MAXLOCALS    256
  31. #define MAXGLOBALS    8192    /* must fit into a short */
  32.  
  33. typedef struct mproc_s *mproc;
  34.  
  35. #ifdef __MSDOS__
  36.   typedef struct element_s huge *eptr;
  37. #else
  38.   typedef struct element_s *eptr;
  39. #endif
  40. typedef short eindex;        /* index in element table */
  41. typedef void (*submitfct)(mproc p, void *data, eindex m);
  42. typedef void (*receivefct)(int fd, eindex *msgr, eindex *orig);
  43.  
  44. /* adjust type_names (in l_elemnt.c) if you change this sequence: */
  45. enum {
  46.   T_EMPTY=0, T_NULL, T_INT, T_TIME, T_NAME, T_KEY, T_ARRAY,
  47.   T_STRING, T_DICT, T_PROC, T_MARK, T_QUEUE, T_CHANNEL, LAST_TYPE
  48. };
  49.  
  50.  
  51. /* attributes: */
  52. #define A_READ        0x01
  53. #define A_WRITE        0x02
  54. #define A_EXEC        0x04
  55. #define A_EXECUTABLE    0x08
  56. #define A_SUB        0x10    /* sub for name/key/array/dict/string */
  57. #define A_FRAG        0x20    /* for strings only */
  58. /*
  59. #define A_COPYONWRITE    0x40
  60. */
  61. #define A_VISITED    0x80
  62.  
  63. #define A_ALL        (A_READ|A_WRITE|A_EXEC)
  64.  
  65. /* --------------------------------------------------------------------- */
  66.  
  67. /* type specific data structures, no more than 8 bytes */
  68.  
  69. struct array_s {
  70.     uint alen; /* number of allocated entries */
  71.     eindex *a;
  72. };
  73.  
  74.  
  75. struct chan_s {
  76.     void *data;
  77.     submitfct submit;
  78. };
  79.  
  80. struct dict_s {
  81.     uint alen;    /* number of allocated entries */
  82.     eindex *d;    /* array of 2*alen eindex entries */
  83. };
  84.  
  85. struct frag_s {
  86.     eindex f[2];    /* the two fragments */
  87. };
  88.  
  89. struct name_s {
  90.     eindex next;    /* used for external hashing */
  91.     union {
  92.         byteptr s;
  93. #define SHORTNAMELEN    6    /* (2*sizeof(byteptr)-2) */
  94.         byte n[SHORTNAMELEN];    /* short names with length <= 6 */
  95.     } u;
  96. };
  97. /* note: keys are stored as 8-byte names i.e., under V.nam.u.s */
  98.  
  99. struct proc_s {
  100.     ushort pop, push; /* number of elements cons. and prod. */
  101.     int (*fct)();
  102. };
  103.  
  104. struct queue_s {
  105.     mproc head;
  106.     mproc tail;
  107. };
  108.  
  109. struct string_s {
  110.     uint alen; /* length of allocated buffer */
  111.     byteptr s;
  112. };
  113.  
  114. struct sub_s {
  115.     uint offset;
  116.     eindex e;
  117. };
  118.  
  119. struct time_s {
  120.     uint sec;    /* seconds since 1970-01-01 */
  121.     uint usec;    /* micro seconds */
  122. };
  123.  
  124.  
  125. /* --------------------------------------------------------------------- */
  126.  
  127. struct element_s {    /* 16 bytes:        */
  128.     byte T;    /*   1 byte   type    */
  129.     byte A;    /*   1 byte   attribute    */
  130.     ushort R;    /*   2 bytes  reference count */
  131.     uint L;        /*   4 bytes  length    */
  132.     union {            /*   8 bytes  value:    */
  133.         long        i;        /* integer */
  134.         struct array_s    arr;
  135.         struct chan_s    cha;
  136.         struct dict_s    dic;
  137.         struct frag_s    fra;
  138.         struct name_s    nam;
  139.         struct proc_s    pro;
  140.         struct queue_s    que;
  141.         struct string_s    str;
  142.         struct sub_s    sub;
  143.         struct time_s    tim;
  144.     } V;
  145. };
  146.  
  147. /* --------------------------------------------------------------------- */
  148.  
  149. #define eptype(ep)    ((ep)->T)
  150. #define eplen(ep)    ((ep)->L)
  151. #define epattr(ep)    ((ep)->A)
  152. #define eprefcnt(ep)    ((ep)->R)
  153.  
  154. #define epis_str(ep)    (eptype(ep)==T_STRING \
  155.              || eptype(ep)==T_SUBSTR \
  156.              || eptype(ep)==T_FRAGSTR)
  157.  
  158. #define gaddr(ei)    (global-(ei)-1)
  159. #define eaddr(p,ei)    ((ei)<0 ? gaddr(ei) : p->local+(ei)-1)
  160.  
  161. #define etype(p,ei)    eptype(eaddr(p,ei))
  162. #define elen(p,ei)    eplen(eaddr(p,ei))
  163. #define eattr(p,ei)    epattr(eaddr(p,ei))
  164. #define erefcnt(p,ei)    eprefcnt(eaddr(p,ei))
  165.  
  166. #define is_string(p,ei)    epis_string(eaddr(p,ei))
  167.  
  168. #define incref(p,ei)    erefcnt(p,ei)++
  169. #define increfp(ep)    eprefcnt(ep)++
  170.  
  171. #define decrefp(p,ei,ep) if (ei && eptype(ep)!=T_EMPTY) {    \
  172.                 eprefcnt(ep)--;            \
  173.                 if (eprefcnt(ep) == 0)        \
  174.                     free_element(p,ei);    \
  175.             }
  176.  
  177. /* ---------------------------------------------------------------------- */
  178.  
  179. #endif
  180.