util: Remove the stub pack/unpack functions for YUV formats.
[mesa.git] / src / util / vma.c
index 4887bdece0ced874ac126fdab0fd5888a64a2d87..dddc3fe187cd6faad05f3171f0a6d5b4533ef32a 100644 (file)
@@ -22,6 +22,7 @@
  */
 
 #include <stdlib.h>
+#include <inttypes.h>
 
 #include "util/u_math.h"
 #include "util/vma.h"
@@ -38,12 +39,18 @@ struct util_vma_hole {
 #define util_vma_foreach_hole_safe(_hole, _heap) \
    list_for_each_entry_safe(struct util_vma_hole, _hole, &(_heap)->holes, link)
 
+#define util_vma_foreach_hole_safe_rev(_hole, _heap) \
+   list_for_each_entry_safe_rev(struct util_vma_hole, _hole, &(_heap)->holes, link)
+
 void
 util_vma_heap_init(struct util_vma_heap *heap,
                    uint64_t start, uint64_t size)
 {
    list_inithead(&heap->holes);
    util_vma_heap_free(heap, start, size);
+
+   /* Default to using high addresses */
+   heap->alloc_high = true;
 }
 
 void
@@ -141,35 +148,96 @@ util_vma_heap_alloc(struct util_vma_heap *heap,
 
    util_vma_heap_validate(heap);
 
-   util_vma_foreach_hole_safe(hole, heap) {
-      if (size > hole->size)
-         continue;
+   if (heap->alloc_high) {
+      util_vma_foreach_hole_safe(hole, heap) {
+         if (size > hole->size)
+            continue;
 
-      /* Compute the offset as the highest address where a chunk of the given
-       * size can be without going over the top of the hole.
-       *
-       * This calculation is known to not overflow because we know that
-       * hole->size + hole->offset can only overflow to 0 and size > 0.
-       */
-      uint64_t offset = (hole->size - size) + hole->offset;
+         /* Compute the offset as the highest address where a chunk of the
+          * given size can be without going over the top of the hole.
+          *
+          * This calculation is known to not overflow because we know that
+          * hole->size + hole->offset can only overflow to 0 and size > 0.
+          */
+         uint64_t offset = (hole->size - size) + hole->offset;
 
-      /* Align the offset.  We align down and not up because we are allocating
-       * from the top of the hole and not the bottom.
-       */
-      offset = (offset / alignment) * alignment;
+         /* Align the offset.  We align down and not up because we are
+          * allocating from the top of the hole and not the bottom.
+          */
+         offset = (offset / alignment) * alignment;
 
-      if (offset < hole->offset)
-         continue;
+         if (offset < hole->offset)
+            continue;
 
-      util_vma_hole_alloc(hole, offset, size);
-      util_vma_heap_validate(heap);
-      return offset;
+         util_vma_hole_alloc(hole, offset, size);
+         util_vma_heap_validate(heap);
+         return offset;
+      }
+   } else {
+      util_vma_foreach_hole_safe_rev(hole, heap) {
+         if (size > hole->size)
+            continue;
+
+         uint64_t offset = hole->offset;
+
+         /* Align the offset */
+         uint64_t misalign = offset % alignment;
+         if (misalign) {
+            uint64_t pad = alignment - misalign;
+            if (pad > hole->size - size)
+               continue;
+
+            offset += pad;
+         }
+
+         util_vma_hole_alloc(hole, offset, size);
+         util_vma_heap_validate(heap);
+         return offset;
+      }
    }
 
    /* Failed to allocate */
    return 0;
 }
 
+bool
+util_vma_heap_alloc_addr(struct util_vma_heap *heap,
+                         uint64_t offset, uint64_t size)
+{
+   /* An offset of 0 is reserved for allocation failure.  It is not a valid
+    * address and cannot be allocated.
+    */
+   assert(offset > 0);
+
+   /* Allocating something with a size of 0 is also not valid. */
+   assert(size > 0);
+
+   /* It's possible for offset + size to wrap around if we touch the top of
+    * the 64-bit address space, but we cannot go any higher than 2^64.
+    */
+   assert(offset + size == 0 || offset + size > offset);
+
+   /* Find the hole if one exists. */
+   util_vma_foreach_hole_safe(hole, heap) {
+      if (hole->offset > offset)
+         continue;
+
+      /* Holes are ordered high-to-low so the first hole we find with
+       * hole->offset <= is our hole.  If it's not big enough to contain the
+       * requested range, then the allocation fails.
+       */
+      assert(hole->offset <= offset);
+      if (hole->size < offset - hole->offset + size)
+         return false;
+
+      util_vma_hole_alloc(hole, offset, size);
+      return true;
+   }
+
+   /* We didn't find a suitable hole */
+   return false;
+}
+
 void
 util_vma_heap_free(struct util_vma_heap *heap,
                    uint64_t offset, uint64_t size)
@@ -237,3 +305,22 @@ util_vma_heap_free(struct util_vma_heap *heap,
 
    util_vma_heap_validate(heap);
 }
+
+void
+util_vma_heap_print(struct util_vma_heap *heap, FILE *fp,
+                    const char *tab, uint64_t total_size)
+{
+   fprintf(fp, "%sutil_vma_heap:\n", tab);
+
+   uint64_t total_free = 0;
+   util_vma_foreach_hole(hole, heap) {
+      fprintf(fp, "%s    hole: offset = %"PRIu64" (0x%"PRIx64", "
+              "size = %"PRIu64" (0x%"PRIx64")\n",
+              tab, hole->offset, hole->offset, hole->size, hole->size);
+      total_free += hole->size;
+   }
+   assert(total_free <= total_size);
+   fprintf(fp, "%s%"PRIu64"B (0x%"PRIx64") free (%.2f%% full)\n",
+           tab, total_free, total_free,
+           ((double)(total_size - total_free) / (double)total_size) * 100);
+}