2014-09-13 04:41:19 +08:00
|
|
|
/**************************************************************************
|
|
|
|
* Filename:
|
2007-03-08 04:03:40 +08:00
|
|
|
emalloc.c
|
2014-09-13 04:41:19 +08:00
|
|
|
** Purpose:
|
2007-03-08 04:03:40 +08:00
|
|
|
Routines for trapping memory allocation errors.
|
2014-09-13 04:41:19 +08:00
|
|
|
** Author:
|
2007-03-08 04:03:40 +08:00
|
|
|
Dan Johnson
|
|
|
|
HP-UX 6.2
|
|
|
|
HP-UX 6.2
|
2014-09-13 04:41:19 +08:00
|
|
|
** History:
|
2007-03-08 04:03:40 +08:00
|
|
|
4/3/89, DSJ, Created.
|
|
|
|
**
|
|
|
|
** (c) Copyright Hewlett-Packard Company, 1988.
|
|
|
|
** 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.
|
|
|
|
******************************************************************************/
|
2014-09-13 04:41:19 +08:00
|
|
|
/*----------------------------------------------------------------------------
|
2007-03-08 04:03:40 +08:00
|
|
|
Include Files and Type Defines
|
2014-09-13 04:41:19 +08:00
|
|
|
----------------------------------------------------------------------------*/
|
2007-03-08 04:03:40 +08:00
|
|
|
#include "emalloc.h"
|
|
|
|
#include "danerror.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-09-13 04:41:19 +08:00
|
|
|
/*----------------------------------------------------------------------------
|
2007-03-08 04:03:40 +08:00
|
|
|
Public Code
|
2014-09-13 04:41:19 +08:00
|
|
|
----------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* This routine attempts to allocate the specified number of
|
|
|
|
* bytes. If the memory can be allocated, a pointer to the
|
|
|
|
* memory is returned. If the memory cannot be allocated, or
|
|
|
|
* if the allocation request is negative or zero,
|
|
|
|
* an error is trapped.
|
|
|
|
* @param Size number of bytes of memory to be allocated
|
|
|
|
* @return Pointer to allocated memory.
|
|
|
|
* @note Exceptions:
|
|
|
|
* - #NOTENOUGHMEMORY unable to allocate Size bytes
|
|
|
|
* - #ILLEGALMALLOCREQUEST negative or zero request size
|
|
|
|
* @note History: 4/3/89, DSJ, Created.
|
2007-03-08 04:03:40 +08:00
|
|
|
*/
|
2014-09-13 04:41:19 +08:00
|
|
|
void *Emalloc(int Size) {
|
2007-03-08 04:03:40 +08:00
|
|
|
void *Buffer;
|
|
|
|
|
|
|
|
if (Size <= 0)
|
|
|
|
DoError (ILLEGALMALLOCREQUEST, "Illegal malloc request size");
|
|
|
|
Buffer = (void *) malloc (Size);
|
|
|
|
if (Buffer == NULL) {
|
|
|
|
DoError (NOTENOUGHMEMORY, "Not enough memory");
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return (Buffer);
|
|
|
|
|
|
|
|
} /* Emalloc */
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2014-01-10 09:38:00 +08:00
|
|
|
void *Erealloc(void *ptr, int size) {
|
2007-03-08 04:03:40 +08:00
|
|
|
void *Buffer;
|
|
|
|
|
|
|
|
if (size < 0 || (size == 0 && ptr == NULL))
|
|
|
|
DoError (ILLEGALMALLOCREQUEST, "Illegal realloc request size");
|
|
|
|
|
|
|
|
Buffer = (void *) realloc (ptr, size);
|
|
|
|
if (Buffer == NULL && size != 0)
|
|
|
|
DoError (NOTENOUGHMEMORY, "Not enough memory");
|
|
|
|
return (Buffer);
|
|
|
|
|
|
|
|
} /* Erealloc */
|
|
|
|
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void Efree(void *ptr) {
|
|
|
|
if (ptr == NULL)
|
|
|
|
DoError (ILLEGALMALLOCREQUEST, "Attempted to free NULL ptr");
|
|
|
|
|
|
|
|
free(ptr);
|
|
|
|
|
|
|
|
} /* Efree */
|