*improved gc with an array; *polished array manipulation code.

This commit is contained in:
paladin-t 2015-12-29 10:59:24 +08:00
parent a643df1915
commit 545b96b669
2 changed files with 47 additions and 68 deletions

View File

@ -1,5 +1,6 @@
Dec. 29 2015 Dec. 29 2015
Improved GC with an array Improved GC with an array
Polished array manipulation code
Dec. 28 2015 Dec. 28 2015
Fixed a GC bug Fixed a GC bug

View File

@ -123,6 +123,8 @@ extern "C" {
# define toupper(__c) ((islower(__c)) ? ((__c) - 'a' + 'A') : (__c)) # define toupper(__c) ((islower(__c)) ? ((__c) - 'a' + 'A') : (__c))
#endif /* toupper */ #endif /* toupper */
#define _copy_bytes(__l, __r) do { memcpy((__l), (__r), sizeof(mb_val_bytes_t)); } while(0)
#define _mb_check(__expr, __exit) do { if((__expr) != MB_FUNC_OK) goto __exit; } while(0) #define _mb_check(__expr, __exit) do { if((__expr) != MB_FUNC_OK) goto __exit; } while(0)
#define DON(__o) ((__o) ? ((_object_t*)((__o)->data)) : 0) #define DON(__o) ((__o) ? ((_object_t*)((__o)->data)) : 0)
@ -2710,17 +2712,7 @@ int _calc_expression(mb_interpreter_t* s, _ls_node_t** l, _object_t** val) {
_ls_pushback(garbage, arr_elem); _ls_pushback(garbage, arr_elem);
arr_elem->type = arr_type; arr_elem->type = arr_type;
arr_elem->ref = true; arr_elem->ref = true;
if(arr_type == _DT_INT) { _copy_bytes(arr_elem->data.bytes, arr_val.bytes);
arr_elem->data.integer = arr_val.integer;
} else if(arr_type == _DT_REAL) {
arr_elem->data.float_point = arr_val.float_point;
} else if(arr_type == _DT_STRING) {
arr_elem->data.string = arr_val.string;
} else if(arr_type == _DT_USERTYPE) {
arr_elem->data.usertype = arr_val.usertype;
} else {
mb_assert(0 && "Unsupported.");
}
if(f) { if(f) {
_handle_error_on_obj(s, SE_RN_OPERATOR_EXPECTED, 0, DON(ast), MB_FUNC_ERR, _exit, result); _handle_error_on_obj(s, SE_RN_OPERATOR_EXPECTED, 0, DON(ast), MB_FUNC_ERR, _exit, result);
} }
@ -4834,21 +4826,8 @@ bool_t _get_array_elem(mb_interpreter_t* s, _array_t* arr, unsigned int index, m
val->float_point = *((real_t*)rawptr); val->float_point = *((real_t*)rawptr);
*type = _DT_REAL; *type = _DT_REAL;
#else /* MB_SIMPLE_ARRAY */ #else /* MB_SIMPLE_ARRAY */
if(arr->types[index] == _DT_REAL) { _copy_bytes(val->bytes, *((mb_val_bytes_t*)rawptr));
val->float_point = *((real_t*)rawptr); *type = arr->types[index];
*type = _DT_REAL;
} else if(arr->types[index] == _DT_INT) {
val->integer = *((int_t*)rawptr);
*type = _DT_INT;
} else if(arr->types[index] == _DT_USERTYPE) {
val->usertype = *((void**)rawptr);
*type = _DT_USERTYPE;
} else if(arr->types[index] == _DT_STRING) {
val->string = *((char**)rawptr);
*type = _DT_STRING;
} else {
mb_assert(0 && "Unsupported.");
}
#endif /* MB_SIMPLE_ARRAY */ #endif /* MB_SIMPLE_ARRAY */
} else if(arr->type == _DT_STRING) { } else if(arr->type == _DT_STRING) {
val->string = *((char**)rawptr); val->string = *((char**)rawptr);
@ -4875,38 +4854,46 @@ int _set_array_elem(mb_interpreter_t* s, _ls_node_t* ast, _array_t* arr, unsigne
elemsize = _get_size_of(arr->type); elemsize = _get_size_of(arr->type);
pos = (unsigned int)(elemsize * index); pos = (unsigned int)(elemsize * index);
rawptr = (void*)((intptr_t)arr->raw + pos); rawptr = (void*)((intptr_t)arr->raw + pos);
if(*type == _DT_INT) {
#ifdef MB_SIMPLE_ARRAY #ifdef MB_SIMPLE_ARRAY
switch(*type) {
case _DT_INT:
*((real_t*)rawptr) = (real_t)val->integer; *((real_t*)rawptr) = (real_t)val->integer;
#else /* MB_SIMPLE_ARRAY */
*((int_t*)rawptr) = val->integer; break;
arr->types[index] = _DT_INT; case _DT_STRING: {
#endif /* MB_SIMPLE_ARRAY */ size_t _sl = 0;
} else if(*type == _DT_REAL) { if(arr->type != _DT_STRING) {
*((real_t*)rawptr) = val->float_point; _handle_error_on_obj(s, SE_RN_STRING_EXPECTED, 0, DON(ast), MB_FUNC_ERR, _exit, result);
#ifndef MB_SIMPLE_ARRAY }
arr->types[index] = _DT_REAL; _sl = strlen(val->string);
#endif /* MB_SIMPLE_ARRAY */ *((char**)rawptr) = (char*)mb_malloc(_sl + 1);
} else if(*type == _DT_STRING) { memcpy(*((char**)rawptr), val->string, _sl + 1);
size_t _sl = 0;
#ifndef MB_SIMPLE_ARRAY
arr->types[index] = _DT_STRING;
#else /* MB_SIMPLE_ARRAY */
if(arr->type != _DT_STRING) {
_handle_error_on_obj(s, SE_RN_STRING_EXPECTED, 0, DON(ast), MB_FUNC_ERR, _exit, result);
} }
#endif /* MB_SIMPLE_ARRAY */
_sl = strlen(val->string); break;
*((char**)rawptr) = (char*)mb_malloc(_sl + 1); default:
memcpy(*((char**)rawptr), val->string, _sl + 1); _copy_bytes(*((mb_val_bytes_t*)rawptr), val->bytes);
#ifndef MB_SIMPLE_ARRAY
} else if(*type == _DT_USERTYPE) { break;
*((void**)rawptr) = val->usertype;
arr->types[index] = _DT_USERTYPE;
#endif /* MB_SIMPLE_ARRAY */
} else {
mb_assert(0 && "Unsupported.");
} }
#else /* MB_SIMPLE_ARRAY */
switch(*type) {
case _DT_STRING: {
size_t _sl = 0;
_sl = strlen(val->string);
*((char**)rawptr) = (char*)mb_malloc(_sl + 1);
memcpy(*((char**)rawptr), val->string, _sl + 1);
arr->types[index] = _DT_STRING;
}
break;
default:
_copy_bytes(*((mb_val_bytes_t*)rawptr), val->bytes);
arr->types[index] = *type;
break;
}
#endif /* MB_SIMPLE_ARRAY */
goto _exit; /* Avoid an unreferenced label warning */ goto _exit; /* Avoid an unreferenced label warning */
@ -10067,24 +10054,15 @@ int _core_let(mb_interpreter_t* s, void** l) {
} else if(arr) { } else if(arr) {
mb_value_u _val; mb_value_u _val;
switch(val->type) { switch(val->type) {
case _DT_INT: case _DT_NIL: /* Fall through */
_val.integer = val->data.integer; case _DT_INT: /* Fall through */
case _DT_REAL: /* Fall through */
break; case _DT_STRING: /* Fall through */
case _DT_REAL:
_val.float_point = val->data.float_point;
break;
case _DT_STRING:
_val.string = val->data.string;
break;
case _DT_USERTYPE: case _DT_USERTYPE:
_val.usertype = val->data.usertype; _copy_bytes(_val.bytes, val->data.bytes);
break; break;
default: default:
mb_assert(0 && "Unsupported.");
safe_free(val); safe_free(val);
_handle_error_on_obj(s, SE_RN_NOT_SUPPORTED, 0, TON(l), MB_FUNC_ERR, _exit, result); _handle_error_on_obj(s, SE_RN_NOT_SUPPORTED, 0, TON(l), MB_FUNC_ERR, _exit, result);