Skip to content

Commit

Permalink
Minor zero-initalization cleanups (#301)
Browse files Browse the repository at this point in the history
Either via new
Or via list-init for structs
Or via calloc for malloc (which has the benefit of potentially being
faster that malloc+memset because of CoW magic with zero pages on
Linux).

c.f., https://en.cppreference.com/w/cpp/language/zero_initialization
&   https://blog.petrzemek.net/2015/10/03/structures-and-member-initializers-in-cpp/
  • Loading branch information
NiLuJe authored Jul 18, 2019
1 parent ccb8986 commit 3433c07
Show file tree
Hide file tree
Showing 16 changed files with 154 additions and 215 deletions.
6 changes: 2 additions & 4 deletions crengine/include/lvhashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ template <typename keyT, typename valueT> class LVHashTable
{
if (size < 16 )
size = 16;
_table = new pair* [ size ];
memset( _table, 0, sizeof(pair*) * size );
_table = new pair* [ size ]();
_size = size;
_count = 0;
}
Expand Down Expand Up @@ -133,8 +132,7 @@ template <typename keyT, typename valueT> class LVHashTable
int size() { return _size; }
void resize( int nsize )
{
pair ** new_table = new pair * [ nsize ];
memset( new_table, 0, sizeof(pair*) * nsize );
pair ** new_table = new pair * [ nsize ]();
if (_table) {
for ( int i=0; i<_size; i++ ) {
pair * p = _table[i];
Expand Down
38 changes: 17 additions & 21 deletions crengine/src/crtxtenc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,15 @@ static const lChar16 __cp1251[128] = {

static const lChar16 __cp1252[128] = {
/* 0x80*/
0x0402, 0x0403, 0x201a, 0x0453,
0x201e, 0x2026, 0x2020, 0x2021,
0x20ac, 0x2030, 0x0409, 0x2039,
0x040a, 0x040c, 0x040b, 0x040f,
0x0402, 0x0403, 0x201a, 0x0453,
0x201e, 0x2026, 0x2020, 0x2021,
0x20ac, 0x2030, 0x0409, 0x2039,
0x040a, 0x040c, 0x040b, 0x040f,
/* 0x90*/
0x0452, 0x2018, 0x2019, 0x201c,
0x201d, 0x2022, 0x2013, 0x2014,
0x0000, 0x2122, 0x0459, 0x203a,
0x045a, 0x045c, 0x045b, 0x045f,
0x0452, 0x2018, 0x2019, 0x201c,
0x201d, 0x2022, 0x2013, 0x2014,
0x0000, 0x2122, 0x0459, 0x203a,
0x045a, 0x045c, 0x045b, 0x045f,
/* 0xa0*/
0x00a0, 0x00a1, 0x00a2, 0x00a3,
0x00a4, 0x00a5, 0x00a6, 0x00a7,
Expand Down Expand Up @@ -1431,8 +1431,8 @@ const lChar8 ** GetCharsetUnicode2ByteTable( const lChar16 * enc_name )
#define DBL_CHAR_STAT_SIZE 256

class CDoubleCharStat
{
{

struct CDblCharNode
{
unsigned char ch1;
Expand Down Expand Up @@ -1637,7 +1637,7 @@ int sort_dblstats_by_ch( const void * p1, const void * p2 )
}

class CDoubleCharStat2
{
{
private:
lUInt16 * * stats;
int total;
Expand All @@ -1649,15 +1649,13 @@ class CDoubleCharStat2
void Add( unsigned char c1, unsigned char c2 )
{
if ( !stats ) {
stats = new lUInt16* [256];
memset( stats, 0, sizeof(lUInt16*)*256);
stats = new lUInt16* [256]();
}
if (c1==' ' && c2==' ')
return;
total++;
if ( stats[c1]==NULL ) {
stats[c1] = new lUInt16[256];
memset( stats[c1], 0, sizeof(lUInt16)*256 );
stats[c1] = new lUInt16[256]();
}
if ( stats[c1][c2]++ == 0)
items++;
Expand Down Expand Up @@ -1792,8 +1790,7 @@ void MakeDblCharStat(const unsigned char * buf, int buf_size, dbl_char_stat_t *

void MakeCharStat(const unsigned char * buf, int buf_size, short stat_table[256], bool skipHtml)
{
int stat[256];
memset( stat, 0, sizeof(int)*256 );
int stat[256] = { 0 };
int total=0;
unsigned char ch;
bool insideTag = false;
Expand Down Expand Up @@ -2085,8 +2082,7 @@ void MakeStatsForFile( const char * fname, const char * cp_name, const char * la
fseek( in, 0, SEEK_SET );
unsigned char * buf = new unsigned char[buf_size];
fread(buf, 1, buf_size, in);
short char_stat[256];
memset(char_stat, 0, sizeof(short)*256);
short char_stat[256] = { 0 };
dbl_char_stat_t dbl_char_stat[DBL_CHAR_STAT_SIZE];
bool skipHtml = hasXmlTags(buf, buf_size);
MakeCharStat(buf, buf_size, char_stat, skipHtml);
Expand All @@ -2095,7 +2091,7 @@ void MakeStatsForFile( const char * fname, const char * cp_name, const char * la
int i;
for (i=0; i<16; i++)
{
for (int j=0; j<16; j++)
for (int j=0; j<16; j++)
{
fprintf(f, "0x%04x,", (unsigned int)char_stat[i*16+j] );
}
Expand All @@ -2105,7 +2101,7 @@ void MakeStatsForFile( const char * fname, const char * cp_name, const char * la
fprintf(f, "static const dbl_char_stat_t dbl_ch_stat_%s_%s%d[%d] = {\n", cp_name, lang_name, index, DBL_CHAR_STAT_SIZE );
for (i=0; i<DBL_CHAR_STAT_SIZE/16; i++)
{
for (int j=0; j<16; j++)
for (int j=0; j<16; j++)
{
fprintf(f, "{0x%02x,0x%02x,0x%04x}, ", (unsigned int)dbl_char_stat[i*16+j].ch1, (unsigned int)dbl_char_stat[i*16+j].ch2, (unsigned int)((lUInt16)dbl_char_stat[i*16+j].count) );
}
Expand Down
3 changes: 1 addition & 2 deletions crengine/src/hist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,7 @@ int CRFileHistRecord::getLastShortcutBookmark()
int CRFileHistRecord::getFirstFreeShortcutBookmark()
{
//int last = -1;
char flags[MAX_SHORTCUT_BOOKMARKS+1];
memset( flags, 0, sizeof(flags) );
char flags[MAX_SHORTCUT_BOOKMARKS+1] = { 0 };
for ( int i=0; i<_bookmarks.length(); i++ ) {
if ( _bookmarks[i]->getShortcut()>0 && _bookmarks[i]->getShortcut() < MAX_SHORTCUT_BOOKMARKS && _bookmarks[i]->getType() == bmkt_pos )
flags[ _bookmarks[i]->getShortcut() ] = 1;
Expand Down
39 changes: 17 additions & 22 deletions crengine/src/hyphman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ bool HyphDictionary::activate()
bool HyphDictionaryList::activate( lString16 id )
{
CRLog::trace("HyphDictionaryList::activate(%s)", LCSTR(id));
HyphDictionary * p = find(id);
if ( p )
return p->activate();
else
HyphDictionary * p = find(id);
if ( p )
return p->activate();
else
return false;
}

Expand All @@ -279,7 +279,7 @@ void HyphDictionaryList::addDefault()
if ( !find( lString16( HYPH_DICT_ID_SOFTHYPHENS ) ) ) {
_list.add( new HyphDictionary( HDT_SOFTHYPHENS, _16("[Soft-hyphens Hyphenation]"), lString16(HYPH_DICT_ID_SOFTHYPHENS), lString16(HYPH_DICT_ID_SOFTHYPHENS) ) );
}

}

HyphDictionary * HyphDictionaryList::find( lString16 id )
Expand Down Expand Up @@ -328,15 +328,15 @@ bool HyphDictionaryList::open(lString16 hyphDirectory, bool clear)
t = HDT_DICT_TEX;
} else
continue;



lString16 filename = hyphDirectory + name;
lString16 id = name;
lString16 title = name;
if ( title.endsWith( suffix ) )
title.erase( title.length() - suffix.length(), suffix.length() );

_list.add( new HyphDictionary( t, title, id, filename ) );
count++;
}
Expand Down Expand Up @@ -408,12 +408,12 @@ static int isCorrectHyphFile(LVStream * stream)
stream->SetPos(0);
lvByteOrderConv cnv;
w=cnv.msf(HDR.numrec);
if (dw!=78 || w>0xff)
if (dw!=78 || w>0xff)
w = 0;

if (strncmp((const char*)&HDR.type, "HypHAlR4", 8) != 0)
if (strncmp((const char*)&HDR.type, "HypHAlR4", 8) != 0)
w = 0;

return w;
}

Expand Down Expand Up @@ -639,13 +639,12 @@ bool TexHyph::load( LVStreamRef stream )
stream->SetPos(p);
if ( stream->SetPos(p)!=p )
return false;
lChar16 charMap[256];
lChar16 charMap[256] = { 0 };
unsigned char buf[0x10000];
memset( charMap, 0, sizeof( charMap ) );
// make char map table
for (i=0; i<hyph_count; i++)
{
if ( stream->Read( &hyph, 522, &dw )!=LVERR_OK || dw!=522 )
if ( stream->Read( &hyph, 522, &dw )!=LVERR_OK || dw!=522 )
return false;
cnv.msf( &hyph.len ); //rword(_main_hyph[i].len);
lvpos_t newPos;
Expand Down Expand Up @@ -687,11 +686,11 @@ bool TexHyph::load( LVStreamRef stream )
for (i=0; i<hyph_count; i++)
{
stream->Read( &hyph, 522, &dw );
if (dw!=522)
if (dw!=522)
return false;
cnv.msf( &hyph.len );

stream->Read(buf, hyph.len, &dw);
stream->Read(buf, hyph.len, &dw);
if (dw!=hyph.len)
return false;

Expand Down Expand Up @@ -814,8 +813,8 @@ bool TexHyph::hyphenate( const lChar16 * str, int len, lUInt16 * widths, lUInt8
return false;
if ( len>=WORD_LENGTH )
len = WORD_LENGTH - 2;
lChar16 word[WORD_LENGTH+4];
char mask[WORD_LENGTH+4];
lChar16 word[WORD_LENGTH+4] = { 0 };
char mask[WORD_LENGTH+4] = { 0 };

// Make word from str, with soft-hyphens stripped out.
// Prepend and append a space so patterns can match word boundaries.
Expand All @@ -829,9 +828,6 @@ bool TexHyph::hyphenate( const lChar16 * str, int len, lUInt16 * widths, lUInt8
}
wlen = w-1;
word[w++] = ' ';
word[w++] = 0;
word[w++] = 0;
word[w++] = 0;
if ( wlen<=3 )
return false;
lStr_lowercase(word+1, wlen);
Expand All @@ -843,8 +839,7 @@ bool TexHyph::hyphenate( const lChar16 * str, int len, lUInt16 * widths, lUInt8

// Find matches from dict patterns, at any position in word.
// Places where hyphenation is allowed are put into 'mask'.
memset( mask, '0', wlen+3 );
mask[wlen+3] = 0;
memset( mask, '0', wlen+3 ); // 0x30!
bool found = false;
for ( int i=0; i<=wlen; i++ ) {
found = match( word + i, mask + i ) || found;
Expand Down
8 changes: 3 additions & 5 deletions crengine/src/lstridmap.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*******************************************************
CoolReader Engine DOM Tree
CoolReader Engine DOM Tree
LDOMNodeIdMap.cpp: Name to Id map
Expand Down Expand Up @@ -153,10 +153,8 @@ LDOMNameIdMap::LDOMNameIdMap(lUInt16 maxId)
{
m_size = maxId+1;
m_count = 0;
m_by_id = new LDOMNameIdMapItem * [m_size];
memset( m_by_id, 0, sizeof(LDOMNameIdMapItem *)*m_size );
m_by_name = new LDOMNameIdMapItem * [m_size];
memset( m_by_name, 0, sizeof(LDOMNameIdMapItem *)*m_size );
m_by_id = new LDOMNameIdMapItem * [m_size]();
m_by_name = new LDOMNameIdMapItem * [m_size]();
m_sorted = true;
m_changed = false;
}
Expand Down
21 changes: 9 additions & 12 deletions crengine/src/lvdrawbuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,8 +265,7 @@ void LVGrayDrawBuf::Rotate( cr_rotate_angle_t angle )
}
int newrowsize = _bpp<=2 ? (_dy * _bpp + 7) / 8 : _dy;
sz = (newrowsize * _dx);
lUInt8 * dst = (lUInt8 *)malloc(sz);
memset( dst, 0, sz );
lUInt8 * dst = (lUInt8 *)calloc(sz, sizeof(*dst));
for ( int y=0; y<_dy; y++ ) {
lUInt8 * src = _data + _rowsize*y;
int dstx, dsty;
Expand Down Expand Up @@ -1096,10 +1095,11 @@ void LVGrayDrawBuf::Resize( int dx, int dy )
_dy = dy;
_rowsize = _bpp<=2 ? (_dx * _bpp + 7) / 8 : _dx;
if (dx > 0 && dy > 0) {
_data = (unsigned char *)malloc(_rowsize * _dy + 1);
_data = (unsigned char *)calloc(_rowsize * _dy + 1, sizeof(*_data));
_data[_rowsize * _dy] = GUARD_BYTE;
} else {
Clear(0);
}
Clear(0);
SetClipRect( NULL );
}

Expand Down Expand Up @@ -1143,9 +1143,8 @@ LVGrayDrawBuf::LVGrayDrawBuf(int dx, int dy, int bpp, void * auxdata )
_data = (lUInt8 *) auxdata;
_ownData = false;
} else if (_dx && _dy) {
_data = (lUInt8 *) malloc(_rowsize * _dy + 1);
_data = (lUInt8 *) calloc(_rowsize * _dy + 1, sizeof(*_data));
_data[_rowsize * _dy] = GUARD_BYTE;
Clear(0);
}
SetClipRect( NULL );
CHECK_GUARD_BYTE;
Expand Down Expand Up @@ -1389,8 +1388,7 @@ void LVGrayDrawBuf::ConvertToBitmap(bool flgDither)
return;
// TODO: implement for byte per pixel mode
int sz = GetRowSize();
lUInt8 * bitmap = (lUInt8*) malloc( sizeof(lUInt8) * sz );
memset( bitmap, 0, sz );
lUInt8 * bitmap = (lUInt8*) calloc(sz, sizeof(*bitmap));
if (flgDither)
{
static const lUInt8 cmap[4][4] = {
Expand Down Expand Up @@ -1773,8 +1771,7 @@ void LVColorDrawBuf::Resize( int dx, int dy )
_dy = dy;
_rowsize = dx*(_bpp>>3);
#if !defined(__SYMBIAN32__) && defined(_WIN32) && !defined(QT_GL)
BITMAPINFO bmi;
memset( &bmi, 0, sizeof(bmi) );
BITMAPINFO bmi = { 0 };
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
bmi.bmiHeader.biWidth = _dx;
bmi.bmiHeader.biHeight = _dy;
Expand All @@ -1790,10 +1787,10 @@ void LVColorDrawBuf::Resize( int dx, int dy )
_drawbmp = CreateDIBSection( NULL, &bmi, DIB_RGB_COLORS, (void**)(&_data), NULL, 0 );
_drawdc = CreateCompatibleDC(NULL);
SelectObject(_drawdc, _drawbmp);
memset( _data, 0, _rowsize * _dy );
#else
_data = (lUInt8 *)malloc( (_bpp>>3) * _dx * _dy);
_data = (lUInt8 *)calloc((_bpp>>3) * _dx * _dy, sizeof(*_data));
#endif
memset( _data, 0, _rowsize * _dy );
}
SetClipRect( NULL );
}
Expand Down
12 changes: 4 additions & 8 deletions crengine/src/lvfntman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3982,8 +3982,7 @@ class LVWin32FontManager : public LVFontManager
virtual bool Init( lString8 path )
{
LVColorDrawBuf drawbuf(1,1);
LOGFONTA lf;
memset(&lf, 0, sizeof(lf));
LOGFONTA lf = { 0 };
lf.lfCharSet = ANSI_CHARSET;
int res =
EnumFontFamiliesExA(
Expand Down Expand Up @@ -4531,8 +4530,7 @@ bool LVBaseWin32Font::Create(int size, int weight, bool italic, css_font_family_
if (!IsNull())
Clear();
//
LOGFONTA lf;
memset(&lf, 0, sizeof(LOGFONTA));
LOGFONTA lf = { 0 };
lf.lfHeight = size;
lf.lfWeight = weight;
lf.lfItalic = italic?1:0;
Expand Down Expand Up @@ -4604,8 +4602,7 @@ int LVWin32DrawFont::getCharWidth( lChar16 ch, lChar16 def_char )
if (_hfont==NULL)
return 0;
// measure character widths
GCP_RESULTSW gcpres;
memset( &gcpres, 0, sizeof(gcpres) );
GCP_RESULTSW gcpres = { 0 };
gcpres.lStructSize = sizeof(gcpres);
lChar16 str[2];
str[0] = ch;
Expand Down Expand Up @@ -4683,8 +4680,7 @@ lUInt16 LVWin32DrawFont::measureText(
}
assert(pstr[len]==0);
// measure character widths
GCP_RESULTSW gcpres;
memset( &gcpres, 0, sizeof(gcpres) );
GCP_RESULTSW gcpres = { 0 };
gcpres.lStructSize = sizeof(gcpres);
LVArray<int> dx( len+1, 0 );
gcpres.lpDx = dx.ptr();
Expand Down
6 changes: 1 addition & 5 deletions crengine/src/lvmemman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@ void crSetSignalHandler()
if (signals_are_set)
return;
signals_are_set = true;
struct sigaction handler; // = {0};
//size_t s = sizeof(handler);
//void * p = &handler;
//memset(p, 0, s);
memset(&handler, 0, sizeof(handler));
struct sigaction handler = { 0 };
handler.sa_sigaction = cr_sigaction;
handler.sa_flags = SA_RESETHAND;
#define CATCHSIG(X) sigaction(X, &handler, &old_sa[X])
Expand Down
Loading

0 comments on commit 3433c07

Please sign in to comment.