summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOneric <oneric@oneric.stub>2020-12-16 22:12:14 +0100
committerOneric <oneric@oneric.stub>2021-03-28 22:42:16 +0200
commit43af8f7b3b0ec43723a256ec04de3a571ebf0117 (patch)
tree6e7e0230670b68a2ddf997d05ecd21c379a272af
parent11493fbde283dc712cc45df9ceb209a1833c9a7f (diff)
downloadlibass-43af8f7b3b0ec43723a256ec04de3a571ebf0117.tar.bz2
libass-43af8f7b3b0ec43723a256ec04de3a571ebf0117.tar.xz
Add strdup fallback
And move fallback declarations to ass_compat.h As ass_compat.h is already included in every source file we no longer need to include _both_ string.h and ass_utils.h to use str(n)dup. Definitions are still in ass_utils.c since a separate source file just for two functions seemed overkill.
-rw-r--r--libass/ass_compat.h11
-rw-r--r--libass/ass_utils.c14
-rw-r--r--libass/ass_utils.h5
3 files changed, 24 insertions, 6 deletions
diff --git a/libass/ass_compat.h b/libass/ass_compat.h
index dc3395e..ae16bb7 100644
--- a/libass/ass_compat.h
+++ b/libass/ass_compat.h
@@ -26,4 +26,15 @@
#define inline __inline
#endif
+#ifndef HAVE_STRDUP
+char *ass_strdup_fallback(const char *s); // definition in ass_utils.c
+#define strdup ass_strdup_fallback
+#endif
+
+#ifndef HAVE_STRNDUP
+#include <stddef.h>
+char *ass_strndup_fallback(const char *s, size_t n); // definition in ass_utils.c
+#define strndup ass_strndup_fallback
+#endif
+
#endif /* LIBASS_COMPAT_H */
diff --git a/libass/ass_utils.c b/libass/ass_utils.c
index 7fba778..65b6f74 100644
--- a/libass/ass_utils.c
+++ b/libass/ass_utils.c
@@ -66,8 +66,20 @@ int has_avx2(void)
#endif // ASM
+// Fallbacks
+#ifndef HAVE_STRDUP
+char *ass_strdup_fallback(const char *str)
+{
+ size_t len = strlen(str) + 1;
+ char *new_str = malloc(len);
+ if (new_str)
+ memcpy(new_str, str, len);
+ return new_str;
+}
+#endif
+
#ifndef HAVE_STRNDUP
-char *ass_strndup(const char *s, size_t n)
+char *ass_strndup_fallback(const char *s, size_t n)
{
char *end = memchr(s, 0, n);
size_t len = end ? end - s : n;
diff --git a/libass/ass_utils.h b/libass/ass_utils.h
index 0379df6..567f5b5 100644
--- a/libass/ass_utils.h
+++ b/libass/ass_utils.h
@@ -72,11 +72,6 @@ static inline bool ass_string_equal(ASS_StringView str1, ASS_StringView str2)
return str1.len == str2.len && !memcmp(str1.str, str2.str, str1.len);
}
-#ifndef HAVE_STRNDUP
-char *ass_strndup(const char *s, size_t n);
-#define strndup ass_strndup
-#endif
-
void *ass_aligned_alloc(size_t alignment, size_t size, bool zero);
void ass_aligned_free(void *ptr);