Skip to content

Commit

Permalink
more PVS-Studio suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Nov 16, 2012
1 parent 19389d2 commit c0a4625
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 47 deletions.
28 changes: 13 additions & 15 deletions src/root/root.c
Expand Up @@ -1479,7 +1479,7 @@ void OutBuffer::mark()
mem.mark(data);
}

void OutBuffer::reserve(unsigned nbytes)
void OutBuffer::reserve(size_t nbytes)
{
//printf("OutBuffer::reserve: size = %d, offset = %d, nbytes = %d\n", size, offset, nbytes);
if (size - offset < nbytes)
Expand All @@ -1494,12 +1494,12 @@ void OutBuffer::reset()
offset = 0;
}

void OutBuffer::setsize(unsigned size)
void OutBuffer::setsize(size_t size)
{
offset = size;
}

void OutBuffer::write(const void *data, unsigned nbytes)
void OutBuffer::write(const void *data, size_t nbytes)
{
reserve(nbytes);
memcpy(this->data + offset, data, nbytes);
Expand All @@ -1517,9 +1517,8 @@ void OutBuffer::writestring(const char *string)
}

void OutBuffer::prependstring(const char *string)
{ unsigned len;

len = strlen(string);
{
size_t len = strlen(string);
reserve(len);
memmove(data + len, data, offset);
memcpy(data, string, len);
Expand Down Expand Up @@ -1651,17 +1650,16 @@ void OutBuffer::write(Object *obj)
}
}

void OutBuffer::fill0(unsigned nbytes)
void OutBuffer::fill0(size_t nbytes)
{
reserve(nbytes);
memset(data + offset,0,nbytes);
offset += nbytes;
}

