Skip to content

Commit

Permalink
Merge pull request #861 from stweil/malloc
Browse files Browse the repository at this point in the history
Remove freelist,c and freelist.h
  • Loading branch information
zdenop authored May 2, 2017
2 parents b9dff16 + 6bfde5d commit b8137d8
Show file tree
Hide file tree
Showing 12 changed files with 35 additions and 117 deletions.
7 changes: 0 additions & 7 deletions classify/adaptive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
----------------------------------------------------------------------------*/
#include "adaptive.h"
#include "emalloc.h"
#include "freelist.h"
#include "globals.h"
#include "classify.h"

Expand Down Expand Up @@ -81,11 +80,8 @@ void AddAdaptedClass(ADAPT_TEMPLATES Templates,
*/
void FreeTempConfig(TEMP_CONFIG Config) {
assert (Config != NULL);

destroy_nodes (Config->ContextsSeen, memfree);
FreeBitVector (Config->Protos);
free(Config);

} /* FreeTempConfig */

/*---------------------------------------------------------------------------*/
Expand Down Expand Up @@ -231,7 +227,6 @@ TEMP_CONFIG NewTempConfig(int MaxProtoId, int FontinfoId) {
Config->NumTimesSeen = 1;
Config->MaxProtoId = MaxProtoId;
Config->ProtoVectorSize = WordsInVectorOfSize (NumProtos);
Config->ContextsSeen = NIL_LIST;
zero_all_bits (Config->Protos, Config->ProtoVectorSize);
Config->FontinfoId = FontinfoId;

Expand Down Expand Up @@ -547,8 +542,6 @@ void WritePermConfig(FILE *File, PERM_CONFIG Config) {
*/
void WriteTempConfig(FILE *File, TEMP_CONFIG Config) {
assert (Config != NULL);
/* contexts not yet implemented */
assert (Config->ContextsSeen == NULL);

fwrite ((char *) Config, sizeof (TEMP_CONFIG_STRUCT), 1, File);
fwrite ((char *) Config->Protos, sizeof (uinT32),
Expand Down
1 change: 0 additions & 1 deletion classify/adaptive.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ typedef struct
uinT8 NumTimesSeen;
uinT8 ProtoVectorSize;
PROTO_ID MaxProtoId;
LIST ContextsSeen;
BIT_VECTOR Protos;
int FontinfoId; // font information inferred from pre-trained templates
} TEMP_CONFIG_STRUCT;
Expand Down
45 changes: 19 additions & 26 deletions classify/cluster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "matrix.h"
#include "tprintf.h"
#include "danerror.h"
#include "freelist.h"
#include <math.h>

#define HOTELLING 1 // If true use Hotelling's test to decide where to split.
Expand Down Expand Up @@ -546,7 +545,7 @@ LIST ClusterSamples(CLUSTERER *Clusterer, CLUSTERCONFIG *Config) {
*/
void FreeClusterer(CLUSTERER *Clusterer) {
if (Clusterer != NULL) {
memfree (Clusterer->ParamDesc);
free(Clusterer->ParamDesc);
if (Clusterer->KDTree != NULL)
FreeKDTree (Clusterer->KDTree);
if (Clusterer->Root != NULL)
Expand All @@ -558,7 +557,7 @@ void FreeClusterer(CLUSTERER *Clusterer) {
FreeBuckets(Clusterer->bucket_cache[d][c]);
}

memfree(Clusterer);
free(Clusterer);
}
} // FreeClusterer

Expand Down Expand Up @@ -593,19 +592,14 @@ void FreePrototype(void *arg) { //PROTOTYPE *Prototype)
Prototype->Cluster->Prototype = FALSE;

// deallocate the prototype statistics and then the prototype itself
if (Prototype->Distrib != NULL)
memfree (Prototype->Distrib);
if (Prototype->Mean != NULL)
memfree (Prototype->Mean);
free (Prototype->Distrib);
free (Prototype->Mean);
if (Prototype->Style != spherical) {
if (Prototype->Variance.Elliptical != NULL)
memfree (Prototype->Variance.Elliptical);
if (Prototype->Magnitude.Elliptical != NULL)
memfree (Prototype->Magnitude.Elliptical);
if (Prototype->Weight.Elliptical != NULL)
memfree (Prototype->Weight.Elliptical);
free (Prototype->Variance.Elliptical);
free (Prototype->Magnitude.Elliptical);
free (Prototype->Weight.Elliptical);
}
memfree(Prototype);
free(Prototype);
} // FreePrototype

/**
Expand Down Expand Up @@ -756,7 +750,7 @@ void CreateClusterTree(CLUSTERER *Clusterer) {
FreeKDTree(context.tree);
Clusterer->KDTree = NULL;
delete context.heap;
memfree(context.candidates);
free(context.candidates);
} // CreateClusterTree

/**
Expand Down Expand Up @@ -1173,9 +1167,9 @@ PROTOTYPE *TestEllipticalProto(CLUSTERER *Clusterer,
}
Tsq += Delta[x] * temp;
}
memfree(Covariance);
memfree(Inverse);
memfree(Delta);
free(Covariance);
free(Inverse);
free(Delta);
// Changed this function to match the formula in
// Statistical Methods in Medical Research p 473
// By Peter Armitage, Geoffrey Berry, J. N. S. Matthews.
Expand Down Expand Up @@ -1485,7 +1479,7 @@ ComputeStatistics (inT16 N, PARAM_DESC ParamDesc[], CLUSTER * Cluster) {
1.0 / N);

// release temporary memory and return
memfree(Distance);
free(Distance);
return (Statistics);
} // ComputeStatistics

Expand Down Expand Up @@ -2157,10 +2151,10 @@ BOOL8 DistributionOK(BUCKETS *Buckets) {
* @note History: 6/5/89, DSJ, Created.
*/
void FreeStatistics(STATISTICS *Statistics) {
memfree (Statistics->CoVariance);
memfree (Statistics->Min);
memfree (Statistics->Max);
memfree(Statistics);
free(Statistics->CoVariance);
free(Statistics->Min);
free(Statistics->Max);
free(Statistics);
} // FreeStatistics

/**
Expand Down Expand Up @@ -2190,7 +2184,7 @@ void FreeCluster(CLUSTER *Cluster) {
if (Cluster != NULL) {
FreeCluster (Cluster->Left);
FreeCluster (Cluster->Right);
memfree(Cluster);
free(Cluster);
}
} // FreeCluster

Expand Down Expand Up @@ -2487,8 +2481,7 @@ CLUSTER * Cluster, FLOAT32 MaxIllegal)
NumIllegalInCluster = 0;

if (Clusterer->NumChar > NumFlags) {
if (CharFlags != NULL)
memfree(CharFlags);
free(CharFlags);
NumFlags = Clusterer->NumChar;
CharFlags = (BOOL8 *) Emalloc (NumFlags * sizeof (BOOL8));
}
Expand Down
7 changes: 3 additions & 4 deletions classify/kdtree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "kdtree.h"
#include "const.h"
#include "emalloc.h"
#include "freelist.h"
#include <stdio.h>
#include <math.h>

Expand Down Expand Up @@ -349,7 +348,7 @@ void KDWalk(KDTREE *Tree, void_proc action, void *context) {
*/
void FreeKDTree(KDTREE *Tree) {
FreeSubTree(Tree->Root.Left);
memfree(Tree);
free(Tree);
} /* FreeKDTree */


Expand Down Expand Up @@ -389,7 +388,7 @@ KDNODE *MakeKDNode(KDTREE *tree, FLOAT32 Key[], void *Data, int Index) {

/*---------------------------------------------------------------------------*/
void FreeKDNode(KDNODE *Node) {
memfree ((char *)Node);
free(Node);
}


Expand Down Expand Up @@ -555,6 +554,6 @@ void FreeSubTree(KDNODE *sub_tree) {
if (sub_tree != NULL) {
FreeSubTree(sub_tree->Left);
FreeSubTree(sub_tree->Right);
memfree(sub_tree);
free(sub_tree);
}
}
3 changes: 1 addition & 2 deletions classify/ocrfeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "emalloc.h"
#include "callcpp.h"
#include "danerror.h"
#include "freelist.h"
#include "scanutils.h"

#include <assert.h>
Expand Down Expand Up @@ -75,7 +74,7 @@ void FreeFeatureSet(FEATURE_SET FeatureSet) {
if (FeatureSet) {
for (i = 0; i < FeatureSet->NumFeatures; i++)
FreeFeature(FeatureSet->Features[i]);
memfree(FeatureSet);
free(FeatureSet);
}
} /* FreeFeatureSet */

Expand Down
5 changes: 2 additions & 3 deletions classify/protos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include "protos.h"
#include "const.h"
#include "emalloc.h"
#include "freelist.h"
#include "callcpp.h"
#include "tprintf.h"
#include "scanutils.h"
Expand Down Expand Up @@ -230,11 +229,11 @@ void FreeClassFields(CLASS_TYPE Class) {

if (Class) {
if (Class->MaxNumProtos > 0)
memfree (Class->Prototypes);
free(Class->Prototypes);
if (Class->MaxNumConfigs > 0) {
for (i = 0; i < Class->NumConfigs; i++)
FreeBitVector (Class->Configurations[i]);
memfree (Class->Configurations);
free(Class->Configurations);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions cutil/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ endif

noinst_HEADERS = \
bitvec.h callcpp.h const.h cutil.h cutil_class.h danerror.h efio.h \
emalloc.h freelist.h globals.h \
emalloc.h globals.h \
oldlist.h structures.h

if !USING_MULTIPLELIBS
Expand All @@ -22,7 +22,5 @@ endif

libtesseract_cutil_la_SOURCES = \
bitvec.cpp callcpp.cpp cutil.cpp cutil_class.cpp danerror.cpp efio.cpp \
emalloc.cpp freelist.cpp \
emalloc.cpp \
oldlist.cpp structures.cpp


2 changes: 1 addition & 1 deletion cutil/cutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Import original HP distribution

#define CHARS_PER_LINE 500

#if defined(__STDC__) || defined(__cplusplus) || MAC_OR_DOS
#if defined(__STDC__) || defined(__cplusplus)
# define _ARGS(s) s
#else
# define _ARGS(s) ()
Expand Down
18 changes: 0 additions & 18 deletions cutil/freelist.cpp

This file was deleted.

31 changes: 0 additions & 31 deletions cutil/freelist.h

This file was deleted.

8 changes: 1 addition & 7 deletions cutil/oldlist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,6 @@
#include "oldlist.h"
#include "structures.h"
#include <stdio.h>
#if MAC_OR_DOS
#include <stdlib.h>
#else
#include "freelist.h"
#endif

/*----------------------------------------------------------------------
M a c r o s
Expand Down Expand Up @@ -202,8 +197,7 @@ LIST destroy(LIST list) {
* Return the space taken by the LISTs of a list to the heap.
**********************************************************************/
void destroy_nodes(LIST list, void_dest destructor) {
if (destructor == NULL)
destructor = memfree;
ASSERT_HOST(destructor != NULL);

while (list != NIL_LIST) {
if (first_node(list) != NULL) (*destructor)(first_node(list));
Expand Down
19 changes: 6 additions & 13 deletions training/commontraining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "emalloc.h"
#include "featdefs.h"
#include "fontinfo.h"
#include "freelist.h"
#include "globals.h"
#include "intfeaturespace.h"
#include "mastertrainer.h"
Expand Down Expand Up @@ -579,18 +578,12 @@ void CleanUpUnusedData(
iterate(ProtoList)
{
Prototype = (PROTOTYPE *) first_node (ProtoList);
if (Prototype->Variance.Elliptical != nullptr) {
memfree(Prototype->Variance.Elliptical);
Prototype->Variance.Elliptical = nullptr;
}
if (Prototype->Magnitude.Elliptical != nullptr) {
memfree(Prototype->Magnitude.Elliptical);
Prototype->Magnitude.Elliptical = nullptr;
}
if (Prototype->Weight.Elliptical != nullptr) {
memfree(Prototype->Weight.Elliptical);
Prototype->Weight.Elliptical = nullptr;
}
free(Prototype->Variance.Elliptical);
Prototype->Variance.Elliptical = nullptr;
free(Prototype->Magnitude.Elliptical);
Prototype->Magnitude.Elliptical = nullptr;
free(Prototype->Weight.Elliptical);
Prototype->Weight.Elliptical = nullptr;
}
}

Expand Down

2 comments on commit b8137d8

@theraysmith
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like this change at all. I'm not pulling it into the Google version.
It's just more incentive to work on deleting the legacy engine.
I commented on some issue related to this a while ago:
If you are going to replace these, do it properly with std::unique_ptr or std::array as appropriate, NOT malloc/free, or even new/delete.

@amitdo
Copy link
Collaborator

@amitdo amitdo commented on b8137d8 May 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I commented on some issue related to this a while ago:

I believe you refer to this comment:
#11 (review)

Please sign in to comment.