[Mesa-dev] [PATCH 7/8] gallium/util: add a linear allocator for reducing malloc overhead

Marek Olšák maraeo at gmail.com
Fri Jan 18 16:43:58 UTC 2019


From: Marek Olšák <marek.olsak at amd.com>

---
 src/gallium/auxiliary/Makefile.sources      |  1 +
 src/gallium/auxiliary/meson.build           |  1 +
 src/gallium/auxiliary/util/u_cpu_suballoc.h | 90 +++++++++++++++++++++
 3 files changed, 92 insertions(+)
 create mode 100644 src/gallium/auxiliary/util/u_cpu_suballoc.h

diff --git a/src/gallium/auxiliary/Makefile.sources b/src/gallium/auxiliary/Makefile.sources
index 50e88088ff8..b26415858f6 100644
--- a/src/gallium/auxiliary/Makefile.sources
+++ b/src/gallium/auxiliary/Makefile.sources
@@ -211,20 +211,21 @@ C_SOURCES := \
 	util/u_bitmask.c \
 	util/u_bitmask.h \
 	util/u_blend.h \
 	util/u_blit.c \
 	util/u_blit.h \
 	util/u_blitter.c \
 	util/u_blitter.h \
 	util/u_box.h \
 	util/u_cache.c \
 	util/u_cache.h \
+	util/u_cpu_suballoc.h \
 	util/u_debug_gallium.h \
 	util/u_debug_gallium.c \
 	util/u_debug_describe.c \
 	util/u_debug_describe.h \
 	util/u_debug_flush.c \
 	util/u_debug_flush.h \
 	util/u_debug_image.c \
 	util/u_debug_image.h \
 	util/u_debug_memory.c \
 	util/u_debug_refcnt.c \
diff --git a/src/gallium/auxiliary/meson.build b/src/gallium/auxiliary/meson.build
index 57f7e69050f..7e1e4732421 100644
--- a/src/gallium/auxiliary/meson.build
+++ b/src/gallium/auxiliary/meson.build
@@ -231,20 +231,21 @@ files_libgallium = files(
   'util/u_bitmask.c',
   'util/u_bitmask.h',
   'util/u_blend.h',
   'util/u_blit.c',
   'util/u_blit.h',
   'util/u_blitter.c',
   'util/u_blitter.h',
   'util/u_box.h',
   'util/u_cache.c',
   'util/u_cache.h',
+  'util/u_cpu_suballoc.h',
   'util/u_debug_gallium.h',
   'util/u_debug_gallium.c',
   'util/u_debug_describe.c',
   'util/u_debug_describe.h',
   'util/u_debug_flush.c',
   'util/u_debug_flush.h',
   'util/u_debug_image.c',
   'util/u_debug_image.h',
   'util/u_debug_memory.c',
   'util/u_debug_refcnt.c',
diff --git a/src/gallium/auxiliary/util/u_cpu_suballoc.h b/src/gallium/auxiliary/util/u_cpu_suballoc.h
new file mode 100644
index 00000000000..2373c1f7c70
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_cpu_suballoc.h
@@ -0,0 +1,90 @@
+/**************************************************************************
+ *
+ * Copyright 2019 Advanced Micro Devices, Inc.
+ * All Rights Reserved.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sub license, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the
+ * next paragraph) shall be included in all copies or substantial portions
+ * of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
+ * IN NO EVENT SHALL AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
+ * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ *
+ **************************************************************************/
+
+/* A simple utility for suballocating out of malloc_aligned. */
+
+#ifndef U_CPU_SUBALLOC_H
+#define U_CPU_SUBALLOC_H
+
+#include <stdint.h>
+#include "util/os_memory.h"
+
+struct u_cpu_suballoc {
+   unsigned default_size;  /* Default size of the buffer, in bytes. */
+   unsigned current_size;  /* Current size of the buffer, in bytes. */
+   unsigned alignment;     /* malloc alignment. */
+   unsigned offset;        /* Offset pointing to the first unused byte. */
+   uint8_t *buffer;        /* Pointer to the CPU buffer. */
+};
+
+
+static inline void
+u_cpu_suballoc_init(struct u_cpu_suballoc *alloc, unsigned default_size,
+		    unsigned alignment)
+{
+   memset(alloc, 0, sizeof(*alloc));
+   alloc->default_size = default_size;
+   alloc->alignment = alignment;
+}
+
+
+static inline void
+u_cpu_suballoc_deinit(struct u_cpu_suballoc *alloc)
+{
+   os_free_aligned(alloc->buffer);
+   alloc->buffer = NULL;
+}
+
+
+static inline void *
+u_cpu_suballoc(struct u_cpu_suballoc *alloc, unsigned size, unsigned alignment)
+{
+   unsigned offset = align(alloc->offset, alignment);
+
+   /* Make sure we have enough space in the buffer for the sub-allocation. */
+   if (unlikely(!alloc->buffer || offset + size > alloc->current_size)) {
+      os_free_aligned(alloc->buffer);
+
+      alloc->current_size = MAX2(alloc->default_size, size);
+      alloc->offset = 0;
+      alloc->buffer = (uint8_t*)os_malloc_aligned(alloc->current_size,
+                                                  alloc->alignment);
+
+      if (unlikely(!alloc->buffer))
+         return NULL;
+
+      offset = 0;
+   }
+
+   assert(offset + size <= alloc->current_size);
+
+   void *ptr = alloc->buffer + offset;
+   alloc->offset = offset + size;
+   return ptr;
+}
+
+#endif
-- 
2.17.1



More information about the mesa-dev mailing list