Skip to content

Commit

Permalink
Delete the oldest entry when ringbuffer is full
Browse files Browse the repository at this point in the history
  • Loading branch information
jianjun-huang committed Dec 4, 2023
1 parent 2118762 commit 5fa890a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/dlt/dlt_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
50 changes: 46 additions & 4 deletions src/shared/dlt_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down

0 comments on commit 5fa890a

Please sign in to comment.