diff --git a/Makefile b/Makefile index 3a763620..791fb1c8 100644 --- a/Makefile +++ b/Makefile @@ -221,8 +221,8 @@ CCOPTS_FEATURES += -DDUK_OPT_DEBUG_BUFSIZE=512 #CCOPTS_FEATURES += -DDUK_OPT_BUFLEN16 #CCOPTS_FEATURES += -DDUK_OPT_OBJSIZES16 #CCOPTS_FEATURES += -DDUK_OPT_HEAPPTR16 -#CCOPTS_FEATURES += '-DDUK_OPT_HEAPPTR_ENC16(p)=XXX' -#CCOPTS_FEATURES += '-DDUK_OPT_HEAPPTR_DEC16(x)=XXX' +#CCOPTS_FEATURES += '-DDUK_OPT_HEAPPTR_ENC16(ud,p)=XXX' +#CCOPTS_FEATURES += '-DDUK_OPT_HEAPPTR_DEC16(ud,x)=XXX' #CCOPTS_FEATURES += '-DDUK_OPT_DECLARE=XXX' CCOPTS_FEATURES += -DDUK_CMDLINE_FANCY CCOPTS_FEATURES += -DDUK_CMDLINE_ALLOC_LOGGING @@ -840,16 +840,16 @@ CCOPTS_AJDUK += -DDUK_OPT_OBJSIZES16 CCOPTS_AJDUK += -DDUK_OPT_STRTAB_CHAIN CCOPTS_AJDUK += -DDUK_OPT_STRTAB_CHAIN_SIZE=128 CCOPTS_AJDUK += -DDUK_OPT_HEAPPTR16 -CCOPTS_AJDUK += '-DDUK_OPT_HEAPPTR_ENC16(p)=ajsheap_enc16(p)' -CCOPTS_AJDUK += '-DDUK_OPT_HEAPPTR_DEC16(x)=ajsheap_dec16(x)' +CCOPTS_AJDUK += '-DDUK_OPT_HEAPPTR_ENC16(ud,p)=ajsheap_enc16((ud),(p))' +CCOPTS_AJDUK += '-DDUK_OPT_HEAPPTR_DEC16(ud,x)=ajsheap_dec16((ud),(x))' CCOPTS_AJDUK += -DDUK_OPT_EXTERNAL_STRINGS -#CCOPTS_AJDUK += '-DDUK_OPT_EXTSTR_INTERN_CHECK(ptr,len)=ajsheap_extstr_check_1((ptr),(len))' -#CCOPTS_AJDUK += '-DDUK_OPT_EXTSTR_FREE(ptr)=ajsheap_extstr_free_1((ptr))' -CCOPTS_AJDUK += '-DDUK_OPT_EXTSTR_INTERN_CHECK(ptr,len)=ajsheap_extstr_check_2((ptr),(len))' -CCOPTS_AJDUK += '-DDUK_OPT_EXTSTR_FREE(ptr)=ajsheap_extstr_free_2((ptr))' -#CCOPTS_AJDUK += '-DDUK_OPT_EXTSTR_INTERN_CHECK(ptr,len)=ajsheap_extstr_check_3((ptr),(len))' -#CCOPTS_AJDUK += '-DDUK_OPT_EXTSTR_FREE(ptr)=ajsheap_extstr_free_3((ptr))' -CCOPTS_AJDUK += '-DDUK_OPT_DECLARE=extern uint8_t *ajsheap_ram; extern duk_uint16_t ajsheap_enc16(void *p); extern void *ajsheap_dec16(duk_uint16_t x); extern const void *ajsheap_extstr_check_1(const void *ptr, duk_size_t len); extern const void *ajsheap_extstr_check_2(const void *ptr, duk_size_t len); extern const void *ajsheap_extstr_check_3(const void *ptr, duk_size_t len); extern void ajsheap_extstr_free_1(const void *ptr); extern void ajsheap_extstr_free_2(const void *ptr); extern void ajsheap_extstr_free_3(const void *ptr);' +#CCOPTS_AJDUK += '-DDUK_OPT_EXTSTR_INTERN_CHECK(ud,ptr,len)=ajsheap_extstr_check_1((ptr),(len))' +#CCOPTS_AJDUK += '-DDUK_OPT_EXTSTR_FREE(ud,ptr)=ajsheap_extstr_free_1((ptr))' +CCOPTS_AJDUK += '-DDUK_OPT_EXTSTR_INTERN_CHECK(ud,ptr,len)=ajsheap_extstr_check_2((ptr),(len))' +CCOPTS_AJDUK += '-DDUK_OPT_EXTSTR_FREE(ud,ptr)=ajsheap_extstr_free_2((ptr))' +#CCOPTS_AJDUK += '-DDUK_OPT_EXTSTR_INTERN_CHECK(ud,ptr,len)=ajsheap_extstr_check_3((ptr),(len))' +#CCOPTS_AJDUK += '-DDUK_OPT_EXTSTR_FREE(ud,ptr)=ajsheap_extstr_free_3((ptr))' +CCOPTS_AJDUK += '-DDUK_OPT_DECLARE=extern uint8_t *ajsheap_ram; extern duk_uint16_t ajsheap_enc16(void *ud, void *p); extern void *ajsheap_dec16(void *ud, duk_uint16_t x); extern const void *ajsheap_extstr_check_1(const void *ptr, duk_size_t len); extern const void *ajsheap_extstr_check_2(const void *ptr, duk_size_t len); extern const void *ajsheap_extstr_check_3(const void *ptr, duk_size_t len); extern void ajsheap_extstr_free_1(const void *ptr); extern void ajsheap_extstr_free_2(const void *ptr); extern void ajsheap_extstr_free_3(const void *ptr);' #CCOPTS_AJDUK += -DDUK_OPT_DEBUG -DDUK_OPT_DPRINT #CCOPTS_AJDUK += -DDUK_OPT_DEBUG -DDUK_OPT_DPRINT -DDUK_OPT_DDPRINT -DDUK_OPT_DDDPRINT diff --git a/examples/cmdline/duk_cmdline.c b/examples/cmdline/duk_cmdline.c index 4387209d..1afde017 100644 --- a/examples/cmdline/duk_cmdline.c +++ b/examples/cmdline/duk_cmdline.c @@ -551,8 +551,8 @@ int main(int argc, char *argv[]) { ctx = duk_create_heap(AJS_Alloc, AJS_Realloc, AJS_Free, - (void *) &ctx, /* alloc_udata */ - NULL); /* fatal_handler */ + (void *) 0xdeadbeef, /* heap_udata: ignored by AjsHeap, use as marker */ + NULL); /* fatal_handler */ #else fprintf(stderr, "Warning: option --alloc-ajsheap ignored, no ajsheap allocator support\n"); fflush(stderr); diff --git a/examples/cmdline/duk_cmdline_ajduk.c b/examples/cmdline/duk_cmdline_ajduk.c index d53cdf48..40e9499c 100644 --- a/examples/cmdline/duk_cmdline_ajduk.c +++ b/examples/cmdline/duk_cmdline_ajduk.c @@ -145,10 +145,31 @@ void ajsheap_register(duk_context *ctx) { * which is reserved for NULL pointers. */ -duk_uint16_t ajsheap_enc16(void *p) { +duk_uint16_t ajsheap_enc16(void *ud, void *p) { duk_uint32_t ret; char *base = (char *) ajsheap_ram - 4; + /* Userdata is not needed in this case but would be useful if heap + * pointer compression were used for multiple heaps. The userdata + * allows the callback to distinguish between heaps and their base + * pointers. + * + * If not needed, the userdata can be left out during compilation + * by simply ignoring the userdata argument of the pointer encode + * and decode macros. It is kept here so that any bugs in actually + * providing the value inside Duktape are revealed during compilation. + */ + (void) ud; +#if 1 + /* Ensure that we always get the heap_udata given in heap creation. + * (Useful for Duktape development, not needed for user programs.) + */ + if (ud != (void *) 0xdeadbeef) { + fprintf(stderr, "invalid udata for ajsheap_enc16: %p\n", ud); + fflush(stderr); + } +#endif + if (p == NULL) { ret = 0; } else { @@ -164,10 +185,20 @@ duk_uint16_t ajsheap_enc16(void *p) { } return (duk_uint16_t) ret; } -void *ajsheap_dec16(duk_uint16_t x) { +void *ajsheap_dec16(void *ud, duk_uint16_t x) { void *ret; char *base = (char *) ajsheap_ram - 4; + /* See userdata discussion in ajsheap_enc16(). */ + (void) ud; +#if 1 + /* Ensure that we always get the heap_udata given in heap creation. */ + if (ud != (void *) 0xdeadbeef) { + fprintf(stderr, "invalid udata for ajsheap_dec16: %p\n", ud); + fflush(stderr); + } +#endif + if (x == 0) { ret = NULL; } else {