home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / emerald / emrldsys.lha / Kernel / Checkpoint / replicant.c < prev   
Encoding:
C/C++ Source or Header  |  1990-08-17  |  4.9 KB  |  176 lines

  1. #ifdef CHECKPOINT
  2. /* @(#)replicant.c    1.4 4/5/89 - emerald replicant abstraction
  3.    Clinton L. Jeffery
  4.    last edit: 4/28/88
  5.  
  6.    Replicants are *really* _*honestly*_ (I mean it) just a hunk of RAM.
  7.    Pretty much.  They are objects which haven't 'unpacked', plus a header.
  8.  
  9.    Operations (most implemented as #defines in replicant.h):
  10.    read/write from replicant to disk file
  11.    read/write from memory to replicant
  12.    allocate/init replicant
  13.    destroy/dealloc replicant
  14.  
  15.    These are fairly low level.  Two higher level routines are sort-of
  16.    called for:
  17.    regenerate (replicant => primary object, a.k.a. RecoverItemHandler), and
  18.    construct  (replicant => local read-only *replica*)
  19.  
  20.    Everyone has to invent their own terminology; mine is shamelessly
  21.    stolen from "Blade Runner".  They really were to be structurally
  22.    different from the original (as opposed to a replica, which retains
  23.    the structure of the object)...they were never to be unpacked unless
  24.    there was an actual regeneration going on...but norm quickly changed
  25.    all that.
  26. */
  27. #include <sys/file.h>
  28. #include <stdio.h>
  29. #include <ndbm.h>
  30. #include "Kernel/h/system.h"
  31. #include "Kernel/h/assert.h"
  32. #include "Kernel/h/macros.h"
  33. #include "Kernel/h/errMsgs.h"
  34. #include "Kernel/h/mmCodes.h"
  35. #include "Kernel/h/emTypes.h"
  36. #include "Kernel/h/timerTypes.h"
  37. #include "Kernel/h/kmdTypes.h"
  38. #include "Kernel/h/kEvents.h"
  39. #include "Kernel/h/emCodes.h"
  40. #include "Kernel/h/mmMsgTypes.h"
  41. #include "Kernel/h/lmTypes.h"
  42. #include "Kernel/h/emkDefs.h"
  43. #include "Kernel/h/lmCodes.h"
  44. #include "Kernel/h/hotsTypes.h"
  45. #include "Kernel/h/map.h"
  46. #include "Kernel/h/set.h"
  47. #include "Kernel/h/expandArray.h"
  48. #include "Kernel/h/consts.h"
  49. #include "Kernel/h/utils.h"
  50. #include "Kernel/h/replicant.h"
  51. #include "Kernel/h/cPLog.h"
  52.  
  53. extern char *emalloc();
  54.  
  55. replicantPtr CP_replicant;
  56.  
  57. CP_InitReplicant()
  58. {
  59.   if((CP_replicant = (replicantPtr) malloc(sizeof(replicant)))==NULL)
  60.     FAIL("malloc #1 fails in CP_InitReplicant\n");
  61.   if((CP_replicant->image = malloc(MINREPLICANTSIZE))==NULL)
  62.     FAIL("malloc #2 fails in CP_InitReplicant\n");
  63.   CP_replicant->ptr = CP_replicant->size = 0;
  64.   CP_replicant->all = MINREPLICANTSIZE;
  65.   SUCCEED;
  66. }
  67.  
  68. replicantPtr replicant_diskread(ote)
  69. GODP ote;
  70. {
  71.   int entrySize;
  72.   replicantPtr rp;
  73.  
  74.   entrySize = ((int)(ote->cPSize));
  75.   KMDTrace("Recover", 5, "Replicant diskread, entrysize %d\n",entrySize);
  76.  
  77.   /* seek into the logfile */
  78.   if (fseek(LF, ote->cPOffset, 0)== -1) {
  79.     ErrMsg("replicant diskread cannot fseek to offset %d, supplied by dbase\n",
  80.        ote->cPOffset);
  81.     KMDTrace("Recover", 3, "replicant_diskread fseek fails\n");
  82.     return ((replicantPtr)(-1));
  83.   }
  84.  
  85.   /* allocate space, read and verify the entry */
  86.   if (((rp = (replicantPtr) emalloc(sizeof(replicant)))==NULL) ||
  87.       (fread((char *)rp,sizeof(replicant),1,LF)!=1) ||
  88.       ((rp->image = emalloc((unsigned) entrySize))==NULL) ||
  89.       (fread(rp->image,1,entrySize,LF)!=entrySize) ||
  90.       (rp->rep_Signature!=REPSIGNATURE) ||
  91.       (rp->nextOID!=ote->ownOID)) {
  92.     KMDTrace("Checkpoint", 3, "replicant_diskread of entry fails\n");
  93.     return ((replicantPtr)(-1));
  94.   }
  95.   KMDTrace("Recover", 5, "Replicant diskread reports successful completion\n");
  96.  
  97.   rp->ptr = 0;
  98.   rp->size = rp->all = entrySize;
  99.   return rp;
  100. }
  101.  
  102. /* write replicant rp to file f */
  103. long replicant_diskwrite(rp,f)
  104. replicantPtr rp;
  105. FILE *f;
  106. {
  107.   int retval,siz = (rp)->size;
  108.   retval = ftell(f);
  109.   if((fwrite((char *)(rp),sizeof(replicant),1,(f))!=1) ||
  110.      (fwrite(((replicantPtr)(rp))->image,sizeof(char),siz,(f))!=siz) ||
  111.      (fflush(f)==EOF)) {
  112.     FAIL("replicant_diskwrite fails\n");
  113.   }
  114.   return retval;
  115. }
  116.  
  117. /* write buffer b into replicant r */
  118. replicant_write(r,b,s)
  119. replicantPtr r;
  120. char *b;
  121. int s;
  122. {
  123.   if(r->ptr + s > r->all) {
  124.     register int newsize;
  125.     register char *newimage;
  126.  
  127.     newsize = (s+r->all)<<1;
  128.     if((newimage = malloc(newsize))==0) {
  129.       FAIL("out of memory in replicant write re-alloc!  die...die...\n");
  130.     }
  131.     bcopy(r->image, newimage, (int)r->ptr);
  132.     r->all = newsize;
  133.     free(r->image);
  134.     r->image = newimage;
  135.   }
  136.   bcopy(b,r->image+r->ptr,s);
  137.   r->ptr  += s;
  138.   if(r->ptr>r->size) r->size = r->ptr;
  139.   SUCCEED;
  140. }
  141.  
  142. replicant_seek(r,offset,fromwhere)
  143. replicantPtr r;
  144. long offset,fromwhere;
  145. {
  146.   if(r == NULL) FAIL("replicant_seek on a NULL handle\n");
  147.   switch((int)fromwhere) {
  148.   case 0: /* seek from beginning */
  149.     if ((offset>r->size) || (offset<0)){
  150.       FAIL("replicant_seek is seeking out of range\n");
  151.     } else {
  152.       r->ptr = offset;
  153.     }
  154.     break;
  155.   case 1: /* seek from current offset */
  156.     if ((offset+r->ptr<0) ||
  157.     (offset+r->ptr>r->size)) {
  158.       FAIL("replicant_seek is seeking out of range\n");
  159.     } else {
  160.       r->ptr += offset;
  161.     }
  162.     break;
  163.   case 2: /* seek from end */
  164.     if ((offset+r->size<0) || (offset>0)) {
  165.       FAIL("replicant_seek is seeking out of range\n");
  166.     } else {
  167.       r->ptr = r->size+offset;
  168.     }
  169.     break;
  170.   default:
  171.     FAIL("replicant_seek gets invalid direction != [012]\n");
  172.   }
  173.   SUCCEED;
  174. }
  175. #endif CHECKPOINT
  176.