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

Issue 1199: Remove the vestigal traces of custom memory allocators, part 1, updated #11

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion ccmain/adaptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <string.h>
#include "tessbox.h"
#include "tessvars.h"
#include "memry.h"
#include "reject.h"
#include "control.h"
#include "stopper.h"
Expand Down
10 changes: 5 additions & 5 deletions ccstruct/coutln.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ICOORD top_right, inT16 length //length of loop
return;
}
//get memory
steps = (uinT8 *) alloc_mem (step_mem());
steps = (uinT8 *) malloc (step_mem());
memset(steps, 0, step_mem());
edgept = startpt;

Expand Down Expand Up @@ -94,7 +94,7 @@ inT16 length //length of loop
pos = startpt;
stepcount = length; // No. of steps.
ASSERT_HOST(length >= 0);
steps = reinterpret_cast<uinT8*>(alloc_mem(step_mem())); // Get memory.
steps = reinterpret_cast<uinT8*>(malloc(step_mem())); // Get memory.
memset(steps, 0, step_mem());

lastdir = new_steps[length - 1];
Expand Down Expand Up @@ -159,7 +159,7 @@ C_OUTLINE::C_OUTLINE( //constructor
return;
}
//get memory
steps = (uinT8 *) alloc_mem (step_mem());
steps = (uinT8 *) malloc (step_mem());
memset(steps, 0, step_mem());

