예제 #1
0
파일: distdrop.c 프로젝트: zarch/distdrop
int print_dir ( cell_map *map, seg_map *seg )
{
    CELL dir_cell = 0;
    FILE *fp;
    fp = fopen(map->name, "w");
    fprintf(fp, "# -*- coding: utf-8 -*-\n");

    for ( int row = 0; row < seg->nrows; row++ )
    {
        //printf("%d: ", row);
        for ( int col = 0; col < seg->ncols; col++ )
        {
            segment_get( &map->seg, &dir_cell, row, col );
            switch ( dir_cell )
            {
                // utf8 symbols: ⬅ ⬆ ⬇ ⬈ ⬉ ⬊ ⬋ ⬚ ← ↑ → ↓ ↗ ↖ ↘ ↙ ⬛ ⬣
                case NULL_MAP: fprintf ( fp, " ⬚" ); break;
                case 1: fprintf ( fp, " ↖" ); break;
                case 2: fprintf ( fp, " ↑" ); break;
                case 3: fprintf ( fp, " ↗" ); break;
                case 4: fprintf ( fp, " ←" ); break;
                case 5: fprintf ( fp, " ⬣" ); break;
                case 6: fprintf ( fp, " →" ); break;
                case 7: fprintf ( fp, " ↙" ); break;
                case 8: fprintf ( fp, " ↓" ); break;
                case 9: fprintf ( fp, " ↘" ); break;
            }
        }
        fprintf ( fp, "\n" );
    }
    fclose(fp);
    return 0;
}
예제 #2
0
파일: segment.c 프로젝트: sathnaga/ltp
/*
 * segment_show() -- show specifed segment, or all, if none specified.
 */
int segment_show(char *name)
{
	glctx_t *gcp = &glctx;
	segment_t *segp, **segpp;
	bool header;

	if (name != NULL) {
		segp = segment_get(name);
		if (segp == NULL) {
			fprintf(stderr, "%s:  no such segment:  %s\n",
				gcp->program_name, name);
			return SEG_ERR;
		}
		show_one_segment(segp, false);
		return SEG_OK;
	}

	/*
	 * show all
	 */
	header = true;
	for (segpp = gcp->seglist; (segp = *segpp); ++segpp) {
		if (segp->seg_type != SEGT_NONE) {
			show_one_segment(segp, header);
			header = false;	/* first time only */
		}
	}

	return SEG_OK;

}
예제 #3
0
int
mark_visible_points(struct point *head, SEGMENT * seg_out_p, int row_viewpt,
		    int col_viewpt, double color_factor, double COLOR_SHIFT)
{
    struct point *PT_TO_MARK;
    FCELL data;

    PT_TO_MARK = head;

    while (PT_TO_MARK != NULL) {	/*        loop till end of list   */
	segment_get(seg_out_p, &data,
		    row_viewpt - PT_TO_MARK_Y, PT_TO_MARK_X + col_viewpt);

	if (data != (FCELL) 1) {	/* point has not been deleted previously        */
	    /* old value    
	       data = (FCELL ) (PT_TO_MARK_INCL* 57.3 * color_factor 
	       + COLOR_SHIFT);
	       end of old data      */
	    data = (FCELL) (PT_TO_MARK_INCL * 57.325 + 90.0);
	    segment_put(seg_out_p, &data,
			row_viewpt - PT_TO_MARK_Y, PT_TO_MARK_X + col_viewpt);
	}

	PT_TO_MARK = NEXT_PT_TO_MARK;	/* next visible point   */
    }
    return 0;
}
예제 #4
0
struct point *make_point( double orientation, double inclination,
			  int y, int x, int row_viewpt, int col_viewpt, 
			  SEGMENT *seg_out_p, CELL cell_no)
{
    struct point *NEW_PT;
    CELL data_read, data_to_write;
    char *value_read, *value_to_write;
    
    NEW_PT = (struct point *) G_malloc(sizeof(struct point));
    NEW_PT_ORIENTATION = orientation;
    NEW_PT_INCLINATION = inclination;
    NEW_PT_Y           = y;
    NEW_PT_X           = x;
    NEXT_NEW_PT        = NULL_HERE;
    
    value_read = (char*) &data_read;
    value_to_write = (char*) &data_to_write;
    data_to_write = 0;
    segment_get (seg_out_p, value_read,
		 row_viewpt - y, x + col_viewpt);
    if (data_read != cell_no)
	segment_put (seg_out_p, value_to_write,
		     row_viewpt - y, x + col_viewpt);
    return(NEW_PT);
}
예제 #5
0
파일: distdrop.c 프로젝트: zarch/distdrop
static int print_map_with_seg ( cell_map *map, seg_map *seg )
{
    CELL map_cell = 0;
    FILE *fp;
    char *format;

    fp = fopen(map->name, "w");
    fprintf(fp, "# -*- coding: utf-8 -*-\n");

    if(map->type == CELL_TYPE)
        format = " %d";
    if(map->type == FCELL_TYPE)
        format = " %f";
    if(map->type == DCELL_TYPE)
        format = " %f";

    for ( int row = 0; row < seg->nrows; row++ ){
        for ( int col = 0; col < seg->ncols; col++ ){
            segment_get( &map->seg, &map_cell, row, col );
            fprintf ( fp, format, map_cell );
        }
        fprintf ( fp, "\n" );
    }
    fclose(fp);
    return 0;
}
예제 #6
0
파일: segment.c 프로젝트: sathnaga/ltp
/*
 * segment_unmap() -  unmap the specified segment, if any, from seg_start
 *                    to seg_start+seg_lenth.  Leave the segment in the
 *                    table;
 */
int segment_unmap(char *name)
{
	glctx_t *gcp = &glctx;
	segment_t *segp;

	segp = segment_get(name);
	if (segp == NULL) {
		fprintf(stderr, "%s:  no such segment:  %s\n",
			gcp->program_name, name);
		return SEG_ERR;
	}

	if (segp->seg_start == MAP_FAILED)
		return SEG_OK;	/* silent success */

	switch (segp->seg_type) {
	case SEGT_ANON:
	case SEGT_FILE:
		munmap(segp->seg_start, segp->seg_length);
		break;

	case SEGT_SHM:
		//TODO:  shmdt()...
		break;
		/* Handle default to get rid of -Wswitch-enum */
	default:
		break;
	}

	segp->seg_start = MAP_FAILED;

	return SEG_OK;
}
예제 #7
0
int dseg_get(DSEG * dseg, double *value, int row, int col)
{
    if (segment_get(&(dseg->seg), (DCELL *) value, row, col) < 0) {
	G_warning("dseg_get(): could not read segment file");
	return -1;
    }
    return 0;
}
예제 #8
0
파일: segment.c 프로젝트: sathnaga/ltp
/*
 * segment_mbind() - set memory policy for a range of specified segment
 *
 * NOTE:  offset is relative to start of mapping, not start of file
 */
int
segment_mbind(char *name, range_t * range, int policy,
	      nodemask_t * nodemask, int flags)
{
	glctx_t *gcp = &glctx;
	segment_t *segp;
	char *start;
	off_t offset;
	size_t length, maxlength;
	int ret;

	segp = segment_get(name);
	if (segp == NULL) {
		fprintf(stderr, "%s:  no such segment:  %s\n",
			gcp->program_name, name);
		return SEG_ERR;
	}

	if (segp->seg_start == MAP_FAILED) {
		fprintf(stderr, "%s:  segment %s not mapped\n",
			gcp->program_name, name);
		return SEG_ERR;
	}

	offset = round_down_to_pagesize(range->offset);
	if (offset >= segp->seg_length) {
		fprintf(stderr, "%s:  offset %ld is past end of segment %s\n",
			gcp->program_name, offset, name);
		return SEG_ERR;
	}

	start = segp->seg_start + offset;
	maxlength = segp->seg_length - offset;

	length = range->length;
	if (length)
		length = round_up_to_pagesize(length);

	/*
	 * note:  we silently truncate to max length [end of segment]
	 */
	if (length == 0 || length > maxlength)
		length = maxlength;

	ret = mbind(segp->seg_start + offset, length, policy, nodemask->n,
		    NUMA_NUM_NODES, flags);

	if (ret == -1) {
		int err = errno;
		fprintf(stderr, "%s:  mbind() of segment %s failed - %s\n",
			gcp->program_name, name, strerror(err));
		return SEG_ERR;
	}

	return SEG_OK;
}
예제 #9
0
/*
 * segment_register:  register an anon, file or shm segment based on args.
 *	for anon and shm, 'name' = segment name.
 *	for file, 'name' = path name; segment name = basename(path)
 *
 * returns: !0 on success; 0 on failure
 */
