1 /*
2  * Copyright (c) 2017, NVIDIA CORPORATION.  All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #include "stdioInterf.h"
19 #include "fioMacros.h"
20 
21 #include <memory.h>
22 
23 #include "fort_vars.h"
24 
25 extern void *shmalloc(size_t);
26 
27 /* ==================== local heap routines ====================== */
28 
29 #define ZIP ((char *)15L)
30 
31 /* malloc */
32 
33 void *
__fort_malloc_without_abort(size_t n)34 __fort_malloc_without_abort(size_t n)
35 {
36   char *p;
37 
38   if (n == 0)
39     return ZIP;
40   p = malloc(n);
41   if (__fort_zmem && (p != NULL))
42     memset(p, '\0', n);
43   return p;
44 }
45 
46 void *
__fort_malloc(size_t n)47 __fort_malloc(size_t n)
48 {
49   char *p;
50 
51   p = __fort_malloc_without_abort(n);
52   if (p == (char *)0)
53     __fort_abort("__fort_malloc: not enough memory");
54   return p;
55 }
56 
57 /* realloc */
58 
59 void *
__fort_realloc(void * ptr,size_t n)60 __fort_realloc(void *ptr, size_t n)
61 {
62   char *p;
63 
64   if (ptr == (char *)0 | ptr == ZIP) {
65     if (n == 0)
66       return ZIP;
67     p = malloc(n);
68     if (__fort_zmem && (p != NULL))
69       memset(p, '\0', n);
70   } else {
71     if (n == 0) {
72       free(ptr);
73       return ZIP;
74     }
75     p = realloc(ptr, n);
76   }
77   if (p == (char *)0) {
78     __fort_abort("__fort_realloc: not enough memory");
79   }
80   return (p);
81 }
82 
83 /* calloc */
84 
85 void *
__fort_calloc_without_abort(size_t n)86 __fort_calloc_without_abort(size_t n)
87 {
88   char *p;
89 
90   if (n == 0)
91     return ZIP;
92   p = malloc(n);
93   if (p != NULL)
94     memset(p, '\0', n);
95   return p;
96 }
97 
98 void *
__fort_calloc(size_t n,size_t s)99 __fort_calloc(size_t n, size_t s)
100 {
101   char *p;
102 
103   if (n == 0 | s == 0)
104     return ZIP;
105   p = calloc(n, s);
106   if (p == (char *)0) {
107     __fort_abort("__fort_calloc: not enough memory");
108   }
109   return (p);
110 }
111 
112 /* free */
113 
114 void
__fort_free(void * ptr)115 __fort_free(void *ptr)
116 {
117   if (ptr != (char *)0 & ptr != ZIP) {
118     free(ptr);
119   }
120 }
121 
122 /* ================= pseudo-global heap routines ================= */
123 
124 /* stubs for global shared memory (mmapped) allocation calls */
125 
126 void *
__fort_gmalloc_without_abort(size_t n)127 __fort_gmalloc_without_abort(size_t n)
128 {
129   return __fort_malloc_without_abort(n);
130 }
131 
132 void *
__fort_gmalloc(size_t n)133 __fort_gmalloc(size_t n)
134 {
135   return __fort_malloc(n);
136 }
137 
138 void *
__fort_grealloc(void * ptr,size_t n)139 __fort_grealloc(void *ptr, size_t n)
140 {
141   return __fort_realloc(ptr, n);
142 }
143 
144 void *
__fort_gcalloc_without_abort(size_t n)145 __fort_gcalloc_without_abort(size_t n)
146 {
147   return __fort_calloc_without_abort(n);
148 }
149 
150 void *
__fort_gcalloc(size_t n,size_t s)151 __fort_gcalloc(size_t n, size_t s)
152 {
153   return __fort_calloc(n, s);
154 }
155 
156 void
__fort_gfree(void * ptr)157 __fort_gfree(void *ptr)
158 {
159   __fort_free(ptr);
160 }
161 
162