<div dir="ltr"><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Jan 18, 2019 at 6:08 PM Bas Nieuwenhuizen <<a href="mailto:bas@basnieuwenhuizen.nl">bas@basnieuwenhuizen.nl</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Fri, Jan 18, 2019 at 5:44 PM Marek Olšák <<a href="mailto:maraeo@gmail.com" target="_blank">maraeo@gmail.com</a>> wrote:<br>
><br>
> From: Marek Olšák <<a href="mailto:marek.olsak@amd.com" target="_blank">marek.olsak@amd.com</a>><br>
><br>
> ---<br>
>  src/gallium/auxiliary/Makefile.sources      |  1 +<br>
>  src/gallium/auxiliary/meson.build           |  1 +<br>
>  src/gallium/auxiliary/util/u_cpu_suballoc.h | 90 +++++++++++++++++++++<br>
>  3 files changed, 92 insertions(+)<br>
>  create mode 100644 src/gallium/auxiliary/util/u_cpu_suballoc.h<br>
><br>
> diff --git a/src/gallium/auxiliary/Makefile.sources b/src/gallium/auxiliary/Makefile.sources<br>
> index 50e88088ff8..b26415858f6 100644<br>
> --- a/src/gallium/auxiliary/Makefile.sources<br>
> +++ b/src/gallium/auxiliary/Makefile.sources<br>
> @@ -211,20 +211,21 @@ C_SOURCES := \<br>
>         util/u_bitmask.c \<br>
>         util/u_bitmask.h \<br>
>         util/u_blend.h \<br>
>         util/u_blit.c \<br>
>         util/u_blit.h \<br>
>         util/u_blitter.c \<br>
>         util/u_blitter.h \<br>
>         util/u_box.h \<br>
>         util/u_cache.c \<br>
>         util/u_cache.h \<br>
> +       util/u_cpu_suballoc.h \<br>
>         util/u_debug_gallium.h \<br>
>         util/u_debug_gallium.c \<br>
>         util/u_debug_describe.c \<br>
>         util/u_debug_describe.h \<br>
>         util/u_debug_flush.c \<br>
>         util/u_debug_flush.h \<br>
>         util/u_debug_image.c \<br>
>         util/u_debug_image.h \<br>
>         util/u_debug_memory.c \<br>
>         util/u_debug_refcnt.c \<br>
> diff --git a/src/gallium/auxiliary/meson.build b/src/gallium/auxiliary/meson.build<br>
> index 57f7e69050f..7e1e4732421 100644<br>
> --- a/src/gallium/auxiliary/meson.build<br>
> +++ b/src/gallium/auxiliary/meson.build<br>
> @@ -231,20 +231,21 @@ files_libgallium = files(<br>
>    'util/u_bitmask.c',<br>
>    'util/u_bitmask.h',<br>
>    'util/u_blend.h',<br>
>    'util/u_blit.c',<br>
>    'util/u_blit.h',<br>
>    'util/u_blitter.c',<br>
>    'util/u_blitter.h',<br>
>    'util/u_box.h',<br>
>    'util/u_cache.c',<br>
>    'util/u_cache.h',<br>
> +  'util/u_cpu_suballoc.h',<br>
>    'util/u_debug_gallium.h',<br>
>    'util/u_debug_gallium.c',<br>
>    'util/u_debug_describe.c',<br>
>    'util/u_debug_describe.h',<br>
>    'util/u_debug_flush.c',<br>
>    'util/u_debug_flush.h',<br>
>    'util/u_debug_image.c',<br>
>    'util/u_debug_image.h',<br>
>    'util/u_debug_memory.c',<br>
>    'util/u_debug_refcnt.c',<br>
> diff --git a/src/gallium/auxiliary/util/u_cpu_suballoc.h b/src/gallium/auxiliary/util/u_cpu_suballoc.h<br>
> new file mode 100644<br>
> index 00000000000..2373c1f7c70<br>
> --- /dev/null<br>
> +++ b/src/gallium/auxiliary/util/u_cpu_suballoc.h<br>
> @@ -0,0 +1,90 @@<br>
> +/**************************************************************************<br>
> + *<br>
> + * Copyright 2019 Advanced Micro Devices, Inc.<br>
> + * All Rights Reserved.<br>
> + *<br>
> + * Permission is hereby granted, free of charge, to any person obtaining a<br>
> + * copy of this software and associated documentation files (the<br>
> + * "Software"), to deal in the Software without restriction, including<br>
> + * without limitation the rights to use, copy, modify, merge, publish,<br>
> + * distribute, sub license, and/or sell copies of the Software, and to<br>
> + * permit persons to whom the Software is furnished to do so, subject to<br>
> + * the following conditions:<br>
> + *<br>
> + * The above copyright notice and this permission notice (including the<br>
> + * next paragraph) shall be included in all copies or substantial portions<br>
> + * of the Software.<br>
> + *<br>
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS<br>
> + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF<br>
> + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.<br>
> + * IN NO EVENT SHALL AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR<br>
> + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,<br>
> + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE<br>
> + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.<br>
> + *<br>
> + **************************************************************************/<br>
> +<br>
> +/* A simple utility for suballocating out of malloc_aligned. */<br>
> +<br>
> +#ifndef U_CPU_SUBALLOC_H<br>
> +#define U_CPU_SUBALLOC_H<br>
> +<br>
> +#include <stdint.h><br>
> +#include "util/os_memory.h"<br>
> +<br>
> +struct u_cpu_suballoc {<br>
> +   unsigned default_size;  /* Default size of the buffer, in bytes. */<br>
> +   unsigned current_size;  /* Current size of the buffer, in bytes. */<br>
> +   unsigned alignment;     /* malloc alignment. */<br>
> +   unsigned offset;        /* Offset pointing to the first unused byte. */<br>
> +   uint8_t *buffer;        /* Pointer to the CPU buffer. */<br>
> +};<br>
> +<br>
> +<br>
> +static inline void<br>
> +u_cpu_suballoc_init(struct u_cpu_suballoc *alloc, unsigned default_size,<br>
> +                   unsigned alignment)<br>
> +{<br>
> +   memset(alloc, 0, sizeof(*alloc));<br>
> +   alloc->default_size = default_size;<br>
> +   alloc->alignment = alignment;<br>
> +}<br>
> +<br>
> +<br>
> +static inline void<br>
> +u_cpu_suballoc_deinit(struct u_cpu_suballoc *alloc)<br>
> +{<br>
> +   os_free_aligned(alloc->buffer);<br>
> +   alloc->buffer = NULL;<br>
> +}<br>
> +<br>
> +<br>
> +static inline void *<br>
> +u_cpu_suballoc(struct u_cpu_suballoc *alloc, unsigned size, unsigned alignment)<br>
> +{<br>
> +   unsigned offset = align(alloc->offset, alignment);<br>
> +<br>
> +   /* Make sure we have enough space in the buffer for the sub-allocation. */<br>
> +   if (unlikely(!alloc->buffer || offset + size > alloc->current_size)) {<br>
> +      os_free_aligned(alloc->buffer);<br>
<br>
So, if we can free the memory during a suballocation, how do we make<br>
sure that the previous allocations are valid? If they don't have to<br>
stay valid, why use a linear allocator at all?<br></blockquote><div><br></div><div>The assumption is that previously allocated memory won't be accessed after the next call to u_cpu_suballoc. Yeah it's pretty fragile.</div><div><br></div><div>Marek</div></div></div>