int
segment_register(seg_type_t type, char *name, range_t *range, int flags)
{
    glctx_t   *gcp = &glctx;
    segment_t *segp;
    char      *path;

    segp = segment_get(basename(name));	/* ensure unique name */
    if (segp != NULL) {
        fprintf(stderr, "%s:  segment %s already exists\n",
                gcp->program_name, segp->seg_name);
        return SEG_ERR;
    }

    segp = get_seg_slot();
    if (segp == NULL)
        return SEG_ERR;

    path = strdup(name);	/* save a copy */
    segp->seg_name   = strdup(basename(name));
    segp->seg_start  = MAP_FAILED;
    segp->seg_length = round_up_to_pagesize(range->length);
    segp->seg_offset = round_down_to_pagesize(range->offset);
    segp->seg_type   = type;
    segp->seg_flags  = flags;			/* possibly 0 */
    segp->seg_prot   = PROT_READ|PROT_WRITE;	/* default */
    segp->seg_fd     = SEG_FD_NONE;
    segp->seg_shmid  = SHM_ID_NONE;

    switch (type) {
    case SEGT_ANON:
        free(path);
        break;

    case SEGT_FILE:
        segp->seg_path = path;
        return open_file(segp);
        break;

    case SEGT_SHM:
        free(path);
        return get_shm_segment(segp);
        break;

    default:
        free(path);
    }
    return SEG_OK;
}
예제 #10
0
t_status	interface_segment_attribute_permissions(o_syscall*	message)
{
  o_segment*		o;

  if (segment_get(message->u.request.u.segment_attribute_permissions.arg1, &o) != STATUS_OK)
    {
      message->u.reply.error = STATUS_ERROR;
    }
  else
    {
      message->u.reply.error = STATUS_OK;
      message->u.reply.u.segment_attribute_permissions.result1 = o->permissions;
    }

  return (STATUS_OK);
}
예제 #11
0
/*
 * segment_touch() - "touch" [read or write] each page of specified range
 *                   -- from offset to offset+length -- to fault in or to
 *                   test protection.
 * NOTE:  offset is relative to start of mapping, not start of file!
 */
int
segment_touch(char *name, range_t *range, int rw)
{
    glctx_t       *gcp = &glctx;
    segment_t     *segp;
    off_t          offset;
    size_t         length, maxlength;
    unsigned long *memp;
    struct timeval t_start, t_end;

    segp = segment_get(name);
    if (segp == NULL) {
        fprintf(stderr, "%s:  no such segment:  %s\n",
                gcp->program_name, name);
        return SEG_ERR;
    }

    offset    = round_down_to_pagesize(range->offset);
    if (offset >= segp->seg_length) {
        fprintf(stderr, "%s:  offset %ld is past end of segment %s\n",
                gcp->program_name, offset, name);
        return SEG_ERR;
    }

    memp      = (unsigned long*)(segp->seg_start + offset);
    maxlength = segp->seg_length - offset;

    length = range->length;
    if (length)
        length = round_up_to_pagesize(length);

    /*
     * note:  we silently truncate to max length [end of segment]
     */
    if (length == 0 || length > maxlength)
        length = maxlength;

    gettimeofday(&t_start, NULL);
    touch_memory(rw, memp, length);
    gettimeofday(&t_end, NULL);
    printf("%s:  touched %d pages in %6.3f secs\n",
           gcp->program_name, length/gcp->pagesize,
           (float)(tv_diff_usec(&t_start, &t_end))/1000000.0);

    return SEG_OK;
}
예제 #12
0
/*
 * segment_map() -- [re] map() a previously unmapped segment
 *                  no-op if already mapped.
 *                  range only applies to mapped file.
 */
int
segment_map(char *name, range_t *range, int flags)
{
    glctx_t       *gcp = &glctx;
    segment_t     *segp;

    segp = segment_get(name);
    if (segp == NULL) {
        fprintf(stderr, "%s:  no such segment:  %s\n",
                gcp->program_name, name);
        return SEG_ERR;
    }

    if (segp->seg_start != MAP_FAILED) {
        fprintf(stderr, "%s:  segment %s already mapped\n",
                gcp->program_name, name);
        return SEG_OK;	/* treat as success */
    }

    if (flags != 0)
        segp->seg_flags = flags;

    switch (segp->seg_type) {
    case SEGT_ANON:
        return map_anon_segment(segp);
        break;

    case SEGT_FILE:
        if (range != NULL) {
            segp->seg_offset = range->offset;
            segp->seg_length = range->length;
        }
        return map_file_segment(segp);
        break;

    case SEGT_SHM:
        return map_shm_segment(segp);
        break;
    /* Handle default to get rid of -Wswitch-enum */
    default:
        break;
    }

    return SEG_ERR;	/* unrecognized segment type -- shouldn't happen */

}
예제 #13
0
파일: segment.c 프로젝트: sathnaga/ltp
/*
 * segment_remove() - remove the specified segment, if exists.
 */
