Tesseract  3.02
tesseract-ocr/cutil/emalloc.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  **                                                     Filename:
00003               emalloc.c
00004 **                                                      Purpose:
00005               Routines for trapping memory allocation errors.
00006 **                                                      Author:
00007               Dan Johnson
00008               HP-UX 6.2
00009               HP-UX 6.2
00010 **                                                      History:
00011               4/3/89, DSJ, Created.
00012 **
00013 **      (c) Copyright Hewlett-Packard Company, 1988.
00014 ** Licensed under the Apache License, Version 2.0 (the "License");
00015 ** you may not use this file except in compliance with the License.
00016 ** You may obtain a copy of the License at
00017 ** http://www.apache.org/licenses/LICENSE-2.0
00018 ** Unless required by applicable law or agreed to in writing, software
00019 ** distributed under the License is distributed on an "AS IS" BASIS,
00020 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00021 ** See the License for the specific language governing permissions and
00022 ** limitations under the License.
00023 ******************************************************************************/
00027 #include "emalloc.h"
00028 #include "danerror.h"
00029 #include <stdlib.h>
00030 
00034 /*---------------------------------------------------------------------------*/
00035 void *Emalloc(size_t Size) { 
00036 /*
00037  **                                                     Parameters:
00038  **                                                     Size
00039               number of bytes of memory to be allocated
00040 **                                                      Globals: none
00041 **                                                      Operation:
00042 **                                                      This routine attempts to allocate the specified number of
00043 **                                                      bytes.  If the memory can be allocated, a pointer to the
00044 **                                                      memory is returned.  If the memory cannot be allocated, or
00045 **                                                      if the allocation request is negative or zero,
00046 **                                                      an error is trapped.
00047 **                                                      Return: Pointer to allocated memory.
00048 **                                                      Exceptions: NOTENOUGHMEMORY
00049               unable to allocate Size bytes
00050 **                                                      ILLEGALMALLOCREQUEST
00051               negative or zero request size
00052 **                                                      History: 4/3/89, DSJ, Created.
00053 */
00054   void *Buffer;
00055 
00056   if (Size <= 0)
00057     DoError (ILLEGALMALLOCREQUEST, "Illegal malloc request size");
00058   Buffer = (void *) malloc (Size);
00059   if (Buffer == NULL) {
00060     DoError (NOTENOUGHMEMORY, "Not enough memory");
00061     return (NULL);
00062   }
00063   else
00064     return (Buffer);
00065 
00066 }                                /* Emalloc */
00067 
00068 
00069 /*---------------------------------------------------------------------------*/
00070 void *Erealloc(void *ptr, size_t size) { 
00071   void *Buffer;
00072 
00073   if (size < 0 || (size == 0 && ptr == NULL))
00074     DoError (ILLEGALMALLOCREQUEST, "Illegal realloc request size");
00075 
00076   Buffer = (void *) realloc (ptr, size);
00077   if (Buffer == NULL && size != 0)
00078     DoError (NOTENOUGHMEMORY, "Not enough memory");
00079   return (Buffer);
00080 
00081 }                                /* Erealloc */
00082 
00083 
00084 /*---------------------------------------------------------------------------*/
00085 void Efree(void *ptr) { 
00086   if (ptr == NULL)
00087     DoError (ILLEGALMALLOCREQUEST, "Attempted to free NULL ptr");
00088 
00089   free(ptr); 
00090 
00091 }                                /* Efree */