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 ----------------------------------------------------------------------------**/
34 /*---------------------------------------------------------------------------*/
Emalloc(size_t Size)35 void *Emalloc(size_t Size) {
36 /*
37 ** Parameters:
38 ** Size
39 number of bytes of memory to be allocated
40 ** Globals: none
41 ** Operation:
42 ** This routine attempts to allocate the specified number of
43 ** bytes. If the memory can be allocated, a pointer to the
44 ** memory is returned. If the memory cannot be allocated, or
45 ** if the allocation request is negative or zero,
46 ** an error is trapped.
47 ** Return: Pointer to allocated memory.
48 ** Exceptions: NOTENOUGHMEMORY
49 unable to allocate Size bytes
50 ** ILLEGALMALLOCREQUEST
51 negative or zero request size
52 ** History: 4/3/89, DSJ, Created.
53 */
54 void *Buffer;
55
56 if (Size <= 0)
57 DoError (ILLEGALMALLOCREQUEST, "Illegal malloc request size");
58 Buffer = (void *) malloc (Size);
59 if (Buffer == NULL) {
60 DoError (NOTENOUGHMEMORY, "Not enough memory");
61 return (NULL);
62 }
63 else
64 return (Buffer);
65
66 } /* Emalloc */
67
68
69 /*---------------------------------------------------------------------------*/
Erealloc(void * ptr,size_t size)70 void *Erealloc(void *ptr, size_t size) {
71 void *Buffer;
72
73 if (size < 0 || (size == 0 && ptr == NULL))
74 DoError (ILLEGALMALLOCREQUEST, "Illegal realloc request size");
75
76 Buffer = (void *) realloc (ptr, size);
77 if (Buffer == NULL && size != 0)
78 DoError (NOTENOUGHMEMORY, "Not enough memory");
79 return (Buffer);
80
81 } /* Erealloc */
82
83
84 /*---------------------------------------------------------------------------*/
Efree(void * ptr)85 void Efree(void *ptr) {
86 if (ptr == NULL)
87 DoError (ILLEGALMALLOCREQUEST, "Attempted to free NULL ptr");
88
89 free(ptr);
90
91 } /* Efree */
92