From 92bbb8e8b15e8eb52d0ba112f658de82dacc666d Mon Sep 17 00:00:00 2001 From: jp9000 Date: Mon, 18 Jan 2016 19:59:31 -0800 Subject: [PATCH] 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. --- libobs/callback/calldata.c | 18 +++++++++++++----- libobs/callback/calldata.h | 18 ++++++++++++++++-- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/libobs/callback/calldata.c b/libobs/callback/calldata.c index c10f20a24..6fa0f62d6 100644 --- a/libobs/callback/calldata.c +++ b/libobs/callback/calldata.c @@ -17,6 +17,7 @@ #include #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); diff --git a/libobs/callback/calldata.h b/libobs/callback/calldata.h index 3402beed0..0aa8e5bb4 100644 --- a/libobs/callback/calldata.h +++ b/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,