gallium/util: make util_copy_framebuffer_state(src=NULL) work
[mesa.git] / src / util / ralloc.c
index 36bc61fd07585c6db585a5d60797af3fb12a3beb..9526011b83674079815a21b744187c593480e689 100644 (file)
@@ -109,6 +109,18 @@ ralloc_context(const void *ctx)
 
 void *
 ralloc_size(const void *ctx, size_t size)
+{
+   /* ralloc_size was originally implemented using calloc, which meant some
+    * code accidentally relied on its zero filling behavior.
+    *
+    * TODO: Make ralloc_size not zero fill memory, and cleanup any code that
+    * should instead be using rzalloc.
+    */
+   return rzalloc_size(ctx, size);
+}
+
+void *
+rzalloc_size(const void *ctx, size_t size)
 {
    void *block = calloc(1, size + sizeof(ralloc_header));
    ralloc_header *info;
@@ -128,15 +140,6 @@ ralloc_size(const void *ctx, size_t size)
    return PTR_FROM_HEADER(info);
 }
 
-void *
-rzalloc_size(const void *ctx, size_t size)
-{
-   void *ptr = ralloc_size(ctx, size);
-   if (likely(ptr != NULL))
-      memset(ptr, 0, size);
-   return ptr;
-}
-
 /* helper function - assumes ptr != NULL */
 static void *
 resize(void *ptr, size_t size)
@@ -271,6 +274,33 @@ ralloc_steal(const void *new_ctx, void *ptr)
    add_child(parent, info);
 }
 
+void
+ralloc_adopt(const void *new_ctx, void *old_ctx)
+{
+   ralloc_header *new_info, *old_info, *child;
+
+   if (unlikely(old_ctx == NULL))
+      return;
+
+   old_info = get_header(old_ctx);
+   new_info = get_header(new_ctx);
+
+   /* If there are no children, bail. */
+   if (unlikely(old_info->child == NULL))
+      return;
+
+   /* Set all the children's parent to new_ctx; get a pointer to the last child. */
+   for (child = old_info->child; child->next != NULL; child = child->next) {
+      child->parent = new_info;
+   }
+
+   /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */
+   child->next = new_info->child;
+   child->parent = new_info;
+   new_info->child = old_info->child;
+   old_info->child = NULL;
+}
+
 void *
 ralloc_parent(const void *ptr)
 {
@@ -333,10 +363,7 @@ ralloc_strndup(const void *ctx, const char *str, size_t max)
    if (unlikely(str == NULL))
       return NULL;
 
-   n = strlen(str);
-   if (n > max)
-      n = max;
-
+   n = strnlen(str, max);
    ptr = ralloc_array(ctx, char, n + 1);
    memcpy(ptr, str, n);
    ptr[n] = '\0';
@@ -476,6 +503,7 @@ ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
    if (unlikely(*str == NULL)) {
       // Assuming a NULL context is probably bad, but it's expected behavior.
       *str = ralloc_vasprintf(NULL, fmt, args);
+      *start = strlen(*str);
       return true;
    }