choosing the appropriate alloc strategies

This commit is contained in:
psyc://psyced.org/~lynX 2011-04-26 22:00:45 +02:00
parent 6342e69416
commit aaece680c7
2 changed files with 26 additions and 21 deletions

View File

@ -50,13 +50,13 @@ f_psyc_render(svalue_t *sp) {
/* NOTREACHED */ /* NOTREACHED */
return sp; return sp;
} }
if (v->item[PSYC_METHOD]->type != T_STRING) { if (v->item[PSYC_METHOD].type != T_STRING) {
errorf("Wrong type for PSYC_METHOD element in PSYC packet.\n", i); errorf("Wrong type for PSYC_METHOD element in PSYC packet.\n");
/* NOTREACHED */ /* NOTREACHED */
return sp; return sp;
} }
if (v->item[PSYC_BODY]->type != T_STRING) { if (v->item[PSYC_BODY].type != T_STRING) {
errorf("Wrong type for PSYC_BODY element in PSYC packet.\n", i); errorf("Wrong type for PSYC_BODY element in PSYC packet.\n");
/* NOTREACHED */ /* NOTREACHED */
return sp; return sp;
} }
@ -68,11 +68,13 @@ f_psyc_render(svalue_t *sp) {
PSYC_PACKET_CHECK_LENGTH); PSYC_PACKET_CHECK_LENGTH);
printf("rendering... packet.length = %d\n", packet.length); printf("rendering... packet.length = %d\n", packet.length);
// alloc_mstring creates an *untabled* string suitable for tmp data
memsafe(out = alloc_mstring(packet.length), packet.length, "f_psyc_render"); memsafe(out = alloc_mstring(packet.length), packet.length, "f_psyc_render");
psyc_render(&packet, get_txt(out), packet.length); psyc_render(&packet, get_txt(out), packet.length);
free_svalue(sp); free_svalue(sp);
put_string(sp, out); put_string(sp, out);
// stack should take care of freeing the string after use
return sp; return sp;
} /* f_psyc_render */ } /* f_psyc_render */
@ -121,25 +123,30 @@ f_psyc_parse (svalue_t *sp) {
puts("_failure_unsupported_state"); puts("_failure_unsupported_state");
continue; continue;
} }
// not sure if the following code is good.. TODO
sv = pxalloc( sizeof( svalue_t ) ); sv = pxalloc( sizeof( svalue_t ) );
put_string(sv, new_n_mstring(name.ptr, name.length)); // new_n_tabled fetches a reference of a probably existing
// shared string
put_string(sv, new_n_tabled(name.ptr, name.length));
sv = get_map_lvalue(v->item[PSYC_ROUTING].u.map, sv); sv = get_map_lvalue(v->item[PSYC_ROUTING].u.map, sv);
// should support int* when necessary.. TODO // strings are capable of containing 0 so we can do this
put_string(sv, new_n_mstring(value.ptr, value.length)); // for binary data too. let's use a tabled string even
// for values of routing variables as they repeat a lot
put_string(sv, new_n_tabled(value.ptr, value.length));
break; break;
case PSYC_PARSE_ENTITY: case PSYC_PARSE_ENTITY:
if (oper != ':') { if (oper != ':') {
puts("_failure_unsupported_state"); puts("_failure_unsupported_state");
continue; continue;
} }
// not sure if the following code is good.. TODO
sv = pxalloc( sizeof( svalue_t ) ); sv = pxalloc( sizeof( svalue_t ) );
put_string(sv, new_n_mstring(name.ptr, name.length)); put_string(sv, new_n_tabled(name.ptr, name.length));
sv = get_map_lvalue(v->item[PSYC_ENTITY].u.map, sv); sv = get_map_lvalue(v->item[PSYC_ENTITY].u.map, sv);
// should support int* when necessary.. TODO // is it good to put entity variable values into the
put_string(sv, new_n_mstring(value.ptr, value.length)); // shared string table? probably yes.. but it's a guess
//t_string(sv, new_n_mstring(value.ptr, value.length));
put_string(sv, new_n_tabled(value.ptr, value.length));
// list parsing not supported yet.. TODO
if (memcmp(name.ptr, "_list", 5) == 0) if (memcmp(name.ptr, "_list", 5) == 0)
{ {
write(1, ">>> LIST START\n", 15); write(1, ">>> LIST START\n", 15);
@ -171,15 +178,13 @@ f_psyc_parse (svalue_t *sp) {
break; break;
case PSYC_PARSE_BODY: case PSYC_PARSE_BODY:
if (str) errorf("Got two PSYC methods in the same packet!?\n"); if (str) errorf("Got two PSYC methods in the same packet!?\n");
// not sure if new_n_mstring is the right thing here.. TODO // new_n_tabled gets the shared string for the method
str = new_n_mstring(name.ptr, name.length); put_string(&v->item[PSYC_METHOD],
// make_tabled gets the shared string for the method new_n_tabled(name.ptr, name.length));
put_string(&v->item[PSYC_METHOD], make_tabled(str));
// not sure if new_n_mstring is the right thing here.. TODO // allocate an untabled string for the packet body
// should support int* when necessary.. TODO put_string(&v->item[PSYC_BODY],
str = new_n_mstring(value.ptr, value.length); new_n_mstring(value.ptr, value.length));
put_string(&v->item[PSYC_BODY], str);
break; break;
case PSYC_PARSE_COMPLETE: case PSYC_PARSE_COMPLETE:

View File

@ -17,7 +17,7 @@ version_longtype="stable"
# A timestamp, to be used by bumpversion and other scripts. # A timestamp, to be used by bumpversion and other scripts.
# It can be used, for example, to 'touch' this file on every build, thus # It can be used, for example, to 'touch' this file on every build, thus
# forcing revision control systems to add it on every checkin automatically. # forcing revision control systems to add it on every checkin automatically.
version_stamp="Tue Apr 26 09:46:14 CEST 2011" version_stamp="Tue Apr 26 22:00:17 CEST 2011"
# Okay, LDMUD is using 3.x.x so to avoid conflicts let's just use 4.x.x # Okay, LDMUD is using 3.x.x so to avoid conflicts let's just use 4.x.x
version_major=4 version_major=4