Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace remaining malloc / strdup / free #3368

Merged
merged 7 commits into from
Mar 29, 2021
Merged
2 changes: 1 addition & 1 deletion src/classify/intproto.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ struct INT_CLASS_STRUCT {
int font_set_id; // FontSet id, see above
};

struct INT_TEMPLATES_STRUCT {
struct TESS_API INT_TEMPLATES_STRUCT {
INT_TEMPLATES_STRUCT();
~INT_TEMPLATES_STRUCT();
int NumClasses;
Expand Down
6 changes: 3 additions & 3 deletions src/training/cntraining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ int main(int argc, char *argv[]) {
printf(
"0 significant protos for %s."
" Retrying clustering with MinSamples = %f%%\n",
CharSample->Label, Config.MinSamples);
CharSample->Label.c_str(), Config.MinSamples);
}
}
Config.MinSamples = SavedMinSamples;
Expand Down Expand Up @@ -216,11 +216,11 @@ static void WriteNormProtos(const char *Directory, LIST LabeledProtoList,
"\nError! Not enough protos for %s: %d protos"
" (%d significant protos"
", %d insignificant protos)\n",
LabeledProto->Label, N, NumberOfProtos(LabeledProto->List, true, false),
LabeledProto->Label.c_str(), N, NumberOfProtos(LabeledProto->List, true, false),
NumberOfProtos(LabeledProto->List, false, true));
exit(1);
}
fprintf(File, "\n%s %d\n", LabeledProto->Label, N);
fprintf(File, "\n%s %d\n", LabeledProto->Label.c_str(), N);
WriteProtos(File, feature_desc->NumParams, LabeledProto->List, true, false);
}
fclose(File);
Expand Down
4 changes: 2 additions & 2 deletions src/training/common/commandlineflags.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#define STRING_PARAM_FLAG(name, val, comment) STRING_VAR(FLAGS_##name, val, comment)
#define DECLARE_STRING_PARAM_FLAG(name) extern STRING_VAR_H(FLAGS_##name, "", "")