int segment_remove(char *name)
{
	glctx_t *gcp = &glctx;
	segment_t *segp;

	segp = segment_get(name);
	if (segp == NULL) {
		fprintf(stderr, "%s:  no such segment:  %s\n",
			gcp->program_name, name);
		return SEG_ERR;
	}

	unmap_segment(segp);

	free_seg_slot(segp);

	return SEG_OK;
}
예제 #14
0
extern void segment_PutGet_test(umMem_T memory)
{
        uint32_t array[10];
                       
        for (int i = 0; i < 10; i++) {
                array[i] = (uint32_t) (i * 10);
        }
        
        /* Map segment 8, and store array elements in segment 8 */
        segment_map(memory, 8, 10);
        for(int i = 0; i < 10; i++) {
                segment_put(memory, 8, i, array[i]);
        }
        for(int i = 0; i < 10; i++) {
                if (segment_get(memory, 8, i) != array[i]) {
                        fprintf(stderr, "Offset %d has incorrect value\n", i);
                }
        }
        
        fprintf(stderr, "segment_PutGet is fine with valid inputs\n");
}
예제 #15
0
파일: distdrop.c 프로젝트: zarch/distdrop
static int queue_pixel_core ( move *movements, queue **redo_segments,
                              cell_map *dist, cell_map *elev,
                              cell_map *dir, cell_map *up, cell_map *dw,
                              seg_map *segment_info, int seg, int *count,
                              int **neighbours )
{
    FCELL dist_cell = 0, dist_cell_neig = 0, up_cell = 0, dw_cell = 0;
    FCELL elev_cell = 0, elev_cell_neig = 0;
    //CELL dir_cell = 0;

    elem *el = pop( redo_segments[seg] ), *prev;
    //int *neighbours[NMV] = {{0, 0},{0, 0},};

    G_debug ( 4, "Segment number: %d\n", seg );

    // check if there is pixel to do in the row
    while (el != NULL)
    {
        int row = el->point.row;
        int col = el->point.col;
        segment_get( &dist->seg, &dist_cell, row, col );

        if ( dist_cell >= 0 )
        {
            // get cell neighbours
            get_neighbours ( row, col, movements, neighbours,
                             segment_info->nrows, segment_info->ncols );

            for ( int n = 0; n < ( int ) sizeof ( movements ) ; n++ )
            {
                // TODO: after check if there are performce consegunece to declaire here or not
                int nx = neighbours[n][0];
                int ny = neighbours[n][1];

                //Check if neighbours are inside the region AND the domain
                if ( nx != NULL_NEIG){
                    // get distance value
                    segment_get( &dist->seg, &dist_cell_neig, nx, ny );
                    //Check if distance is in the domain
                    if (dist_cell_neig>0 )
                    {
                        float new_dist = dist_cell + movements[n].dist;
                        if ( dist_cell_neig > new_dist )
                        {
                            // assign the smaller one
                            segment_put ( &dist->seg, &new_dist, nx, ny );
                            segment_put ( &dir->seg, &movements[n].dir, nx, ny );
                            //check if drop is positive or negative
                            segment_get( &elev->seg, &elev_cell, row, col );
                            segment_get( &elev->seg, &elev_cell_neig, nx, ny );
                            float drop = elev_cell_neig - elev_cell;

                            if ( drop >= 0 )
                            {
                                segment_get( &up->seg, &up_cell, row, col );
                                up_cell += drop;
                                segment_put ( &up->seg, &up_cell, nx, ny );
                                //up_cache[nx][ny] = up_cache[1][col] + drop;
                                segment_get( &dw->seg, &dw_cell, row, col );
                                segment_put ( &dw->seg, &dw_cell, nx, ny );
                                //dw_cache[nx][ny] = dw_cache[1][col];
                            }
                            else
                            {
                                segment_get( &up->seg, &up_cell, row, col );
                                segment_put ( &up->seg, &up_cell, nx, ny );
                                //up_cache[nx][ny] = up_cache[1][col];
                                segment_get( &dw->seg, &dw_cell, row, col );
                                dw_cell += drop;
                                segment_put ( &dw->seg, &dw_cell, nx, ny );
                                //dw_cache[nx][ny] = dw_cache[1][col] + drop;
                            }
                            int seg_numb = get_segment_number(nx, ny, segment_info);
                            array_append(seg_numb, nx, ny, redo_segments);
                            *count += 1;
                        }
                    }
                }
            }
        }
        prev = el;
        free(prev);
        el = pop( redo_segments[seg] );
    }
    return 0;
}
예제 #16
0
struct Point *P_Read_Raster_Region_masked(SEGMENT *mask_seg,
				       struct Cell_head *Original,
				       struct bound_box output_box,
				       struct bound_box General,
				       int *num_points, int dim_vect,
				       double mean)
{
    int col, row, startcol, endcol, startrow, endrow, nrows, ncols;
    int pippo, npoints;
    double X, Y;
    struct Point *obs;
    char mask_val;

    pippo = dim_vect;
    obs = (struct Point *)G_calloc(pippo, sizeof(struct Point));

    /* Reading points inside output box and inside General box */

    npoints = 0;
    nrows = Original->rows;
    ncols = Original->cols;

    /* original region = output region
     * -> General box is somewhere inside output region */
    if (Original->north > General.N) {
	startrow = (double)((Original->north - General.N) / Original->ns_res - 1);
	if (startrow < 0)
	    startrow = 0;
    }
    else
	startrow = 0;
    if (Original->south < General.S) {
	endrow = (double)((Original->north - General.S) / Original->ns_res + 1);
	if (endrow > nrows)
	    endrow = nrows;
    }
    else
	endrow = nrows;
    if (General.W > Original->west) {
	startcol = (double)((General.W - Original->west) / Original->ew_res - 1);
	if (startcol < 0)
	    startcol = 0;
    }
    else
	startcol = 0;
    if (General.E < Original->east) {
	endcol = (double)((General.E - Original->west) / Original->ew_res + 1);
	if (endcol > ncols)
	    endcol = ncols;
    }
    else
	endcol = ncols;

    for (row = startrow; row < endrow; row++) {
	for (col = startcol; col < endcol; col++) {

	    segment_get(mask_seg, &mask_val, row, col);
	    if (!mask_val)
		continue;

	    X = Rast_col_to_easting((double)(col) + 0.5, Original);
	    Y = Rast_row_to_northing((double)(row) + 0.5, Original);

	    /* Here, mean is just for asking if obs point is in box */
	    if (Vect_point_in_box(X, Y, mean, &General)) { /* General */
		if (npoints >= pippo) {
		    pippo += dim_vect;
		    obs =
			(struct Point *)G_realloc((void *)obs,
						  (signed int)pippo *
						  sizeof(struct Point));
		}

		/* Storing observation vector */
		obs[npoints].coordX = X;
		obs[npoints].coordY = Y;
		obs[npoints].coordZ = 0;
		npoints++;
	    }
	}
    }

    *num_points = npoints;
    return obs;
}
예제 #17
0
int P_Sparse_Raster_Points(SEGMENT *out_seg, struct Cell_head *Elaboration,
		struct Cell_head *Original, struct bound_box General, struct bound_box Overlap,
		struct Point *obs, double *param, double pe, double pn,
		double overlap, int nsplx, int nsply, int num_points,
		int bilin, double mean)
{
    int i, row, col;
    double X, Y, interpolation, csi, eta, weight, dval;
    int points_in_box = 0;

    /* Reading points inside output region and inside general box */
    /* all points available here are inside the output box,
     * selected by P_Read_Raster_Region_Nulls(), no check needed */

    for (i = 0; i < num_points; i++) {

	X = obs[i].coordX;
	Y = obs[i].coordY;

	/* X,Y are cell center cordinates, MUST be inside General box */
	row = (int) (floor(Rast_northing_to_row(Y, Original)) + 0.1);
	col = (int) (floor((X - Original->west) / Original->ew_res) + 0.1);

	if (row < 0 || row >= Original->rows) {
	    G_fatal_error("row index out of range");
	    continue;
	}

	if (col < 0 || col >= Original->cols) {
	    G_fatal_error("col index out of range");
	    continue;
	}
	points_in_box++;

	G_debug(3, "P_Sparse_Raster_Points: interpolate point %d...", i);
	if (bilin)
	    interpolation =
		dataInterpolateBilin(X, Y, pe, pn, nsplx,
				     nsply, Elaboration->west,
				     Elaboration->south, param);
	else
	    interpolation =
		dataInterpolateBicubic(X, Y, pe, pn, nsplx,
				       nsply, Elaboration->west,
				       Elaboration->south, param);

	interpolation += mean;

	if (Vect_point_in_box(X, Y, interpolation, &Overlap)) {	/* (5) */
	    dval = interpolation;
	}
	else {
	    segment_get(out_seg, &dval, row, col);
	    if ((X > Overlap.E) && (X < General.E)) {
		if ((Y > Overlap.N) && (Y < General.N)) {	/* (3) */
		    csi = (General.E - X) / overlap;
		    eta = (General.N - Y) / overlap;
		    weight = csi * eta;
		    interpolation *= weight;
		    dval += interpolation;
		}
		else if ((Y < Overlap.S) && (Y > General.S)) {	/* (1) */
		    csi = (General.E - X) / overlap;
		    eta = (Y - General.S) / overlap;
		    weight = csi * eta;
		    interpolation *= weight;
		    dval = interpolation;
		}
		else if ((Y >= Overlap.S) && (Y <= Overlap.N)) {	/* (1) */
		    weight = (General.E - X ) / overlap;
		    interpolation *= weight;
		    dval = interpolation;
		}
	    }
	    else if ((X < Overlap.W) && (X > General.W)) {
		if ((Y > Overlap.N) && (Y < General.N)) {	/* (4) */
		    csi = (X - General.W) / overlap;
		    eta = (General.N - Y) / overlap;
		    weight = eta * csi;
		    interpolation *= weight;
		    dval += interpolation;
		}
		else if ((Y < Overlap.S) && (Y > General.S)) {	/* (2) */
		    csi = (X - General.W) / overlap;
		    eta = (Y - General.S) / overlap;
		    weight = csi * eta;
		    interpolation *= weight;
		    dval += interpolation;
		}
		else if ((Y >= Overlap.S) && (Y <= Overlap.N)) {	/* (2) */
		    weight = (X - General.W) / overlap;
		    interpolation *= weight;
		    dval += interpolation;
		}
	    }
	    else if ((X >= Overlap.W) && (X <= Overlap.E)) {
		if ((Y > Overlap.N) && (Y < General.N)) {	/* (3) */
		    weight = (General.N - Y) / overlap;
		    interpolation *= weight;
		    dval += interpolation;
		}
		else if ((Y < Overlap.S) && (Y > General.S)) {	/* (1) */
		    weight = (Y - General.S) / overlap;
		    interpolation *= weight;
		    dval = interpolation;
		}
	    }
	} /* end not in overlap */
	segment_put(out_seg, &dval, row, col);
    }  /* for num_points */

    return 1;
}
예제 #18
0
struct point *hidden_point_elimination (struct point *head,
					double viewpt_elev,
					SEGMENT *seg_in_p,
					SEGMENT *seg_out_p,
					SEGMENT *seg_patt_p, 
					int quadrant, 
					int sign_on_y, int sign_on_x, 
					int row_viewpt, int col_viewpt,
					int patt_flag, CELL cell_no,
					RASTER_MAP_TYPE data_type)
{
    struct point *CHECKED_PT, *BLOCKING_PT, *delete();
    double orientation_neighbor_1,orientation_neighbor_2,
    find_orientation(),inclination_neighbor_1,
    inclination_neighbor_2,interpolated_inclination,
    find_inclination(),find_inclination2(),correct_neighbor_inclination,
    correct_neighbor_orientation,fabs();
    int correct_neighbor_x,correct_neighbor_y,neighbor_1_y,
    neighbor_1_x,neighbor_2_x,neighbor_2_y, uu,vv;
    CELL mask;
    char *value;
    int do_check = 1;
    extern double RADIUS1;
    extern int OFFSETB_SET;
    double del_x, del_y,dist,atan(),sqrt();


    uu = (sign_on_y + sign_on_x)/2;
    vv = (sign_on_y - sign_on_x)/2;
    
    value = (char *) &mask;
    
    
#ifdef DEBUG
    printf ("\nrow = %d, col = %d", row_viewpt, col_viewpt);
    fflush (stdout);
#endif

    /*   move blocking pt. from the 2nd pt till the end	*/
    for(BLOCKING_PT  = SECOND_PT; 
	BLOCKING_PT != NULL; 
	BLOCKING_PT  = NEXT_BLOCKING_PT)
    {

#ifdef DEBUG
      printf ("\nBlocking point y = %d, x = %d", 
	      BLOCKING_PT_Y, BLOCKING_PT_X);
      fflush (stdout);
#endif

	/*   calc coors of the two immediate neighbors on either  */
	/*   side of the blocking point                           */

	if(BLOCKING_PT_X == 0 || BLOCKING_PT_Y == 0)
	{
	    neighbor_1_x = BLOCKING_PT_X - vv;
	    neighbor_1_y = BLOCKING_PT_Y + uu;
	    
	    neighbor_2_x = BLOCKING_PT_X + uu;
	    neighbor_2_y = BLOCKING_PT_Y + vv;
	}
	else
	{
	    neighbor_1_x = BLOCKING_PT_X - uu;
	    neighbor_1_y = BLOCKING_PT_Y - vv;
	    
	    neighbor_2_x = BLOCKING_PT_X + vv;
	    neighbor_2_y = BLOCKING_PT_Y - uu;
	}
	
	/*   find orientation and inclination for both neighbors	*/
	orientation_neighbor_1 = 
	    find_orientation(neighbor_1_x,neighbor_1_y,quadrant);
	
	orientation_neighbor_2 = 
	    find_orientation(neighbor_2_x,neighbor_2_y,quadrant);

	inclination_neighbor_1 = 
	    find_inclination(neighbor_1_x,neighbor_1_y,viewpt_elev,
			     seg_in_p,row_viewpt,col_viewpt, data_type);
	
	inclination_neighbor_2 = 
	    find_inclination(neighbor_2_x,neighbor_2_y,viewpt_elev,
			     seg_in_p,row_viewpt,col_viewpt, data_type);

	/*   check all points behind the blocking point		*/
	for(CHECKED_PT  = head;
	    CHECKED_PT != BLOCKING_PT;
	    CHECKED_PT  = NEXT_CHECKED_PT)
	{
	    
#ifdef DEBUG
	  printf ("\nChecked point y = %d, x = %d",
		  CHECKED_PT_Y, CHECKED_PT_X);
	  fflush (stdout);
#endif

		
	    /*   if pattern layer specified, check to see if checked	*/
	    /*   point is of interest. If not, delete it from list	*/
	    if(patt_flag == 1)
	    {
		segment_get(seg_patt_p,value,
			    row_viewpt-CHECKED_PT_Y,col_viewpt+CHECKED_PT_X);
		
		if(mask == 0)
		{
		    head=delete(CHECKED_PT,head,seg_out_p,
				row_viewpt,col_viewpt,cell_no);
		    goto next_iter;
		}
	    }

	    /* if the OFFSETB parameter is set, we will raise the height of only the
	       target point under consideration, leaving all blocking points the
	       way they are. This improves visibility of current target point and
	       simulates an object of a known height */
	    if (OFFSETB_SET) {
	    	CHECKED_PT_INCLINATION = find_inclination2(CHECKED_PT_X,CHECKED_PT_Y,viewpt_elev,
			     seg_in_p,row_viewpt,col_viewpt, data_type);
	    }
	    
	    do_check = 1;

	    /* no need to check if this condition holds true */
	    if(BLOCKING_PT_INCLINATION <= CHECKED_PT_INCLINATION) {
	       do_check = 0;
	    } 
	    if (BLOCKING_PT_INCLINATION == NULLPT) {
	       do_check = 0;
	    }
	    
            /* delete this point, if it lies closer than the minimum distance */
	    del_x = abs(CHECKED_PT_X);
    	    del_y = abs(CHECKED_PT_Y);
    	    dist=sqrt(del_x * del_x + del_y * del_y)*window.ns_res;
	    if ( dist < RADIUS1) {
		head= delete(CHECKED_PT,head,seg_out_p,row_viewpt,col_viewpt,cell_no);		   
		   do_check = 0;
	    }
	
	    if (do_check == 1)
	    
	    {  	/*   otherwise, proceed to check */


		if(CHECKED_PT_ORIENTATION == BLOCKING_PT_ORIENTATION)
		{
		    head= delete(CHECKED_PT,head,seg_out_p,
				 row_viewpt,col_viewpt,cell_no);
		/*   if checked point directly behind, delete it */
		}
		else
		{	/*   if checked point not directly behind, check */
		    
		    /*   find the coors of the actual neighbor that might be  */
		    /*   required for interpolation.			      */
		    if(CHECKED_PT_ORIENTATION > BLOCKING_PT_ORIENTATION)
		    {
			correct_neighbor_x = neighbor_1_x;
			correct_neighbor_y = neighbor_1_y;
			correct_neighbor_inclination = inclination_neighbor_1;
			correct_neighbor_orientation = orientation_neighbor_1;
		    }
		    else
		    {
			correct_neighbor_x = neighbor_2_x;
			correct_neighbor_y = neighbor_2_y;
			correct_neighbor_inclination = inclination_neighbor_2;
			correct_neighbor_orientation = orientation_neighbor_2;
		    }
		    
		    if(fabs(BLOCKING_PT_ORIENTATION-CHECKED_PT_ORIENTATION) < 
		       fabs(BLOCKING_PT_ORIENTATION-correct_neighbor_orientation))
			
		    { /*   yes, the point neighboring the blocking point */
		      /*    must be taken into consideration		 */

			if(CHECKED_PT_Y == correct_neighbor_y && 
			   CHECKED_PT_X == correct_neighbor_x);	/*   same point */

			else
			{     /*   CHECK !! */
			    
			    
			    /*   if the checked point's inclination is even lower  */
			    /*   than that of the blocking pt.'s neighbor, blocked */
			    if(CHECKED_PT_INCLINATION < correct_neighbor_inclination)
			    {
				head= delete(CHECKED_PT,head,seg_out_p,
					     row_viewpt,col_viewpt,cell_no);
			    }
			    
			    else
			    {	/*   INTERPOLATION */

				
				interpolated_inclination= BLOCKING_PT_INCLINATION + 
				    (CHECKED_PT_ORIENTATION - BLOCKING_PT_ORIENTATION)/
					(correct_neighbor_orientation-BLOCKING_PT_ORIENTATION)*
					    (correct_neighbor_inclination - BLOCKING_PT_INCLINATION);
				
				if(CHECKED_PT_INCLINATION < interpolated_inclination)
				{	/*   interpolated point blocks */
				    head= delete(CHECKED_PT,head,seg_out_p,
						 row_viewpt,col_viewpt,cell_no);
				}
			    }
			}
		    }
		}
	    }
	  next_iter: 
	    ;
	}       /*   end of loop over points to be checked */




	/*   if pattern layer specified, check if blocking point */
	/*   itself is an area of interest. If not, of no use	 */
	if(patt_flag == 1)
	{
	    segment_get(seg_patt_p, value,row_viewpt- BLOCKING_PT_Y,
			col_viewpt+BLOCKING_PT_X);
	    if(mask == 0)
	    {

	      /* Commenting out the following fixes a bug in r.los.
		 In that program the 8 cells around the viewpoint
		 are marked as visible (when visible)
		 even if they fall outside the area of interest 
		 specified by the patt_map.  This occurs because
		 these cells are always the last blocking points
		 on the segment lists, and therefore don't get 
		 deleted when they should.  This fix allows them
		 to be deleted, but it required modifications
		 to delete, in delete3.c.  MWL 25/6/99 */
		 
	      /* if (NEXT_BLOCKING_PT != NULL) */

	      head = delete(BLOCKING_PT, head, seg_out_p,
				  row_viewpt, col_viewpt,cell_no);

	    }
	}

    }       /*   end of loop over blocking points */
    
    return(head);
    
}
예제 #19
0
/* THIS IS CALLED IF ONLY THE TARGET POINT UNDER CURRENT CONSIDERATION IS TO
   BE RAISED BY 'OFFSETB' AMOUNT */
