Browse Source

libobs/util: Add circlebuf_push_front function

jp9000 9 years ago
parent
commit
c6914fda45
1 changed files with 21 additions and 0 deletions
  1. 21 0
      libobs/util/circlebuf.h

+ 21 - 0
libobs/util/circlebuf.h

@@ -168,6 +168,27 @@ static inline void circlebuf_push_back(struct circlebuf *cb, const void *data,
 	cb->end_pos = new_end_pos;
 }
 
+static inline void circlebuf_push_front(struct circlebuf *cb, const void *data,
+		size_t size)
+{
+	cb->size += size;
+	circlebuf_ensure_capacity(cb);
+
+	if (cb->start_pos < size) {
+		size_t back_size = size - cb->start_pos;
+
+		if (cb->start_pos)
+			memcpy(cb->data, (uint8_t*)data + back_size,
+					cb->start_pos);
+
+		cb->start_pos = cb->capacity - back_size;
+		memcpy((uint8_t*)cb->data + cb->start_pos, data, back_size);
+	} else {
+		cb->start_pos -= size;
+		memcpy((uint8_t*)cb->data + cb->start_pos, data, size);
+	}
+}
+
 static inline void circlebuf_peek_front(struct circlebuf *cb, void *data,
 		size_t size)
 {