/* *converts the variable to string-variable */ void v_tostr(var_t *arg) { if (arg->type != V_STR) { char *tmp; int l; tmp = tmp_alloc(64); switch (arg->type) { case V_UDS: break; case V_PTR: //ltostr(arg->v.ap.p, tmp); break; case V_INT: //ltostr(arg->v.i, tmp); break; case V_NUM: //ftostr(arg->v.n, tmp); break; default: //err_varisarray(); tmp_free(tmp); return; } l = strlen(tmp) + 1; arg->type = V_STR; arg->v.p.ptr = tmp_alloc(l); arg->v.p.size = l; strcpy(arg->v.p.ptr, tmp); tmp_free(tmp); } }
void * tmp_realloc(void * ptr, size_t size) { if (ptr == NULL) return tmp_alloc(size); assert(is_dispatch_thread()); assert(tmp_gc_posted); #if ENABLE_FastMemAlloc { void * p; size_t m = *((size_t *)ptr - 1); if (m >= size) return ptr; if ((char *)ptr >= tmp_pool && (char *)ptr <= tmp_pool + tmp_pool_max) { size_t pos = tmp_pool_pos - m; if (ptr == tmp_pool + pos && pos + size <= tmp_pool_max) { tmp_pool_pos = pos + size; *((size_t *)ptr - 1) = size; return ptr; } } p = tmp_alloc(size); if (m > size) m = size; return memcpy(p, ptr, m); } #else { LINK * l = (LINK *)ptr - 1; list_remove(l); l = (LINK *)loc_realloc(l, sizeof(LINK) + size); list_add_last(l, &tmp_alloc_list); return l + 1; } #endif }
/** * Using VMT's with keys (like environment-variables) * * sets a variable * * if varvalue == NULL, the variable will be deleted */ int dbt_setvar(dbt_t fh, const char *varname, const char *varvalue) { char *buf, *newrec = NULL; int i, idx; dbt_var_t nd, new_nd; if (varvalue) { // create the new header new_nd.sign = dbt_var_sign; new_nd.var_len = strlen(varname) + 1; new_nd.val_len = strlen(varvalue) + 1; new_nd.node_len = sizeof(new_nd) + new_nd.var_len + new_nd.val_len; // create record newrec = tmp_alloc(new_nd.node_len); memcpy(newrec, &new_nd, sizeof(new_nd)); memcpy(newrec + sizeof(new_nd), varname, new_nd.var_len); memcpy(newrec + sizeof(new_nd) + new_nd.var_len, varvalue, new_nd.val_len); } // default index (new record) idx = dbt_count(fh); // find if already exists for (i = 0; i < dbt_count(fh); i++) { char *nd_var, *nd_val; // load the record dbt_read(fh, i, &nd, sizeof(nd)); if (nd.sign == dbt_var_sign) { buf = tmp_alloc(nd.node_len); dbt_read(fh, i, buf, nd.node_len); nd_var = buf + sizeof(nd); nd_val = buf + sizeof(nd) + nd.var_len; // check varname if (strcmp(nd_var, varname) == 0) { idx = i; tmp_free(buf); break; } tmp_free(buf); } } if (varvalue) { // store the record dbt_write(fh, idx, newrec, new_nd.node_len); tmp_free(newrec); } else // delete the record dbt_remove(fh, idx); return 0; }
/* * assign (dest = src) */ void v_set(var_t *dest, const var_t *src) { int i; var_t *dest_vp, *src_vp; if (src->type == V_UDS) { uds_set(dest, (const var_p_t) src); return; } else if (dest->type == V_UDS) { // lvalue struct assigned to non-struct rvalue uds_clear(dest); return; } else if (src->type == V_HASH) { hash_set(dest, (const var_p_t) src); return; } else if (dest->type == V_HASH) { // lvalue struct assigned to non-struct rvalue hash_clear(dest); return; } v_free(dest); *dest = *src; dest->const_flag = 0; switch (src->type) { case V_STR: dest->v.p.ptr = (byte *) tmp_alloc(strlen((char *)src->v.p.ptr) + 1); strcpy((char *) dest->v.p.ptr, (char *) src->v.p.ptr); break; case V_ARRAY: if (src->v.a.size) { dest->v.a.ptr = tmp_alloc(src->v.a.size * sizeof(var_t)); // copy each element for (i = 0; i < src->v.a.size; i++) { src_vp = (var_t *) (src->v.a.ptr + (sizeof(var_t) * i)); dest_vp = (var_t *) (dest->v.a.ptr + (sizeof(var_t) * i)); v_init(dest_vp); v_set(dest_vp, src_vp); } } else { dest->v.a.size = 0; dest->v.a.ptr = NULL; dest->v.a.ubound[0] = dest->v.a.lbound[0] = opt_base; dest->v.a.maxdim = 1; } break; case V_PTR: dest->v.ap = src->v.ap; dest->type = src->type; break; } }
/* * set the value of 'var' to string */ void v_setstrf(var_t *var, const char *fmt, ...) { char *buf; va_list ap; va_start(ap, fmt); #if defined(OS_LIMITED) buf = tmp_alloc(1024); #else buf = tmp_alloc(0x10000); #endif vsnprintf(buf, 1024, fmt, ap); v_setstr(var, buf); tmp_free(buf); va_end(ap); }
char *Controller::readSource(const char *fileName) { char *buffer = NULL; bool networkFile = strstr(fileName, "://"); const char *delim = strchr(fileName, '?'); int len = strlen(fileName); int endIndex = delim ? (delim - fileName) : len; if (delim && !networkFile) { strcpy(opt_command, delim + 1); } _mainBas = false; trace("readSource %s %d %s", fileName, endIndex, opt_command); if (networkFile) { buffer = readConnection(fileName); } else if (strncasecmp("main.bas", fileName, endIndex) == 0) { // load as resource int len = maGetDataSize(MAIN_BAS); buffer = (char *)tmp_alloc(len + 1); maReadData(MAIN_BAS, buffer, 0, len); buffer[len] = '\0'; _mainBas = true; } else { // load from file system MAHandle handle = maFileOpen(fileName, MA_ACCESS_READ); if (maFileExists(handle)) { int len = maFileSize(handle); buffer = (char *)tmp_alloc(len + 1); maFileRead(handle, buffer, len); buffer[len] = '\0'; } maFileClose(handle); } if (buffer == NULL) { buffer = (char *)tmp_alloc(strlen(ERROR_BAS) + 1); strcpy(buffer, ERROR_BAS); } delete [] _programSrc; len = strlen(buffer); _programSrc = new char[len + 1]; strncpy(_programSrc, buffer, len); _programSrc[len] = 0; logPrint("Opened: %s %d bytes\n", fileName, len); return buffer; }
/* * creates and returns a new variable */ var_t *v_new() { var_t *ptr; ptr = (var_t *) tmp_alloc(sizeof(var_t)); v_init(ptr); return ptr; }
/** * Using VMT's with keys (like environment-variables) * * gets a variable's value * if variable not found, returns NULL otherwise returns a newly created string with the value */ char *dbt_getvar(dbt_t fh, const char *varname) { char *buf, *retval = NULL; int i; dbt_var_t nd; // find if already exists for (i = 0; i < dbt_count(fh); i++) { char *nd_var, *nd_val; // load the record dbt_read(fh, i, &nd, sizeof(nd)); if (nd.sign == dbt_var_sign) { buf = tmp_alloc(nd.node_len); dbt_read(fh, i, buf, nd.node_len); nd_var = buf + sizeof(nd); nd_val = buf + sizeof(nd) + nd.var_len; // check varname if (strcmp(nd_var, varname) == 0) { retval = tmp_strdup(nd_val); tmp_free(buf); break; } tmp_free(buf); } } return retval; }
/** * allocates additional space on database for 'recs' records of 'recsize' size */ int dbt_file_append(dbt_t t, int recs, int recsize) { vmt_rec_t rec; int i; int n; rec.ver = 1; rec.size = recsize; // create empty records if (vmt[t].count + recs >= vmt[t].size) { int newsize; char *data; data = tmp_alloc(recsize); memset(data, 0, recsize); newsize = vmt[t].size + recs; for (i = vmt[t].size; i < newsize; i++) { lseek(vmt[t].i_handle, sizeof(vmt_rec_t) * i + sizeof(vmt_t), SEEK_SET); rec.offset = lseek(vmt[t].f_handle, 0, SEEK_END); n = write(vmt[t].i_handle, &rec, sizeof(vmt_rec_t)); n = write(vmt[t].f_handle, data, recsize); } tmp_free(data); vmt[t].size = newsize; } vmt[t].count++; // dbt_file_write_header(t); return vmt[t].count - 1; }
/* * Set the environment variable "name" to the value "value". If the variable * exists already, override it or just skip. */ static void envp_add(char *** envp, const char * name, const char * value, int env_override) { int i = 0; size_t len = strlen(name); char ** env = *envp; assert(name); assert(value); if (env == NULL) { env = *envp = (char **)tmp_alloc_zero(sizeof(char *) * 2); } else { for (i = 0; env[i]; i++) { if (strncmp(env[i], name, len) == 0 && env[i][len] == '=') break; } if (env[i]) { /* override */ if (!env_override) return; } else { /* new variable */ env = *envp = (char **)tmp_realloc(env, sizeof(char *) * (i + 2)); env[i + 1] = NULL; } } len += strlen(value) + 2; env[i] = (char *)tmp_alloc(len); snprintf(env[i], len, "%s=%s", name, value); }
static void read_field(Context * ctx, const Symbol * sym, ContextAddress base, ContextAddress * value) { LocationInfo * loc_info = NULL; LocationExpressionState * state = NULL; uint64_t args[1]; void * buf = NULL; size_t size = 0; size_t i; args[0] = base; if (get_location_info(sym, &loc_info) < 0) exception(errno); if (loc_info->args_cnt != 1) str_exception(ERR_OTHER, "Wrong object kind"); state = evaluate_location_expression(ctx, NULL, loc_info->value_cmds.cmds, loc_info->value_cmds.cnt, args, 1); if (state->pieces_cnt > 0) { read_location_pieces(state->ctx, state->stack_frame, state->pieces, state->pieces_cnt, loc_info->big_endian, &buf, &size); } else { ContextAddress sym_size = 0; if (state->stk_pos != 1) str_exception(ERR_OTHER, "Invalid location expression"); if (get_symbol_size(sym, &sym_size) < 0) exception(errno); size = (size_t)sym_size; buf = tmp_alloc(size); if (context_read_mem(state->ctx, (ContextAddress)state->stk[0], buf, size) < 0) exception(errno); } *value = 0; for (i = 0; i < size && i < sizeof(ContextAddress); i++) { *value = *value << 8; *value |= ((uint8_t *)buf)[loc_info->big_endian ? i : size - i - 1]; } }
void v_setstr(var_t *var, const char *string) { v_free(var); var->type = V_STR; var->v.p.size = strlen(string) + 1; var->v.p.ptr = tmp_alloc(var->v.p.size); strcpy(var->v.p.ptr, string); }
static void address_to_line_cb(CodeArea * area, void * args) { CodeArea ** p = (CodeArea **)args; if (*p == NULL || (*p)->start_address < area->start_address) { *p = (CodeArea *)tmp_alloc(sizeof(CodeArea)); **p = *area; } }
/* * set an empty string */ void v_zerostr(var_t *r) { v_free(r); r->type = V_STR; r->v.p.ptr = tmp_alloc(1); r->v.p.ptr[0] = '\0'; r->v.p.size = 1; }
/* * return a full copy of the 'source' */ var_t *v_clone(const var_t *source) { var_t *vnew; vnew = (var_t *) tmp_alloc(sizeof(var_t)); v_init(vnew); v_set(vnew, source); return vnew; }
char * tmp_strdup2(const char * s1, const char * s2) { size_t l1 = strlen(s1); size_t l2 = strlen(s2); char * rval = (char *)tmp_alloc(l1 + l2 + 1); memcpy(rval, s1, l1); memcpy(rval + l1, s2, l2 + 1); return rval; }
/* * setup a string variable */ void v_createstr(var_t *v, const char *src) { int l; l = strlen(src) + 1; v->type = V_STR; v->v.p.ptr = tmp_alloc(l); v->v.p.size = l; strcpy(v->v.p.ptr, src); }
void v_setstrn(var_t *var, const char *string, int len) { if (var->type != V_STR || strncmp(string, var->v.p.ptr, len) != 0) { v_free(var); var->type = V_STR; var->v.p.size = len + 1; var->v.p.ptr = tmp_alloc(var->v.p.size); strncpy(var->v.p.ptr, string, len); var->v.p.ptr[len] = 0; } }
/* *set the value of 'var' to string */ void v_setstrf(var_t *var, const char *fmt, ...) { char *buf; va_list ap; va_start(ap, fmt); buf = tmp_alloc(1024); vsnprintf(buf, 1024, fmt, ap); v_setstr(var, buf); tmp_free(buf); va_end(ap); }
/* * add format node */ void fmt_addfmt(const char *fmt, int type) { fmt_node_t *node; node = &fmt_stack[fmt_count]; fmt_count++; if (fmt_count >= MAX_FMT_N) { panic("Maximum format-node reached"); } node->fmt = tmp_alloc(strlen(fmt) + 1); strcpy(node->fmt, fmt); node->type = type; }
/* * converts the variable to string-variable */ void v_tostr(var_t *arg) { if (arg->type != V_STR) { char *tmp; int l; tmp = tmp_alloc(64); switch (arg->type) { case V_UDS: uds_to_str(arg, tmp, 64); uds_free(arg); break; case V_HASH: hash_to_str(arg, tmp, 64); hash_free_var(arg); break; case V_PTR: ltostr(arg->v.ap.p, tmp); break; case V_INT: ltostr(arg->v.i, tmp); break; case V_NUM: ftostr(arg->v.n, tmp); break; default: err_varisarray(); tmp_free(tmp); return; } l = strlen(tmp) + 1; arg->type = V_STR; arg->v.p.ptr = tmp_alloc(l); arg->v.p.size = l; strcpy(arg->v.p.ptr, tmp); tmp_free(tmp); } }
void v_toarray1(var_t *v, dword r) #endif { var_t *e; #if defined(OS_ADDR16) word i; #else dword i; #endif v_free(v); v->type = V_ARRAY; if (r > 0) { // create data v->v.a.size = r; #if defined(OS_ADDR32) v->v.a.ptr = tmp_alloc(sizeof(var_t) * (v->v.a.size + ARR_ALLOC)); #else v->v.a.ptr = tmp_alloc(sizeof(var_t) * v->v.a.size); #endif for (i = 0; i < r; i++) { e = (var_t *) (v->v.a.ptr + (sizeof(var_t) * i)); v_init(e); } // array info v->v.a.maxdim = 1; v->v.a.lbound[0] = opt_base; v->v.a.ubound[0] = opt_base + (r - 1); } else { v->v.a.size = 0; v->v.a.ptr = NULL; v->v.a.lbound[0] = v->v.a.ubound[0] = opt_base; v->v.a.maxdim = 1; } }
/* *create RxC array */ void v_tomatrix(var_t *v, int r, int c) { var_t *e; int i; v_free(v); v->type = V_ARRAY; // create data v->v.a.size = r *c; v->v.a.ptr = tmp_alloc(sizeof(var_t) *v->v.a.size); for (i = 0; i < r *c; i++) { e = (var_t *) (v->v.a.ptr + (sizeof(var_t) *i)); v_init(e); } // array info v->v.a.lbound[0] = v->v.a.lbound[1] = opt_base; v->v.a.ubound[0] = opt_base + (r - 1); v->v.a.ubound[1] = opt_base + (c - 1); v->v.a.maxdim = 2; }
/** * removes deleted chuncks (defrag) */ void dbt_file_pack(dbt_t t) { vmt_rec_t rec; int i, idx_offset, new_h, new_offset, n; char *data; char old_db_name[OS_PATHNAME_SIZE]; char new_db_name[OS_PATHNAME_SIZE]; sprintf(old_db_name, "%s.dbt", vmt[t].base); sprintf(new_db_name, "%s-new.dbt", vmt[t].base); new_h = open(new_db_name, O_BINARY | O_RDWR); for (i = 0; i < vmt[t].size; i++) { // read original record data idx_offset = i * sizeof(vmt_rec_t) + sizeof(vmt_t); lseek(vmt[t].i_handle, idx_offset, SEEK_SET); // read data data = tmp_alloc(rec.size); lseek(vmt[t].f_handle, rec.offset, rec.size); n = read(vmt[t].f_handle, data, rec.size); // copy record new_offset = lseek(new_h, 0, SEEK_END); rec.offset = new_offset; n = write(new_h, data, rec.size); tmp_free(data); // update index n = write(vmt[t].i_handle, &rec, sizeof(vmt_rec_t)); } // swap databases and reopen close(vmt[t].f_handle); remove(old_db_name); close(new_h); rename(new_db_name, old_db_name); vmt[t].f_handle = open(old_db_name, O_BINARY | O_RDWR); }
/* * format: format a number * * symbols: * # = digit or space * 0 = digit or zero * ^ = exponential digit/format * . = decimal point * , = thousands * - = minus for negative * + = sign of number */ void format_num(char *dest, const char *fmt_cnst, var_num_t x) { char *p, *fmt; char left[64], right[64]; char lbuf[64], rbuf[64]; int dp = 0, lc = 0, sign = 0; int rsz, lsz; // backup of format fmt = tmp_alloc(strlen(fmt_cnst) + 1); strcpy(fmt, fmt_cnst); // check sign if (strchr(fmt, '-') || strchr(fmt, '+')) { sign = 1; if (x < 0.0) { sign = -1; x = -x; } } if (strchr(fmt_cnst, '^')) { // // E format // lc = fmt_cdig(fmt); if (lc < 4) { fmt_omap(dest, fmt); tmp_free(fmt); return; } // convert expfta(x, dest); // format p = strchr(dest, 'E'); if (p) { *p = '\0'; strcpy(left, dest); strcpy(right, p + 1); lsz = strlen(left); rsz = strlen(right) + 1; if (lc < rsz + 1) { fmt_omap(dest, fmt); tmp_free(fmt); return; } if (lc < lsz + rsz + 1) left[lc - rsz] = '\0'; strcpy(lbuf, left); strcat(lbuf, "E"); strcat(lbuf, right); fmt_nmap(-1, dest, fmt, lbuf); } else { strcpy(left, dest); fmt_nmap(-1, dest, fmt, left); } } else { // // normal format // // rounding p = strchr(fmt, '.'); if (p) { x = fround(x, fmt_cdig(p + 1)); } else { x = fround(x, 0); } // convert bestfta(x, dest); if (strchr(dest, 'E')) { fmt_omap(dest, fmt); tmp_free(fmt); return; } // left & right parts left[0] = right[0] = '\0'; p = strchr(dest, '.'); if (p) { *p = '\0'; strcpy(right, p + 1); } strcpy(left, dest); // map format rbuf[0] = lbuf[0] = '\0'; p = strchr(fmt, '.'); if (p) { dp = 1; *p = '\0'; fmt_nmap(1, rbuf, p + 1, right); } lc = fmt_cdig(fmt); if (lc < strlen(left)) { fmt_omap(dest, fmt_cnst); tmp_free(fmt); return; } fmt_nmap(-1, lbuf, fmt, left); strcpy(dest, lbuf); if (dp) { strcat(dest, "."); strcat(dest, rbuf); } } // sign in format if (sign) { // 24/6 Snoopy42 modifications char *e; e = strchr(dest, 'E'); if (e) { // special treatment for E format p = strchr(dest, '+'); if (p && p < e) { // the sign bust be before the E *p = (sign > 0) ? '+' : '-'; } p = strchr(dest, '-'); if (p && p < e) { *p = (sign > 0) ? ' ' : '-'; } } else { // no E format p = strchr(dest, '+'); if (p) { *p = (sign > 0) ? '+' : '-'; } p = strchr(dest, '-'); if (p) { *p = (sign > 0) ? ' ' : '-'; } } } // cleanup tmp_free(fmt); }
void InitEMalloc() /* initialize storage allocator */ { int i, j, p; #ifdef ELINUX pagesize = getpagesize(); #else eu_dll_exists = (Argc == 0); // Argc is 0 only in Euphoria .dll pool[0].size = 8; pool[0].first = NULL; p = RESOLUTION * 2; for (i = 1; i < NUMBER_OF_SIZES; i = i + 2) { pool[i].size = p; pool[i].first = NULL; if (i+1 < NUMBER_OF_SIZES) { pool[i+1].size = 3 * p / 2; pool[i+1].first = NULL; p = p * 2; } } j = 0; for (i = 0; i <= MAX_CACHED_SIZE / RESOLUTION; i++) { if (pool[j].size < i * RESOLUTION) j++; pool_map[i] = &pool[j]; } #endif call_back_arg1 = tmp_alloc(); call_back_arg1->mode = M_TEMP; call_back_arg1->obj = NOVALUE; call_back_arg2 = tmp_alloc(); call_back_arg2->mode = M_TEMP; call_back_arg2->obj = NOVALUE; call_back_arg3 = tmp_alloc(); call_back_arg3->mode = M_TEMP; call_back_arg3->obj = NOVALUE; call_back_arg4 = tmp_alloc(); call_back_arg4->mode = M_TEMP; call_back_arg4->obj = NOVALUE; call_back_arg5 = tmp_alloc(); call_back_arg5->mode = M_TEMP; call_back_arg5->obj = NOVALUE; call_back_arg6 = tmp_alloc(); call_back_arg6->mode = M_TEMP; call_back_arg6->obj = NOVALUE; call_back_arg7 = tmp_alloc(); call_back_arg7->mode = M_TEMP; call_back_arg7->obj = NOVALUE; call_back_arg8 = tmp_alloc(); call_back_arg8->mode = M_TEMP; call_back_arg8->obj = NOVALUE; call_back_arg9 = tmp_alloc(); call_back_arg9->mode = M_TEMP; call_back_arg9->obj = NOVALUE; call_back_result = tmp_alloc(); call_back_result->mode = M_TEMP; call_back_result->obj = NOVALUE; }
/* * The final format - create the format-list * (that list it will be used later by fmt_printN and fmt_printS) * * '_' the next character is not belongs to format (simple string) */ void build_format(const char *fmt_cnst) { char *fmt; char *p; int nc; #if defined(OS_LIMITED) char buf[128], *b; #else char buf[1024], *b; #endif free_format(); // backup of format fmt = tmp_alloc(strlen(fmt_cnst) + 1); strcpy(fmt, fmt_cnst); p = fmt; b = buf; nc = 0; while (*p) { switch (*p) { case '_': // store prev. buf *b = '\0'; if (strlen(buf)) { fmt_addfmt(buf, 0); } // store the new buf[0] = *(p + 1); buf[1] = '\0'; fmt_addfmt(buf, 0); b = buf; p++; break; case '-': case '+': case '^': case '0': case '#': // store prev. buf *b = '\0'; if (strlen(buf)) { fmt_addfmt(buf, 0); } // get num-fmt p = fmt_getnumfmt(buf, p); fmt_addfmt(buf, 1); b = buf; nc = 1; break; case '&': case '!': case '\\': // store prev. buf *b = '\0'; if (strlen(buf)) { fmt_addfmt(buf, 0); } // get str-fmt p = fmt_getstrfmt(buf, p); fmt_addfmt(buf, 2); b = buf; nc = 1; break; default: *b++ = *p; } if (*p) { if (nc) { // do not advance nc = 0; } else { p++; } } } // store prev. buf *b = '\0'; if (strlen(buf)) { fmt_addfmt(buf, 0); } // cleanup tmp_free(fmt); }
// returns the contents of the given url char *Controller::readConnection(const char *url) { char *result = NULL; logEntered(); _output->print("\033[ LLoading..."); MAHandle conn = maConnect(url); if (conn < 1) { logPrint("Failed connecting to %s\n", url); } else { _runMode = conn_state; logPrint("Connecting to %s\n", url); bool connected = false; byte buffer[1024]; int length = 0; int size = 0; int now = maGetMilliSecondCount(); MAEvent event; // pause until connected while (_runMode == conn_state) { event = processEvents(EVENT_WAIT_INFINITE, EVENT_TYPE_CONN); if (event.type == EVENT_TYPE_CONN) { switch (event.conn.opType) { case CONNOP_CONNECT: // connection established if (!connected) { connected = (event.conn.result > 0); if (connected) { memset(buffer, 0, sizeof(buffer)); maConnRead(conn, buffer, sizeof(buffer)); } else { logPrint("Connection error\n"); _runMode = init_state; } } break; case CONNOP_READ: // connRead completed if (event.conn.result > 0) { size = event.conn.result; if (result == NULL) { result = (char *)tmp_alloc(size + 1); memcpy(result, buffer, size); length = size; } else { result = (char *)tmp_realloc(result, length + size + 1); memcpy(result + length, buffer, size); length += size; } result[length] = 0; memset(buffer, 0, sizeof(buffer)); maConnRead(conn, buffer, sizeof(buffer)); } else { // no more data _runMode = init_state; } break; default: logPrint("Connection error\n"); _runMode = init_state; } } } logPrint("Loaded in %d msecs\n", maGetMilliSecondCount() - now); } maConnClose(conn); return result; }
static void safe_memory_fill(void * parm) { MemoryCommandArgs * args = (MemoryCommandArgs *)parm; Channel * c = args->c; Context * ctx = args->ctx; if (!is_channel_closed(c)) { Trap trap; if (set_trap(&trap)) { InputStream * inp = &c->inp; OutputStream * out = &c->out; char * token = args->token; ContextAddress addr0 = args->addr; ContextAddress addr = args->addr; unsigned long size = args->size; MemoryErrorInfo err_info; MemoryFillBuffer buf; char * tmp = NULL; int err = 0; memset(&err_info, 0, sizeof(err_info)); if (ctx->exiting || ctx->exited) err = ERR_ALREADY_EXITED; memset(&buf, 0, sizeof(buf)); buf.buf = (char *)tmp_alloc(buf.max = BUF_SIZE); if (err) json_skip_object(inp); else json_read_array(inp, read_memory_fill_array_cb, &buf); json_test_char(inp, MARKER_EOA); json_test_char(inp, MARKER_EOM); while (err == 0 && buf.pos < size && buf.pos <= buf.max / 2) { if (buf.pos == 0) { buf.buf[buf.pos++] = 0; } else { memcpy(buf.buf + buf.pos, buf.buf, buf.pos); buf.pos *= 2; } } while (err == 0 && addr < addr0 + size) { /* Note: context_write_mem() modifies buffer contents */ unsigned wr = (unsigned)(addr0 + size - addr); if (tmp == NULL) tmp = (char *)tmp_alloc(buf.pos); if (wr > buf.pos) wr = buf.pos; /* TODO: word size, mode */ memcpy(tmp, buf.buf, wr); if (context_write_mem(ctx, addr, tmp, wr) < 0) { err = errno; #if ENABLE_ExtendedMemoryErrorReports context_get_mem_error_info(&err_info); #endif } else { addr += wr; } } send_event_memory_changed(ctx, addr0, size); write_stringz(out, "R"); write_stringz(out, token); write_errno(out, err); if (err == 0) { write_stringz(out, "null"); } else { write_ranges(out, addr0, (int)(addr - addr0), BYTE_CANNOT_WRITE, &err_info); } write_stream(out, MARKER_EOM); clear_trap(&trap); } else { trace(LOG_ALWAYS, "Exception in Memory.fill: %s", errno_to_str(trap.error)); channel_close(c); } } channel_unlock(c); context_unlock(ctx); loc_free(args); }
/* * add two variables * result = a + b */ void v_add(var_t *result, var_t *a, var_t *b) { char tmpsb[64]; if (a->type == V_STR && b->type == V_STR) { result->type = V_STR; result->v.p.ptr = (byte *) tmp_alloc(strlen((char *)a->v.p.ptr) + strlen((char *)b->v.p.ptr) + 1); strcpy((char *) result->v.p.ptr, (char *) a->v.p.ptr); strcat((char *) result->v.p.ptr, (char *) b->v.p.ptr); result->v.p.size = strlen((char *) result->v.p.ptr) + 1; return; } else if (a->type == V_INT && b->type == V_INT) { result->type = V_INT; result->v.i = a->v.i + b->v.i; return; } else if (a->type == V_NUM && b->type == V_NUM) { result->type = V_NUM; result->v.n = a->v.n + b->v.n; return; } else if (a->type == V_NUM && b->type == V_INT) { result->type = V_NUM; result->v.n = a->v.n + b->v.i; return; } else if (a->type == V_INT && b->type == V_NUM) { result->type = V_NUM; result->v.n = a->v.i + b->v.n; return; } else if (a->type == V_STR && (b->type == V_INT || b->type == V_NUM)) { if (is_number((char *) a->v.p.ptr)) { result->type = V_NUM; if (b->type == V_INT) { result->v.n = b->v.i + v_getval(a); } else { result->v.n = b->v.n + v_getval(a); } } else { result->type = V_STR; result->v.p.ptr = (byte *) tmp_alloc(strlen((char *)a->v.p.ptr) + 64); strcpy((char *) result->v.p.ptr, (char *) a->v.p.ptr); if (b->type == V_INT) { ltostr(b->v.i, tmpsb); } else { ftostr(b->v.n, tmpsb); } strcat((char *) result->v.p.ptr, tmpsb); result->v.p.size = strlen((char *) result->v.p.ptr) + 1; } } else if ((a->type == V_INT || a->type == V_NUM) && b->type == V_STR) { if (is_number((char *) b->v.p.ptr)) { result->type = V_NUM; if (a->type == V_INT ) result->v.n = a->v.i + v_getval(b); else result->v.n = a->v.n + v_getval(b); } else { result->type = V_STR; result->v.p.ptr = (byte *) tmp_alloc(strlen((char *)b->v.p.ptr) + 64); if (a->type == V_INT) { ltostr(a->v.i, tmpsb); } else { ftostr(a->v.n, tmpsb); } strcpy((char *) result->v.p.ptr, tmpsb); strcat((char *) result->v.p.ptr, (char *) b->v.p.ptr); result->v.p.size = strlen((char *) result->v.p.ptr) + 1; } } }