tesseract  4.00.00dev
emalloc.cpp
Go to the documentation of this file.
1 /**************************************************************************
2  * Filename:
3  emalloc.c
4 ** Purpose:
5  Routines for trapping memory allocation errors.
6 ** Author:
7  Dan Johnson
8  HP-UX 6.2
9  HP-UX 6.2
10 ** History:
11  4/3/89, DSJ, Created.
12 **
13 ** (c) Copyright Hewlett-Packard Company, 1988.
14 ** Licensed under the Apache License, Version 2.0 (the "License");
15 ** you may not use this file except in compliance with the License.
16 ** You may obtain a copy of the License at
17 ** http://www.apache.org/licenses/LICENSE-2.0
18 ** Unless required by applicable law or agreed to in writing, software
19 ** distributed under the License is distributed on an "AS IS" BASIS,
20 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 ** See the License for the specific language governing permissions and
22 ** limitations under the License.
23 ******************************************************************************/
24 /*----------------------------------------------------------------------------
25  Include Files and Type Defines
26 ----------------------------------------------------------------------------*/
27 #include "emalloc.h"
28 #include "danerror.h"
29 #include <stdlib.h>
30 
31 /*----------------------------------------------------------------------------
32  Public Code
33 ----------------------------------------------------------------------------*/
47 void *Emalloc(int Size) {
48  void *Buffer;
49 
50  if (Size <= 0)
51  DoError (ILLEGALMALLOCREQUEST, "Illegal malloc request size");
52  Buffer = (void *) malloc (Size);
53  if (Buffer == NULL) {
54  DoError (NOTENOUGHMEMORY, "Not enough memory");
55  return (NULL);
56  }
57  else
58  return (Buffer);
59 
60 } /* Emalloc */
61 
62 
63 /*---------------------------------------------------------------------------*/
64 void *Erealloc(void *ptr, int size) {
65  void *Buffer;
66 
67  if (size < 0 || (size == 0 && ptr == NULL))
68  DoError (ILLEGALMALLOCREQUEST, "Illegal realloc request size");
69 
70  Buffer = (void *) realloc (ptr, size);
71  if (Buffer == NULL && size != 0)
72  DoError (NOTENOUGHMEMORY, "Not enough memory");
73  return (Buffer);
74 
75 } /* Erealloc */
76 
77 
78 /*---------------------------------------------------------------------------*/
79 void Efree(void *ptr) {
80  if (ptr == NULL)
81  DoError (ILLEGALMALLOCREQUEST, "Attempted to free NULL ptr");
82 
83  free(ptr);
84 
85 } /* Efree */
#define ILLEGALMALLOCREQUEST
Definition: emalloc.h:28
voidpf void uLong size
Definition: ioapi.h:39
void * Emalloc(int Size)
Definition: emalloc.cpp:47
#define NOTENOUGHMEMORY
Definition: emalloc.h:27
void * Erealloc(void *ptr, int size)
Definition: emalloc.cpp:64
void DoError(int Error, const char *Message)
Definition: danerror.cpp:42
void Efree(void *ptr)
Definition: emalloc.cpp:79