178acc472SPeter Tyser /* 278acc472SPeter Tyser * (C) Copyright 2003 378acc472SPeter Tyser * Gerry Hamel, geh@ti.com, Texas Instruments 478acc472SPeter Tyser * 5*1a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 678acc472SPeter Tyser */ 778acc472SPeter Tyser 878acc472SPeter Tyser #include <common.h> 978acc472SPeter Tyser #include <malloc.h> 1078acc472SPeter Tyser 1178acc472SPeter Tyser #include <circbuf.h> 1278acc472SPeter Tyser 1378acc472SPeter Tyser 1478acc472SPeter Tyser int buf_init (circbuf_t * buf, unsigned int size) 1578acc472SPeter Tyser { 1678acc472SPeter Tyser assert (buf != NULL); 1778acc472SPeter Tyser 1878acc472SPeter Tyser buf->size = 0; 1978acc472SPeter Tyser buf->totalsize = size; 2078acc472SPeter Tyser buf->data = (char *) malloc (sizeof (char) * size); 2178acc472SPeter Tyser assert (buf->data != NULL); 2278acc472SPeter Tyser 2378acc472SPeter Tyser buf->top = buf->data; 2478acc472SPeter Tyser buf->tail = buf->data; 2578acc472SPeter Tyser buf->end = &(buf->data[size]); 2678acc472SPeter Tyser 2778acc472SPeter Tyser return 1; 2878acc472SPeter Tyser } 2978acc472SPeter Tyser 3078acc472SPeter Tyser int buf_free (circbuf_t * buf) 3178acc472SPeter Tyser { 3278acc472SPeter Tyser assert (buf != NULL); 3378acc472SPeter Tyser assert (buf->data != NULL); 3478acc472SPeter Tyser 3578acc472SPeter Tyser free (buf->data); 3678acc472SPeter Tyser memset (buf, 0, sizeof (circbuf_t)); 3778acc472SPeter Tyser 3878acc472SPeter Tyser return 1; 3978acc472SPeter Tyser } 4078acc472SPeter Tyser 4178acc472SPeter Tyser int buf_pop (circbuf_t * buf, char *dest, unsigned int len) 4278acc472SPeter Tyser { 4378acc472SPeter Tyser unsigned int i; 4478acc472SPeter Tyser char *p = buf->top; 4578acc472SPeter Tyser 4678acc472SPeter Tyser assert (buf != NULL); 4778acc472SPeter Tyser assert (dest != NULL); 4878acc472SPeter Tyser 4978acc472SPeter Tyser /* Cap to number of bytes in buffer */ 5078acc472SPeter Tyser if (len > buf->size) 5178acc472SPeter Tyser len = buf->size; 5278acc472SPeter Tyser 5378acc472SPeter Tyser for (i = 0; i < len; i++) { 5478acc472SPeter Tyser dest[i] = *p++; 5578acc472SPeter Tyser /* Bounds check. */ 5678acc472SPeter Tyser if (p == buf->end) { 5778acc472SPeter Tyser p = buf->data; 5878acc472SPeter Tyser } 5978acc472SPeter Tyser } 6078acc472SPeter Tyser 6178acc472SPeter Tyser /* Update 'top' pointer */ 6278acc472SPeter Tyser buf->top = p; 6378acc472SPeter Tyser buf->size -= len; 6478acc472SPeter Tyser 6578acc472SPeter Tyser return len; 6678acc472SPeter Tyser } 6778acc472SPeter Tyser 6878acc472SPeter Tyser int buf_push (circbuf_t * buf, const char *src, unsigned int len) 6978acc472SPeter Tyser { 7078acc472SPeter Tyser /* NOTE: this function allows push to overwrite old data. */ 7178acc472SPeter Tyser unsigned int i; 7278acc472SPeter Tyser char *p = buf->tail; 7378acc472SPeter Tyser 7478acc472SPeter Tyser assert (buf != NULL); 7578acc472SPeter Tyser assert (src != NULL); 7678acc472SPeter Tyser 7778acc472SPeter Tyser for (i = 0; i < len; i++) { 7878acc472SPeter Tyser *p++ = src[i]; 7978acc472SPeter Tyser if (p == buf->end) { 8078acc472SPeter Tyser p = buf->data; 8178acc472SPeter Tyser } 8278acc472SPeter Tyser /* Make sure pushing too much data just replaces old data */ 8378acc472SPeter Tyser if (buf->size < buf->totalsize) { 8478acc472SPeter Tyser buf->size++; 8578acc472SPeter Tyser } else { 8678acc472SPeter Tyser buf->top++; 8778acc472SPeter Tyser if (buf->top == buf->end) { 8878acc472SPeter Tyser buf->top = buf->data; 8978acc472SPeter Tyser } 9078acc472SPeter Tyser } 9178acc472SPeter Tyser } 9278acc472SPeter Tyser 9378acc472SPeter Tyser /* Update 'tail' pointer */ 9478acc472SPeter Tyser buf->tail = p; 9578acc472SPeter Tyser 9678acc472SPeter Tyser return len; 9778acc472SPeter Tyser } 98