summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Staaf <robotboy@chromium.org>2015-01-07 11:03:51 -0800
committerChromeOS Commit Bot <chromeos-commit-bot@chromium.org>2015-01-08 00:38:13 +0000
commitdc1362ca82e72ac488bb25d308c97aaf733274fc (patch)
treede3023bc3c0c9362afaf30d030287f1df27722a4
parent18d3bde7f89cb0bf62e3703d1954791dbb2a0df3 (diff)
downloadchrome-ec-dc1362ca82e72ac488bb25d308c97aaf733274fc.tar.gz
Queue: Add methods that accept a memcpy routine
These versions of the queue add and remove methods support using memcpy like routines to access regions of memory with specific requirements. In particular, this will allow for transfers between queues and USB packet RAM on the STM32 which has specific access requirements. This change also includes an update to the mem* util routines to make their prototypes compatible with C89 and POSIX standards. Signed-off-by: Anton Staaf <robotboy@chromium.org> BRANCH=None BUG=None TEST=make buildall -j Test USB Echo functionality on discovery-stm32f072 board to ensure that queues still function correctly. Change-Id: I557064d99abfc3e8cfc98099a1d94334a976550c Reviewed-on: https://chromium-review.googlesource.com/239217 Tested-by: Anton Staaf <robotboy@chromium.org> Reviewed-by: Randall Spangler <rspangler@chromium.org> Commit-Queue: Anton Staaf <robotboy@chromium.org> Trybot-Ready: Anton Staaf <robotboy@chromium.org>
-rw-r--r--common/queue.c29
-rw-r--r--common/util.c10
-rw-r--r--include/queue.h16
-rw-r--r--include/util.h12
4 files changed, 54 insertions, 13 deletions
diff --git a/common/queue.c b/common/queue.c
index b301b338ea..70b4bd40d0 100644
--- a/common/queue.c
+++ b/common/queue.c
@@ -49,6 +49,16 @@ size_t queue_add_unit(struct queue const *q, void const *src)
size_t queue_add_units(struct queue const *q, void const *src, size_t count)
{
+ return queue_add_memcpy(q, src, count, memcpy);
+}
+
+size_t queue_add_memcpy(struct queue const *q,
+ void const *src,
+ size_t count,
+ void *(*memcpy)(void *dest,
+ void const *src,
+ size_t n))
+{
size_t transfer = MIN(count, queue_space(q));
size_t tail = q->state->tail & (q->buffer_units - 1);
size_t first = MIN(transfer, q->buffer_units - tail);
@@ -70,7 +80,10 @@ size_t queue_add_units(struct queue const *q, void const *src, size_t count)
static void queue_read_safe(struct queue const *q,
void *dest,
size_t head,
- size_t transfer)
+ size_t transfer,
+ void *(*memcpy)(void *dest,
+ void const *src,
+ size_t n))
{
size_t first = MIN(transfer, q->buffer_units - head);
@@ -103,10 +116,20 @@ size_t queue_remove_unit(struct queue const *q, void *dest)
size_t queue_remove_units(struct queue const *q, void *dest, size_t count)
{
+ return queue_remove_memcpy(q, dest, count, memcpy);
+}
+
+size_t queue_remove_memcpy(struct queue const *q,
+ void *dest,
+ size_t count,
+ void *(*memcpy)(void *dest,
+ void const *src,
+ size_t n))
+{
size_t transfer = MIN(count, queue_count(q));
size_t head = q->state->head & (q->buffer_units - 1);
- queue_read_safe(q, dest, head, transfer);
+ queue_read_safe(q, dest, head, transfer, memcpy);
q->state->head += transfer;
@@ -124,7 +147,7 @@ size_t queue_peek_units(struct queue const *q,
if (i < available) {
size_t head = (q->state->head + i) & (q->buffer_units - 1);
- queue_read_safe(q, dest, head, transfer);
+ queue_read_safe(q, dest, head, transfer, memcpy);
}
return transfer;
diff --git a/common/util.c b/common/util.c
index f1c3e9a41e..2b745e8621 100644
--- a/common/util.c
+++ b/common/util.c
@@ -58,7 +58,7 @@ int strcasecmp(const char *s1, const char *s2)
}
-int strncasecmp(const char *s1, const char *s2, int size)
+int strncasecmp(const char *s1, const char *s2, size_t size)
{
int diff;
@@ -155,7 +155,7 @@ int parse_bool(const char *s, int *dest)
}
}
-int memcmp(const void *s1, const void *s2, int len)
+int memcmp(const void *s1, const void *s2, size_t len)
{
const char *sa = s1;
const char *sb = s2;
@@ -171,7 +171,7 @@ int memcmp(const void *s1, const void *s2, int len)
}
-void *memcpy(void *dest, const void *src, int len)
+void *memcpy(void *dest, const void *src, size_t len)
{
char *d = (char *)dest;
const char *s = (const char *)src;
@@ -215,7 +215,7 @@ void *memcpy(void *dest, const void *src, int len)
}
-void *memset(void *dest, int c, int len)
+void *memset(void *dest, int c, size_t len)
{
char *d = (char *)dest;
uint32_t cccc;
@@ -253,7 +253,7 @@ void *memset(void *dest, int c, int len)
}
-void *memmove(void *dest, const void *src, int len)
+void *memmove(void *dest, const void *src, size_t len)
{
if ((uintptr_t)dest <= (uintptr_t)src ||
(uintptr_t)dest >= (uintptr_t)src + len) {
diff --git a/include/queue.h b/include/queue.h
index 9be0f31bdc..c38e5313f5 100644
--- a/include/queue.h
+++ b/include/queue.h
@@ -80,12 +80,28 @@ size_t queue_add_unit(struct queue const *q, void const *src);
/* Add multiple units to queue. */
size_t queue_add_units(struct queue const *q, void const *src, size_t count);
+/* Add multiple units to queue using supplied memcpy. */
+size_t queue_add_memcpy(struct queue const *q,
+ void const *src,
+ size_t count,
+ void *(*memcpy)(void *dest,
+ void const *src,
+ size_t n));
+
/* Remove one unit from the begin of the queue. */
size_t queue_remove_unit(struct queue const *q, void *dest);
/* Remove multiple units from the begin of the queue. */
size_t queue_remove_units(struct queue const *q, void *dest, size_t count);
+/* Remove multiple units from the begin of the queue using supplied memcpy. */
+size_t queue_remove_memcpy(struct queue const *q,
+ void *dest,
+ size_t count,
+ void *(*memcpy)(void *dest,
+ void const *src,
+ size_t n));
+
/* Peek (return but don't remove) the count elements starting with the i'th. */
size_t queue_peek_units(struct queue const *q,
void *dest,
diff --git a/include/util.h b/include/util.h
index e83e2fe05e..86c7a8dc0c 100644
--- a/include/util.h
+++ b/include/util.h
@@ -12,6 +12,8 @@
#include "compile_time_macros.h"
#include "panic.h"
+#include <stddef.h>
+
/**
* Trigger a debug exception if the condition
* is not verified at runtime.
@@ -88,12 +90,12 @@ int isdigit(int c);
int isspace(int c);
int isalpha(int c);
int isprint(int c);
-int memcmp(const void *s1, const void *s2, int len);
-void *memcpy(void *dest, const void *src, int len);
-void *memset(void *dest, int c, int len);
-void *memmove(void *dest, const void *src, int len);
+int memcmp(const void *s1, const void *s2, size_t len);
+void *memcpy(void *dest, const void *src, size_t len);
+void *memset(void *dest, int c, size_t len);
+void *memmove(void *dest, const void *src, size_t len);
int strcasecmp(const char *s1, const char *s2);
-int strncasecmp(const char *s1, const char *s2, int size);
+int strncasecmp(const char *s1, const char *s2, size_t size);
int strlen(const char *s);
/* Like strtol(), but for integers. */