Ejemplo n.º 1
0
/* Basic adding function */
int
_sf_add_internal(slist *s, void *msg, size_t len) {

	if(s->count + 1 >= s->listlen) {
		void *z;
		size_t newlistlen;

		if((newlistlen = s->listlen << 2) == 0)
			newlistlen = 4;

		z = sf_realloc(s->list, sizeof(char *) * newlistlen);
		if(z)
			s->list = (char **)z;
		else
			return -1;

		z = sf_realloc(s->lens, sizeof(size_t) * newlistlen);
		if(z)
			s->lens = (size_t *)z;
		else
			return -1;
		s->listlen = newlistlen;
	}

	s->list[s->count] = msg;
	s->lens[s->count] = len;

	if(len > s->maxlen)
		s->maxlen = len;

	s->count++;
	s->list[s->count] = NULL;
	s->lens[s->count] = 0;

	return 0;
}
Ejemplo n.º 2
0
Archivo: file.c Proyecto: liumch/src
void sf_unpipe (sf_file file, off_t size) 
/*< Redirect a pipe input to a direct access file >*/
{
    off_t nbuf, len, bufsiz;
    char *dataname=NULL;
    FILE* tmp;
    char *buf;
	
    if (!(file->pipe)) return;
	
    tmp = sf_tempfile(&dataname,"wb");
	
    bufsiz = sf_bufsiz(file);
    buf = sf_charalloc(SF_MIN(bufsiz,size));
	
    while (size > 0) {
	nbuf = (bufsiz < size)? bufsiz : size;
	if (nbuf != fread(buf,1,nbuf,file->stream) ||
	    nbuf != fwrite(buf,1,nbuf,tmp))
	    sf_error ("%s: trouble unpiping:",__FILE__);
	size -= nbuf;
    }
	
    free(buf);
	
    if (NULL != file->dataname ) {
	len = strlen(dataname)+1;
	file->dataname = (char*) sf_realloc(file->dataname,len,sizeof(char));
	memcpy(file->dataname,dataname,len);
    }
	
    /*
      if (unlink(file->dataname))
      sf_warning ("%s: trouble removing %s:",__FILE__,file->dataname);
    */
	
    (void) fclose(file->stream);
    file->stream = freopen(dataname,"rb",tmp);
	
    if (NULL == file->stream)
	sf_error ("%s: Trouble reading data file %s:",__FILE__,dataname);
} 
Ejemplo n.º 3
0
void sf_simtab_enter(sf_simtab table, const char *key, const char* val)
/*< Add an entry key=val to the table >*/
{
    int h;
    struct entry *e;
    size_t len;

    h = hash(key,table->size);

    /* check if exists */
    
    for (e = table->pars[h]; e != NULL; e = e->next) {
	if (strcmp(key,e->key) == 0) {
	    if (strcmp(val,e->val) != 0) {
		len = strlen(val)+1;
		e->val = (char*) sf_realloc(e->val,len,1);
		memcpy(e->val,val,len);
	    }
	    return;
	}
    }

    /* create new */
    
    e = (struct entry*) sf_alloc (1,sizeof(*e));

    len = strlen(key)+1;
    e->key = sf_charalloc(len); 
    memcpy(e->key,key,len);
    
    len = strlen(val)+1;
    e->val = sf_charalloc(len); 
    memcpy(e->val,val,len);

    e->next = table->pars[h];
    table->pars[h] = e;
}
Ejemplo n.º 4
0
int main(int argc, char *argv[]) {
    // Initialize the custom allocator
    sf_mem_init(MAX_HEAP_SIZE);

    // Tell the user about the fields
    info("Initialized heap with %dmb of heap space.\n", MAX_HEAP_SIZE >> 20);
    press_to_cont();

    // Print out title for first test
    printf("=== Test1: Allocation test ===\n");
    // Test #1: Allocate an integer
    int *value1 = sf_calloc(1,sizeof(int));
    null_check(value1, sizeof(int));
    payload_check(value1);
    // Print out the allocator block
    sf_varprint(value1);
    press_to_cont();

    sf_snapshot(true);

    // Now assign a value
    printf("=== Test2: Assignment test ===\n");
    info("Attempting to assign value1 = %d\n", VALUE1_VALUE);
    // Assign the value
    *value1 = VALUE1_VALUE;
    // Now check its value
    check_prim_contents(value1, VALUE1_VALUE, "%d", "value1");
    press_to_cont();

    sf_snapshot(true);

    printf("=== Test3: Allocate a second variable ===\n");
    info("Attempting to assign value2 = %ld\n", VALUE2_VALUE);
    long *value2 = sf_calloc(1,sizeof(long));
    null_check(value2, sizeof(long));
    payload_check(value2);
    sf_varprint(value2);
    // Assign a value
    *value2 = VALUE2_VALUE;
    // Check value
    check_prim_contents(value2, VALUE2_VALUE, "%ld", "value2");
    press_to_cont();

    printf("=== Test4: does value1 still equal %d ===\n", VALUE1_VALUE);
    check_prim_contents(value1, VALUE1_VALUE, "%d", "value1");
    press_to_cont();

    // Snapshot the freelist
    printf("=== Test5: Perform a snapshot ===\n");
    sf_snapshot(true);
    press_to_cont();

    // Free a variable
    printf("=== Test6: Free a block and snapshot ===\n");
    info("Freeing value1...\n");
    sf_free(value1);
    sf_snapshot(true);
    press_to_cont();

    // Allocate more memory
    printf("=== Test7: 8192 byte allocation ===\n");
    void *memory = sf_malloc(8192);
    sf_varprint(memory);

    memory = sf_realloc(memory, 8);
    sf_varprint(memory);
    sf_free(memory);
    press_to_cont();

    memory = sf_malloc(10);
    sf_free(memory);
    malloc(25);
    malloc(30);
    malloc(2);

    return EXIT_SUCCESS;
}
Ejemplo n.º 5
0
int main(int argc, char* argv[])
{
    bool transp, start, scalebar, nomin=true, nomax=true, barreverse, framenum;
    int n1, n2, n3, i1, i2, i3, len, nreserve;
    float min1, max1, min2, max2, o3, d3, o1, d1, xi, yi, tt;
    float **x, **y, **tmp, *symbolsz=NULL, symsize, xc, yc;    
    float ***data=NULL, barmin, barmax, minmax[2];
    char *symbol, sym[2]=" ", *color=NULL, *barfile;
    unsigned char **z=NULL, *barbuf[1];
    sf_datatype type;
    sf_file in, depth, bar=NULL;

    sf_init(argc,argv);
    in = sf_input("in");
    vp_init();

    if (NULL != sf_getstring("depth")) {
	depth = sf_input("depth"); /* values for colored plots */
	if (SF_UCHAR != sf_gettype(depth)) 
	    sf_error("Need uchar in depth");
    } else {
	depth = NULL;
    }

    if (!sf_histint(in,"n1",&n1)) sf_error("No n1= in input");
    if (!sf_histint(in,"n2",&n2)) n2=1;
    n = n1*n2;
    n3 = sf_leftsize(in,2);
    if (n3 > 1) {
	if (!sf_histfloat(in,"o3",&o3)) o3=0.;
	if (!sf_histfloat(in,"d3",&d3)) d3=1.;
    }

    x = sf_floatalloc2(n1,n2);
    y = sf_floatalloc2(n1,n2);
    t = sf_floatalloc(n);

    if (!sf_getbool("scalebar",&scalebar)) scalebar=false;
    /* if y, draw scalebar */

    if (!sf_getbool("wantframenum",&framenum)) framenum = (bool) (n3 > 1);
    /* if y, display third axis position in the corner */
    
    if (NULL != depth) {
	z = sf_ucharalloc2(n1,n2);
	/* initialize color table */
	if (NULL == (color = sf_getstring("color"))) color="j";
	/* color scheme (default is j) */
	if (!sf_getint ("nreserve",&nreserve)) nreserve = 8;
	/* reserved colors */
	vp_rascoltab(nreserve,color);

	if (scalebar) {
	    barfile = sf_getstring("bar");
	    /* file for scalebar data */
	    if (NULL == barfile) {
		barfile=sf_histstring(depth,"bar");
		if (NULL == barfile) sf_error("Need bar=");
	    }

	    nomin = (bool) (!sf_getfloat("minval",&barmin));
	    /* minimum value for scalebar (default is the data minimum) */
	    nomax = (bool) (!sf_getfloat("maxval",&barmax));
	    /* maximum value for scalebar (default is the data maximum) */
	
	    bar = sf_input(barfile);
	    if (SF_UCHAR != sf_gettype(bar)) sf_error("Need uchar in bar");

	    if (nomin) nomin = (bool) (!sf_histfloat(bar,"minval",&barmin));
	    if (nomax) nomax = (bool) (!sf_histfloat(bar,"maxval",&barmax));

	    barbuf[0] = (unsigned char*) sf_alloc(VP_BSIZE,sizeof(unsigned char));

	    if (!sf_getbool("barreverse",&barreverse)) barreverse=false;
	    /* if y, go from small to large on the bar scale */
	}
    } 

    if (!sf_getfloat("pclip",&pclip)) pclip=100.; /* clip percentile */

    type = sf_gettype(in);
    switch (type) {
	case SF_FLOAT:
	    if (!sf_histfloat(in,"o1",&o1)) o1=0.;
	    if (!sf_histfloat(in,"d1",&d1)) d1=1.;
	    
	    for (i2=0; i2 < n2; i2++) {
		for (i1=0; i1 < n1; i1++) {
		    x[i2][i1] = o1 + i1*d1;
		}
	    }
	    break;
	case SF_COMPLEX:
	    data = sf_floatalloc3(2,n1,n2);
	    break;
	default:
	    sf_error("Wrong data type (need float or complex)");
    }

    vp_plot_init(n2);

    symbol = sf_getstring("symbol");
    /* if set, plot with symbols instead of lines */
    if (NULL != symbol) {
	len = strlen(symbol);
	if (len < n2) {
	    symbol = (char*) sf_realloc(symbol,n2,sizeof(char));
	    for (i2=len; i2 < n2; i2++) {
		symbol[i2] = symbol[i2 % len];
	    }
	}

	symbolsz = sf_floatalloc(n2);
	if (!sf_getfloats("symbolsz",symbolsz,n2)) {
	    /* symbol size (default is 2) */
	    for (i2 = 0; i2 < n2; i2++)
		symbolsz[i2] = 2./33.;
	} else {
	    for (i2 = 0; i2 < n2; i2++)
		symbolsz[i2] /= 33.;
	}
    }

    if (!sf_getbool ("transp",&transp)) transp=false;
    /* if y, transpose the axes */
 
    for (i3 = 0; i3 < n3; i3++) {
	if (SF_COMPLEX == type) {
	    sf_floatread(data[0][0],2*n,in);
	    for (i2=0; i2 < n2; i2++) {
		for (i1=0; i1 < n1; i1++) {
		    x[i2][i1] = data[i2][i1][0];
		    y[i2][i1] = data[i2][i1][1];
		}
	    }
	    getminmax(x[0],&min1,&max1);
	} else {
	    sf_floatread(y[0],n,in);
	    min1=o1;
	    max1=o1+(n1-1)*d1;
	}
	getminmax(y[0],&min2,&max2);

	if (NULL != depth) sf_ucharread(z[0],n,depth);
	
	vp_stdplot_init (min1, max1, min2, max2,
			 transp,false,false,true);
	vp_frame_init(in,"blt",false);

	if (transp) {
	    tmp=x; x=y; y=tmp;
	    tt=max1; max1=max2; max2=tt;
	    tt=min1; min1=min2; min2=tt;
	}

	if (i3 > 0) vp_erase();

	if (framenum) vp_framenum(o3+i3*d3);
	vp_frame();

	for (i2=0; i2 < n2; i2++) {
	    vp_plot_set (i2);
	    
	    symsize = 2./33.;
	    if (NULL != symbol) {
		sym[0] = symbol[i2];
		symsize = symbolsz[i2];
	    } 

	    start = true;

	    for (i1=0; i1 < n1; i1++) {
		xi = x[i2][i1];
		yi = y[i2][i1];
		if (NULL != depth) vp_color(z[i2][i1]+256);

		if (isfinite(xi) && 
		    isfinite(yi)) {
		    if (NULL != symbol) {
			vp_umove(xi,yi);
			vp_where (&xc, &yc);
			vp_tjust (TH_SYMBOL, TV_SYMBOL);
			vp_gtext (xc,yc,symsize,0.,0.,symsize,sym);
		    } else if (start) {
			vp_umove(xi,yi);
			start=false;
		    } else {
			vp_udraw(xi,yi);
		    }
		} else {
		    start=true;
		}
	    }
	}

	if (depth && scalebar) {
	    sf_floatread(minmax,2,bar);
	    sf_ucharread(barbuf[0],VP_BSIZE,bar);

	    if (nomin) barmin=minmax[0];
	    if (nomax) barmax=minmax[1];

	    if (barreverse) {
		vp_barframe_init (depth,barmax,barmin);
	    } else {
		vp_barframe_init (depth,barmin,barmax);
	    }
	    vp_barraster(VP_BSIZE, barbuf);
	}
	
	if (transp) {
	    tmp=x; x=y; y=tmp;
	    tt=max1; max1=max2; max2=tt;
	    tt=min1; min1=min2; min2=tt;
	}
    } 
   

    exit(0);
}