home *** CD-ROM | disk | FTP | other *** search
- #ifdef CHECKPOINT
- /* @(#)replicant.c 1.4 4/5/89 - emerald replicant abstraction
- Clinton L. Jeffery
- last edit: 4/28/88
-
- Replicants are *really* _*honestly*_ (I mean it) just a hunk of RAM.
- Pretty much. They are objects which haven't 'unpacked', plus a header.
-
- Operations (most implemented as #defines in replicant.h):
- read/write from replicant to disk file
- read/write from memory to replicant
- allocate/init replicant
- destroy/dealloc replicant
-
- These are fairly low level. Two higher level routines are sort-of
- called for:
- regenerate (replicant => primary object, a.k.a. RecoverItemHandler), and
- construct (replicant => local read-only *replica*)
-
- Everyone has to invent their own terminology; mine is shamelessly
- stolen from "Blade Runner". They really were to be structurally
- different from the original (as opposed to a replica, which retains
- the structure of the object)...they were never to be unpacked unless
- there was an actual regeneration going on...but norm quickly changed
- all that.
- */
- #include <sys/file.h>
- #include <stdio.h>
- #include <ndbm.h>
- #include "Kernel/h/system.h"
- #include "Kernel/h/assert.h"
- #include "Kernel/h/macros.h"
- #include "Kernel/h/errMsgs.h"
- #include "Kernel/h/mmCodes.h"
- #include "Kernel/h/emTypes.h"
- #include "Kernel/h/timerTypes.h"
- #include "Kernel/h/kmdTypes.h"
- #include "Kernel/h/kEvents.h"
- #include "Kernel/h/emCodes.h"
- #include "Kernel/h/mmMsgTypes.h"
- #include "Kernel/h/lmTypes.h"
- #include "Kernel/h/emkDefs.h"
- #include "Kernel/h/lmCodes.h"
- #include "Kernel/h/hotsTypes.h"
- #include "Kernel/h/map.h"
- #include "Kernel/h/set.h"
- #include "Kernel/h/expandArray.h"
- #include "Kernel/h/consts.h"
- #include "Kernel/h/utils.h"
- #include "Kernel/h/replicant.h"
- #include "Kernel/h/cPLog.h"
-
- extern char *emalloc();
-
- replicantPtr CP_replicant;
-
- CP_InitReplicant()
- {
- if((CP_replicant = (replicantPtr) malloc(sizeof(replicant)))==NULL)
- FAIL("malloc #1 fails in CP_InitReplicant\n");
- if((CP_replicant->image = malloc(MINREPLICANTSIZE))==NULL)
- FAIL("malloc #2 fails in CP_InitReplicant\n");
- CP_replicant->ptr = CP_replicant->size = 0;
- CP_replicant->all = MINREPLICANTSIZE;
- SUCCEED;
- }
-
- replicantPtr replicant_diskread(ote)
- GODP ote;
- {
- int entrySize;
- replicantPtr rp;
-
- entrySize = ((int)(ote->cPSize));
- KMDTrace("Recover", 5, "Replicant diskread, entrysize %d\n",entrySize);
-
- /* seek into the logfile */
- if (fseek(LF, ote->cPOffset, 0)== -1) {
- ErrMsg("replicant diskread cannot fseek to offset %d, supplied by dbase\n",
- ote->cPOffset);
- KMDTrace("Recover", 3, "replicant_diskread fseek fails\n");
- return ((replicantPtr)(-1));
- }
-
- /* allocate space, read and verify the entry */
- if (((rp = (replicantPtr) emalloc(sizeof(replicant)))==NULL) ||
- (fread((char *)rp,sizeof(replicant),1,LF)!=1) ||
- ((rp->image = emalloc((unsigned) entrySize))==NULL) ||
- (fread(rp->image,1,entrySize,LF)!=entrySize) ||
- (rp->rep_Signature!=REPSIGNATURE) ||
- (rp->nextOID!=ote->ownOID)) {
- KMDTrace("Checkpoint", 3, "replicant_diskread of entry fails\n");
- return ((replicantPtr)(-1));
- }
- KMDTrace("Recover", 5, "Replicant diskread reports successful completion\n");
-
- rp->ptr = 0;
- rp->size = rp->all = entrySize;
- return rp;
- }
-
- /* write replicant rp to file f */
- long replicant_diskwrite(rp,f)
- replicantPtr rp;
- FILE *f;
- {
- int retval,siz = (rp)->size;
- retval = ftell(f);
- if((fwrite((char *)(rp),sizeof(replicant),1,(f))!=1) ||
- (fwrite(((replicantPtr)(rp))->image,sizeof(char),siz,(f))!=siz) ||
- (fflush(f)==EOF)) {
- FAIL("replicant_diskwrite fails\n");
- }
- return retval;
- }
-
- /* write buffer b into replicant r */
- replicant_write(r,b,s)
- replicantPtr r;
- char *b;
- int s;
- {
- if(r->ptr + s > r->all) {
- register int newsize;
- register char *newimage;
-
- newsize = (s+r->all)<<1;
- if((newimage = malloc(newsize))==0) {
- FAIL("out of memory in replicant write re-alloc! die...die...\n");
- }
- bcopy(r->image, newimage, (int)r->ptr);
- r->all = newsize;
- free(r->image);
- r->image = newimage;
- }
- bcopy(b,r->image+r->ptr,s);
- r->ptr += s;
- if(r->ptr>r->size) r->size = r->ptr;
- SUCCEED;
- }
-
- replicant_seek(r,offset,fromwhere)
- replicantPtr r;
- long offset,fromwhere;
- {
- if(r == NULL) FAIL("replicant_seek on a NULL handle\n");
- switch((int)fromwhere) {
- case 0: /* seek from beginning */
- if ((offset>r->size) || (offset<0)){
- FAIL("replicant_seek is seeking out of range\n");
- } else {
- r->ptr = offset;
- }
- break;
- case 1: /* seek from current offset */
- if ((offset+r->ptr<0) ||
- (offset+r->ptr>r->size)) {
- FAIL("replicant_seek is seeking out of range\n");
- } else {
- r->ptr += offset;
- }
- break;
- case 2: /* seek from end */
- if ((offset+r->size<0) || (offset>0)) {
- FAIL("replicant_seek is seeking out of range\n");
- } else {
- r->ptr = r->size+offset;
- }
- break;
- default:
- FAIL("replicant_seek gets invalid direction != [012]\n");
- }
- SUCCEED;
- }
- #endif CHECKPOINT
-