summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Staessens <[email protected]>2016-02-23 22:00:27 +0100
committerDimitri Staessens <[email protected]>2016-02-23 22:12:21 +0100
commitb97e438d696617a2b209c543abf673e3dc4ca077 (patch)
tree4134897b78e9162a7c232adc761171a949e0a201
parent8061cf8fb53866adc1238cae92a741c9346f618f (diff)
downloadouroboros-b97e438d696617a2b209c543abf673e3dc4ca077.tar.gz
ouroboros-b97e438d696617a2b209c543abf673e3dc4ca077.zip
lib: bugfixes to du_buff.c
The initial commit was untested and caused some segmentation faults. This version should be bugfree (no promises).
-rw-r--r--src/lib/du_buff.c61
1 files changed, 41 insertions, 20 deletions
diff --git a/src/lib/du_buff.c b/src/lib/du_buff.c
index e4b56b5b..84fa22a0 100644
--- a/src/lib/du_buff.c
+++ b/src/lib/du_buff.c
@@ -59,17 +59,17 @@ void buffer_destroy(struct buffer * buf)
}
-void buffer_destroy_list(struct buffer * buf)
+void buffer_destroy_list(struct buffer * head)
{
struct list_head * ptr;
struct list_head * n;
- if (buf == NULL) {
+ if (head == NULL) {
LOG_DBGF("Bogus input, bugging out.");
return;
}
- list_for_each_safe(ptr, n, &(buf->list)) {
+ list_for_each_safe(ptr, n, &(head->list)) {
struct buffer * tmp = list_entry(ptr, struct buffer, list);
list_del(ptr);
buffer_destroy(tmp);
@@ -78,10 +78,16 @@ void buffer_destroy_list(struct buffer * buf)
struct buffer * buffer_create (size_t size)
{
- struct buffer * head = NULL;
+ struct buffer * head = NULL;
size_t remaining = size;
const size_t page_size = sysconf(_SC_PAGESIZE);
+ head = (struct buffer *) malloc(sizeof(struct buffer));
+ head->size=0;
+ head->data=NULL;
+
+ INIT_LIST_HEAD(&(head->list));
+
while (remaining > 0) {
struct buffer * buf;
size_t sz = remaining < page_size ? remaining : page_size;
@@ -98,14 +104,9 @@ struct buffer * buffer_create (size_t size)
buffer_destroy_list(head);
return NULL;
}
-
buf->size = sz;
- INIT_LIST_HEAD(&(buf->list));
- if (head == NULL)
- head = buf;
- else
- list_add_tail(&(buf->list), &(head->list));
+ list_add_tail(&(buf->list), &(head->list));
remaining -= buf->size;
}
@@ -127,8 +128,12 @@ struct buffer * buffer_seek(const struct buffer * head, size_t pos)
list_for_each(ptr, &(head->list)) {
struct buffer * tmp = list_entry(ptr, struct buffer, list);
- cur_buf_end = cur_buf_start + tmp->size;
+ if (tmp == NULL) {
+ LOG_WARN("Could not iterate over elements %p", head);
+ return NULL;
+ }
+ cur_buf_end = cur_buf_start + tmp->size;
if (cur_buf_end > pos)
return tmp;
@@ -186,8 +191,8 @@ int buffer_copy_data(struct buffer * head,
if (buf_start == NULL || buf_end == NULL) {
LOG_DBGF("Index out of bounds %d, %d",
- pos,
- pos+len);
+ (int) pos,
+ (int) (pos+len));
return -EINVAL;
}
@@ -200,9 +205,12 @@ int buffer_copy_data(struct buffer * head,
copy_pos = (uint8_t *)src;
bytes_remaining = len;
- list_for_each(ptr, &(buf_start->list)) {
+ list_for_each(ptr, &(head->list)) {
struct buffer * tmp = list_entry(ptr, struct buffer, list);
- space_in_buf = tmp->data + tmp->size - ptr_start;
+ if (tmp != buf_start)
+ continue;
+
+ space_in_buf = (tmp->data + tmp->size) - ptr_start;
if (space_in_buf >= bytes_remaining) {
memcpy(ptr_start, copy_pos, bytes_remaining);
return 0;
@@ -257,38 +265,52 @@ int du_buff_init(du_buff_t * dub,
uint8_t * data,
size_t len)
{
+ int ret = -EINVAL;
+
if (dub == NULL || data == NULL) {
LOG_DBG("Bogus input, bugging out.");
return -EINVAL;
}
if (start + len > dub->size) {
- LOG_DBGF("Index out of bounds %d", start);
+ LOG_DBGF("Index out of bounds %d.", (int) start);
return -EINVAL;
}
dub->du_start = start;
dub->du_end = start + len;
- return buffer_copy_data(dub->buffer, start, data, len);
+ ret = buffer_copy_data(dub->buffer, start, data, len);
+
+ return ret;
}
uint8_t * du_buff_data_ptr_start(du_buff_t * dub)
{
+ uint8_t * ret = NULL;
+
if (dub == NULL) {
LOG_DBGF("Bogus input, bugging out.");
return NULL;
}
- return buffer_seek_pos(dub->buffer, dub->du_start);
+
+ ret = buffer_seek_pos(dub->buffer, dub->du_start);
+
+ return ret;
}
uint8_t * du_buff_data_ptr_end(du_buff_t * dub)
{
+ uint8_t * ret = NULL;
+
if (dub == NULL) {
LOG_DBG("Bogus input, bugging out.");
return NULL;
}
- return buffer_seek_pos(dub->buffer, dub->du_end);
+
+ ret = buffer_seek_pos(dub->buffer, dub->du_end);
+
+ return ret;
}
int du_buff_head_alloc(du_buff_t * dub, size_t size)
@@ -322,7 +344,6 @@ int du_buff_tail_alloc(du_buff_t * dub, size_t size)
dub->du_end += size;
return 0;
-
}
int du_buff_head_release(du_buff_t * dub, size_t size)