160 likes | 251 Views
FIFO ADT. version 3.0. Lecture 9, 23 November 2011. MTE_errno.h MTE_type.h FIFO_Specif.h FIFO_Test.c FIFO_Imp_U_L1.c FIFO_Imp_B_A1.c. Imperative and System Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip. MTE_errno.h. int MTE_errno; // place where the error numbers go
E N D
FIFO ADT version 3.0 Lecture 9, 23 November 2011 • MTE_errno.h • MTE_type.h • FIFO_Specif.h • FIFO_Test.c • FIFO_Imp_U_L1.c • FIFO_Imp_B_A1.c Imperative and System Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip
MTE_errno.h • int MTE_errno; // place where the error numbers go • // Numerical values of the error numbers • #define MTE_ERROR_not_yet_implemented 1 • #define MTE_ERROR_out_of_memory 2 • #define MTE_ERROR_ADT_is_empty 11 • #define MTE_ERROR_ADT_is_full 12 • #define MTE_ERROR_ADT_is_not_existing 13 • #define MTE_ERROR_ADT_element_is_not_existing 14 • #define MTE_ERROR_placeholder_for_new_kinds_of_errors -1 Lines 11-22
MTE_type.h • #ifndef _MTE_TYPE_H // avoids to define 'boolean_t' and 'elt_t' • #define _MTE_TYPE_H // in multiple file inclusions • #define TRUE 1 • #define FALSE 0 • typedef int boolean_t; • typedef struct elt_t { // 4K = 2**12 bytes • int key; • char data[4092]; // fixed array size for simplicity reasons !!! • } elt_t; • #endif /* _MTE_TYPE_H */ Lines 43-55
FIFO_Specif.h • #define FIFO_SPECIF_VERSION "FIFO Specification: version 3.0, 20 November 2011" • typedef void *fifo_t; // private type • int fifo_capacity; // global variable initialized by the user • // Constructors • fifo_t *fifo_create (void); • fifo_t *fifo_destroy(fifo_t *f); • // Access functions • boolean_t fifo_is_empty (fifo_t *f); • boolean_t fifo_is_existing(fifo_t *f); • boolean_t fifo_is_full (fifo_t *f); • int fifo_size (fifo_t *f); • char* fifo_imp_version(); • void fifo_consult (fifo_t *f, elt_t *e); • void fifo_traverse(fifo_t *f, void user_fct(elt_t *)); • // Manipulators • void fifo_put(fifo_t *f, elt_t *e); • void fifo_get(fifo_t *f, elt_t *e); Lines 81-109
FIFO_Test.c – Usage • void print_usage() • //---------------- • { • printf("ADT FIFO usage:\n"); • printf(" Constructors : c)create d)destroy\n"); • printf(" Access fcts : E)exists e)isEmpty f)isFull s)size v)version\n"); • printf(" : o)consult()\n"); • printf(" : t)traverse_display r)traverse_read_only\n"); • printf(" Manipulators : p)put(i,s) g)get\n"); • printf(" : a)traverse_add_one\n"); • printf("Test program usage:\n"); • printf(" C)SetADTCapacity(i) K)GetADTCapacity\n"); • printf(" S)SetCurrentADT(0..3) G)GetCurrentADT\n"); • printf(" D)Debugger(0..1) V)Version"); • printf(" H)Help Q)Quit\n"); • printf("\n"); • printf(" A)FillADT M)FillMem\n"); • printf(" X)Dealloc put/get Y)Dealloc create..destroy Z)ProcCall\n"); • } Lines 197-215
FIFO_Test.c – Main Loop • while ( (c = getchar()) != 'Q') { // Main loop of the test program • switch(c) { • case 'c': • f = f_a[current_fifo_index] = fifo_create(); • if (MTE_errno != 0) goto error; /*----- ERROR TREATMENT -----*/ • printf("ADT has been created\n"); • break; • . . . • default: • print_usage(); • break; • } • while (getchar() != '\n') {} /* skip end of line */ • printf("%i%s ", prompt_count++, prompt); • continue; // all is ok, go to the beginning of the main loop • error: // ----- ERROR TREATMENT • while (getchar() != '\n') {} /* skip end of line */ • printf("Error reporting from the test program> Error: %i\n", MTE_errno); • printf("%i%s ", prompt_count++, prompt);; • MTE_errno = 0; //----- END ERROR TREATMENT • } // End of the main loop of the test program Lines 237-439
FIFO_Test.c – include+global+local+init • #include <stdio.h> // printf, scanf • #include <stdlib.h> // rand() • #include "MTE_errno.h" // MTE_errno, ... • #include "MTE_type.h" // boolean_t, elt_t • #include "FIFO_Specif.h" // fifo_capacity, … • #define FIFO_TEST_VERSION "FIFO Test Program: version 1.5, 20 November 2011" • // user-supplied functions for fifo_traverse • void display (elt_t *elt) { printf("%d %s\n", elt->key, elt->data); } • void read_only (elt_t *elt) { int i; i = elt->key; } • void add_one (elt_t *elt) { ++elt->key; } • . . . • main() /* Test program for the FIFO ADT */ • { • fifo *f = NULL, *f_a[4] = {NULL,NULL,NULL,NULL}; • elt_t elt = {1,"Hello"}; • long grain = 16*1024; // 2**14 = 16 K • MTE_errno = 0; // by default, there is no error • fifo_capacity = 16; // default value • . . . Lines 153-232
FIFO_Test.c – Misc: Fill ADT, Fill memory • case 'A': // Fill up an FIFO ADT • li = 0; • elt.key = 0; • f = fifo_create(); • while (TRUE) { • ++elt.key; • fifo_put(f, &elt); • if (MTE_errno != 0) goto error; /*----- ERROR TREATMENT -----*/ • if ( !(++li % grain) ) printf("%li\n", li/grain); • } • break; • case 'M': // Fill memory with empty ADTs • li = 0; • while (TRUE) { • f = fifo_create(); • if (MTE_errno != 0) goto error; /*----- ERROR TREATMENT -----*/ • if ( !(++li % grain) ) printf("%d\n", li/grain); • } • break; Lines 362-381
FIFO_imp_U_L1.c – Includes + Private decl. • #include <stdlib.h> // malloc, free • #include "MTE_errno.h" • #include "MTE_type.h" • #include "FIFO_Specif.h" • //--- Private declarations • typedef struct fifo_node_t { // FIFO instance is a linked list of fifo_node_t • elt_t e; // element to be stored in the FIFO instance • struct fifo_node_t *next; // points to the next element, i.e. next head • } fifo_node_t; • typedef struct fifo_descr_t { • fifo_node_t *head; // points to the head (i.e. first in) element • fifo_node_t *tail; // points to the tail (i.e. last in) element • int size; // number of elements in the FIFO • } fifo_descr_t; • #define FIFO_IMP_VERSION "FIFO Implementation: version U_L1 3.0, 20 November 2011" Lines 463-484
FIFO_imp_U_L1.c – create + destroy • fifo_t *fifo_create(void) { • fifo_descr_t *fd; • fd = (fifo_descr_t *) malloc(sizeof(fifo_descr_t)); • if(fd==NULL) • { • MTE_errno = MTE_ERROR_out_of_memory; • return NULL; • } • fd->head = NULL; • fd->tail = NULL; • fd->size = 0; • return (fifo_t *) fd; • } • fifo_t *fifo_destroy(fifo_t *f) { • elt_t e; • while ( !fifo_is_empty(f) ) fifo_get(f, &e); // free FIFO instance • free( (fifo_descr_t *) f ); // free FIFO descriptor • return NULL; // FIFO no longer exists • } Lines 491-519
FIFO_imp_U_L1.c – isEmpty + isFull • boolean_t fifo_is_empty(fifo_t *f) • { • fifo_descr_t *fd = (fifo_descr_t *) f; // casting the generic pointer f • return ( (fd->head == NULL) ? TRUE : FALSE ); • } • boolean_t fifo_is_full(fifo_t *f) • { • fifo_node_t *new_node; • new_node = (fifo_node_t *) malloc(sizeof(fifo_node_t)); • if(new_node==NULL) returnTRUE; • free(new_node); • returnFALSE; • } Lines 533-552
FIFO_imp_U_L1.c – put • void fifo_put(fifo *f, elt_t *e) • { • fifo_descr_t *fd= (fifo_descr_t *) f; // casting the generic pointer f • fifo_node_t *new_node; • new_node = (fifo_node_t *) malloc(sizeof(fifo_node_t)); • if(new_node==NULL) • { • MTE_errno = MTE_ERROR_out_of_memory; • return; • } • new_node->elt = *e; • new_node->next = NULL; • if (fd->head == NULL) fd->head = new_node; • if (fd->tail != NULL) fd->tail->next = new_node; • fd->tail = new_node; • ++fd->size; • } Lines 602-622
FIFO_imp_B_A1.c – Includes + Private decl. • #include <stdlib.h> // malloc, free • #include "MTE_errno.h" • #include "MTE_type.h" • #include "FIFO_Specif.h" • //--- Private declarations • typedef struct fifo_descr_t { • std_elt *array; // points to the stack instance • int size; // 0 <= size <= capacity • int capacity; // capacity of the ADT instance • int head; // pseudo pointer refering to the head • int tail; // pseudo pointer refering to the next free entry • } fifo_descr_t; • #define FIFO_IMP_VERSION "Fifo Implementation: version B_A1 3.0, 20 November 2011" Lines 667-684
FIFO_imp_B_A1.c – create + destroy • fifo_t *fifo_create(void) { • fifo_descr_t *fd; elt_t *array; • fd = (fifo_descr_t *) malloc(sizeof(fifo_descr_t)); • if(fd==NULL) { • MTE_errno = MTE_ERROR_out_of_memory; • returnNULL; • } • array = (elt_t *) malloc(fifo_capacity * sizeof(elt_t)); • if(array==NULL) { • MTE_errno = MTE_ERROR_out_of_memory; • free(fd); • returnNULL; • } • fd->array=array; fd->size=0; fd->capacity=fifo_capacity; fd->head=0; fd->tail=0; • return (fifo_t *) fd; • } • fifo_t *fifo_destroy(fifo_t *f) { • fifo_descr_t *fd = (fifo_descr_t *) f; // casting the generic pointer f • free(fd->array); free(fd); // free fifo_instance and fifo_descriptor • return NULL; // fifo no longer exists • } Lines 691-729
FIFO_imp_B_A1.c – isEmpty + isFull • boolean fifo_is_empty(fifo *f) • { • fifo_descr *fd = (fifo_descr *) f; // casting the generic pointer f • return ((fd->size==0) ? TRUE : FALSE ); • } • boolean fifo_is_full(fifo *f) • { • fifo_descr *fd = (fifo_descr *) f; // casting the generic pointer f • return ((fd->size==fd->capacity) ? TRUE : FALSE ); • } Lines 743-758
FIFO_imp_B_A1.c – put • void fifo_put(fifo *f, elt_t *e) • { • fifo_descr *fd = (fifo_descr *)f; // casting the generic pointer f • if(fd->size == fifo_capacity) • { • MTE_errno = MTE_ERROR_ADT_is_full; • return; • } • (fd->array)[fd->tail] = *e; • ++fd->size; • fd->tail = (fd->tail + 1) % fd->capacity; • } Lines 816-830