You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.1 KiB

12 years ago
/*
* duk_hbuffer operations such as resizing and inserting/appending data to
* a dynamic buffer.
12 years ago
*/
#include "duk_internal.h"
/*
* Resizing
*/
DUK_INTERNAL void duk_hbuffer_resize(duk_hthread *thr, duk_hbuffer_dynamic *buf, duk_size_t new_size) {
12 years ago
void *res;
duk_size_t prev_size;
12 years ago
DUK_ASSERT(thr != NULL);
DUK_ASSERT(buf != NULL);
DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
DUK_ASSERT(!DUK_HBUFFER_HAS_EXTERNAL(buf));
12 years ago
/*
* Maximum size check
*/
if (new_size > DUK_HBUFFER_MAX_BYTELEN) {
DUK_ERROR_RANGE(thr, "buffer too long");
}
12 years ago
/*
* Note: use indirect realloc variant just in case mark-and-sweep
* (finalizers) might resize this same buffer during garbage
* collection.
*/
res = DUK_REALLOC_INDIRECT(thr->heap, duk_hbuffer_get_dynalloc_ptr, (void *) buf, new_size);
if (DUK_LIKELY(res != NULL || new_size == 0)) {
/* 'res' may be NULL if new allocation size is 0. */
DUK_DDD(DUK_DDDPRINT("resized dynamic buffer %p:%ld -> %p:%ld",
(void *) DUK_HBUFFER_DYNAMIC_GET_DATA_PTR(thr->heap, buf),
16-bit fields and heap pointer compression work Memory optimization work for very low memory devices (96 to 256kB system RAM). Overall changes are: - 16-bit fields for various internal structures to reduce their size - Heap pointer compression to reduce pointer size to 16 bits When DUK_OPT_LIGHTFUNC_BUILTINS and the new low memory options are enabled, Duktape initial heap memory usage is about 23kB (compared to baseline of about 45kB) on x86. Unless low memory feature options are enabled, there should be no visible changes to Duktape behavior. More detailed changes: - 16-bit changes for duk_heaphdr: pointer compression, refcount - 16-bit changes for duk_hstring: hash, blen, and clen can all be 16 bits, use 0xFFFF as string byte length limit (call sites ensure this limit is never exceeded) - 16-bit changes for duk_hbuffer, use 0xFFFF as buffer length limit - 16-bit fields for hobject size (entry part, array part), drop hash part since it's not usually needed for extremely low memory environments - 16-bit changes for duk_hcompiledfunction - Heap pointer packing for stringtable - Heap pointer packing for 'strs' built-in strings list (saves around 600 to 700 bytes but may not be a good tradeoff because call site size will increase) Other changes: - Heaphdr NULL init fix. The original macros were broken: the double/single linked macro variants were the wrong way around. Now sets through macro to work properly with compressed pointers. - Rename duk_hbuffer CURR_DATA_PTR -> DATA_PTR to reduce macro length (previous name was tediously long) - Rename buffer "usable_size" to "alloc_size" throughout as they have been the same for a while now (they used to differ when buffer had an extra NUL). - Add memory optimization markers to Duktape.env (pointer compression and individual 16-bit field options) - Rename a few internal fields for clarity: duk_hobject 'p' to 'props', heap->st to heap->strtable - Add a safety check for buffer alloc size (should not be triggered but prevents wrapping if call sites don't properly check for sizes) - Other minor cleanups
10 years ago
(long) DUK_HBUFFER_DYNAMIC_GET_SIZE(buf),
(void *) res,
(long) new_size));
12 years ago
/*
* The entire allocated buffer area, regardless of actual used
* size, is kept zeroed in resizes for simplicity. If the buffer
* is grown, zero the new part.
12 years ago
*/
prev_size = DUK_HBUFFER_DYNAMIC_GET_SIZE(buf);
if (new_size > prev_size) {
DUK_ASSERT(new_size - prev_size > 0);
#if defined(DUK_USE_ZERO_BUFFER_DATA)
DUK_MEMZERO((void *) ((char *) res + prev_size),
(duk_size_t) (new_size - prev_size));
#endif
}
12 years ago
16-bit fields and heap pointer compression work Memory optimization work for very low memory devices (96 to 256kB system RAM). Overall changes are: - 16-bit fields for various internal structures to reduce their size - Heap pointer compression to reduce pointer size to 16 bits When DUK_OPT_LIGHTFUNC_BUILTINS and the new low memory options are enabled, Duktape initial heap memory usage is about 23kB (compared to baseline of about 45kB) on x86. Unless low memory feature options are enabled, there should be no visible changes to Duktape behavior. More detailed changes: - 16-bit changes for duk_heaphdr: pointer compression, refcount - 16-bit changes for duk_hstring: hash, blen, and clen can all be 16 bits, use 0xFFFF as string byte length limit (call sites ensure this limit is never exceeded) - 16-bit changes for duk_hbuffer, use 0xFFFF as buffer length limit - 16-bit fields for hobject size (entry part, array part), drop hash part since it's not usually needed for extremely low memory environments - 16-bit changes for duk_hcompiledfunction - Heap pointer packing for stringtable - Heap pointer packing for 'strs' built-in strings list (saves around 600 to 700 bytes but may not be a good tradeoff because call site size will increase) Other changes: - Heaphdr NULL init fix. The original macros were broken: the double/single linked macro variants were the wrong way around. Now sets through macro to work properly with compressed pointers. - Rename duk_hbuffer CURR_DATA_PTR -> DATA_PTR to reduce macro length (previous name was tediously long) - Rename buffer "usable_size" to "alloc_size" throughout as they have been the same for a while now (they used to differ when buffer had an extra NUL). - Add memory optimization markers to Duktape.env (pointer compression and individual 16-bit field options) - Rename a few internal fields for clarity: duk_hobject 'p' to 'props', heap->st to heap->strtable - Add a safety check for buffer alloc size (should not be triggered but prevents wrapping if call sites don't properly check for sizes) - Other minor cleanups
10 years ago
DUK_HBUFFER_DYNAMIC_SET_SIZE(buf, new_size);
DUK_HBUFFER_DYNAMIC_SET_DATA_PTR(thr->heap, buf, res);
12 years ago
} else {
DUK_ERROR_ALLOC_FAILED(thr);
12 years ago
}
DUK_ASSERT(res != NULL || new_size == 0);
12 years ago
}
DUK_INTERNAL void duk_hbuffer_reset(duk_hthread *thr, duk_hbuffer_dynamic *buf) {
12 years ago
DUK_ASSERT(thr != NULL);
DUK_ASSERT(buf != NULL);
DUK_ASSERT(DUK_HBUFFER_HAS_DYNAMIC(buf));
DUK_ASSERT(!DUK_HBUFFER_HAS_EXTERNAL(buf));
12 years ago
duk_hbuffer_resize(thr, buf, 0);
12 years ago
}