struct fifo *init_fifo(int num) { struct fifo *f; f = malloc((size_t)FIFOSZ(num)); if(f == NULL) { perror("Malloc error"); return NULL; } f->sz = 0; f->maxsz = num; f->head = 0; f->tail = -1; return(f); }
int insert_fifo(int x, struct fifo **f) { struct fifo *tf; int oldmaxsz, size; if ((*f)->sz == (*f)->maxsz) { size = FIFOSZ((*f)->maxsz+INCRFIFOSZ); tf = realloc(*f, (size_t)size); if (tf == NULL) { perror("Realloc error"); return(-1); } *f = tf; oldmaxsz = (*f)->maxsz; (*f)->maxsz += INCRFIFOSZ; if ((*f)->head) { size = (oldmaxsz - (*f)->head)*sizeof(int); memmove((*f)->data+(*f)->head+INCRFIFOSZ, (*f)->data+(*f)->head, size); (*f)->head += INCRFIFOSZ; } } if ((*f)->tail == (*f)->maxsz-1) { (*f)->tail = 0; } else { (*f)->tail++; } (*f)->data[(*f)->tail] = x; (*f)->sz++; return(0); }
int insert_fifo(int x, struct fifo **f) { struct fifo *g; int oldmaxsz, size; if((*f)->sz == (*f)->maxsz) { size = FIFOSZ((*f)->maxsz+INCRFIFOSZ); g = realloc(*f, (size_t)size); if( g == NULL) { perror("Realloc error"); return (-1); } *f = g; oldmaxsz = (*f)->maxsz; (*f)->maxsz += INCRFIFOSZ; /* Head to maxsz of list gets moved down */ if( (*f)->head) { size = (oldmaxsz - (*f)->head)*sizeof(int); memmove((*f)->data+(*f)->head+INCRFIFOSZ, (*f)->data+(*f)->head,size); (*f)->head += INCRFIFOSZ; } } if( (*f)->tail == (*f)->maxsz-1) (*f)->tail = 0; else (*f)->tail++; (*f)->data[(*f)->tail] = x; (*f)->sz++; return(0); }