[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH v1 03/14] docs/devel: expand style section of memory management
From: |
Alex Bennée |
Subject: |
[PATCH v1 03/14] docs/devel: expand style section of memory management |
Date: |
Sat, 20 Mar 2021 13:36:55 +0000 |
This aims to provide a bit more guidance for those who take on one of
our "clean up memory allocation" bite-sized tasks.
Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
docs/devel/style.rst | 46 ++++++++++++++++++++++++++++++++------------
1 file changed, 34 insertions(+), 12 deletions(-)
diff --git a/docs/devel/style.rst b/docs/devel/style.rst
index 8b0bdb3570..260e3263fa 100644
--- a/docs/devel/style.rst
+++ b/docs/devel/style.rst
@@ -385,17 +385,37 @@ avoided.
Low level memory management
===========================
-Use of the malloc/free/realloc/calloc/valloc/memalign/posix_memalign
+Use of the ``malloc/free/realloc/calloc/valloc/memalign/posix_memalign``
APIs is not allowed in the QEMU codebase. Instead of these routines,
-use the GLib memory allocation routines g_malloc/g_malloc0/g_new/
-g_new0/g_realloc/g_free or QEMU's qemu_memalign/qemu_blockalign/qemu_vfree
-APIs.
-
-Please note that g_malloc will exit on allocation failure, so there
-is no need to test for failure (as you would have to with malloc).
-Calling g_malloc with a zero size is valid and will return NULL.
-
-Prefer g_new(T, n) instead of g_malloc(sizeof(T) ``*`` n) for the following
+use the GLib memory allocation routines
+``g_malloc/g_malloc0/g_new/g_new0/g_realloc/g_free``
+or QEMU's ``qemu_memalign/qemu_blockalign/qemu_vfree`` APIs.
+
+Please note that ``g_malloc`` will exit on allocation failure, so
+there is no need to test for failure (as you would have to with
+``malloc``). Generally using ``g_malloc`` on start-up is fine as the
+result of a failure to allocate memory is going to be a fatal exit
+anyway. There may be some start-up cases where failing is unreasonable
+(for example speculatively loading a large debug symbol table).
+
+Care should be taken to avoid introducing places where the guest could
+trigger an exit by causing a large allocation. For small allocations,
+of the order of 4k, a failure to allocate is likely indicative of an
+overloaded host and allowing ``g_malloc`` to ``exit`` is a reasonable
+approach. However for larger allocations where we could realistically
+fall-back to a smaller one if need be we should use functions like
+``g_try_new`` and check the result. For example this is valid approach
+for a time/space trade-off like ``tlb_mmu_resize_locked`` in the
+SoftMMU TLB code.
+
+If the lifetime of the allocation is within the function and there are
+multiple exist paths you can also improve the readability of the code
+by using ``g_autofree`` and related annotations. See :ref:`autofree-ref`
+for more details.
+
+Calling ``g_malloc`` with a zero size is valid and will return NULL.
+
+Prefer ``g_new(T, n)`` instead of ``g_malloc(sizeof(T) * n)`` for the following
reasons:
* It catches multiplication overflowing size_t;
@@ -409,8 +429,8 @@ Declarations like
are acceptable, though.
-Memory allocated by qemu_memalign or qemu_blockalign must be freed with
-qemu_vfree, since breaking this will cause problems on Win32.
+Memory allocated by ``qemu_memalign`` or ``qemu_blockalign`` must be freed with
+``qemu_vfree``, since breaking this will cause problems on Win32.
String manipulation
===================
@@ -485,6 +505,8 @@ In addition, QEMU assumes that the compiler does not use
the latitude
given in C99 and C11 to treat aspects of signed '<<' as undefined, as
documented in the GNU Compiler Collection manual starting at version 4.0.
+.. _autofree-ref:
+
Automatic memory deallocation
=============================
--
2.20.1
- [PATCH for 6.0 v1 00/14] fixes for rc1 (kernel-doc, semihosting, testing), Alex Bennée, 2021/03/20
- [PATCH v1 01/14] scripts/kernel-doc: strip QEMU_ from function definitions, Alex Bennée, 2021/03/20
- [PATCH v1 08/14] linux-user/riscv: initialise the TaskState heap/stack info, Alex Bennée, 2021/03/20
- [PATCH v1 02/14] docs/devel: include the plugin API information from the headers, Alex Bennée, 2021/03/20
- [PATCH v1 05/14] semihosting: move semihosting tests to multiarch, Alex Bennée, 2021/03/20
- [PATCH v1 04/14] tools/virtiofsd: include --socket-group in help, Alex Bennée, 2021/03/20
- [PATCH v1 03/14] docs/devel: expand style section of memory management,
Alex Bennée <=
- [PATCH v1 07/14] semihosting/arm-compat-semi: don't use SET_ARG to report SYS_HEAPINFO, Alex Bennée, 2021/03/20
- [PATCH v1 11/14] configure: Don't use the __atomic_*_16 functions for testing 128-bit support, Alex Bennée, 2021/03/20
- [PATCH v1 10/14] gitlab-ci.yml: Merge the trace-backend testing into other jobs, Alex Bennée, 2021/03/20
- [PATCH v1 06/14] semihosting/arm-compat-semi: unify GET/SET_ARG helpers, Alex Bennée, 2021/03/20
- [PATCH v1 09/14] tests/tcg: add HeapInfo checking to semihosting test, Alex Bennée, 2021/03/20
- [PATCH v1 12/14] cirrus.yml: Update the FreeBSD task to version 12.2, Alex Bennée, 2021/03/20
- [PATCH v1 14/14] utils: Work around mingw strto*l bug with 0x, Alex Bennée, 2021/03/20