Exemplo n.º 1
0
/* cons - construct a new cons node */
LVAL cons(LVAL x, LVAL y)
{
    LVAL nnode;
    /* get a free node */
    if ((nnode = fnodes) == NIL) {
        xlstkcheck(2);
        xlprotect(x);
        xlprotect(y);
        findmem();
        if ((nnode = fnodes) == NIL)
            xlabort("insufficient node space");
        xlpop();
        xlpop();
    }

    /* unlink the node from the free list */
    fnodes = cdr(nnode);
    --nfree;

    /* initialize the new node */
    nnode->n_type = CONS;
    rplaca(nnode,x);
    rplacd(nnode,y);

    /* return the new node */
    return (nnode);
}
Exemplo n.º 2
0
Arquivo: xldmem.c Projeto: 8l/csolve
/* newvector - allocate and initialize a new vector node */
NODE *newvector(int size)
{
    NODE ***oldstk,*vect __HEAPIFY;
    int bsize;

    /* establish a new stack frame */
    oldstk = xlsave1(&vect);

    /* allocate a vector node and set the size to zero (in case of gc) */
    vect = newnode(VECT);
    vect->n_vsize = 0;

    /* allocate memory for the vector */
    bsize = size * sizeof(NODE *);
    vect->n_vsize = size;
    if ((vect->n_vdata = (NODE **) calloc(1,bsize)) == NULL) {
	findmem();
	if ((vect->n_vdata = (NODE **) calloc(1,bsize)) == NULL)
	    xlfail("insufficient vector space");
    }
    total += (long) bsize;
 
    /* restore the previous stack frame */
    xlstack = oldstk;

    /* return the new vector */
    return (vect);
}
Exemplo n.º 3
0
Arquivo: xldmem.c Projeto: 8l/csolve
/* stralloc - allocate memory for a string adding a byte for the terminator */
LOCAL char *stralloc(int size)
{
    char *sptr;

    /* allocate memory for the string copy */
    if ((sptr = malloc(size+1)) == NULL) {
	findmem();  
	if ((sptr = malloc(size+1)) == NULL)
	    xlfail("insufficient string space");
    }
    total += (long) (size+1);

    /* return the new string memory */
    return (sptr);
}
Exemplo n.º 4
0
/* newvector - allocate and initialize a new vector node */
LVAL newvector(int size)
{
    LVAL vect;
    int bsize;
    xlsave1(vect);
    vect = newnode(VECTOR);
    vect->n_vsize = 0;
    if (bsize = size * sizeof(LVAL)) {
        if ((vect->n_vdata = (LVAL *)calloc(1,bsize)) == NULL) {
            findmem();
            if ((vect->n_vdata = (LVAL *)calloc(1,bsize)) == NULL)
                xlfail("insufficient vector space");
        }
        vect->n_vsize = size;
        total += (long) bsize;
    }
    xlpop();
    return (vect);
}
Exemplo n.º 5
0
int
P(int n)
{
    int x;

    if((x=findmem(n)) != -1)
        return x;

    if(n<0)
        return 0;

    int sum=0;
    for(int k=1;k<=n;++k)
    {
        int p1 = n-k*(3*k-1)/2;
        int p2 = n-k*(3*k+1)/2;
        sum += ipow(-1, k+1)*(P(p1)+P(p2));
    }
    memoize(n, sum);
    return sum;
}
Exemplo n.º 6
0
/* newnode - allocate a new node */
LVAL newnode(int type)
{
    LVAL nnode;

    /* get a free node */
    if ((nnode = fnodes) == NIL) {
        findmem();
        if ((nnode = fnodes) == NIL)
            xlabort("insufficient node space");
    }

    /* unlink the node from the free list */
    fnodes = cdr(nnode);
    nfree -= 1L;

    /* initialize the new node */
    nnode->n_type = type;
    rplacd(nnode,NIL);

    /* return the new node */
    return (nnode);
}
Exemplo n.º 7
0
Arquivo: xldmem.c Projeto: 8l/csolve
/* newnode - allocate a new node */
LOCAL NODE *newnode(int type)
{
    NODE *nnode;

    /* get a free node */
    if ((nnode = fnodes) == NIL) {
	findmem();
	if ((nnode = fnodes) == NIL)
	    xlabort("insufficient node space");
    }

    /* unlink the node from the free list */
    fnodes = cdr(nnode);
    nfree -= 1;

    /* initialize the new node */
    memset(&nnode->n_info, 0, sizeof(nnode->n_info)); //matth
    nnode->n_type = type;
    //rplacd(nnode,NIL); matth

    /* return the new node */
    return (nnode);
}