for (int iteration = 0; iteration < 2; ++iteration) {
Expand Down Expand Up @@ -1003,9 +1003,9 @@ const C_OUTLINE & source //from this
box = source.box;
start = source.start;
if (steps != NULL)
free_mem(steps);
free(steps);
stepcount = source.stepcount;
steps = (uinT8 *) alloc_mem (step_mem());
steps = (uinT8 *) malloc (step_mem());
memmove (steps, source.steps, step_mem());
if (!children.empty ())
children.clear ();
Expand Down
2 changes: 1 addition & 1 deletion ccstruct/coutln.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class DLLSYM C_OUTLINE:public ELIST_LINK {

~C_OUTLINE () { //destructor
if (steps != NULL)
free_mem(steps);
free (steps);
steps = NULL;
delete [] offsets;
}
Expand Down
33 changes: 16 additions & 17 deletions ccstruct/quspline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
**********************************************************************/

#include "allheaders.h"
#include "memry.h"
#include "quadlsq.h"
#include "quspline.h"

Expand All @@ -43,8 +42,8 @@ QSPLINE::QSPLINE( //constructor
inT32 index; //segment index

//get memory
xcoords = (inT32 *) alloc_mem ((count + 1) * sizeof (inT32));
quadratics = (QUAD_COEFFS *) alloc_mem (count * sizeof (QUAD_COEFFS));
xcoords = (inT32 *) malloc ((count + 1) * sizeof (inT32));
quadratics = (QUAD_COEFFS *) malloc (count * sizeof (QUAD_COEFFS));
segments = count;
for (index = 0; index < segments; index++) {
//copy them
Expand Down Expand Up @@ -77,9 +76,9 @@ int degree //fit required
QLSQ qlsq; /*accumulator */

segments = segcount;
xcoords = (inT32 *) alloc_mem ((segcount + 1) * sizeof (inT32));
ptcounts = (inT32 *) alloc_mem ((segcount + 1) * sizeof (inT32));
quadratics = (QUAD_COEFFS *) alloc_mem (segcount * sizeof (QUAD_COEFFS));
xcoords = (inT32 *) malloc ((segcount + 1) * sizeof (inT32));
ptcounts = (inT32 *) malloc ((segcount + 1) * sizeof (inT32));
quadratics = (QUAD_COEFFS *) malloc (segcount * sizeof (QUAD_COEFFS));
memmove (xcoords, xstarts, (segcount + 1) * sizeof (inT32));
ptcounts[0] = 0; /*none in any yet */
for (segment = 0, pointindex = 0; pointindex < pointcount; pointindex++) {
Expand Down Expand Up @@ -123,7 +122,7 @@ int degree //fit required
quadratics[segment].b = qlsq.get_b ();
quadratics[segment].c = qlsq.get_c ();
}
free_mem(ptcounts);
free(ptcounts);
}


Expand Down Expand Up @@ -151,11 +150,11 @@ QSPLINE::QSPLINE( //constructor
QSPLINE::~QSPLINE ( //constructor
) {
if (xcoords != NULL) {
free_mem(xcoords);
free(xcoords);
xcoords = NULL;
}
if (quadratics != NULL) {
free_mem(quadratics);
free(quadratics);
quadratics = NULL;
}
}
Expand All @@ -170,13 +169,13 @@ QSPLINE::~QSPLINE ( //constructor
QSPLINE & QSPLINE::operator= ( //assignment
const QSPLINE & source) {
if (xcoords != NULL)
free_mem(xcoords);
free(xcoords);
if (quadratics != NULL)
free_mem(quadratics);
free(quadratics);

segments = source.segments;
xcoords = (inT32 *) alloc_mem ((segments + 1) * sizeof (inT32));
quadratics = (QUAD_COEFFS *) alloc_mem (segments * sizeof (QUAD_COEFFS));
xcoords = (inT32 *) malloc ((segments + 1) * sizeof (inT32));
quadratics = (QUAD_COEFFS *) malloc (segments * sizeof (QUAD_COEFFS));
memmove (xcoords, source.xcoords, (segments + 1) * sizeof (inT32));
memmove (quadratics, source.quadratics, segments * sizeof (QUAD_COEFFS));
return *this;
Expand Down Expand Up @@ -319,9 +318,9 @@ void QSPLINE::extrapolate( //linear extrapolation
increment++;
if (increment == 0)
return;
xstarts = (int *) alloc_mem ((segments + 1 + increment) * sizeof (int));
xstarts = (int *) malloc ((segments + 1 + increment) * sizeof (int));
quads =
(QUAD_COEFFS *) alloc_mem ((segments + increment) * sizeof (QUAD_COEFFS));
(QUAD_COEFFS *) malloc ((segments + increment) * sizeof (QUAD_COEFFS));
if (xmin < xcoords[0]) {
xstarts[0] = xmin;
quads[0].a = 0;
Expand All @@ -346,8 +345,8 @@ void QSPLINE::extrapolate( //linear extrapolation
xstarts[dest_segment] = xmax + 1;
}
segments = dest_segment;
free_mem(xcoords);
free_mem(quadratics);
free(xcoords);
free(quadratics);
xcoords = (inT32 *) xstarts;
quadratics = quads;
}
Expand Down
1 change: 0 additions & 1 deletion ccstruct/quspline.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

#include "quadratc.h"
#include "serialis.h"
#include "memry.h"
#include "rect.h"

class ROW;
Expand Down
19 changes: 5 additions & 14 deletions ccstruct/rejctmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,13 +265,6 @@ void REJ::full_print(FILE *fp) {
}


//The REJMAP class has been hacked to use alloc_struct instead of new [].
//This is to reduce memory fragmentation only as it is rather kludgy.
//alloc_struct by-passes the call to the contsructor 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.

REJMAP::REJMAP( //classwise copy
const REJMAP &source) {
REJ *to;
Expand All @@ -281,7 +274,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 @@ -318,11 +311,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 +366,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 *) calloc(len, sizeof (REJ));
else
new_ptr = NULL;

Expand All @@ -386,7 +377,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 @@ -44,7 +44,6 @@ OF THIS IMPLIED TEMPORAL ORDERING OF THE FLAGS!!!!
#ifdef __UNIX__
#include <assert.h>
#endif
#include "memry.h"
#include "bits16.h"
#include "params.h"

Expand Down Expand Up @@ -221,7 +220,7 @@ class REJMAP

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

void initialise( //Redefine map
Expand Down
4 changes: 2 additions & 2 deletions ccutil/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ AM_CPPFLAGS += -DTESS_EXPORTS
endif

include_HEADERS = \
basedir.h errcode.h fileerr.h genericvector.h helpers.h host.h memry.h \
basedir.h errcode.h fileerr.h genericvector.h helpers.h host.h \
ndminx.h params.h ocrclass.h platform.h serialis.h strngs.h \
tesscallback.h unichar.h unicharmap.h unicharset.h

Expand All @@ -35,7 +35,7 @@ libtesseract_ccutil_la_SOURCES = \
ccutil.cpp clst.cpp \
elst2.cpp elst.cpp errcode.cpp \
globaloc.cpp indexmapbidi.cpp \
mainblk.cpp memry.cpp \
mainblk.cpp \
serialis.cpp strngs.cpp scanutils.cpp \
tessdatamanager.cpp tprintf.cpp \
unichar.cpp unicharmap.cpp unicharset.cpp unicodes.cpp \
Expand Down
61 changes: 0 additions & 61 deletions ccutil/memry.cpp
Original file line number Diff line number Diff line change
@@ -1,61 +0,0 @@
/**********************************************************************
* File: memry.c (Formerly memory.c)
* Description: Memory allocation with builtin safety checks.
* Author: Ray Smith
* Created: Wed Jan 22 09:43:33 GMT 1992
*
* (C) Copyright 1992, Hewlett-Packard Ltd.
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
** http://www.apache.org/licenses/LICENSE-2.0
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*
**********************************************************************/

#include "memry.h"
#include <stdlib.h>

// With improvements in OS memory allocators, internal memory management
// is no longer required, so all these functions now map to their malloc
// family equivalents.

// TODO(rays) further cleanup by redirecting calls to new and creating proper
// constructors.

char *alloc_string(inT32 count) {
// Round up the amount allocated to a multiple of 4
return static_cast<char*>(malloc((count + 3) & ~3));
}

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));
}

void *alloc_big_zeros(inT32 count) {
return calloc(static_cast<size_t>(count), 1);
}

void free_mem(void *oldchunk) {
free(oldchunk);
}

void free_big_mem(void *oldchunk) {
free(oldchunk);
}
43 changes: 0 additions & 43 deletions ccutil/memry.h
Original file line number Diff line number Diff line change
@@ -1,43 +0,0 @@
/**********************************************************************
* File: memry.h (Formerly memory.h)
* Description: Header file for basic memory allocation/deallocation.
* Author: Ray Smith
* Created: Tue May 8 16:03:48 BST 1990
*
* (C) Copyright 1990, Hewlett-Packard Ltd.
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
** http://www.apache.org/licenses/LICENSE-2.0
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*
**********************************************************************/

#ifndef MEMRY_H
#define MEMRY_H

#include <stddef.h>
#include "host.h"

// allocate string
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.
extern void *alloc_big_zeros(inT32 count);
// free mem from alloc_mem
extern void free_mem(void *oldchunk);
// free mem from alloc_big_zeros
extern void free_big_mem(void *oldchunk);

#endif
6 changes: 3 additions & 3 deletions ccutil/strngs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const int kMaxDoubleSize = 15;
const int kMinCapacity = 16;

char* STRING::AllocData(int used, int capacity) {
data_ = (STRING_HEADER *)alloc_string(capacity + sizeof(STRING_HEADER));
data_ = (STRING_HEADER *)malloc(capacity + sizeof(STRING_HEADER));

// header is the metadata for this memory block
STRING_HEADER* header = GetHeader();
Expand All @@ -62,7 +62,7 @@ char* STRING::AllocData(int used, int capacity) {
}

void STRING::DiscardData() {
free_string((char *)data_);
free(data_);
}

// This is a private method; ensure FixHeader is called (or used_ is well defined)
Expand All @@ -79,7 +79,7 @@ char* STRING::ensure_cstr(inT32 min_capacity) {
min_capacity = 2 * orig_header->capacity_;

int alloc = sizeof(STRING_HEADER) + min_capacity;
STRING_HEADER* new_header = (STRING_HEADER*)(alloc_string(alloc));
STRING_HEADER* new_header = (STRING_HEADER*)malloc(alloc);

memcpy(&new_header[1], GetCStr(), orig_header->used_);
new_header->capacity_ = min_capacity;
Expand Down
2 changes: 1 addition & 1 deletion ccutil/strngs.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <stdio.h>
#include <string.h>
#include "platform.h"
#include "memry.h"
#include "host.h"

namespace tesseract {
class TFile;
Expand Down