summaryrefslogtreecommitdiff
path: root/py/malloc.c
AgeCommit message (Collapse)Author
2025-05-02py/malloc: Add mutex for tracked allocations.Angus Gratton
Fixes thread safety issue that could cause memory corruption on ports with (MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL) - currently only rp2 and unix have this configuration. Adds unit test for TLS sockets that exercises this code path. I wasn't able to make this fail on rp2, the race condition window is pretty narrow and may not have a direct impact on a quiet system. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
2024-03-07all: Remove the "STATIC" macro and just use "static" instead.Angus Gratton
The STATIC macro was introduced a very long time ago in commit d5df6cd44a433d6253a61cb0f987835fbc06b2de. The original reason for this was to have the option to define it to nothing so that all static functions become global functions and therefore visible to certain debug tools, so one could do function size comparison and other things. This STATIC feature is rarely (if ever) used. And with the use of LTO and heavy inline optimisation, analysing the size of individual functions when they are not static is not a good representation of the size of code when fully optimised. So the macro does not have much use and it's simpler to just remove it. Then you know exactly what it's doing. For example, newcomers don't have to learn what the STATIC macro is and why it exists. Reading the code is also less "loud" with a lowercase static. One other minor point in favour of removing it, is that it stops bugs with `STATIC inline`, which should always be `static inline`. Methodology for this commit was: 1) git ls-files | egrep '\.[ch]$' | \ xargs sed -Ei "s/(^| )STATIC($| )/\1static\2/" 2) Do some manual cleanup in the diff by searching for the word STATIC in comments and changing those back. 3) "git-grep STATIC docs/", manually fixed those cases. 4) "rg -t python STATIC", manually fixed codegen lines that used STATIC. This work was funded through GitHub Sponsors. Signed-off-by: Angus Gratton <angus@redyak.com.au>
2023-09-04py/malloc: Fix DEBUG_print() args in m_realloc_maybe.IhorNehrutsa
Signed-off-by: Ihor Nehrutsa <IhorNehrutsa@gmail.com>
2022-05-05py/malloc: Introduce m_tracked_calloc, m_tracked_free functions.Damien George
Enabled by MICROPY_TRACKED_ALLOC. Signed-off-by: Damien George <damien@micropython.org>
2020-02-28all: Reformat C and Python source code with tools/codeformat.py.Damien George
This is run with uncrustify 0.70.1, and black 19.10b0.
2020-02-28py/malloc: Put { on separate line for funcs that have selective sigs.Damien George
To ensure there are balanced {}'s in the file, and to help with formatting.
2018-07-09py/malloc: Give a compile warning if using finaliser without GC.Damien George
Fixes issue #3844.
2017-12-20py/malloc: Remove unneeded code checking m_malloc return value.Damien George
m_malloc already checks for a failed allocation so there's no need to check for it in m_malloc0.
2017-12-07py/malloc: Allow to use debug logging if !MICROPY_MALLOC_USES_ALLOCATED_SIZE.Paul Sokolovsky
This is mostly a workaround for forceful rebuilding of mpy-cross on every codebase change. If this file has debug logging enabled (by patching), mpy-cross build failed.
2017-12-07py/malloc: MICROPY_MEM_STATS requires MICROPY_MALLOC_USES_ALLOCATED_SIZE.Paul Sokolovsky
Error out if they're set incompatibly.
2017-08-31py: Make m_malloc_fail() have void return type, since it doesn't return.Damien George
2017-08-15py: Add verbose debug compile-time flag MICROPY_DEBUG_VERBOSE.Stefan Naumann
It enables all the DEBUG_printf outputs in the py/ source code.
2017-07-31all: Use the name MicroPython consistently in commentsAlexander Steffen
There were several different spellings of MicroPython present in comments, when there should be only one.
2016-08-26py/gc: Add MICROPY_GC_CONSERVATIVE_CLEAR option to always zero memory.Damien George
There can be stray pointers in memory blocks that are not properly zero'd after allocation. This patch adds a new config option to always zero all allocated memory (via gc_alloc and gc_realloc) and hence help to eliminate stray pointers. See issue #2195.
2016-02-23py/malloc: Provide a proper malloc-based implementation of realloc_ext.Damien George
2015-07-14py: Improve allocation policy of qstr data.Damien George
Previous to this patch all interned strings lived in their own malloc'd chunk. On average this wastes N/2 bytes per interned string, where N is the number-of-bytes for a quanta of the memory allocator (16 bytes on 32 bit archs). With this patch interned strings are concatenated into the same malloc'd chunk when possible. Such chunks are enlarged inplace when possible, and shrunk to fit when a new chunk is needed. RAM savings with this patch are highly varied, but should always show an improvement (unless only 3 or 4 strings are interned). New version typically uses about 70% of previous memory for the qstr data, and can lead to savings of around 10% of total memory footprint of a running script. Costs about 120 bytes code size on Thumb2 archs (depends on how many calls to gc_realloc are made).
2015-03-03py: Add MICROPY_MALLOC_USES_ALLOCATED_SIZE to allow simpler malloc API.Damien George
2015-01-07py: Put all global state together in state structures.Damien George
This patch consolidates all global variables in py/ core into one place, in a global structure. Root pointers are all located together to make GC tracing easier and more efficient.
2015-01-01py: Move to guarded includes, everywhere in py/ core.Damien George
Addresses issue #1022.
2014-10-31py: Make gc.enable/disable just control auto-GC; alloc is still allowed.Damien George
gc.enable/disable are now the same as CPython: they just control whether automatic garbage collection is enabled or not. If disabled, you can still allocate heap memory, and initiate a manual collection.
2014-10-23py: Clean up edge cases of malloc/realloc/free.Damien George
2014-09-25py: For malloc and vstr functions, use size_t exclusively for int type.Damien George
It seems most sensible to use size_t for measuring "number of bytes" in malloc and vstr functions (since that's what size_t is for). We don't use mp_uint_t because malloc and vstr are not Micro Python specific.
2014-06-21py: Include mpconfig.h before all other includes.Paul Sokolovsky
It defines types used by all other headers. Fixes #691.
2014-05-03Add license header to (almost) all files.Damien George
Blanket wide to all .c and .h files. Some files originating from ST are difficult to deal with (license wise) so it was left out of those. Also merged modpyb.h, modos.h, modstm.h and modtime.h in stmhal/.
2014-04-26py, gc: Only zero out the extra bytes at the end of the heap chunk.Damien George
This is a small optimisation to zero out only the additional bytes that the caller did not ask for.
2014-04-25py, gc: Zero out newly allocated blocks in the GC.Damien George
Also add some more debugging output to gc_dump_alloc_table(). Now that newly allocated heap is always zero'd, maybe we just make this a policy for the uPy API to keep it simple (ie any new implementation of memory allocation must zero all allocations). This follows the D language philosophy. Before this patch, a previously used memory block which had pointers in it may still retain those pointers if the new user of that block does not actually use the entire block. Eg, if I want 5 blocks worth of heap, I actually get 8 (round up to nearest 4). Then I never use the last 3, so they keep their old values, which may be pointers pointing to the heap, hence preventing GC. In rare (or maybe not that rare) cases, this leads to long, unintentional "linked lists" within the GC'd heap, filling it up completely. It's pretty rare, because you have to reuse exactly that memory which is part of this "linked list", and reuse it in just the right way. This should fix issue #522, and might have something to do with issue #510.
2014-04-10py: Check explicitly for memory allocation failure in parser.Damien George
Previously, a failed malloc/realloc would throw an exception, which was not caught. I think it's better to keep the parser free from NLR (exception throwing), hence this patch.
2014-04-10py: Add emergency exception object for when heap allocation fails.Damien George
2014-04-05Improve GC finalisation code; add option to disable it.Damien George
2014-04-05Merge pull request #425 from iabdalkader/delDamien George
Implement del
2014-04-04py: Add m_malloc_fail function to handle memory allocation error.Damien George
A malloc/realloc fail now throws MemoryError.
2014-04-03Implement delmux
2014-04-02py: Fix up so that it can compile without float.Damien George
2014-03-17py: Clean up includes.xbe
Remove unnecessary includes. Add includes that improve portability.
2014-02-26py: Remove name of var arg from macros with var args.Damien George
2014-02-16Make DEBUG_printf() a proper function, implementation is port-dependent.Paul Sokolovsky
In particular, unix outputs to stderr, to allow to run testsuite against micropython built with debug output (by redirecting stderr to /dev/null).
2014-02-12Replace global "static" -> "STATIC", to allow "analysis builds". Part 2.Paul Sokolovsky
2014-02-11Generalize malloc-via-gc-heap support, make it available to unix port.Paul Sokolovsky
2014-02-10Get rid of calloc().Paul Sokolovsky
If there's malloc and memset, then there's no need for calloc, especially if we need to implement it ourselves.
2014-01-30realloc(): Log original memory ptr too.Paul Sokolovsky
To alloc complete memory alloc flow tracing.
2014-01-29stm: Add optional memory debugging output.Damien George
2014-01-03Rename default config file to mpconfig.h, and port's to mpconfigport.h.Paul Sokolovsky
mpconfig.h will automatically pull mpconfigport.h.
2014-01-03Make it possible to turn off collecting memory stats (MICROPY_MEM_STATS).Paul Sokolovsky
2014-01-03Add new alloc metric: peak_bytes_allocated.Paul Sokolovsky
This is just max value of current_bytes_allocated seen.
2014-01-03Add new alloc metric: current_bytes_allocated.Paul Sokolovsky
Unlike total_bytes_allocated, this tracks m_free()'s too.
2014-01-03m_realloc: Account only allocation size difference in total_bytes_allocated.Paul Sokolovsky
2013-12-29Change memory allocation API to require size for free and realloc.Damien
2013-10-23Fix func decls with no arguments: () -> (void).Damien
2013-10-04Initial commit.Damien