浏览代码

libobs/callback: Allow ability to use calldata_t with stack

Allows the ability to use fixed stack memory to construct a calldata_t
structure rather than having to allocate each time.  This is fine to do
for certain signals where the calldata never goes above a specific size.
If by some chance the size is insufficient, it will output a log
message.
jp9000 9 年之前
父节点
当前提交
92bbb8e8b1
共有 2 个文件被更改,包括 29 次插入7 次删除
  1. 13 5
      libobs/callback/calldata.c
  2. 16 2
      libobs/callback/calldata.h

+ 13 - 5
libobs/callback/calldata.c

@@ -17,6 +17,7 @@
 #include <string.h>
 
 #include "../util/bmem.h"
+#include "../util/base.h"
 
 #include "calldata.h"
 
@@ -137,14 +138,18 @@ static inline void cd_set_first_param(calldata_t *data, const char *name,
 	memset(pos, 0, sizeof(size_t));
 }
 
-static inline void cd_ensure_capacity(calldata_t *data, uint8_t **pos,
+static inline bool cd_ensure_capacity(calldata_t *data, uint8_t **pos,
 		size_t new_size)
 {
 	size_t offset;
 	size_t new_capacity;
 
 	if (new_size < data->capacity)
-		return;
+		return true;
+	if (data->fixed) {
+		blog(LOG_ERROR, "Tried to go above fixed calldata stack size!");
+		return false;
+	}
 
 	offset = *pos - data->stack;
 
@@ -156,6 +161,7 @@ static inline void cd_ensure_capacity(calldata_t *data, uint8_t **pos,
 	data->capacity = new_capacity;
 
 	*pos = data->stack + offset;
+	return true;
 }
 
 /* ------------------------------------------------------------------------- */
@@ -188,7 +194,7 @@ void calldata_set_data(calldata_t *data, const char *name, const void *in,
 	if (!data || !name || !*name)
 		return;
 
-	if (!data->stack) {
+	if (!data->fixed && !data->stack) {
 		cd_set_first_param(data, name, in, size);
 		return;
 	}
@@ -201,7 +207,8 @@ void calldata_set_data(calldata_t *data, const char *name, const void *in,
 			size_t offset = size - cur_size;
 			size_t bytes = data->size;
 
-			cd_ensure_capacity(data, &pos, bytes + offset);
+			if (!cd_ensure_capacity(data, &pos, bytes + offset))
+				return;
 			memmove(pos+offset, pos, bytes - (pos - data->stack));
 			data->size += offset;
 
@@ -218,7 +225,8 @@ void calldata_set_data(calldata_t *data, const char *name, const void *in,
 	} else {
 		size_t name_len = strlen(name)+1;
 		size_t offset = name_len + size + sizeof(size_t)*2;
-		cd_ensure_capacity(data, &pos, data->size + offset);
+		if (!cd_ensure_capacity(data, &pos, data->size + offset))
+			return;
 		data->size += offset;
 
 		cd_copy_string(&pos, name, 0);

+ 16 - 2
libobs/callback/calldata.h

@@ -44,9 +44,10 @@ enum call_param_type {
 #define CALL_PARAM_OUT (1<<1)
 
 struct calldata {
+	uint8_t *stack;
 	size_t  size;     /* size of the stack, in bytes */
 	size_t  capacity; /* capacity of the stack, in bytes */
-	uint8_t *stack;
+	bool    fixed;    /* fixed size (using call stack) */
 };
 
 typedef struct calldata calldata_t;
@@ -56,9 +57,22 @@ static inline void calldata_init(struct calldata *data)
 	memset(data, 0, sizeof(struct calldata));
 }
 
+static inline void calldata_clear(struct calldata *data);
+
+static inline void calldata_init_fixed(struct calldata *data, uint8_t *stack,
+		size_t size)
+{
+	data->stack = stack;
+	data->capacity = size;
+	data->fixed = true;
+	data->size = 0;
+	calldata_clear(data);
+}
+
 static inline void calldata_free(struct calldata *data)
 {
-	bfree(data->stack);
+	if (!data->fixed)
+		bfree(data->stack);
 }
 
 EXPORT bool calldata_get_data(const calldata_t *data, const char *name,