summaryrefslogtreecommitdiffstatshomepage
path: root/py/vstr.c
Commit message (Collapse)AuthorAge
* all: Reformat C and Python source code with tools/codeformat.py.Damien George2020-02-28
| | | | This is run with uncrustify 0.70.1, and black 19.10b0.
* various: Add and update my copyright line based on git history.Paul Sokolovsky2019-05-17
| | | | For modules I initially created or made substantial contributions to.
* py/vstr: Raise a RuntimeError if fixed vstr buffer overflows.Damien George2017-09-21
| | | | | | | | | | | | Current users of fixed vstr buffers (building file paths) assume that there is no overflow and do not check for overflow after building the vstr. This has the potential to lead to NULL pointer dereferences (when vstr_null_terminated_str returns NULL because it can't allocate RAM for the terminating byte) and stat'ing and loading invalid path names (due to the path being truncated). The safest and simplest thing to do in these cases is just raise an exception if a write goes beyond the end of a fixed vstr buffer, which is what this patch does. It also simplifies the vstr code.
* all: Use the name MicroPython consistently in commentsAlexander Steffen2017-07-31
| | | | | There were several different spellings of MicroPython present in comments, when there should be only one.
* py,extmod: Some casts and minor refactors to quiet compiler warnings.Tom Collins2017-07-07
|
* py/vstr: Combine vstr_new_size with vstr_new since they are rarely used.Damien George2016-10-14
| | | | | | | Now there is just one function to allocate a new vstr, namely vstr_new (in addition to vstr_init etc). The caller of this function should know what initial size to allocate for the buffer, or at least have some policy or config option, instead of leaving it to a default (as it was before).
* py/vstr: Remove vstr.had_error flag and inline basic vstr functions.Damien George2016-09-19
| | | | | | | | | | | | | | | | | | The vstr.had_error flag was a relic from the very early days which assumed that the malloc functions (eg m_new, m_renew) returned NULL if they failed to allocate. But that's no longer the case: these functions will raise an exception if they fail. Since it was impossible for had_error to be set, this patch introduces no change in behaviour. An alternative option would be to change the malloc calls to the _maybe variants, which return NULL instead of raising, but then a lot of code will need to explicitly check if the vstr had an error and raise if it did. The code-size savings for this patch are, in bytes: bare-arm:188, minimal:456, unix(NDEBUG,x86-64):368, stmhal:228, esp8266:360.
* py/vstr: Change allocation policy, +16 to requested size, instead of *2.Paul Sokolovsky2016-05-10
| | | | | | | | | | | | | | | | | | | | Effect measured on esp8266 port: Before: >>> pystone_lowmem.main(10000) Pystone(1.2) time for 10000 passes = 44214 ms This machine benchmarks at 226 pystones/second >>> pystone_lowmem.main(10000) Pystone(1.2) time for 10000 passes = 44246 ms This machine benchmarks at 226 pystones/second After: >>> pystone_lowmem.main(10000) Pystone(1.2) time for 10000 passes = 44343ms This machine benchmarks at 225 pystones/second >>> pystone_lowmem.main(10000) Pystone(1.2) time for 10000 passes = 44376ms This machine benchmarks at 225 pystones/second
* py/vstr: vstr_null_terminated_str(): Extend string by at most one byte.Paul Sokolovsky2016-05-09
| | | | | | | vstr_null_terminated_str is almost certainly a vstr finalization operation, so it should add the requested NUL byte, and not try to pre-allocate more. The previous implementation could actually allocate double of the buffer size.
* py: Prevent many extra vstr allocations.Dave Hylands2015-07-06
| | | | | | | | | | | | | | | | | | | | | | | | | I checked the entire codebase, and every place that vstr_init_len was called, there was a call to mp_obj_new_str_from_vstr after it. mp_obj_new_str_from_vstr always tries to reallocate a new buffer 1 byte larger than the original to store the terminating null character. In many cases, if we allocated the initial buffer to be 1 byte longer, we can prevent this extra allocation, and just reuse the originally allocated buffer. Asking to read 256 bytes and only getting 100 will still cause the extra allocation, but if you ask to read 256 and get 256 then the extra allocation will be optimized away. Yes - the reallocation is optimized in the heap to try and reuse the buffer if it can, but it takes quite a few cycles to figure this out. Note by Damien: vstr_init_len should now be considered as a string-init convenience function and used only when creating null-terminated objects.
* py/vstr.c: Allow vstr_printf to print correctly to a fixed buffer.Damien George2015-04-18
| | | | | This patch allows vstr_printf to use all the available space of a fixed vstr buffer. vstr_printf is a good alternative to snprintf.
* py: Overhaul and simplify printf/pfenv mechanism.Damien George2015-04-16
| | | | | | | | | | | | | | | | | | | | | | Previous to this patch the printing mechanism was a bit of a tangled mess. This patch attempts to consolidate printing into one interface. All (non-debug) printing now uses the mp_print* family of functions, mainly mp_printf. All these functions take an mp_print_t structure as their first argument, and this structure defines the printing backend through the "print_strn" function of said structure. Printing from the uPy core can reach the platform-defined print code via two paths: either through mp_sys_stdout_obj (defined pert port) in conjunction with mp_stream_write; or through the mp_plat_print structure which uses the MP_PLAT_PRINT_STRN macro to define how string are printed on the platform. The former is only used when MICROPY_PY_IO is defined. With this new scheme printing is generally more efficient (less layers to go through, less arguments to pass), and, given an mp_print_t* structure, one can call mp_print_str for efficiency instead of mp_printf("%s", ...). Code size is also reduced by around 200 bytes on Thumb2 archs.
* py: Change vstr_null_terminate -> vstr_null_terminated_str, returns str.Damien George2015-01-29
|
* py: Change vstr so that it doesn't null terminate buffer by default.Damien George2015-01-28
| | | | | | | | | This cleans up vstr so that it's a pure "variable buffer", and the user can decide whether they need to add a terminating null byte. In most places where vstr is used, the vstr did not need to be null terminated and so this patch saves code size, a tiny bit of RAM, and makes vstr usage more efficient. When null termination is needed it must be done explicitly using vstr_null_terminate.
* py: Add comments for vstr_init and mp_obj_new_str.Damien George2015-01-21
|
* py: Remove mp_obj_str_builder and use vstr instead.Damien George2015-01-21
| | | | | | | | | | | | With this patch str/bytes construction is streamlined. Always use a vstr to build a str/bytes object. If the size is known beforehand then use vstr_init_len to allocate only required memory. Otherwise use vstr_init and the vstr will grow as needed. Then use mp_obj_new_str_from_vstr to create a str/bytes object using the vstr memory. Saves code ROM: 68 bytes on stmhal, 108 bytes on bare-arm, and 336 bytes on unix x64.
* py: Add mp_obj_new_str_from_vstr, and use it where relevant.Damien George2015-01-21
| | | | | | | | This patch allows to reuse vstr memory when creating str/bytes object. This improves memory usage. Also saves code ROM: 128 bytes on stmhal, 92 bytes on bare-arm, and 88 bytes on unix x64.
* py, unix: Allow to compile with -Wsign-compare.Damien George2015-01-16
| | | | See issue #699.
* py: Move to guarded includes, everywhere in py/ core.Damien George2015-01-01
| | | | Addresses issue #1022.
* py: Make functions static where appropriate.Damien George2014-12-10
|
* py: Convert [u]int to mp_[u]int_t where appropriate.Damien George2014-10-03
| | | | Addressing issue #50.
* py: For malloc and vstr functions, use size_t exclusively for int type.Damien George2014-09-25
| | | | | | 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.
* vstr: Restore bytestr compatibility.Paul Sokolovsky2014-06-27
|
* lexer, vstr: Add unicode support.Chris Angelico2014-06-27
|
* py: Include mpconfig.h before all other includes.Paul Sokolovsky2014-06-21
| | | | | | It defines types used by all other headers. Fixes #691.
* Add license header to (almost) all files.Damien George2014-05-03
| | | | | | | 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/.
* py: Fix vstr_init for case that alloc = 0.Damien George2014-03-31
|
* py: Clean up includes.xbe2014-03-17
| | | | Remove unnecessary includes. Add includes that improve portability.
* py: Fix bug in vstr_ins_blank_bytes.Damien George2014-03-15
|
* Add vstr_ins and vstr_cut_out; improve stmhal readline.Damien George2014-03-15
|
* Replace global "static" -> "STATIC", to allow "analysis builds". Part 2.Paul Sokolovsky2014-02-12
|
* Implement fixed buffer vstrs; use them for import path.Damien George2014-02-06
|
* Fix 1 warning and 1 bug.Damien George2014-01-22
|
* Add "buffer management" and "shrink" API calls to vstr.Paul Sokolovsky2014-01-13
| | | | | | | | | | | | | vstr is initially intended to deal with arbitrary-length strings. By providing a bit lower-level API calls, it will be also useful to deal with arbitrary-length I/O buffers (the difference from strings is that buffers are filled from "outside", via I/O). Another issue, especially aggravated by I/O buffer use, is alloc size vs actual size length. If allocated 1Mb for buffer, but actually read 1 byte, we don't want to keep rest of 1Mb be locked by this I/O result, but rather return it to heap ASAP ("shrink" buffer before passing it to qstr_from_str_take()).
* Basic implementation of import.Damien George2014-01-03
| | | | | | import works for simple cases. Still work to do on finding the right script, and setting globals/locals correctly when running an imported function.
* Change memory allocation API to require size for free and realloc.Damien2013-12-29
|
* Add simple var-arg functions; add simple string.format.Damien2013-11-03
|
* Fix func decls with no arguments: () -> (void).Damien2013-10-23
|
* Add vstr and its functions.Damien2013-10-20