summaryrefslogtreecommitdiffstats
path: root/libass/ass_utils.h
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2014-11-14 20:19:14 +0100
committerwm4 <wm4@nowhere>2014-11-14 20:35:31 +0100
commitf2788e6a61e7c2c2d7122f8a90812f55a9958f76 (patch)
tree3748ad6b26bdcea5bf80686bc85d981c0ae24749 /libass/ass_utils.h
parentdc4544a0d493d79de3766f267df893d377fa148a (diff)
downloadlibass-f2788e6a61e7c2c2d7122f8a90812f55a9958f76.tar.bz2
libass-f2788e6a61e7c2c2d7122f8a90812f55a9958f76.tar.xz
Add another helper-macro for array allocation
Diffstat (limited to 'libass/ass_utils.h')
-rw-r--r--libass/ass_utils.h15
1 files changed, 15 insertions, 0 deletions
diff --git a/libass/ass_utils.h b/libass/ass_utils.h
index 5548a931..5055e889 100644
--- a/libass/ass_utils.h
+++ b/libass/ass_utils.h
@@ -25,6 +25,7 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
+#include <errno.h>
#ifdef CONFIG_ENCA
#include <enca.h>
@@ -57,6 +58,20 @@ void *ass_aligned_alloc(size_t alignment, size_t size);
void ass_aligned_free(void *ptr);
void *ass_realloc_array(void *ptr, size_t nmemb, size_t size);
+void *ass_try_realloc_array(void *ptr, size_t nmemb, size_t size);
+
+/**
+ * Reallocate the array in ptr to at least count elements. For example, if
+ * you do "int *ptr = NULL; ASS_REALLOC_ARRAY(ptr, 5)", you can access ptr[0]
+ * through ptr[4] (inclusive).
+ *
+ * If memory allocation fails, ptr is left unchanged, and the macro returns 0:
+ * "if (!ASS_REALLOC_ARRAY(ptr, 5)) goto error;"
+ *
+ * A count of 0 does not free the array (see ass_realloc_array for remarks).
+ */
+#define ASS_REALLOC_ARRAY(ptr, count) \
+ (errno = 0, (ptr) = ass_try_realloc_array(ptr, count, sizeof(*ptr)), !errno)
void skip_spaces(char **str);
void rskip_spaces(char **str, char *limit);