/* Code extrait de l'article Wikipedia Ring Buffer https://en.wikipedia.org/wiki/Circular_buffer consulté le 2014-12-16 © Wikipedia, CC-BY-SA 3.0 */ #include /* Opaque buffer element type. This would be defined by the application. */ typedef struct { int value; } ElemType; /* Circular buffer object */ typedef struct { int size; /* maximum number of elements */ int start; /* index of oldest element */ int end; /* index at which to write new element */ ElemType *elems; /* vector of elements */ } CircularBuffer; void cbInit(CircularBuffer *cb, int size) { cb->size = size + 1; /* include empty elem */ cb->start = 0; cb->end = 0; cb->elems = calloc(cb->size, sizeof(ElemType)); } void cbFree(CircularBuffer *cb) { free(cb->elems); /* OK if null */ } int cbIsFull(CircularBuffer *cb) { return (cb->end + 1) % cb->size == cb->start; } int cbIsEmpty(CircularBuffer *cb) { return cb->end == cb->start; } /* Write an element, overwriting oldest element if buffer is full. App can choose to avoid the overwrite by checking cbIsFull(). */ void cbWrite(CircularBuffer *cb, ElemType *elem) { cb->elems[cb->end] = *elem; cb->end = (cb->end + 1) % cb->size; if (cb->end == cb->start) cb->start = (cb->start + 1) % cb->size; /* full, overwrite */ } /* Read oldest element. App must ensure !cbIsEmpty() first. */ void cbRead(CircularBuffer *cb, ElemType *elem) { *elem = cb->elems[cb->start]; cb->start = (cb->start + 1) % cb->size; } int main() { CircularBuffer cb; ElemType elem = {0}; int testBufferSize = 10; /* arbitrary size */ cbInit(&cb, testBufferSize); /* Fill buffer with test elements 3 times */ for (elem.value = 0; elem.value < 3 * testBufferSize; ++ elem.value) cbWrite(&cb, &elem); /* Remove and print all elements */ while (!cbIsEmpty(&cb)) { cbRead(&cb, &elem); Frama_C_show_each_elem(elem.value); } cbFree(&cb); return 0; } void* calloc(size_t nmemb, size_t size) { void* res = malloc(nmemb * size); if (res) { for(int i = 0; i