void OutBuffer::align(unsigned size)
{ unsigned nbytes;

nbytes = ((offset + size - 1) & ~(size - 1)) - offset;
void OutBuffer::align(size_t size)
{
size_t nbytes = ((offset + size - 1) & ~(size - 1)) - offset;
fill0(nbytes);
}

Expand Down Expand Up @@ -1763,7 +1761,7 @@ void OutBuffer::bracket(char left, char right)
* Return index just past right.
*/

unsigned OutBuffer::bracket(unsigned i, const char *left, unsigned j, const char *right)
size_t OutBuffer::bracket(size_t i, const char *left, size_t j, const char *right)
{
size_t leftlen = strlen(left);
size_t rightlen = strlen(right);
Expand All @@ -1773,7 +1771,7 @@ unsigned OutBuffer::bracket(unsigned i, const char *left, unsigned j, const char
return j + leftlen + rightlen;
}

void OutBuffer::spread(unsigned offset, unsigned nbytes)
void OutBuffer::spread(size_t offset, size_t nbytes)
{
reserve(nbytes);
memmove(data + offset + nbytes, data + offset,
Expand All @@ -1785,14 +1783,14 @@ void OutBuffer::spread(unsigned offset, unsigned nbytes)
* Returns: offset + nbytes
*/

unsigned OutBuffer::insert(unsigned offset, const void *p, unsigned nbytes)
size_t OutBuffer::insert(size_t offset, const void *p, size_t nbytes)
{
spread(offset, nbytes);
memmove(data + offset, p, nbytes);
return offset + nbytes;
}

void OutBuffer::remove(unsigned offset, unsigned nbytes)
void OutBuffer::remove(size_t offset, size_t nbytes)
{
memmove(data + offset, data + offset + nbytes, this->offset - (offset + nbytes));
this->offset -= nbytes;
Expand Down
22 changes: 11 additions & 11 deletions src/root/root.h
Expand Up @@ -231,18 +231,18 @@ struct File : Object
struct OutBuffer : Object
{
unsigned char *data;
unsigned offset;
unsigned size;
size_t offset;
size_t size;

OutBuffer();
~OutBuffer();
char *extractData();
void mark();

void reserve(unsigned nbytes);
void setsize(unsigned size);
void reserve(size_t nbytes);
void setsize(size_t size);
void reset();
void write(const void *data, unsigned nbytes);
void write(const void *data, size_t nbytes);
void writebstring(unsigned char *string);
void writestring(const char *string);
void prependstring(const char *string);
Expand All @@ -256,15 +256,15 @@ struct OutBuffer : Object
void write4(unsigned w);
void write(OutBuffer *buf);
void write(Object *obj);
void fill0(unsigned nbytes);
void align(unsigned size);
void fill0(size_t nbytes);
void align(size_t size);
void vprintf(const char *format, va_list args);
void printf(const char *format, ...);
void bracket(char left, char right);
unsigned bracket(unsigned i, const char *left, unsigned j, const char *right);
void spread(unsigned offset, unsigned nbytes);
unsigned insert(unsigned offset, const void *data, unsigned nbytes);
void remove(unsigned offset, unsigned nbytes);
size_t bracket(size_t i, const char *left, size_t j, const char *right);
void spread(size_t offset, size_t nbytes);
size_t insert(size_t offset, const void *data, size_t nbytes);
void remove(size_t offset, size_t nbytes);
char *toChars();
char *extractString();
};
Expand Down
20 changes: 9 additions & 11 deletions src/root/stringtable.c
Expand Up @@ -67,14 +67,14 @@ hash_t calcHash(const char *str, size_t len)
}
}

void StringValue::ctor(const char *p, unsigned length)
void StringValue::ctor(const char *p, size_t length)
{
this->length = length;
this->lstring[length] = 0;
memcpy(this->lstring, p, length * sizeof(char));
}

void StringTable::init(unsigned size)
void StringTable::init(size_t size)
{
table = (void **)mem.calloc(size, sizeof(void *));
tabledim = size;
Expand All @@ -83,11 +83,9 @@ void StringTable::init(unsigned size)

StringTable::~StringTable()
{
unsigned i;

// Zero out dangling pointers to help garbage collector.
// Should zero out StringEntry's too.
for (i = 0; i < count; i++)
for (size_t i = 0; i < count; i++)
table[i] = NULL;

mem.free(table);
Expand All @@ -102,10 +100,10 @@ struct StringEntry

StringValue value;

static StringEntry *alloc(const char *s, unsigned len);
static StringEntry *alloc(const char *s, size_t len);
};

StringEntry *StringEntry::alloc(const char *s, unsigned len)
StringEntry *StringEntry::alloc(const char *s, size_t len)
{
StringEntry *se;

Expand All @@ -115,7 +113,7 @@ StringEntry *StringEntry::alloc(const char *s, unsigned len)
return se;
}

void **StringTable::search(const char *s, unsigned len)
void **StringTable::search(const char *s, size_t len)
{
hash_t hash;
unsigned u;
Expand Down Expand Up @@ -149,7 +147,7 @@ void **StringTable::search(const char *s, unsigned len)
return (void **)se;
}

StringValue *StringTable::lookup(const char *s, unsigned len)
StringValue *StringTable::lookup(const char *s, size_t len)
{ StringEntry *se;

se = *(StringEntry **)search(s,len);
Expand All @@ -159,7 +157,7 @@ StringValue *StringTable::lookup(const char *s, unsigned len)
return NULL;
}

StringValue *StringTable::update(const char *s, unsigned len)
StringValue *StringTable::update(const char *s, size_t len)
{ StringEntry **pse;
StringEntry *se;

Expand All @@ -173,7 +171,7 @@ StringValue *StringTable::update(const char *s, unsigned len)
return &se->value;
}

StringValue *StringTable::insert(const char *s, unsigned len)
StringValue *StringTable::insert(const char *s, size_t len)
{ StringEntry **pse;
StringEntry *se;

Expand Down
20 changes: 10 additions & 10 deletions src/root/stringtable.h
Expand Up @@ -30,7 +30,7 @@ struct StringValue
char *string;
};
private:
unsigned length;
size_t length;

#ifndef IN_GCC
// Disable warning about nonstandard extension
Expand All @@ -39,33 +39,33 @@ struct StringValue
char lstring[];

public:
unsigned len() const { return length; }
size_t len() const { return length; }
const char *toDchars() const { return lstring; }

private:
friend struct StringEntry;
StringValue(); // not constructible
// This is more like a placement new c'tor
void ctor(const char *p, unsigned length);
void ctor(const char *p, size_t length);
};

struct StringTable
{
private:
void **table;
unsigned count;
unsigned tabledim;
size_t count;
size_t tabledim;

public:
void init(unsigned size = 37);
void init(size_t size = 37);
~StringTable();

StringValue *lookup(const char *s, unsigned len);
StringValue *insert(const char *s, unsigned len);
StringValue *update(const char *s, unsigned len);
StringValue *lookup(const char *s, size_t len);
StringValue *insert(const char *s, size_t len);
StringValue *update(const char *s, size_t len);

private:
void **search(const char *s, unsigned len);
void **search(const char *s, size_t len);
};

#endif

0 comments on commit c0a4625

Please sign in to comment.