diff --git a/include/dlt/dlt_types.h b/include/dlt/dlt_types.h index e36a32c6b..45cea8ab0 100644 --- a/include/dlt/dlt_types.h +++ b/include/dlt/dlt_types.h @@ -83,6 +83,7 @@ typedef unsigned int speed_t; */ typedef enum { + DLT_RETURN_NO_INCREASE_SIZE = -9, DLT_RETURN_FILESZERR = -8, DLT_RETURN_LOGGING_DISABLED = -7, DLT_RETURN_USER_BUFFER_FULL = -6, diff --git a/src/shared/dlt_common.c b/src/shared/dlt_common.c index 4a049efc9..79ae56964 100644 --- a/src/shared/dlt_common.c +++ b/src/shared/dlt_common.c @@ -120,6 +120,7 @@ int dlt_buffer_increase_size(DltBuffer *buf); int dlt_buffer_minimize_size(DltBuffer *buf); void dlt_buffer_write_block(DltBuffer *buf, int *write, const unsigned char *data, unsigned int size); void dlt_buffer_read_block(DltBuffer *buf, int *read, unsigned char *data, unsigned int size); +DltReturnValue dlt_buffer_skip_size(DltBuffer *buf, int skip_size); void dlt_print_hex(uint8_t *ptr, int size) { @@ -2604,12 +2605,12 @@ int dlt_buffer_increase_size(DltBuffer *buf) /* check size */ if (buf->step_size == 0) /* cannot increase size */ - return DLT_RETURN_ERROR; + return DLT_RETURN_NO_INCREASE_SIZE; /* check size */ if ((buf->size + sizeof(DltBufferHead) + buf->step_size) > buf->max_size) /* max size reached, do not increase */ - return DLT_RETURN_ERROR; + return DLT_RETURN_NO_INCREASE_SIZE; /* allocate new buffer */ new_ptr = malloc(buf->size + sizeof(DltBufferHead) + buf->step_size); @@ -2656,6 +2657,39 @@ int dlt_buffer_increase_size(DltBuffer *buf) return DLT_RETURN_OK; /* OK */ } +DltReturnValue dlt_buffer_skip_size(DltBuffer *buf, int skip_size){ + /* catch null pointer */ + if (buf == NULL) { + dlt_vlog(LOG_WARNING, "%s: Wrong parameter: Null pointer\n", __func__); + return DLT_RETURN_WRONG_PARAMETER; + } + + if (buf->size < skip_size) { + dlt_vlog(LOG_WARNING, "%s: Wrong parameter: Null pointer\n", __func__); + return DLT_RETURN_WRONG_PARAMETER; + } + + DltBufferHead *head = (DltBufferHead *)buf->shm; + int free_size = 0; + + while(free_size < skip_size && head->count) { + DltBufferBlockHead block_head; + /* read header */ + dlt_buffer_read_block(buf, &head->read, (unsigned char *)&block_head, sizeof(DltBufferBlockHead)); + + head->read = head->read + block_head.size; + if ((unsigned int) (head->read) >= buf->size) { + head->read = (unsigned int)(head->read) - buf->size; + } + + head->count = head->count -1 ; + free_size = (int)sizeof(DltBufferBlockHead) + block_head.size; + dlt_vlog(LOG_DEBUG, "Clearing needed memory from buffer - need memory(%d) free memory(%d)\n", skip_size, free_size); + } + + return DLT_RETURN_OK; +} + int dlt_buffer_minimize_size(DltBuffer *buf) { unsigned char *new_ptr; @@ -2778,10 +2812,18 @@ int dlt_buffer_push3(DltBuffer *buf, /* check size */ while (free_size < (int) (sizeof(DltBufferBlockHead) + size1 + size2 + size3)) { /* try to increase size if possible */ - if (dlt_buffer_increase_size(buf)) + int ret = dlt_buffer_increase_size(buf); + if (ret <0) { + if( ret == DLT_RETURN_NO_INCREASE_SIZE) { + ret = dlt_buffer_skip_size(buf, (int) (sizeof(DltBufferBlockHead) + size1 + size2 + size3)); + } + + if (ret <0) { /* increase size is not possible */ /*dlt_log(LOG_ERR, "Buffer: Buffer is full\n"); */ - return DLT_RETURN_ERROR; /* ERROR */ + return ret; /* ERROR */ + } + } /* update pointers */ write = ((int *)(buf->shm))[0];