Use rbufs instead of static buffers.

This commit is contained in:
steve 2003-03-13 04:59:21 +00:00
parent f45fd155c4
commit 9e9326cc6d
4 changed files with 128 additions and 134 deletions

View File

@ -17,7 +17,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
#ident "$Id: vpi_const.cc,v 1.24 2003/03/10 23:37:07 steve Exp $"
#ident "$Id: vpi_const.cc,v 1.25 2003/03/13 04:59:21 steve Exp $"
#endif
# include "vpi_priv.h"
@ -29,8 +29,6 @@
# include <string.h>
# include <assert.h>
static char buf[4096];
static int string_get(int code, vpiHandle ref)
{
struct __vpiStringConst*rfp;
@ -59,80 +57,63 @@ static int string_get(int code, vpiHandle ref)
static void string_value(vpiHandle ref, p_vpi_value vp)
{
int size;
unsigned uint_value;
char *cp;
struct __vpiStringConst*rfp = (struct __vpiStringConst*)ref;
int size = strlen(rfp->value);
char*rbuf = 0;
char*cp;
assert((ref->vpi_type->type_code == vpiConstant)
|| ((ref->vpi_type->type_code == vpiParameter)));
switch (vp->format) {
case vpiObjTypeVal:
case vpiStringVal:
vp->value.str = (char*)rfp->value;
vp->format = vpiStringVal;
rbuf = need_result_buf(size + 1, RBUF_VAL);
strcpy(rbuf, (char*)rfp->value);
vp->value.str = rbuf;
break;
case vpiDecStrVal:
size = strlen(rfp->value);
if (size > 4){
// We only support standard integers. Ignore other bytes...
size = 4;
fprintf(stderr, "Warning (vpi_const.cc): %%d on constant strings only looks "
"at first 4 bytes!\n");
}
rbuf = need_result_buf(size + 1, RBUF_VAL);
uint_value = 0;
for(int i=0; i<size;i ++){
uint_value <<=8;
uint_value += (unsigned char)(rfp->value[i]);
}
sprintf(buf, "%u", uint_value);
sprintf(rbuf, "%u", uint_value);
vp->format = vpiDecStrVal;
vp->value.str = buf;
vp->value.str = rbuf;
break;
case vpiBinStrVal:
size = strlen(rfp->value);
if (size*8 > (int)(sizeof(buf)/sizeof(char))-1 ){
// Avoid overflow of 'buf'
// 4096 should be sufficient for most cases though. ;-)
size = (sizeof(buf)/sizeof(char)-2)/8;
}
cp = buf;
rbuf = need_result_buf(8 * size + 1, RBUF_VAL);
cp = rbuf;
for(int i=0; i<size;i ++){
for(int bit=7;bit>=0; bit--){
*cp++ = "01"[ (rfp->value[i]>>bit)&1 ];
}
}
*cp = 0;
vp->format = vpiBinStrVal;
vp->value.str = buf;
vp->value.str = rbuf;
break;
case vpiHexStrVal:
size = strlen(rfp->value);
if (size*2 > (int)(sizeof(buf)/sizeof(char))-1 ){
// Avoid overflow of 'buf'
// 4096 should be sufficient for most cases though. ;-)
size = (sizeof(buf)/sizeof(char)-2)/2;
}
cp = buf;
rbuf = need_result_buf(2 * size + 1, RBUF_VAL);
cp = rbuf;
for(int i=0; i<size;i++){
for(int nibble=1;nibble>=0; nibble--){
*cp++ = "0123456789abcdef"[ (rfp->value[i]>>(nibble*4))&15 ];
}
}
*cp = 0;
vp->format = vpiHexStrVal;
vp->value.str = buf;
vp->value.str = rbuf;
break;
case vpiOctStrVal:
@ -206,11 +187,14 @@ struct __vpiStringParam : public __vpiStringConst {
static char* string_param_get_str(int code, vpiHandle obj)
{
struct __vpiStringParam*rfp = (struct __vpiStringParam*)obj;
char *rbuf = need_result_buf(strlen(rfp->basename) + 1, RBUF_STR);
assert(obj->vpi_type->type_code == vpiParameter);
switch (code) {
case vpiName:
return const_cast<char*>(rfp->basename);
strcpy(rbuf, rfp->basename);
return rbuf;
default:
return 0;
}
@ -272,7 +256,8 @@ static void binary_vpiStringVal(struct __vpiBinaryConst*rfp, p_vpi_value vp)
unsigned nchar = rfp->nbits / 8;
unsigned tail = rfp->nbits%8;
char*cp = buf;
char*rbuf = need_result_buf(nchar + 1, RBUF_VAL);
char*cp = rbuf;
if (tail > 0) {
char char_val = 0;
@ -309,72 +294,73 @@ static void binary_vpiStringVal(struct __vpiBinaryConst*rfp, p_vpi_value vp)
}
*cp = 0;
vp->format = vpiStringVal;
vp->value.str = buf;
vp->value.str = rbuf;
}
static void binary_value(vpiHandle ref, p_vpi_value vp)
{
struct __vpiBinaryConst*rfp = (struct __vpiBinaryConst*)ref;
assert(ref->vpi_type->type_code == vpiConstant);
struct __vpiBinaryConst*rfp = (struct __vpiBinaryConst*)ref;
char*rbuf = 0;
switch (vp->format) {
case vpiObjTypeVal:
case vpiBinStrVal:
assert(rfp->nbits < sizeof buf);
case vpiBinStrVal: {
rbuf = need_result_buf(rfp->nbits + 1, RBUF_VAL);
for (unsigned idx = 0 ; idx < rfp->nbits ; idx += 1) {
unsigned nibble = idx/4;
unsigned shift = 2 * (idx%4);
unsigned val = (rfp->bits[nibble] >> shift) & 3;
buf[rfp->nbits-idx-1] = "01xz"[val];
rbuf[rfp->nbits-idx-1] = "01xz"[val];
}
buf[rfp->nbits] = 0;
vp->value.str = buf;
vp->format = vpiBinStrVal;
rbuf[rfp->nbits] = 0;
vp->value.str = rbuf;
break;
}
case vpiDecStrVal: {
unsigned wid = rfp->nbits;
rbuf = need_result_buf(rfp->nbits + 1, RBUF_VAL);
unsigned char*tmp = new unsigned char[wid];
for (unsigned idx = 0 ; idx < wid ; idx += 1)
tmp[idx] = (rfp->bits[idx/4] >> 2*(idx%4)) & 3;
vpip_bits_to_dec_str(tmp, wid, buf, sizeof buf,
vpip_bits_to_dec_str(tmp, wid, rbuf, wid + 1,
rfp->signed_flag);
delete[]tmp;
vp->value.str = buf;
vp->format = vpiDecStrVal;
vp->value.str = rbuf;
break;
}
case vpiHexStrVal: {
unsigned nchar = (rfp->nbits+3)/4;
assert(nchar < sizeof buf);
rbuf = need_result_buf(nchar + 1, RBUF_VAL);
for (unsigned idx = 0 ; idx < rfp->nbits ; idx += 4) {
unsigned nibble = idx/4;
unsigned vals = rfp->bits[nibble];
if (vals == 0xff) {
buf[nchar-idx/4-1] = 'z';
rbuf[nchar-idx/4-1] = 'z';
} else if (vals == 0xaa) {
buf[nchar-idx/4-1] = 'x';
rbuf[nchar-idx/4-1] = 'x';
} else if (vals & 0xaa) {
buf[nchar-idx/4-1] = 'X';
rbuf[nchar-idx/4-1] = 'X';
} else {
unsigned val = vals&1;
if (vals&0x04) val |= 2;
if (vals&0x10) val |= 4;
if (vals&0x40) val |= 8;
buf[nchar-idx/4-1] = "0123456789abcdef"[val];
rbuf[nchar-idx/4-1] = "0123456789abcdef"[val];
}
}
buf[nchar] = 0;
vp->value.str = buf;
vp->format = vpiHexStrVal;
rbuf[nchar] = 0;
vp->value.str = rbuf;
break;
}
@ -412,7 +398,7 @@ static void binary_value(vpiHandle ref, p_vpi_value vp)
case vpiVectorVal: {
unsigned int obit = 0;
unsigned hwid = (rfp->nbits - 1)/32 + 1;
char*rbuf = need_result_buf(hwid*sizeof(s_vpi_vecval), RBUF_VAL);
rbuf = need_result_buf(hwid*sizeof(s_vpi_vecval), RBUF_VAL);
s_vpi_vecval *op = (p_vpi_vecval)rbuf;
vp->value.vector = op;
@ -550,8 +536,9 @@ static int dec_get(int code, vpiHandle ref)
static void dec_value(vpiHandle ref, p_vpi_value vp)
{
struct __vpiDecConst*rfp = (struct __vpiDecConst*)ref;
char* cp;
assert(ref->vpi_type->type_code == vpiConstant);
char*rbuf = need_result_buf(64 + 1, RBUF_VAL);
char*cp = rbuf;
switch (vp->format) {
@ -562,35 +549,30 @@ static void dec_value(vpiHandle ref, p_vpi_value vp)
}
case vpiDecStrVal:
sprintf(buf, "%d", rfp->value);
sprintf(rbuf, "%d", rfp->value);
vp->format = vpiDecStrVal;
vp->value.str = buf;
vp->value.str = rbuf;
break;
case vpiBinStrVal:
cp = buf;
for(int bit=31; bit<=0;bit--){
*cp++ = "01"[ (rfp->value>>bit)&1 ];
}
*cp = 0;
vp->format = vpiBinStrVal;
vp->value.str = buf;
vp->value.str = rbuf;
break;
case vpiHexStrVal:
sprintf(buf, "%08x", rfp->value);
sprintf(rbuf, "%08x", rfp->value);
vp->format = vpiHexStrVal;
vp->value.str = buf;
vp->value.str = rbuf;
break;
case vpiOctStrVal:
sprintf(buf, "%011x", rfp->value);
sprintf(rbuf, "%011x", rfp->value);
vp->format = vpiOctStrVal;
vp->value.str = buf;
vp->value.str = rbuf;
break;
default:
@ -631,6 +613,9 @@ vpiHandle vpip_make_dec_const(int value)
/*
* $Log: vpi_const.cc,v $
* Revision 1.25 2003/03/13 04:59:21 steve
* Use rbufs instead of static buffers.
*
* Revision 1.24 2003/03/10 23:37:07 steve
* Direct support for string parameters.
*

View File

@ -17,13 +17,14 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
#ident "$Id: vpi_real.cc,v 1.7 2003/03/06 04:32:00 steve Exp $"
#ident "$Id: vpi_real.cc,v 1.8 2003/03/13 04:59:21 steve Exp $"
#endif
# include "vpi_priv.h"
# include "schedule.h"
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
#ifdef HAVE_MALLOC_H
# include <malloc.h>
#endif
@ -43,11 +44,13 @@ static char* real_var_get_str(int code, vpiHandle ref)
assert(ref->vpi_type->type_code == vpiRealVar);
struct __vpiRealVar*rfp = (struct __vpiRealVar*)ref;
char *rbuf = need_result_buf(strlen(rfp->name) + 1, RBUF_STR);
switch (code) {
case vpiName:
return const_cast<char*>(rfp->name);
strcpy(rbuf, rfp->name);
return rbuf;
default:
return 0;
@ -60,8 +63,8 @@ static void real_var_get_value(vpiHandle ref, s_vpi_value*vp)
{
assert(ref->vpi_type->type_code == vpiRealVar);
static char buf[66];
struct __vpiRealVar*rfp = (struct __vpiRealVar*)ref;
char*rbuf = need_result_buf(64 + 1, RBUF_VAL);
switch (vp->format) {
case vpiObjTypeVal:
@ -76,21 +79,19 @@ static void real_var_get_value(vpiHandle ref, s_vpi_value*vp)
break;
case vpiDecStrVal:
sprintf(buf, "%0.0f", rfp->value);
vp->value.str = buf;
sprintf(rbuf, "%0.0f", rfp->value);
vp->value.str = rbuf;
break;
case vpiHexStrVal:
sprintf(buf, "%lx", (long)rfp->value);
vp->value.str = buf;
sprintf(rbuf, "%lx", (long)rfp->value);
vp->value.str = rbuf;
break;
case vpiBinStrVal: {
unsigned long val = (unsigned long)rfp->value;
unsigned len = 0;
assert(8*sizeof(val) < sizeof buf);
while (val > 0) {
len += 1;
val /= 2;
@ -98,16 +99,16 @@ static void real_var_get_value(vpiHandle ref, s_vpi_value*vp)
val = (unsigned long)rfp->value;
for (unsigned idx = 0 ; idx < len ; idx += 1) {
buf[len-idx-1] = (val & 1)? '1' : '0';
rbuf[len-idx-1] = (val & 1)? '1' : '0';
val /= 2;
}
buf[len] = 0;
rbuf[len] = 0;
if (len == 0) {
buf[0] = '0';
buf[1] = 0;
rbuf[0] = '0';
rbuf[1] = 0;
}
vp->value.str = buf;
vp->value.str = rbuf;
break;
}
@ -178,6 +179,9 @@ vpiHandle vpip_make_real_var(const char*name)
/*
* $Log: vpi_real.cc,v $
* Revision 1.8 2003/03/13 04:59:21 steve
* Use rbufs instead of static buffers.
*
* Revision 1.7 2003/03/06 04:32:00 steve
* Use hashed name strings for identifiers.
*

View File

@ -17,7 +17,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
#ident "$Id: vpi_time.cc,v 1.12 2003/02/03 01:09:20 steve Exp $"
#ident "$Id: vpi_time.cc,v 1.13 2003/03/13 04:59:21 steve Exp $"
#endif
# include "vpi_priv.h"
@ -134,8 +134,6 @@ static vpiHandle timevar_handle(int code, vpiHandle ref)
static void timevar_get_value(vpiHandle ref, s_vpi_value*vp)
{
static char buf_obj[128];
/* Keep a persistent structure for passing time values back to
the caller. */
static struct t_vpi_time time_value;
@ -146,6 +144,8 @@ static void timevar_get_value(vpiHandle ref, s_vpi_value*vp)
vvp_time64_t simtime = schedule_simtime();
int units = rfp->scope? rfp->scope->time_units : vpi_time_precision;
char*rbuf = need_result_buf(128, RBUF_VAL);
/* Calculate the divisor needed to scale the simulation time
(in time_precision units) to time units of the scope. */
vvp_time64_t divisor = 1;
@ -180,28 +180,28 @@ static void timevar_get_value(vpiHandle ref, s_vpi_value*vp)
x = simtime;
num_bits = 8 * sizeof(unsigned long);
buf_obj[num_bits] = 0;
rbuf[num_bits] = 0;
for (unsigned i = 1; i <= num_bits; i++) {
buf_obj[num_bits-i] = x & 1 ? '1' : '0';
rbuf[num_bits-i] = x & 1 ? '1' : '0';
x = x >> 1;
}
vp->value.str = buf_obj;
vp->value.str = rbuf;
break;
case vpiDecStrVal:
sprintf(buf_obj, "%lu", simtime);
vp->value.str = buf_obj;
sprintf(rbuf, "%lu", simtime);
vp->value.str = rbuf;
break;
case vpiOctStrVal:
sprintf(buf_obj, "%lo", simtime);
vp->value.str = buf_obj;
sprintf(rbuf, "%lo", simtime);
vp->value.str = rbuf;
break;
case vpiHexStrVal:
sprintf(buf_obj, "%lx", simtime);
vp->value.str = buf_obj;
sprintf(rbuf, "%lx", simtime);
vp->value.str = rbuf;
break;
default:
@ -268,6 +268,9 @@ void vpip_set_time_precision(int pre)
/*
* $Log: vpi_time.cc,v $
* Revision 1.13 2003/03/13 04:59:21 steve
* Use rbufs instead of static buffers.
*
* Revision 1.12 2003/02/03 01:09:20 steve
* Allow $display of $simtime.
*

View File

@ -18,7 +18,7 @@
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
*/
#ifdef HAVE_CVS_IDENT
#ident "$Id: vpi_vthr_vector.cc,v 1.13 2003/03/01 00:46:13 steve Exp $"
#ident "$Id: vpi_vthr_vector.cc,v 1.14 2003/03/13 04:59:21 steve Exp $"
#endif
/*
@ -112,23 +112,25 @@ static char* vthr_vec_get_str(int code, vpiHandle ref)
return 0;
}
static char buf[4096];
static void vthr_vec_DecStrVal(struct __vpiVThrVec*rfp, s_vpi_value*vp)
{
unsigned char*bits = new unsigned char[rfp->wid];
char *rbuf = need_result_buf((rfp->wid+2)/3 + 1, RBUF_VAL);
for (unsigned idx = 0 ; idx < rfp->wid ; idx += 1)
bits[idx] = get_bit(rfp, idx);
vpip_bits_to_dec_str(bits, rfp->wid, buf, sizeof buf, rfp->signed_flag);
vpip_bits_to_dec_str(bits, rfp->wid, rbuf, rfp->wid+1, rfp->signed_flag);
vp->value.str = rbuf;
return;
}
static void vthr_vec_StringVal(struct __vpiVThrVec*rfp, s_vpi_value*vp)
{
char*cp = buf;
char tmp = 0;
assert(rfp->wid/8 < int(sizeof(buf)-1));
char *rbuf = need_result_buf((rfp->wid / 8) + 1, RBUF_VAL);
char *cp = rbuf;
for(int bitnr=rfp->wid-1; bitnr>=0; bitnr--){
tmp <<= 1;
@ -149,6 +151,7 @@ static void vthr_vec_StringVal(struct __vpiVThrVec*rfp, s_vpi_value*vp)
}
}
*cp++ = 0;
vp->value.str = rbuf;
return;
}
@ -164,78 +167,77 @@ static void vthr_vec_get_value(vpiHandle ref, s_vpi_value*vp)
|| (ref->vpi_type->type_code==vpiConstant));
struct __vpiVThrVec*rfp = (struct __vpiVThrVec*)ref;
char *rbuf;
unsigned wid = rfp->wid;
switch (vp->format) {
case vpiBinStrVal:
assert(wid < sizeof buf);
rbuf = need_result_buf(wid+1, RBUF_VAL);
for (unsigned idx = 0 ; idx < wid ; idx += 1) {
buf[wid-idx-1] = "01xz"[get_bit(rfp, idx)];
rbuf[wid-idx-1] = "01xz"[get_bit(rfp, idx)];
}
buf[wid] = 0;
vp->value.str = buf;
rbuf[wid] = 0;
vp->value.str = rbuf;
break;
case vpiHexStrVal: {
unsigned hval, hwid;
hwid = (wid + 3) / 4;
assert(hwid < sizeof buf);
buf[hwid] = 0;
rbuf = need_result_buf(hwid+1, RBUF_VAL);
rbuf[hwid] = 0;
hval = 0;
for (unsigned idx = 0 ; idx < wid ; idx += 1) {
hval = hval | (get_bit(rfp, idx) << 2*(idx % 4));
if (idx%4 == 3) {
hwid -= 1;
buf[hwid] = hex_digits[hval];
rbuf[hwid] = hex_digits[hval];
hval = 0;
}
}
if (hwid > 0) {
hwid -= 1;
buf[hwid] = hex_digits[hval];
rbuf[hwid] = hex_digits[hval];
hval = 0;
}
vp->value.str = buf;
vp->value.str = rbuf;
break;
}
case vpiOctStrVal: {
unsigned hval, hwid;
hwid = (wid + 2) / 3;
assert(hwid < sizeof buf);
buf[hwid] = 0;
rbuf = need_result_buf(hwid+1, RBUF_VAL);
rbuf[hwid] = 0;
hval = 0;
for (unsigned idx = 0 ; idx < wid ; idx += 1) {
hval = hval | (get_bit(rfp,idx) << 2*(idx % 3));
if (idx%3 == 2) {
hwid -= 1;
buf[hwid] = oct_digits[hval];
rbuf[hwid] = oct_digits[hval];
hval = 0;
}
}
if (hwid > 0) {
hwid -= 1;
buf[hwid] = oct_digits[hval];
rbuf[hwid] = oct_digits[hval];
hval = 0;
}
vp->value.str = buf;
vp->value.str = rbuf;
break;
}
case vpiDecStrVal:
vthr_vec_DecStrVal(rfp, vp);
vp->value.str = buf;
break;
case vpiStringVal:
vthr_vec_StringVal(rfp, vp);
vp->value.str = buf;
break;
case vpiIntVal:
@ -388,8 +390,7 @@ static void vthr_real_get_value(vpiHandle ref, s_vpi_value*vp)
assert(ref->vpi_type->type_code==vpiConstant);
struct __vpiVThrWord*obj = (struct __vpiVThrWord*)ref;
static char buf[66];
char *rbuf = need_result_buf(66, RBUF_VAL);
double val = 0.0;
@ -410,21 +411,19 @@ static void vthr_real_get_value(vpiHandle ref, s_vpi_value*vp)
break;
case vpiDecStrVal:
sprintf(buf, "%0.0f", val);
vp->value.str = buf;
sprintf(rbuf, "%0.0f", val);
vp->value.str = rbuf;
break;
case vpiHexStrVal:
sprintf(buf, "%lx", (long)val);
vp->value.str = buf;
sprintf(rbuf, "%lx", (long)val);
vp->value.str = rbuf;
break;
case vpiBinStrVal: {
unsigned long vali = (unsigned long)val;
unsigned len = 0;
assert(8*sizeof(vali) < sizeof buf);
while (vali > 0) {
len += 1;
vali /= 2;
@ -432,16 +431,16 @@ static void vthr_real_get_value(vpiHandle ref, s_vpi_value*vp)
vali = (unsigned long)val;
for (unsigned idx = 0 ; idx < len ; idx += 1) {
buf[len-idx-1] = (vali & 1)? '1' : '0';
rbuf[len-idx-1] = (vali & 1)? '1' : '0';
vali /= 2;
}
buf[len] = 0;
rbuf[len] = 0;
if (len == 0) {
buf[0] = '0';
buf[1] = 0;
rbuf[0] = '0';
rbuf[1] = 0;
}
vp->value.str = buf;
vp->value.str = rbuf;
break;
}
@ -478,6 +477,9 @@ vpiHandle vpip_make_vthr_word(unsigned base, const char*type)
/*
* $Log: vpi_vthr_vector.cc,v $
* Revision 1.14 2003/03/13 04:59:21 steve
* Use rbufs instead of static buffers.
*
* Revision 1.13 2003/03/01 00:46:13 steve
* Careful about compiletf calls.
*