0

Excuse me i have an error in my file buffer.c that says " conflicting type" and in my file buffer.h it says "previous declaration of put_non_bloccante was here !!

/*
 * Buffer.c
 *
 *  Created on: 19/nov/2013
 *      Author: lele
 */
#include "buffer.h"
#include <stdio.h>
#include "msg.h"
#include <stdlib.h>
#include <pthread.h>
#define BUFFER_ERROR (msg_t *) NULL

/* allocazione / deallocazione buffer */
// creazione di un buffer vuoto di dim. max nota





    buffer_t * buffer_init(unsigned int maxsize){
        buffer_t * new_buffer = (buffer_t*)malloc( sizeof(buffer_t) );
        new_buffer->T=0;
        new_buffer->D=0;
        new_buffer->msg_presenti=0;
        new_buffer->array_msg =(msg_t **)malloc(sizeof(msg_t*) * maxsize);
        new_buffer->size=maxsize;
        pthread_cond_init(&(new_buffer->not_empty),NULL);
        pthread_cond_init(&(new_buffer->not_full),NULL);
        pthread_mutex_init(&(new_buffer->mutex),NULL);

        return new_buffer;
    }
    // deallocazione di un buffer



    void buffer_destroy(buffer_t* buffer){
        int i;
        for(i=0;i<buffer->size;i++){
            msg_destroy_string(buffer->array_msg[i]);

        }
        free(buffer->array_msg);
        free(buffer);




    }


    msg_t* put_non_bloccante(buffer_t* buffer, msg_t* msg){
        // inserimento non bloccante: restituisce BUFFER_ERROR se pieno,
            // altrimenti effettua l'inserimento e restituisce il messaggio
            // inserito; N.B.: msg!=null
       if( (pthread_mutex_trylock(&(buffer->mutex))) !=0){

           return BUFFER_ERROR;

       }
        while(buffer->msg_presenti==buffer->size){
            pthread_cond_wait(&(buffer->not_full),&(buffer->mutex));

    }
        buffer->array_msg[buffer->D]=msg;
        buffer->D = (buffer->D+1)%buffer->size;
        buffer->msg_presenti= buffer->msg_presenti+1;
        pthread_cond_signal(&(buffer->not_empty));
        pthread_mutex_unlock(&(buffer->mutex));
        return msg;
    }
    // estrazione bloccante: sospende se vuoto, quindi
    // restituisce il valore estratto non appena disponibile
    msg_t* get_bloccante(buffer_t* buffer){
    // estrazione non bloccante: restituisce BUFFER_ERROR se vuoto
    // ed il valore estratto in caso contrario

        pthread_mutex_lock(&(buffer->mutex));
        while(buffer->msg_presenti==0){
            pthread_cond_wait(&(buffer->not_empty),&(buffer->mutex));
        }
        msg_t * msg =buffer->array_msg[buffer->T];
        buffer->T=(buffer->T+1)%buffer->size;
        buffer->msg_presenti--;
        pthread_cond_signal(&(buffer->not_full));
        pthread_mutex_unlock(&(buffer->mutex));
        return msg;

    }
    msg_t* get_non_bloccante(buffer_t* buffer){
        if( (pthread_mutex_trylock(&(buffer->mutex))) !=0){

                   return BUFFER_ERROR;

               }
                while(buffer->msg_presenti==0){
                    pthread_cond_wait(&(buffer->not_empty),&(buffer->mutex));
                }
                msg_t* msg =buffer->array_msg[buffer->T];
                buffer->T=(buffer->T+1)%buffer->size;
                buffer->msg_presenti--;
                pthread_cond_signal(&(buffer->not_full));
                pthread_mutex_unlock(&(buffer->mutex));
                return msg;
    }


    msg_t* put_bloccante(buffer_t *buffer, msg_t *msg){
        // inserimento non bloccante: restituisce BUFFER_ERROR se pieno,
            // altrimenti effettua l'inserimento e restituisce il messaggio
            // inserito; N.B.: msg!=null
       pthread_mutex_lock(&(buffer->mutex));
        while(buffer->msg_presenti==buffer->size){
            pthread_cond_wait(&(buffer->not_full),&(buffer->mutex));

    }
        buffer->array_msg[buffer->D]=msg;
        buffer->D = (buffer->D+1)%buffer->size;
        buffer->msg_presenti= buffer->msg_presenti+1;
        pthread_cond_signal(&(buffer->not_empty));
        pthread_mutex_unlock(&(buffer->mutex));
        return msg;
    }

this is my buffer.h file ! what should i do ?:

#include <pthread.h>
#include "msg.h"
#define BUFFER_ERROR (msg_t *) NULL
typedef struct buffer {
    int size;
    int T;
    int D;
    int msg_presenti;

    pthread_cond_t not_full;
    pthread_cond_t not_empty;
    pthread_mutex_t mutex;
    msg_t ** array_msg;

} buffer_t;

    buffer_t * buffer_init(unsigned int maxsize);
    void buffer_destroy(buffer_t* buffer);
    msg_t* put_non_bloccante(buffer_t* buffer, msg_t* msg);
user2993592
  • 31
  • 2
  • 6
  • You should debug a bit, rather than posting the whole code here. – Vishal R Nov 29 '13 at 15:07
  • 1
    Buffer.c includes "msg.h", so does Buffer.h. – Charlie Burns Nov 29 '13 at 15:10
  • 1
    When posting questions about compiler errors, please include the *complete* and *unedited* error log in the question. There are most likely more text that will help us understand the error. Also please point out the lines in the code where the error(s) are. – Some programmer dude Nov 29 '13 at 15:10
  • In this case, though, you might want to research the subject of [include guards](http://en.wikipedia.org/wiki/Include_guard). In your `buffer.h` header file, you don't actually *need* to include the `msg.h` header file, just declare the `msg_t` type: `typedef struct msg msg_t;` or similar. – Some programmer dude Nov 29 '13 at 15:11
  • [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). – unwind Nov 29 '13 at 15:33

1 Answers1

1

buffer.c includes "msg.h", so does buffer.h.

The easiest way to fix this is put this at the top of msg.h

#ifndef MSG_H_
#define MSG_H_

And this at the very bottom

#endif

You should do a similar thing with buffer.h. This avoids multiple includes in a single translation unit.

Charlie Burns
  • 6,994
  • 20
  • 29