[Jamvm-general] [PATCH] Array object modifications
Brought to you by:
rlougher
From: <chr...@gm...> - 2014-10-27 01:29:50
|
From: Christopher Friedt <chr...@gm...> This modification to array objects allows for: 1) the classic case where array data is allocated with the array object, contiguously 2) a new application that allows setting the array data to an arbitrary pointer (e.g. mmapped file) --- src/alloc.c | 41 +++++++++++++++++++++++++++++++++++------ src/interp/engine/interp.c | 17 +++++++++-------- src/jam.h | 12 +++++++++++- 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/src/alloc.c b/src/alloc.c index d12abb6..8b31a69 100644 --- a/src/alloc.c +++ b/src/alloc.c @@ -2114,7 +2114,7 @@ Object *allocObject(Class *class) { } Object *allocArray(Class *class, int size, int el_size) { - Object *ob; + ArrayObject *ob; /* Special check to protect against integer overflow */ if(size > (INT_MAX - sizeof(uintptr_t) - sizeof(Object)) / el_size) { @@ -2122,16 +2122,47 @@ Object *allocArray(Class *class, int size, int el_size) { return NULL; } - ob = gcMalloc(size * el_size + sizeof(uintptr_t) + sizeof(Object)); + ob = gcMalloc(size * el_size + sizeof(ArrayObject)); if(ob != NULL) { ob->class = class; - ARRAY_LEN(ob) = size; + ob->size = size; + if ( size > 0 ) { + ob->data = ob->contig_data; + } TRACE_ALLOC("<ALLOC: allocated %s array object @%p>\n", CLASS_CB(class)->name, ob); } - return ob; + return (Object *)ob; +} + +static char *array_names[] = { "[Z", "[C", "[F", "[D", + "[B", "[S", "[I", "[J", }; + +Object *allocTypeArrayFromClassName(const char *className, int size) { + Object *r = NULL; + int type; + char t; + + if ( NULL == className || strlen(className) != 2 || *className != '[' ) + goto out; + t = *(className+1); + + for(type = 0; type < 8; type++) { + if ( t == array_names[type][1] ) { + break; + } + } + if ( type > 8 ) + goto out; + + type += T_BOOLEAN; + + r = allocTypeArray(type, size); + +out: + return r; } Object *allocObjectArray(Class *element_class, int length) { @@ -2153,8 +2184,6 @@ Object *allocObjectArray(Class *element_class, int length) { } Object *allocTypeArray(int type, int size) { - static char *array_names[] = {"[Z", "[C", "[F", "[D", "[B", - "[S", "[I", "[J"}; static int element_sizes[] = {1, 2, 4, 8, 1, 2, 4, 8}; static Class *array_classes[8]; diff --git a/src/interp/engine/interp.c b/src/interp/engine/interp.c index cfb6d67..f66af80 100644 --- a/src/interp/engine/interp.c +++ b/src/interp/engine/interp.c @@ -658,14 +658,15 @@ uintptr_t *executeJava() { #define ARRAY_LOAD_ARY *--ostack #endif -#define ARRAY_LOAD(TYPE) \ -{ \ - int idx = ARRAY_LOAD_IDX; \ - Object *array = (Object *)ARRAY_LOAD_ARY; \ - \ - NULL_POINTER_CHECK(array); \ - ARRAY_BOUNDS_CHECK(array, idx); \ - PUSH_0(ARRAY_DATA(array, TYPE)[idx], 1); \ +#define ARRAY_LOAD(TYPE) \ +{ \ + int idx = ARRAY_LOAD_IDX; \ + Object *array = (Object *)ARRAY_LOAD_ARY; \ + \ + NULL_POINTER_CHECK(array); \ + NULL_POINTER_CHECK(ARRAY_DATA(array, TYPE)); \ + ARRAY_BOUNDS_CHECK(array, idx); \ + PUSH_0(ARRAY_DATA(array, TYPE)[idx], 1); \ } DEF_OPC_012_2( diff --git a/src/jam.h b/src/jam.h index ce73df6..dbd003f 100644 --- a/src/jam.h +++ b/src/jam.h @@ -24,6 +24,7 @@ #include <limits.h> #include <stdio.h> #include <time.h> +#include <stddef.h> /* Configure options */ #include "config.h" @@ -420,6 +421,14 @@ typedef struct line_no_table_entry { typedef struct object Class; +typedef struct array_object { + uintptr_t lock; + Class *class; + uintptr_t size; + uintptr_t data; + char contig_data[]; +} ArrayObject; + typedef struct object { uintptr_t lock; Class *class; @@ -784,7 +793,7 @@ typedef struct InitArgs { #define INST_DATA(obj, type, offset) *(type*)&((char*)obj)[offset] #define INST_BASE(obj, type) ((type*)(obj+1)) -#define ARRAY_DATA(arrayRef, type) ((type*)(((uintptr_t*)(arrayRef+1))+1)) +#define ARRAY_DATA(arrayRef, type) (*((type**)(((uintptr_t*)(arrayRef+1))+1))) #define ARRAY_LEN(arrayRef) *(uintptr_t*)(arrayRef+1) #define IS_CLASS(object) (object->class && IS_CLASS_CLASS( \ @@ -910,6 +919,7 @@ extern int initialiseGC(InitArgs *args); extern Class *allocClass(); extern Object *allocObject(Class *class); extern Object *allocTypeArray(int type, int size); +extern Object *allocTypeArrayFromClassName(const char *className, int size); extern Object *allocObjectArray(Class *element_class, int size); extern Object *allocArray(Class *class, int size, int el_size); extern Object *allocMultiArray(Class *array_class, int dim, intptr_t *count); -- 1.9.1 |