Skip to content

Commit

Permalink
Replace new / delete by std::vector for INT_CLASS_STRUCT::ProtoLengths
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Weil <sw@weilnetz.de>
  • Loading branch information
stweil committed Mar 28, 2021
1 parent 486c257 commit 0c3d244
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
34 changes: 11 additions & 23 deletions src/classify/intproto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,7 @@ int AddIntProto(INT_CLASS_STRUCT *Class) {
memset(ProtoSet, 0, sizeof(*ProtoSet));

/* reallocate space for the proto lengths and install in class */
Class->ProtoLengths = static_cast<uint8_t *>(
realloc(Class->ProtoLengths, MaxNumIntProtosIn(Class) * sizeof(uint8_t)));
memset(&Class->ProtoLengths[Index], 0,
sizeof(*Class->ProtoLengths) * (MaxNumIntProtosIn(Class) - Index));
Class->ProtoLengths.resize(MaxNumIntProtosIn(Class));
}

/* initialize proto so its length is zero and it isn't in any configs */
Expand Down Expand Up @@ -579,15 +576,15 @@ void DisplayIntProto(INT_CLASS_STRUCT *Class, PROTO_ID ProtoId, float Evidence)
/// to handle the specified number of protos and configs.
/// @param MaxNumProtos number of protos to allocate space for
/// @param MaxNumConfigs number of configs to allocate space for
INT_CLASS_STRUCT::INT_CLASS_STRUCT(int MaxNumProtos, int MaxNumConfigs) {
INT_CLASS_STRUCT::INT_CLASS_STRUCT(int MaxNumProtos, int MaxNumConfigs) :
NumProtos(0),
NumProtoSets((MaxNumProtos + PROTOS_PER_PROTO_SET - 1) / PROTOS_PER_PROTO_SET),
NumConfigs(0),
ProtoLengths(MaxNumIntProtosIn(this))
{
assert(MaxNumConfigs <= MAX_NUM_CONFIGS);
NumProtoSets = ((MaxNumProtos + PROTOS_PER_PROTO_SET - 1) / PROTOS_PER_PROTO_SET);

assert(NumProtoSets <= MAX_NUM_PROTO_SETS);

NumProtos = 0;
NumConfigs = 0;

for (int i = 0; i < NumProtoSets; i++) {
/* allocate space for a proto set, install in class, and initialize */
auto ProtoSet = new PROTO_SET_STRUCT;
Expand All @@ -596,20 +593,13 @@ INT_CLASS_STRUCT::INT_CLASS_STRUCT(int MaxNumProtos, int MaxNumConfigs) {

/* allocate space for the proto lengths and install in class */
}
if (MaxNumIntProtosIn(this) > 0) {
ProtoLengths = new uint8_t[MaxNumIntProtosIn(this)];
memset(ProtoLengths, 0, MaxNumIntProtosIn(this) * sizeof(*ProtoLengths));
} else {
ProtoLengths = nullptr;
}
memset(ConfigLengths, 0, sizeof(ConfigLengths));
}

INT_CLASS_STRUCT::~INT_CLASS_STRUCT() {
for (int i = 0; i < NumProtoSets; i++) {
delete ProtoSets[i];
}
delete[] ProtoLengths;
}

/// This constructor allocates a new set of integer templates
Expand Down Expand Up @@ -647,7 +637,6 @@ INT_TEMPLATES_STRUCT *Classify::ReadIntTemplates(TFile *fp) {
INT_TEMPLATES_STRUCT *Templates;
CLASS_PRUNER_STRUCT *Pruner;
INT_CLASS_STRUCT *Class;
uint8_t *Lengths;

/* variables for conversion from older inttemp formats */
int b, bit_number, last_cp_bit_number, new_b, new_i, new_w;
Expand Down Expand Up @@ -797,15 +786,14 @@ INT_TEMPLATES_STRUCT *Classify::ReadIntTemplates(TFile *fp) {
}

/* then read in the proto lengths */
Lengths = nullptr;
Class->ProtoLengths.clear();
if (MaxNumIntProtosIn(Class) > 0) {
Lengths = new uint8_t[MaxNumIntProtosIn(Class)];
if (fp->FRead(Lengths, sizeof(uint8_t), MaxNumIntProtosIn(Class)) !=
Class->ProtoLengths.resize(MaxNumIntProtosIn(Class));
if (fp->FRead(&Class->ProtoLengths[0], sizeof(uint8_t), MaxNumIntProtosIn(Class)) !=
MaxNumIntProtosIn(Class)) {
tprintf("Bad read of inttemp!\n");
}
}
Class->ProtoLengths = Lengths;

/* then read in the proto sets */
for (j = 0; j < Class->NumProtoSets; j++) {
Expand Down Expand Up @@ -970,7 +958,7 @@ void Classify::WriteIntTemplates(FILE *File, INT_TEMPLATES_STRUCT *Templates,

/* then write out the proto lengths */
if (MaxNumIntProtosIn(Class) > 0) {
fwrite(Class->ProtoLengths, sizeof(uint8_t), MaxNumIntProtosIn(Class), File);
fwrite(&Class->ProtoLengths[0], sizeof(uint8_t), MaxNumIntProtosIn(Class), File);
}

/* then write out the proto sets */
Expand Down
2 changes: 1 addition & 1 deletion src/classify/intproto.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ struct INT_CLASS_STRUCT {
uint8_t NumProtoSets;
uint8_t NumConfigs;
PROTO_SET_STRUCT *ProtoSets[MAX_NUM_PROTO_SETS];
uint8_t *ProtoLengths;
std::vector<uint8_t> ProtoLengths;
uint16_t ConfigLengths[MAX_NUM_CONFIGS];
int font_set_id; // FontSet id, see above
};
Expand Down

0 comments on commit 0c3d244

Please sign in to comment.