double find_inclination2(int x, int y, double viewpt_elev, 
			SEGMENT *seg_in_p,
			int row_viewpt, int col_viewpt, RASTER_MAP_TYPE data_type)
{
    double del_x, del_y,dist,atan(),sqrt();
    int abs();
    double dest_elev = 0.0;
    extern struct Cell_head window;
    void *value = NULL;	
	/* these vars can store one data value from the elevation input map, */
	/* which could be CELL, DCELL or FCELL type. */
    CELL c_value;
    FCELL f_value;
    DCELL d_value;
    
    del_x = abs(x) ;
    del_y = abs(y) ;
        
    dist=sqrt(del_x * del_x + del_y * del_y)*window.ns_res;
    
    	/* this takes care, that target elevation has the right format, 
	   depending on type of input DEM */
    	
	if (data_type == CELL_TYPE) {
		value = (CELL*) &c_value;
	}
	if (data_type == FCELL_TYPE) {
		value = (FCELL*) &f_value;
	}
	if (data_type == DCELL_TYPE) {
		value = (DCELL*) &d_value;
	}
	
    /* read value from elevation input map, convert to appropriate */
	/* map type */
    segment_get(seg_in_p,value,row_viewpt-y,x+col_viewpt);

	if (data_type == CELL_TYPE) {	
	  if ( G_is_c_null_value (&c_value) ) {
	  	return (NULLPT);
	  } else {
	  	dest_elev = c_value + OFFSETB;
	  }
	}

	if (data_type == FCELL_TYPE) {
	  if ( G_is_f_null_value (&f_value) ) {
	  	return (NULLPT);
	  } else {
	  	dest_elev = f_value + OFFSETB;
	  }
	}

	if (data_type == DCELL_TYPE) {
	  if ( G_is_d_null_value (&d_value) ) {
	  	return (NULLPT);
	  } else {
	  	dest_elev = d_value + OFFSETB;	
	  }
	}


	/* CURVATURE CORRECTION */
	/* decrease height of target point */
	if (DIST_CC > 0.0) {
		if (dist >= DIST_CC) {
			dest_elev = dest_elev - ((dist*dist) / (2 * Re));
		}
	}
        	
    return(atan((dest_elev - viewpt_elev) / dist));
}
예제 #20
0
int main(int argc, char **argv)
{
    int n, verbose = 1,
	backrow, backcol,
	col, row,
	len, flag,
	srows, scols,
	backrow_fd, backcol_fd, path_fd, in_row_fd, in_col_fd, out_fd;
    const char *current_mapset,
	*search_mapset,
	*path_mapset,
	*backrow_mapset,
	*backcol_mapset, *in_row_file, *in_col_file, *out_file;
    CELL *cell;
    POINT *PRES_PT, *PRESENT_PT, *OLD_PT;
    struct Cell_head window;
    double east, north;
    struct Option *opt1, *opt2, *opt3, *opt4;
    struct Flag *flag1;
    struct GModule *module;

    G_gisinit(argv[0]);

    /* Set description */
    module = G_define_module();
    G_add_keyword(_("raster"));
    G_add_keyword(_("fire"));
    G_add_keyword(_("cumulative costs"));
    module->description =
	_("Recursively traces the least cost path backwards to "
	  "cells from which the cumulative cost was determined.");

    opt1 = G_define_option();
    opt1->key = "x_input";
    opt1->type = TYPE_STRING;
    opt1->required = YES;
    opt1->gisprompt = "old,cell,raster";
    opt1->description =
	_("Name of raster map containing back-path easting information");

    opt2 = G_define_option();
    opt2->key = "y_input";
    opt2->type = TYPE_STRING;
    opt2->required = YES;
    opt2->gisprompt = "old,cell,raster";
    opt2->description =
	_("Name of raster map containing back-path northing information");

    opt3 = G_define_option();
    opt3->key = "coordinate";
    opt3->type = TYPE_STRING;
    opt3->multiple = YES;
    opt3->key_desc = "x,y";
    opt3->description =
	_("The map E and N grid coordinates of starting points");

    opt4 = G_define_option();
    opt4->key = "output";
    opt4->type = TYPE_STRING;
    opt4->required = YES;
    opt4->gisprompt = "new,cell,raster";
    opt4->description = _("Name of spread path raster map");

    flag1 = G_define_flag();
    flag1->key = 'v';
    flag1->description = _("Run verbosely");

    /*   Do command line parsing    */
    if (G_parser(argc, argv))
	exit(EXIT_FAILURE);

    current_mapset = G_mapset();
    in_row_file = G_tempfile();
    in_col_file = G_tempfile();
    out_file = G_tempfile();

    /*  Get database window parameters      */
    G_get_window(&window);

    verbose = flag1->answer;

    /*  Check if backrow layer exists in data base  */
    search_mapset = "";

    strcpy(backrow_layer, opt2->answer);
    strcpy(backcol_layer, opt1->answer);

    backrow_mapset = G_find_raster(backrow_layer, search_mapset);
    backcol_mapset = G_find_raster(backcol_layer, search_mapset);

    if (backrow_mapset == NULL)
	G_fatal_error("%s - not found", backrow_layer);

    if (backcol_mapset == NULL)
	G_fatal_error("%s - not found", backcol_layer);

    search_mapset = "";

    strcpy(path_layer, opt4->answer);

    path_mapset = G_find_raster(path_layer, search_mapset);

    /*  find number of rows and cols in window    */
    nrows = Rast_window_rows();
    ncols = Rast_window_cols();

    cell = Rast_allocate_c_buf();

    /*  Open back cell layers for reading  */
    backrow_fd = Rast_open_old(backrow_layer, backrow_mapset);
    backcol_fd = Rast_open_old(backcol_layer, backcol_mapset);

    /*   Parameters for map submatrices   */
    len = sizeof(CELL);

    srows = nrows / 4 + 1;
    scols = ncols / 4 + 1;

    if (verbose)
	G_message
	    ("\nReading the input map -%s- and -%s- and creating some temporary files...",
	     backrow_layer, backcol_layer);

    /* Create segmented files for back cell and output layers  */
    in_row_fd = creat(in_row_file, 0666);
    segment_format(in_row_fd, nrows, ncols, srows, scols, len);
    close(in_row_fd);
    in_col_fd = creat(in_col_file, 0666);
    segment_format(in_col_fd, nrows, ncols, srows, scols, len);
    close(in_col_fd);

    out_fd = creat(out_file, 0666);
    segment_format(out_fd, nrows, ncols, srows, scols, len);
    close(out_fd);

    /*   Open initialize and segment all files  */
    in_row_fd = open(in_row_file, 2);
    segment_init(&in_row_seg, in_row_fd, 4);
    in_col_fd = open(in_col_file, 2);
    segment_init(&in_col_seg, in_col_fd, 4);

    out_fd = open(out_file, 2);
    segment_init(&out_seg, out_fd, 4);

    /*   Write the back cell layers in the segmented files, and  
     *   Change UTM coordinates to ROWs and COLUMNs */
    for (row = 0; row < nrows; row++) {
	Rast_get_c_row(backrow_fd, cell, row);

	for (col = 0; col < ncols; col++)
	    if (cell[col] > 0)
		cell[col] =
		    (window.north - cell[col]) / window.ns_res /* - 0.5 */ ;
	    else
		cell[col] = -1;
	segment_put_row(&in_row_seg, cell, row);
	Rast_get_c_row(backcol_fd, cell, row);

	for (col = 0; col < ncols; col++)
	    if (cell[col] > 0)
		cell[col] =
		    (cell[col] - window.west) / window.ew_res /* - 0.5 */ ;
	segment_put_row(&in_col_seg, cell, row);
    }

    /* Convert easting and northing from the command line to row and col */
    if (opt3->answer) {
	for (n = 0; opt3->answers[n] != NULL; n += 2) {
	    G_scan_easting(opt3->answers[n], &east, G_projection());
	    G_scan_northing(opt3->answers[n + 1], &north, G_projection());
	    row = (window.north - north) / window.ns_res;
	    col = (east - window.west) / window.ew_res;
	    /* ignore pt outside window */
	    if (east < window.west || east > window.east ||
		north < window.south || north > window.north) {
		G_warning("Ignoring point outside window: ");
		G_warning("   %.4f,%.4f", east, north);
		continue;
	    }

	    value = (char *)&backrow;
	    segment_get(&in_row_seg, value, row, col);
	    /* ignore pt in no-data area */
	    if (backrow < 0) {
		G_warning("Ignoring point in NO-DATA area :");
		G_warning("   %.4f,%.4f", east, north);
		continue;
	    }
	    value = (char *)&backcol;
	    segment_get(&in_col_seg, value, row, col);

	    insert(&PRESENT_PT, row, col, backrow, backcol);
	}
    }

    /*  Set flag according to input */
    if (path_mapset != NULL) {
	if (head_start_pt == NULL)
	    /*output layer exists and start pts are not given on cmd line */
	    flag = 1;

	/* output layer exists and starting pts are given on cmd line */
	else
	    flag = 2;
    }
    else
	flag = 3;		/* output layer does not previously exist */

    /* If the output layer containing the starting positions */
    /* create a linked list of of them  */
    if (flag == 1) {
	path_fd = Rast_open_old(path_layer, path_mapset);

	/*  Search for the marked starting pts and make list    */
	for (row = 0; row < nrows; row++) {
	    Rast_get_c_row(path_fd, cell, row);

	    for (col = 0; col < ncols; col++) {
		if (cell[col] > 0) {
		    value = (char *)&backrow;
		    segment_get(&in_row_seg, value, row, col);
		    /* ignore pt in no-data area */
		    if (backrow < 0) {
			G_warning("Ignoring point in NO-DATA area:");
			G_warning("   %.4f,%.4f\n",
				  window.west + window.ew_res * (col + 0.5),
				  window.north - window.ns_res * (row + 0.5));
			continue;
		    }
		    value = (char *)&backcol;
		    segment_get(&in_col_seg, value, row, col);
		    insert(&PRESENT_PT, row, col, backrow, backcol);
		}
	    }			/* loop over cols */
	}			/* loop over rows */

	Rast_close(path_fd);
    }

    /* loop over the starting points to find the least cost paths */
    if (verbose)
	G_message("\nFinding the least cost paths ...");

    PRES_PT = head_start_pt;
    while (PRES_PT != NULL) {
	path_finder(PRES_PT->row, PRES_PT->col, PRES_PT->backrow,
		    PRES_PT->backcol);

	OLD_PT = PRES_PT;
	PRES_PT = NEXT_PT;
	G_free(OLD_PT);
    }

    /* Write pending updates by segment_put() to outputmap */
    segment_flush(&out_seg);

    if (verbose)
	G_message("\nWriting the output map  -%s-...", path_layer);

    path_fd = Rast_open_c_new(path_layer);
    for (row = 0; row < nrows; row++) {
	segment_get_row(&out_seg, cell, row);
	Rast_put_row(path_fd, cell, CELL_TYPE);
    }

    if (verbose)
	G_message("finished.");

    segment_release(&in_row_seg);	/* release memory  */
    segment_release(&in_col_seg);
    segment_release(&out_seg);

    close(in_row_fd);		/* close all files */
    close(in_col_fd);
    close(out_fd);

    Rast_close(path_fd);
    Rast_close(backrow_fd);
    Rast_close(backcol_fd);

    unlink(in_row_file);	/* remove submatrix files  */
    unlink(in_col_file);
    unlink(out_file);

    exit(EXIT_SUCCESS);
}
예제 #21
0
t_error			ia32_region_reserve(i_as			asid,
					i_segment		segid,
				     	t_paddr			offset,
				     	t_opts			opts,
				     	t_vaddr			address,
				     	t_vsize			size,
				     	i_region*		regid)
{
  t_ia32_pde			pde_start;
  t_ia32_pde			pde_end;
  t_ia32_pte			pte_start;
  t_ia32_pte			pte_end;
  t_ia32_table			table;
  t_ia32_directory		pd;
  o_as*				oas;
  t_ia32_page			page;
  t_paddr			base;
  t_paddr			ram_paddr;
  t_vaddr			pd_addr;
  t_paddr			pt_addr;
  o_segment*			oseg;
  o_region*			oreg;
  int				i = 0;
  int				j = 0;
  int				x = 0;
  int				clear = 0;

  REGION_ENTER(region);
  if (as_get(asid, &oas) != ERROR_NONE)
    REGION_LEAVE(region, ERROR_UNKNOWN);

  if (region_get(asid, *regid, &oreg) != ERROR_NONE)
    REGION_LEAVE(region, ERROR_UNKNOWN);

  if (segment_get(segid, &oseg) != ERROR_NONE)
    REGION_LEAVE(region, ERROR_UNKNOWN);

  ram_paddr = oseg->address;
  pd = oas->machdep.pd;
  base = MK_BASE(pd);
  // Mapping PD into Kernel
  map_page(base, &pd_addr);

  /*   printf("pd %x\n", pd_addr); */

  /*   printf("%x\n", oreg->address); */

  pde_start = PDE_ENTRY(oreg->address);
  pde_end = PDE_ENTRY(oreg->address + size);
  pte_start = PTE_ENTRY(oreg->address);
  pte_end = PTE_ENTRY(oreg->address + size);

  for (i = pde_start; i <= pde_end; i++)
    {
      if (pd_get_table((t_ia32_directory *) &pd_addr, i, &table) == ERROR_UNKNOWN)
	{
	  segment_reserve(asid, PAGESZ, PERM_READ | PERM_WRITE, &segid);
	  if (segment_get(segid, &oseg) != ERROR_NONE)
	    REGION_LEAVE(region, ERROR_UNKNOWN);
	  table.rw = PDE_FLAG_RW;
	  table.present = 1;
	  table.user = (opts & REGION_OPT_USER) ? PT_USER : PT_PRIVILEGED;
	  table.writeback = PT_CACHED;
	  table.cached = 1;
	  pt_build(oseg->address, &table, 0);
	  pd_add_table((t_ia32_directory *) &pd_addr, i, table);
	  clear = 1;
	}
      else
	clear = 0;
      map_page(table.entries, &pt_addr);

      table.entries = pt_addr;

      if (clear)
	memset((void*)pt_addr, '\0', PAGESZ);
      for (j = (i == pde_start ? pte_start : 0); j <= (i == pde_end ? pte_end : 1023); j++)
	{
	  page.addr = x + (offset + ram_paddr);
	  page.present = 1;
	  page.rw = (oseg->perms & PERM_WRITE) ? PG_WRITABLE : PG_READONLY;
	  page.present = 1;
	  page.user = (opts & REGION_OPT_USER) ? PG_USER : PG_PRIVILEGED;
	  page.cached = PG_CACHED;
	  pt_add_page(&table, j, page);
	  x += PAGESZ;
	}
      unmap_page(&pt_addr);
    }

  tlb_flush();
      unmap_page(&pd_addr);
  REGION_LEAVE(region, ERROR_NONE);
}
예제 #22
0
// FIXME: lot of code has been removed here
t_error			map_page(t_paddr paddr, t_vaddr *vaddr)
{
  o_as*			kas;
  t_ia32_pde		pde;
  t_ia32_pde		pte;
  t_ia32_directory	dir;
  t_ia32_table		table;
  t_ia32_page		page;
  o_region*		oreg;
  i_segment		segid;
  o_segment*		segment;
  int			clear = 0;

  REGION_ENTER(region);

  if (as_get(kasid, &kas) != ERROR_NONE)
    REGION_LEAVE(region, ERROR_UNKNOWN);

  if (region_space(kas, PAGESZ, vaddr) != ERROR_NONE)
    REGION_LEAVE(region, ERROR_UNKNOWN);

  if ((oreg = malloc(sizeof(o_region))) == NULL)
    REGION_LEAVE(region, ERROR_UNKNOWN);

  oreg->address = *vaddr;
  oreg->regid = (i_region)oreg->address;
  oreg->opts = REGION_OPT_PRIVILEGED;
  oreg->size = PAGESZ;
  oreg->offset = 0;
  oreg->segid = (i_segment) paddr;

  if (region_inject(kasid, oreg) != ERROR_NONE)
    REGION_LEAVE(region, ERROR_UNKNOWN);

  // Page Dir
  dir = kas->machdep.pd;
  pde = PDE_ENTRY(*vaddr);

  if (pd_get_table(&dir, pde, &table) == ERROR_UNKNOWN)
    {
      segment_reserve(kasid, PAGESZ, PERM_READ | PERM_WRITE, &segid);
      if (segment_get(segid, &segment) != ERROR_NONE)
	REGION_LEAVE(region, ERROR_UNKNOWN);
      table.rw = PT_WRITABLE;
      pt_build(segment->address, &table, 0);
      pd_add_table(&dir, pde, table);
      clear = 1;
    }
  table.entries = ENTRY_ADDR(PD_MIRROR, pde);
  //fixme
  if (clear)
    memset((void *)ENTRY_ADDR(PD_MIRROR, pde), '\0', PAGESZ);
  // Page Table
  pte = PTE_ENTRY(*vaddr);
  page.present = 1;
  page.addr = paddr;
  page.rw = PG_WRITABLE;
  pt_add_page(&table, pte, page);

  tlb_invalidate(*vaddr);

  REGION_LEAVE(region, ERROR_NONE);
}
예제 #23
0
t_status		architecture_environment_server(i_as	id)
{
  i_segment	        segment;
  i_region		region;
  o_as*			as;
  at_pd			pd;
  o_region*		r;
  o_segment*		s;

  /*
   * 1)
   */

  if (as_get(id, &as) != STATUS_OK)
    MACHINE_ESCAPE("unable to retrieve the address space object");

  /*
   * 2)
   */

  if (segment_reserve(as->id,
		      ___kaneton$pagesz,
		      PERMISSION_READ | PERMISSION_WRITE,
		      SEGMENT_OPTION_SYSTEM,
		      &segment) != STATUS_OK)
    MACHINE_ESCAPE("unable to reserve a segment");

  if (segment_get(segment, &s) != STATUS_OK)
    MACHINE_ESCAPE("unable to retrieve the segment object");

  /*
   * 3)
   */

  as->machine.pd = s->address;

  /*
   * 4)
   */

  if (architecture_pd_map(as->machine.pd, &pd) != STATUS_OK)
    MACHINE_ESCAPE("unable to map the page directory");

  if (architecture_pd_build(pd) != STATUS_OK)
    MACHINE_ESCAPE("unable to build the page directory");

  if (architecture_pd_unmap(pd) != STATUS_OK)
    MACHINE_ESCAPE("unable to unmap the page directory");

  /*
   * 5)
   */

  if (region_locate(_kernel.as,
		    _thread.machine.tss,
		    &region) == FALSE)
    MACHINE_ESCAPE("unable to locate the region in which the TSS lies");

  if (region_get(_kernel.as, region, &r) != STATUS_OK)
    MACHINE_ESCAPE("unable to retrieve the region object");

  if (region_reserve(as->id,
		     r->segment,
		     0x0,
		     REGION_OPTION_FORCE |
		     REGION_OPTION_NONE,
		     _thread.machine.tss,
		     r->size,
		     &region) != STATUS_OK)
    MACHINE_ESCAPE("unable to reserve the region mapping the TSS");

  /*
   * 6)
   */

  if (region_locate(_kernel.as,
		    (t_vaddr)_segment.machine.gdt.table,
		    &region) == FALSE)
    MACHINE_ESCAPE("unable to locate the region in which the GDT lies");

  if (region_get(_kernel.as, region, &r) != STATUS_OK)
    MACHINE_ESCAPE("unable to retrieve the region object");

  if (region_reserve(as->id,
		     r->segment,
		     0x0,
		     REGION_OPTION_FORCE |
		     REGION_OPTION_NONE,
		     (t_vaddr)_segment.machine.gdt.table,
		     ___kaneton$pagesz,
		     &region) != STATUS_OK)
    MACHINE_ESCAPE("unable to reserve the region mapping the GDT");

  /*
   * 7)
   */

  if (region_locate(_kernel.as,
		    (t_vaddr)_event.machine.idt.table,
		    &region) == FALSE)
    MACHINE_ESCAPE("unable to locate the region in which the IDT lies");

  if (region_get(_kernel.as, region, &r) != STATUS_OK)
    MACHINE_ESCAPE("unable to retrieve the region object");

  if (region_reserve(as->id,
		     r->segment,
		     0x0,
		     REGION_OPTION_FORCE |
		     REGION_OPTION_NONE,
		     (t_vaddr)_event.machine.idt.table,
		     ___kaneton$pagesz,
		     &region) != STATUS_OK)
    MACHINE_ESCAPE("unable to reserve the region mapping the IDT");

  /*
   * 8)
   */

  if (region_reserve(as->id,
                     _init->kcode,
                     ARCHITECTURE_LINKER_SYMBOL(_handler_code_begin) -
                       _init->kcode,
                     REGION_OPTION_FORCE,
                     ARCHITECTURE_LINKER_SYMBOL(_handler_code_begin),
                     ARCHITECTURE_LINKER_SYMBOL(_handler_code_end) -
                     ARCHITECTURE_LINKER_SYMBOL(_handler_code_begin),
                     &region) != STATUS_OK)
    MACHINE_ESCAPE("unable to map the handler code section");

  /*
   * 9)
   */

  if (region_reserve(as->id,
                     _init->kcode,
                     ARCHITECTURE_LINKER_SYMBOL(_handler_data_begin) -
                       _init->kcode,
                     REGION_OPTION_FORCE,
                     ARCHITECTURE_LINKER_SYMBOL(_handler_data_begin),
                     ARCHITECTURE_LINKER_SYMBOL(_handler_data_end) -
                     ARCHITECTURE_LINKER_SYMBOL(_handler_data_begin),
                     &region) != STATUS_OK)
    MACHINE_ESCAPE("unable to map the handler data section");

  MACHINE_LEAVE();
}
예제 #24
0
파일: segment.c 프로젝트: sathnaga/ltp
int segment_location(char *name, range_t * range)
{
	glctx_t *gcp = &glctx;
	segment_t *segp;
	char *apage, *end;
	off_t offset;
	size_t length, maxlength;
	int pgid, i;
	bool need_nl;

	segp = segment_get(name);
	if (segp == NULL) {
		fprintf(stderr, "%s:  no such segment:  %s\n",
			gcp->program_name, name);
		return SEG_ERR;
	}

	if (segp->seg_start == MAP_FAILED) {
		fprintf(stderr, "%s:  segment %s not mapped\n",
			gcp->program_name, name);
		return SEG_ERR;
	}

	offset = round_down_to_pagesize(range->offset);
	if (offset >= segp->seg_length) {
		fprintf(stderr, "%s:  offset %ld is past end of segment %s\n",
			gcp->program_name, offset, name);
		return SEG_ERR;
	}

	apage = segp->seg_start + offset;
	maxlength = segp->seg_length - offset;

	length = range->length;
	if (length)
		length = round_up_to_pagesize(length);

	/*
	 * note:  we silently truncate to max length [end of segment]
	 */
	if (length == 0 || length > maxlength)
		length = maxlength;

	end = apage + length;
	pgid = offset / gcp->pagesize;

	show_one_segment(segp, false);	/* show mapping, no header */

	printf("page offset   ");
	for (i = 0; i < PG_PER_LINE; ++i)
		printf(" +%02d", i);
	printf("\n");
	if (pgid & PPL_MASK) {
		/*
		 * start partial line
		 */
		int pgid2 = pgid & ~PPL_MASK;
		printf("%12x: ", pgid2);
		while (pgid2 < pgid) {
			printf("    ");
			++pgid2;
		}
		need_nl = true;
	} else
		need_nl = false;

	for (; apage < end; apage += gcp->pagesize, ++pgid) {
		int node;

		node = get_node(apage);
		if (node < 0) {
			fprintf(stderr, "\n%s:  "
				"failed to get node for segment %s, offset 0x%x\n",
				gcp->program_name, name, SEG_OFFSET(segp,
								    apage));
			return SEG_ERR;
		}

		if ((pgid & PPL_MASK) == 0) {
			if (need_nl)
				printf("\n");
			printf("%12x: ", pgid);	/* start a new line */
			need_nl = true;
		}
		printf(" %3d", node);

		if (signalled(gcp)) {
			reset_signal();
			break;
		}
	}
	printf("\n");

	return SEG_OK;
}
예제 #25
0
/* NULL cell handling added by Benjamin Ducke, May 2004 */
void Close_segmented_outfile (char* map_name, int map_fd,
			             char* seg_name, int seg_fd, SEGMENT* seg, 
                         CELL* cell_buf, int terse,
                         SEGMENT* elevation_seg, RASTER_MAP_TYPE data_type, int make_nulls)
{
  unsigned long row, nrows, col, ncols;
  void *value = NULL;
  char *null_flags;
  /* the following are used to store different raster map types */
  CELL c_value;
  FCELL f_value;
  DCELL d_value;  	

	
  /* Find number of rows and columns in elevation map */
  nrows = G_window_rows();
  ncols = G_window_cols();
	
  /* allocate memory for a NULL data row */	
  null_flags = G_calloc ((unsigned) ncols, sizeof (char));
    
  /* Write pending updates by segment_put() to output map */
  segment_flush(seg);
  
  if (!terse) {	
    fprintf (stdout, "\nWriting raster map '%s': \n", map_name);	
  }
	
  /* Convert output submatrices to full cell overlay */
  for(row=0; row< nrows; row++) {
    segment_get_row(seg, cell_buf, (signed) row);
	/* check for NULL values in the corresponding row of the */
	/* elevation input file. If there are any, set the CELLs */
	/* in the output file to NULL, as well */
	
	/* initialize null data row */
	for (col=0; col<ncols; col++) {
		null_flags[col] = 0;
	}
	
	/* convert all -1 cells to NULL, if user wants it so */
    if ( make_nulls == 1 ) {
	    for (col=0; col<ncols; col++) {
		   if ( cell_buf[col] == -1 ) {
		     null_flags[col] = 1;
		   }
		}
	}
	
	/* update NULL flags in row */
    for (col=0; col<ncols; col++) {
		if ( data_type == CELL_TYPE ) {
		    value = (CELL*) &c_value;
		}		  
		if ( data_type == FCELL_TYPE ) {
		    value = (FCELL*) &f_value;
		}		  
		if ( data_type == DCELL_TYPE ) {
		    value = (DCELL*) &d_value;
		}
		
        segment_get (elevation_seg, value, (signed) row, (signed) col);
		
		/* check for NULL value and record in null_flags row */
		if ( data_type == CELL_TYPE ) {
		    if (G_is_c_null_value (&c_value) ) {
			  null_flags[col] = 1; /* signal NULL value in null_flags row */
			}			
		}		  
		if ( data_type == FCELL_TYPE ) {
		    if (G_is_f_null_value (&f_value) ) {
			  null_flags[col] = 1; /* signal NULL value in null_flags row */
			}
		}		  
		if ( data_type == DCELL_TYPE ) {
		    if (G_is_d_null_value (&d_value) ) {
			  null_flags[col] = 1; /* signal NULL value in null_flags row */
			}						
		}
	}
    /* set all NULL cells according to null_flags row */
	G_insert_c_null_values (cell_buf, null_flags, (signed) ncols);
	
	/* now, write this row to disk */  
    if(G_put_raster_row (map_fd, cell_buf, CELL_TYPE) < 0) {
	  G_fatal_error ("Failed to convert output submatrices ");	  
	}
	
	/* progress display */
	if (! terse) {
		G_percent ((signed) row, (signed) nrows-1,2);
	}	
  }
  
  G_free (null_flags);
  
/* Close files */
  segment_release (seg);
  close (seg_fd);
  unlink (seg_name);
  G_close_cell (map_fd);
}