lightning
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] jit_size: Don't round up the size to a multiple of 4 KiB


From: Paul Cercueil
Subject: [PATCH] jit_size: Don't round up the size to a multiple of 4 KiB
Date: Sun, 5 Jun 2022 11:25:37 +0100

When using an external code buffer, a program using Lightning will call
jit_get_code() to get the expected size of the code, then allocate some
space in the code buffer, then call jit_set_code() to specify where the
code should be written to.

If the reported size is rounded up to a multiple of 4 KiB, then the
allocator will always try to allocate 4 KiB for blocks of code that
might even be smaller than a hundred bytes. The program can then choose
to realloc() the allocated block to the actual size of the generated
code, to reduce the memory used.

However, this will cause dramatic memory fragmentation; for instance,
if working with a 2 MiB code buffer, in which 512 blocks of 4 KiB are
allocated but later realloc'd to 128 bytes each, the total amount of
allocated memory will be 128 * 512 == 64 KiB, with almost 1.9 MiB free,
yet it will be impossible to allocate any new blocks as there would be
no way to find a contiguous 4 KiB area.

Besides, I really don't understand why it was rounded up to a multiple
of 4 KiB, as this is not a requirement for mmap().

Signed-off-by: Paul Cercueil <paul@crapouillou.net>
---
 lib/jit_size.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/jit_size.c b/lib/jit_size.c
index 61f1aa4..3a78394 100644
--- a/lib/jit_size.c
+++ b/lib/jit_size.c
@@ -105,7 +105,7 @@ _jit_get_size(jit_state_t *_jit)
     for (size = JIT_INSTR_MAX, node = _jitc->head; node; node = node->next)
        size += _szs[node->code];
 
-    return ((size + 4095) & -4096);
+    return size;
 }
 #endif
 
-- 
2.35.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]