Skip to content

Commit

Permalink
Merge pull request #744 from stweil/malloc
Browse files Browse the repository at this point in the history
Replace alloc_struct, free_struct
  • Loading branch information
zdenop authored Apr 30, 2017
2 parents 7a116ce + f8fba59 commit eaf5629
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 57 deletions.
17 changes: 7 additions & 10 deletions ccstruct/rejctmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ void REJ::full_print(FILE *fp) {
}


//The REJMAP class has been hacked to use alloc_struct instead of new [].
//The REJMAP class has been hacked to use malloc instead of new [].
//This is to reduce memory fragmentation only as it is rather kludgy.
// alloc_struct by-passes the call to the constructor of REJ on each
// malloc by-passes the call to the constructor of REJ on each
// array element. Although the constructor is empty, the BITS16 members
// do have a constructor which sets all the flags to 0. The memset
// replaces this functionality.
Expand All @@ -281,7 +281,7 @@ REJMAP::REJMAP( //classwise copy
len = source.length ();

if (len > 0) {
ptr = (REJ *) alloc_struct (len * sizeof (REJ), "REJ");
ptr = (REJ *) malloc(len * sizeof (REJ));
to = ptr;
for (i = 0; i < len; i++) {
*to = *from;
Expand Down Expand Up @@ -317,12 +317,10 @@ const REJMAP & source //from this

void REJMAP::initialise( //Redefine map
inT16 length) {
if (ptr != NULL)
free_struct (ptr, len * sizeof (REJ), "REJ");
free(ptr);
len = length;
if (len > 0)
ptr = (REJ *) memset (alloc_struct (len * sizeof (REJ), "REJ"),
0, len * sizeof (REJ));
ptr = (REJ *) calloc(len, sizeof(REJ));
else
ptr = NULL;
}
Expand Down Expand Up @@ -374,8 +372,7 @@ void REJMAP::remove_pos( //Cut out an element

len--;
if (len > 0)
new_ptr = (REJ *) memset (alloc_struct (len * sizeof (REJ), "REJ"),
0, len * sizeof (REJ));
new_ptr = (REJ *) malloc(len * sizeof(REJ));
else
new_ptr = NULL;

Expand All @@ -386,7 +383,7 @@ void REJMAP::remove_pos( //Cut out an element
new_ptr[pos] = ptr[pos + 1]; //copy post pos

//delete old map
free_struct (ptr, (len + 1) * sizeof (REJ), "REJ");
free(ptr);
ptr = new_ptr;
}

Expand Down
3 changes: 1 addition & 2 deletions ccstruct/rejctmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ class REJMAP
const REJMAP & source); //from this

~REJMAP () { //destructor
if (ptr != NULL)
free_struct (ptr, len * sizeof (REJ), "REJ");
free(ptr);
}

void initialise( //Redefine map
Expand Down
8 changes: 0 additions & 8 deletions ccutil/memry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ void free_string(char *string) {
free(string);
}

void* alloc_struct(inT32 count, const char *) {
return malloc(count);
}

void free_struct(void *deadstruct, inT32, const char *) {
free(deadstruct);
}

void *alloc_mem(inT32 count) {
return malloc(static_cast<size_t>(count));
}
Expand Down
4 changes: 0 additions & 4 deletions ccutil/memry.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
extern char *alloc_string(inT32 count);
// free a string.
extern void free_string(char *string);
// allocate memory
extern void *alloc_struct(inT32 count, const char *name = NULL);
// free a structure.
extern void free_struct(void *deadstruct, inT32, const char *name = NULL);
// get some memory
extern void *alloc_mem(inT32 count);
// get some memory initialized to 0.
Expand Down
28 changes: 8 additions & 20 deletions classify/adaptive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,21 @@ void FreeTempConfig(TEMP_CONFIG Config) {

destroy_nodes (Config->ContextsSeen, memfree);
FreeBitVector (Config->Protos);
free_struct (Config, sizeof (TEMP_CONFIG_STRUCT), "TEMP_CONFIG_STRUCT");
free(Config);

} /* FreeTempConfig */

/*---------------------------------------------------------------------------*/
void FreeTempProto(void *arg) {
PROTO proto = (PROTO) arg;

free_struct (proto, sizeof (TEMP_PROTO_STRUCT), "TEMP_PROTO_STRUCT");
free(proto);
}

void FreePermConfig(PERM_CONFIG Config) {
assert(Config != NULL);
delete [] Config->Ambigs;
free_struct(Config, sizeof(PERM_CONFIG_STRUCT), "PERM_CONFIG_STRUCT");
free(Config);
}

/*---------------------------------------------------------------------------*/
Expand Down Expand Up @@ -223,12 +223,9 @@ void free_adapted_templates(ADAPT_TEMPLATES templates) {
* @note History: Thu Mar 14 13:28:21 1991, DSJ, Created.
*/
TEMP_CONFIG NewTempConfig(int MaxProtoId, int FontinfoId) {
TEMP_CONFIG Config;
int NumProtos = MaxProtoId + 1;

Config =
(TEMP_CONFIG) alloc_struct (sizeof (TEMP_CONFIG_STRUCT),
"TEMP_CONFIG_STRUCT");
TEMP_CONFIG Config = (TEMP_CONFIG) malloc(sizeof(TEMP_CONFIG_STRUCT));
Config->Protos = NewBitVector (NumProtos);

Config->NumTimesSeen = 1;
Expand All @@ -254,8 +251,7 @@ TEMP_CONFIG NewTempConfig(int MaxProtoId, int FontinfoId) {
* @note History: Thu Mar 14 13:31:31 1991, DSJ, Created.
*/
TEMP_PROTO NewTempProto() {
return ((TEMP_PROTO)
alloc_struct (sizeof (TEMP_PROTO_STRUCT), "TEMP_PROTO_STRUCT"));
return (TEMP_PROTO) malloc(sizeof(TEMP_PROTO_STRUCT));
} /* NewTempProto */


Expand Down Expand Up @@ -317,7 +313,6 @@ ADAPT_CLASS ReadAdaptedClass(TFile *fp) {
int NumConfigs;
int i;
ADAPT_CLASS Class;
TEMP_PROTO TempProto;

/* first read high level adapted class structure */
Class = (ADAPT_CLASS) Emalloc (sizeof (ADAPT_CLASS_STRUCT));
Expand All @@ -335,9 +330,7 @@ ADAPT_CLASS ReadAdaptedClass(TFile *fp) {
fp->FRead(&NumTempProtos, sizeof(int), 1);
Class->TempProtos = NIL_LIST;
for (i = 0; i < NumTempProtos; i++) {
TempProto =
(TEMP_PROTO) alloc_struct (sizeof (TEMP_PROTO_STRUCT),
"TEMP_PROTO_STRUCT");
TEMP_PROTO TempProto = (TEMP_PROTO) malloc(sizeof(TEMP_PROTO_STRUCT));
fp->FRead(TempProto, sizeof(TEMP_PROTO_STRUCT), 1);
Class->TempProtos = push_last (Class->TempProtos, TempProto);
}
Expand Down Expand Up @@ -402,8 +395,7 @@ ADAPT_TEMPLATES Classify::ReadAdaptedTemplates(TFile *fp) {
* @note History: Tue Mar 19 14:25:26 1991, DSJ, Created.
*/
PERM_CONFIG ReadPermConfig(TFile *fp) {
PERM_CONFIG Config = (PERM_CONFIG) alloc_struct(sizeof(PERM_CONFIG_STRUCT),
"PERM_CONFIG_STRUCT");
PERM_CONFIG Config = (PERM_CONFIG) malloc(sizeof(PERM_CONFIG_STRUCT));
uinT8 NumAmbigs;
fp->FRead(&NumAmbigs, sizeof(uinT8), 1);
Config->Ambigs = new UNICHAR_ID[NumAmbigs + 1];
Expand All @@ -429,11 +421,7 @@ PERM_CONFIG ReadPermConfig(TFile *fp) {
* @note History: Tue Mar 19 14:29:59 1991, DSJ, Created.
*/
TEMP_CONFIG ReadTempConfig(TFile *fp) {
TEMP_CONFIG Config;

Config =
(TEMP_CONFIG) alloc_struct (sizeof (TEMP_CONFIG_STRUCT),
"TEMP_CONFIG_STRUCT");
TEMP_CONFIG Config = (TEMP_CONFIG) malloc(sizeof(TEMP_CONFIG_STRUCT));
fp->FRead(Config, sizeof(TEMP_CONFIG_STRUCT), 1);

Config->Protos = NewBitVector (Config->ProtoVectorSize * BITSINLONG);
Expand Down
3 changes: 1 addition & 2 deletions classify/adaptmatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1980,8 +1980,7 @@ void Classify::MakePermanent(ADAPT_TEMPLATES Templates,

// Initialize permanent config.
Ambigs = GetAmbiguities(Blob, ClassId);
PERM_CONFIG Perm = (PERM_CONFIG) alloc_struct(sizeof(PERM_CONFIG_STRUCT),
"PERM_CONFIG_STRUCT");
PERM_CONFIG Perm = (PERM_CONFIG) malloc(sizeof(PERM_CONFIG_STRUCT));
Perm->Ambigs = Ambigs;
Perm->FontinfoId = Config->FontinfoId;

Expand Down
4 changes: 2 additions & 2 deletions classify/mfoutline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ void FreeMFOutline(void *arg) { //MFOUTLINE Outline
Start = list_rest (Outline);
set_rest(Outline, NIL_LIST);
while (Start != NULL) {
free_struct (first_node (Start), sizeof (MFEDGEPT), "MFEDGEPT");
free(first_node(Start));
Start = pop (Start);
}

Expand Down Expand Up @@ -219,7 +219,7 @@ void MarkDirectionChanges(MFOUTLINE Outline) {
/*---------------------------------------------------------------------------*/
/** Return a new edge point for a micro-feature outline. */
MFEDGEPT *NewEdgePoint() {
return ((MFEDGEPT *) alloc_struct(sizeof(MFEDGEPT), "MFEDGEPT"));
return (MFEDGEPT *) malloc(sizeof(MFEDGEPT));
}


Expand Down
12 changes: 3 additions & 9 deletions classify/ocrfeatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,7 @@ BOOL8 AddFeature(FEATURE_SET FeatureSet, FEATURE Feature) {
* @note History: Mon May 21 13:33:27 1990, DSJ, Created.
*/
void FreeFeature(FEATURE Feature) {
if (Feature) {
free_struct (Feature, sizeof (FEATURE_STRUCT)
+ sizeof (FLOAT32) * (Feature->Type->NumParams - 1),
"sizeof(FEATURE_STRUCT)+sizeof(FLOAT32)*(NumParamsIn(Feature)-1)");
}

free(Feature);
} /* FreeFeature */

/**
Expand Down Expand Up @@ -94,10 +89,9 @@ void FreeFeatureSet(FEATURE_SET FeatureSet) {
FEATURE NewFeature(const FEATURE_DESC_STRUCT* FeatureDesc) {
FEATURE Feature;

Feature = (FEATURE) alloc_struct (sizeof (FEATURE_STRUCT) +
Feature = (FEATURE) malloc(sizeof(FEATURE_STRUCT) +
(FeatureDesc->NumParams - 1) *
sizeof (FLOAT32),
"sizeof(FEATURE_STRUCT)+sizeof(FLOAT32)*(NumParamsIn(Feature)-1)");
sizeof (FLOAT32));
Feature->Type = FeatureDesc;
return (Feature);

Expand Down

0 comments on commit eaf5629

Please sign in to comment.