namespace tesseract {

// Flags from commontraining.cpp
// Command line arguments for font_properties, xheights and unicharset.
TESS_COMMON_TRAINING_API
Expand All @@ -55,8 +57,6 @@ DECLARE_STRING_PARAM_FLAG(output_trainer);
TESS_COMMON_TRAINING_API
DECLARE_STRING_PARAM_FLAG(test_ch);

namespace tesseract {

// Parse commandline flags and values. Prints the usage string and exits on
// input of --help or --version.
//
Expand Down
87 changes: 24 additions & 63 deletions src/training/common/commontraining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
# include "params.h"
# include "tprintf.h"

namespace tesseract {

INT_PARAM_FLAG(debug_level, 0, "Level of Trainer debugging");
INT_PARAM_FLAG(load_images, 0, "Load images with tr files");
STRING_PARAM_FLAG(configfile, "", "File to load more configs from");
Expand All @@ -35,8 +37,6 @@ STRING_PARAM_FLAG(fonts_dir, "",
"system default font location");
STRING_PARAM_FLAG(fontconfig_tmpdir, "/tmp", "Overrides fontconfig default temporary dir");

using namespace tesseract;

/**
* This routine parses the command line arguments that were
* passed to the program and uses them to set relevant
Expand All @@ -59,6 +59,8 @@ void ParseArguments(int *argc, char ***argv) {
tesseract::ParseCommandLineFlags(usage.c_str(), argc, argv, true);
}

} // namespace tesseract.

#else

# include <allheaders.h>
Expand All @@ -78,7 +80,7 @@ void ParseArguments(int *argc, char ***argv) {
# include "tprintf.h"
# include "unicity_table.h"

using namespace tesseract;
namespace tesseract {

// Global Variables.

Expand Down Expand Up @@ -142,7 +144,6 @@ void ParseArguments(int *argc, char ***argv) {
}
}

namespace tesseract {
// Helper loads shape table from the given file.
ShapeTable *LoadShapeTable(const std::string &file_prefix) {
ShapeTable *shape_table = nullptr;
Expand Down Expand Up @@ -290,8 +291,6 @@ std::unique_ptr<MasterTrainer> LoadTrainingData(int argc, const char *const *arg
return trainer;
}

} // namespace tesseract.

/*---------------------------------------------------------------------------*/
/**
* This routine returns the next command line argument. If
Expand Down Expand Up @@ -321,40 +320,19 @@ const char *GetNextFilename(int argc, const char *const *argv, int &tessoptind)
* @return Labeled list with the specified label or nullptr.
* @note Globals: none
*/
LABELEDLIST FindList(LIST List, char *Label) {
LABELEDLIST FindList(LIST List, const std::string &Label) {
LABELEDLIST LabeledList;

iterate(List) {
LabeledList = reinterpret_cast<LABELEDLIST> first_node(List);
if (strcmp(LabeledList->Label, Label) == 0) {
if (LabeledList->Label == Label) {
return (LabeledList);
}
}
return (nullptr);

} /* FindList */

/*---------------------------------------------------------------------------*/
/**
* This routine allocates a new, empty labeled list and gives
* it the specified label.
* @param Label label for new list
* @return New, empty labeled list.
* @note Globals: none
*/
LABELEDLIST NewLabeledList(const char *Label) {
LABELEDLIST LabeledList;

LabeledList = static_cast<LABELEDLIST>(malloc(sizeof(LABELEDLISTNODE)));
LabeledList->Label = static_cast<char *>(malloc(strlen(Label) + 1));
strcpy(LabeledList->Label, Label);
LabeledList->List = NIL_LIST;
LabeledList->SampleCount = 0;
LabeledList->font_sample_count = 0;
return (LabeledList);

} /* NewLabeledList */

/*---------------------------------------------------------------------------*/
// TODO(rays) This is now used only by cntraining. Convert cntraining to use
// the new method or get rid of it entirely.
Expand Down Expand Up @@ -403,7 +381,7 @@ void ReadTrainingSamples(const FEATURE_DEFS_STRUCT &feature_definitions, const c
}
char_sample = FindList(*training_samples, unichar);
if (char_sample == nullptr) {
char_sample = NewLabeledList(unichar);
char_sample = new LABELEDLISTNODE(unichar);
*training_samples = push(*training_samples, char_sample);
}
auto char_desc = ReadCharDescription(feature_definitions, file);
Expand All @@ -420,7 +398,7 @@ void ReadTrainingSamples(const FEATURE_DEFS_STRUCT &feature_definitions, const c
delete char_desc->FeatureSets[i];
}
}
free(char_desc);
delete char_desc;
}
} // ReadTrainingSamples

Expand Down Expand Up @@ -458,8 +436,7 @@ void FreeTrainingSamples(LIST CharList) {
*/
void FreeLabeledList(LABELEDLIST LabeledList) {
destroy(LabeledList->List);
free(LabeledList->Label);
free(LabeledList);
delete LabeledList;
} /* FreeLabeledList */

/*---------------------------------------------------------------------------*/
Expand All @@ -477,8 +454,6 @@ void FreeLabeledList(LABELEDLIST LabeledList) {
CLUSTERER *SetUpForClustering(const FEATURE_DEFS_STRUCT &FeatureDefs, LABELEDLIST char_sample,
const char *program_feature_type) {
uint16_t N;
int i, j;
float *Sample = nullptr;
CLUSTERER *Clusterer;
int32_t CharID;
LIST FeatureList = nullptr;
Expand All @@ -490,20 +465,20 @@ CLUSTERER *SetUpForClustering(const FEATURE_DEFS_STRUCT &FeatureDefs, LABELEDLIS

FeatureList = char_sample->List;
CharID = 0;
std::vector<float> Sample;
iterate(FeatureList) {
FeatureSet = reinterpret_cast<FEATURE_SET> first_node(FeatureList);
for (i = 0; i < FeatureSet->MaxNumFeatures; i++) {
if (Sample == nullptr) {
Sample = static_cast<float *>(malloc(N * sizeof(float)));
for (int i = 0; i < FeatureSet->MaxNumFeatures; i++) {
if (Sample.empty()) {
Sample.resize(N);
}
for (j = 0; j < N; j++) {
for (int j = 0; j < N; j++) {
Sample[j] = FeatureSet->Features[i]->Params[j];
}
MakeSample(Clusterer, Sample, CharID);
MakeSample(Clusterer, &Sample[0], CharID);
}
CharID++;
}
free(Sample);
return Clusterer;

} /* SetUpForClustering */
Expand Down Expand Up @@ -638,31 +613,19 @@ LIST RemoveInsignificantProtos(LIST ProtoList, bool KeepSigProtos, bool KeepInsi
} /* RemoveInsignificantProtos */

/*----------------------------------------------------------------------------*/
MERGE_CLASS FindClass(LIST List, const char *Label) {
MERGE_CLASS FindClass(LIST List, const std::string &Label) {
MERGE_CLASS MergeClass;

iterate(List) {
MergeClass = reinterpret_cast<MERGE_CLASS> first_node(List);
if (strcmp(MergeClass->Label, Label) == 0) {
if (MergeClass->Label == Label) {
return (MergeClass);
}
}
return (nullptr);

} /* FindClass */

/*---------------------------------------------------------------------------*/
MERGE_CLASS NewLabeledClass(const char *Label) {
MERGE_CLASS MergeClass;

MergeClass = new MERGE_CLASS_NODE;
MergeClass->Label = static_cast<char *>(malloc(strlen(Label) + 1));
strcpy(MergeClass->Label, Label);
MergeClass->Class = NewClass(MAX_NUM_PROTOS, MAX_NUM_CONFIGS);
return (MergeClass);

} /* NewLabeledClass */

/*-----------------------------------------------------------------------------*/
/**
* This routine deallocates all of the space allocated to
Expand All @@ -676,7 +639,6 @@ void FreeLabeledClassList(LIST ClassList) {
iterate(ClassList) /* iterate through all of the fonts */
{
MergeClass = reinterpret_cast<MERGE_CLASS> first_node(ClassList);
free(MergeClass->Label);
FreeClass(MergeClass->Class);
delete MergeClass;
}
Expand Down Expand Up @@ -704,7 +666,7 @@ CLASS_STRUCT *SetUpForFloat2Int(const UNICHARSET &unicharset, LIST LabeledClassL
iterate(LabeledClassList) {
UnicityTable<int> font_set;
MergeClass = reinterpret_cast<MERGE_CLASS> first_node(LabeledClassList);
Class = &float_classes[unicharset.unichar_to_id(MergeClass->Label)];
Class = &float_classes[unicharset.unichar_to_id(MergeClass->Label.c_str())];
NumProtos = MergeClass->Class->NumProtos;
NumConfigs = MergeClass->Class->NumConfigs;
font_set.move(&MergeClass->Class->font_set);
Expand Down Expand Up @@ -776,13 +738,10 @@ void FreeNormProtoList(LIST CharList)
} // FreeNormProtoList

/*---------------------------------------------------------------------------*/
void AddToNormProtosList(LIST *NormProtoList, LIST ProtoList, char *CharName) {
PROTOTYPE *Proto;
LABELEDLIST LabeledProtoList;

LabeledProtoList = NewLabeledList(CharName);
void AddToNormProtosList(LIST *NormProtoList, LIST ProtoList, const std::string &CharName) {
auto LabeledProtoList = new LABELEDLISTNODE(CharName.c_str());
iterate(ProtoList) {
Proto = reinterpret_cast<PROTOTYPE *> first_node(ProtoList);
auto Proto = reinterpret_cast<PROTOTYPE *> first_node(ProtoList);
LabeledProtoList->List = push(LabeledProtoList->List, Proto);
}
*NormProtoList = push(*NormProtoList, LabeledProtoList);
Expand All @@ -800,4 +759,6 @@ int NumberOfProtos(LIST ProtoList, bool CountSigProtos, bool CountInsigProtos) {
return (N);
}

} // namespace tesseract.

#endif // def DISABLED_LEGACY_ENGINE
Loading