tags:

views:

19

answers:

0

hi guys i need help understanding what this piece of code actually does as it is a part of my project i am stuck here. the code is from libckpt on solaris.

/**************************************************************
 * function: write_heap
 * args: map_fd  -- file descriptor for map file
 *       data_fd -- file descriptor for data file
 * returns: no. of chunks written on success, -1 on failure
 * side effects: writes all included segments of the heap to ckpt files
 * misc.: If we are forking and copyonwrite is set, we will write the
          heap from bottom to top, moving the brk pointer up each time
          so that we don't get a page copied if the
 * called from: take_ckpt()
 *************************************************************/
static int write_heap(int map_fd, int data_fd)
{
  Dlist curptr, endptr;
  int no_chunks=0, pn;
  long size;
  caddr_t stop, addr;

  if(ckptflags.incremental){       /*-- incremental checkpointing on? --*/
    endptr = ckptglobals.inc_list->main->flink;

    /*-- for each included chunk of the heap --*/
    for(curptr = ckptglobals.inc_list->main->blink->blink;
        curptr != endptr; curptr = curptr->blink){

      /*-- write out the last page in the included chunk --*/
      stop = curptr->addr;
      pn = ((long)curptr->stop - (long)sys.DATASTART) / PAGESIZE;

      if(isdirty(pn)){ 
        addr = (caddr_t)max((long)curptr->addr,
                            (long)((pn * PAGESIZE) + sys.DATASTART));
        size = (long)curptr->stop - (long)addr;

        debug(stderr, "DEBUG: Writing heap from 0x%x to 0x%x, pn = %d\n",
                                                   addr, addr+size, pn);
        if(write_chunk(addr, size, map_fd, data_fd) == -1){
          return -1;
        }

        if((int)addr > (int)(&end) && ckptflags.enhanced_fork){
          brk(addr);
        }
        no_chunks++;
      }

      /*-- write out all the whole pages in the middle of the chunk --*/
      for(pn--; pn * PAGESIZE + sys.DATASTART >= stop; pn--){
        if(isdirty(pn)){
          addr = (caddr_t)((pn * PAGESIZE) + sys.DATASTART);
          debug(stderr, "DEBUG: Writing heap from 0x%x to 0x%x, pn = %d\n",
                                                 addr, addr+PAGESIZE, pn);
          if(write_chunk(addr, PAGESIZE, map_fd, data_fd) == -1){
            return -1;
          }
          if((int)addr > (int)(&end) && ckptflags.enhanced_fork){
            brk(addr);
          }
          no_chunks++;
        }
      }

      /*-- write out the first page in the included chunk --*/
      addr = curptr->addr;
      size = ((pn+1) * PAGESIZE + sys.DATASTART) - addr;

      if(size > 0 && (isdirty(pn))){
        debug(stderr, "DEBUG: Writing heap from 0x%x to 0x%x\n", addr, addr+size);
        if(write_chunk(addr, size, map_fd, data_fd) == -1){
          return -1;
        }

        if((int)addr > (int)(&end) && ckptflags.enhanced_fork){
          brk(addr);
        }
        no_chunks++;
      }
    }
  }
  else{  /*-- incremental checkpointing off! --*/
    endptr = ckptglobals.inc_list->main->blink;

    /*-- for each included chunk of the heap --*/
    for(curptr = ckptglobals.inc_list->main->flink->flink;
        curptr != endptr; curptr = curptr->flink){
      debug(stderr, "DEBUG: saving memory from 0x%x to 0x%x\n", 
                         curptr->addr, curptr->addr+curptr->size);

      if(write_chunk(curptr->addr, curptr->size, map_fd, data_fd) == -1){
        return -1;
      }
      if((int)addr > (int)(&end) && ckptflags.enhanced_fork){
        brk(addr);
      }
      no_chunks++;
    }
  }

  return no_chunks;
}