summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2020-02-23 17:45:05 +0100
committerwm4 <wm4@nowhere>2020-02-23 17:45:05 +0100
commitbc3678da65831f81c9151a57b4272563d1df582e (patch)
tree60b474353f6fa0738e715492f5bf3cd8c6772771
parentf6615f00eeb05f072af24d132a78786c65e6aa80 (diff)
downloadmpv-bc3678da65831f81c9151a57b4272563d1df582e.tar.bz2
mpv-bc3678da65831f81c9151a57b4272563d1df582e.tar.xz
ta: change API; ta_set_parent() and ta_set_destructor() never fail
The previous change ensured that these cannot fail anymore (much like in original talloc). Change the APIs to not return a success value anymore, to "cement" this.
-rw-r--r--ta/ta.c30
-rw-r--r--ta/ta.h10
-rw-r--r--ta/ta_utils.c27
3 files changed, 22 insertions, 45 deletions
diff --git a/ta/ta.c b/ta/ta.c
index 5f7a20072b..95e8d4e3c5 100644
--- a/ta/ta.c
+++ b/ta/ta.c
@@ -75,19 +75,18 @@ static struct ta_header *get_header(void *ptr)
}
/* Set the parent allocation of ptr. If parent==NULL, remove the parent.
- * Setting parent==NULL (with ptr!=NULL) always succeeds, and unsets the
- * parent of ptr. Operations ptr==NULL always succeed and do nothing.
- * Returns true on success, false on OOM.
+ * Setting parent==NULL (with ptr!=NULL) unsets the parent of ptr.
+ * With ptr==NULL, the function does nothing.
*
* Warning: if ta_parent is a direct or indirect child of ptr, things will go
* wrong. The function will apparently succeed, but creates circular
* parent links, which are not allowed.
*/
-bool ta_set_parent(void *ptr, void *ta_parent)
+void ta_set_parent(void *ptr, void *ta_parent)
{
struct ta_header *ch = get_header(ptr);
if (!ch)
- return true;
+ return;
struct ta_header *new_parent = get_header(ta_parent);
// Unlink from previous parent
if (ch->prev)
@@ -114,7 +113,6 @@ bool ta_set_parent(void *ptr, void *ta_parent)
new_parent->child = ch;
ch->parent = new_parent;
}
- return true;
}
/* Allocate size bytes of memory. If ta_parent is not NULL, this is used as
@@ -132,10 +130,7 @@ void *ta_alloc_size(void *ta_parent, size_t size)
*h = (struct ta_header) {.size = size};
ta_dbg_add(h);
void *ptr = PTR_FROM_HEADER(h);
- if (!ta_set_parent(ptr, ta_parent)) {
- ta_free(ptr);
- return NULL;
- }
+ ta_set_parent(ptr, ta_parent);
return ptr;
}
@@ -152,10 +147,7 @@ void *ta_zalloc_size(void *ta_parent, size_t size)
*h = (struct ta_header) {.size = size};
ta_dbg_add(h);
void *ptr = PTR_FROM_HEADER(h);
- if (!ta_set_parent(ptr, ta_parent)) {
- ta_free(ptr);
- return NULL;
- }
+ ta_set_parent(ptr, ta_parent);
return ptr;
}
@@ -252,16 +244,12 @@ void ta_free(void *ptr)
* almost anything, but it must not attempt to free or realloc ptr. The
* destructor is run before the allocation's children are freed (also, before
* their destructors are run).
- *
- * Returns false if ptr==NULL, or on OOM.
*/
-bool ta_set_destructor(void *ptr, void (*destructor)(void *))
+void ta_set_destructor(void *ptr, void (*destructor)(void *))
{
struct ta_header *h = get_header(ptr);
- if (!h)
- return false;
- h->destructor = destructor;
- return true;
+ if (h)
+ h->destructor = destructor;
}
#ifdef TA_MEMORY_DEBUGGING
diff --git a/ta/ta.h b/ta/ta.h
index 4450795be8..340394db0f 100644
--- a/ta/ta.h
+++ b/ta/ta.h
@@ -50,8 +50,8 @@ void *ta_realloc_size(void *ta_parent, void *ptr, size_t size);
size_t ta_get_size(void *ptr);
void ta_free(void *ptr);
void ta_free_children(void *ptr);
-bool ta_set_destructor(void *ptr, void (*destructor)(void *));
-bool ta_set_parent(void *ptr, void *ta_parent);
+void ta_set_destructor(void *ptr, void (*destructor)(void *));
+void ta_set_parent(void *ptr, void *ta_parent);
// Utility functions
size_t ta_calc_array_size(size_t element_size, size_t count);
@@ -105,8 +105,6 @@ bool ta_vasprintf_append_buffer(char **str, const char *fmt, va_list ap) TA_PRF(
// code.
#define ta_xalloc_size(...) ta_oom_p(ta_alloc_size(__VA_ARGS__))
#define ta_xzalloc_size(...) ta_oom_p(ta_zalloc_size(__VA_ARGS__))
-#define ta_xset_destructor(...) ta_oom_b(ta_set_destructor(__VA_ARGS__))
-#define ta_xset_parent(...) ta_oom_b(ta_set_parent(__VA_ARGS__))
#define ta_xnew_context(...) ta_oom_p(ta_new_context(__VA_ARGS__))
#define ta_xstrdup_append(...) ta_oom_b(ta_strdup_append(__VA_ARGS__))
#define ta_xstrdup_append_buffer(...) ta_oom_b(ta_strdup_append_buffer(__VA_ARGS__))
@@ -127,6 +125,10 @@ bool ta_vasprintf_append_buffer(char **str, const char *fmt, va_list ap) TA_PRF(
#define ta_xnew_array_ptrtype(...) ta_oom_g(ta_new_array_ptrtype(__VA_ARGS__))
#define ta_xdup(...) ta_oom_g(ta_dup(__VA_ARGS__))
+// Cannot fail anymore.
+#define ta_xset_destructor(...) ta_set_destructor(__VA_ARGS__)
+#define ta_xset_parent(...) ta_set_parent(__VA_ARGS__)
+
#define ta_xsteal(ta_parent, ptr) (TA_TYPEOF(ptr))ta_xsteal_(ta_parent, ptr)
#define ta_xrealloc(ta_parent, ptr, type, count) \
(type *)ta_xrealloc_size(ta_parent, ptr, ta_calc_array_size(sizeof(type), count))
diff --git a/ta/ta_utils.c b/ta/ta_utils.c
index cc122b58a2..8d729a9028 100644
--- a/ta/ta_utils.c
+++ b/ta/ta_utils.c
@@ -44,21 +44,11 @@ size_t ta_calc_prealloc_elems(size_t nextidx)
return (nextidx + 1) * 2;
}
-static void dummy_dtor(void *p){}
-
-/* Create an empty (size 0) TA allocation, which is prepared in a way such that
- * using it as parent with ta_set_parent() always succeed. Calling
- * ta_set_destructor() on it will always succeed as well.
+/* Create an empty (size 0) TA allocation.
*/
void *ta_new_context(void *ta_parent)
{
- void *new = ta_alloc_size(ta_parent, 0);
- // Force it to allocate an extended header.
- if (!ta_set_destructor(new, dummy_dtor)) {
- ta_free(new);
- new = NULL;
- }
- return new;
+ return ta_alloc_size(ta_parent, 0);
}
/* Set parent of ptr to ta_parent, return the ptr.
@@ -68,8 +58,7 @@ void *ta_new_context(void *ta_parent)
*/
void *ta_steal_(void *ta_parent, void *ptr)
{
- if (!ta_set_parent(ptr, ta_parent))
- return NULL;
+ ta_set_parent(ptr, ta_parent);
return ptr;
}
@@ -138,10 +127,7 @@ char *ta_strndup(void *ta_parent, const char *str, size_t n)
return NULL;
char *new = NULL;
strndup_append_at(&new, 0, str, n);
- if (!ta_set_parent(new, ta_parent)) {
- ta_free(new);
- new = NULL;
- }
+ ta_set_parent(new, ta_parent);
return new;
}
@@ -229,7 +215,8 @@ char *ta_vasprintf(void *ta_parent, const char *fmt, va_list ap)
{
char *res = NULL;
ta_vasprintf_append_at(&res, 0, fmt, ap);
- if (!res || !ta_set_parent(res, ta_parent)) {
+ ta_set_parent(res, ta_parent);
+ if (!res) {
ta_free(res);
return NULL;
}
@@ -303,7 +290,7 @@ char *ta_oom_s(char *s)
void *ta_xsteal_(void *ta_parent, void *ptr)
{
- ta_oom_b(ta_set_parent(ptr, ta_parent));
+ ta_set_parent(ptr, ta_parent);
return ptr;
}