summaryrefslogtreecommitdiff
path: root/py/malloc.c
diff options
context:
space:
mode:
authorScott Shawcroft <scott.shawcroft@gmail.com>2018-01-23 16:22:05 -0800
committerScott Shawcroft <scott.shawcroft@gmail.com>2018-01-24 10:33:46 -0800
commit416abe33ed44e44d8f75342731080217e123278b (patch)
tree73e35f752feccaeb8696112a13ba743cb041168d /py/malloc.c
parent56bd0789af7b11bcc15dc901745f61b83b1f7ff3 (diff)
Introduce a long lived section of the heap.
This adapts the allocation process to start from either end of the heap when searching for free space. The default behavior is identical to the existing behavior where it starts with the lowest block and looks higher. Now it can also look from the highest block and lower depending on the long_lived parameter to gc_alloc. As the heap fills, the two sections may overlap. When they overlap, a collect may be triggered in order to keep the long lived section compact. However, free space is always eligable for each type of allocation. By starting from either of the end of the heap we have ability to separate short lived objects from long lived ones. This separation reduces heap fragmentation because long lived objects are easy to densely pack. Most objects are short lived initially but may be made long lived when they are referenced by a type or module. This involves copying the memory and then letting the collect phase free the old portion. QSTR pools and chunks are always long lived because they are never freed. The reallocation, collection and free processes are largely unchanged. They simply also maintain an index to the highest free block as well as the lowest. These indices are used to speed up the allocation search until the next collect. In practice, this change may slightly slow down import statements with the benefit that memory is much less fragmented afterwards. For example, a test import into a 20k heap that leaves ~6k free previously had the largest continuous free space of ~400 bytes. After this change, the largest continuous free space is over 3400 bytes.
Diffstat (limited to 'py/malloc.c')
-rw-r--r--py/malloc.c19
1 files changed, 11 insertions, 8 deletions
diff --git a/py/malloc.c b/py/malloc.c
index ea1d4c4b9..d6983ffdf 100644
--- a/py/malloc.c
+++ b/py/malloc.c
@@ -53,12 +53,15 @@
#undef malloc
#undef free
#undef realloc
-#define malloc(b) gc_alloc((b), false)
-#define malloc_with_finaliser(b) gc_alloc((b), true)
+#define malloc_ll(b, ll) gc_alloc((b), false, (ll))
+#define malloc_with_finaliser(b) gc_alloc((b), true, false)
#define free gc_free
#define realloc(ptr, n) gc_realloc(ptr, n, true)
#define realloc_ext(ptr, n, mv) gc_realloc(ptr, n, mv)
#else
+#define malloc_ll(b, ll) malloc(b)
+#define malloc_with_finaliser(b) malloc((b))
+
STATIC void *realloc_ext(void *ptr, size_t n_bytes, bool allow_move) {
if (allow_move) {
return realloc(ptr, n_bytes);
@@ -71,8 +74,8 @@ STATIC void *realloc_ext(void *ptr, size_t n_bytes, bool allow_move) {
}
#endif // MICROPY_ENABLE_GC
-void *m_malloc(size_t num_bytes) {
- void *ptr = malloc(num_bytes);
+void *m_malloc(size_t num_bytes, bool long_lived) {
+ void *ptr = malloc_ll(num_bytes, long_lived);
if (ptr == NULL && num_bytes != 0) {
m_malloc_fail(num_bytes);
}
@@ -85,8 +88,8 @@ void *m_malloc(size_t num_bytes) {
return ptr;
}
-void *m_malloc_maybe(size_t num_bytes) {
- void *ptr = malloc(num_bytes);
+void *m_malloc_maybe(size_t num_bytes, bool long_lived) {
+ void *ptr = malloc_ll(num_bytes, long_lived);
#if MICROPY_MEM_STATS
MP_STATE_MEM(total_bytes_allocated) += num_bytes;
MP_STATE_MEM(current_bytes_allocated) += num_bytes;
@@ -112,8 +115,8 @@ void *m_malloc_with_finaliser(size_t num_bytes) {
}
#endif
-void *m_malloc0(size_t num_bytes) {
- void *ptr = m_malloc(num_bytes);
+void *m_malloc0(size_t num_bytes, bool long_lived) {
+ void *ptr = m_malloc(num_bytes, long_lived);
if (ptr == NULL && num_bytes != 0) {
m_malloc_fail(num_bytes);
}