1 /*---------------------------------------------------------------------------
2  * Wrapper for the system's malloc() routines
3  *
4  *---------------------------------------------------------------------------
5  */
6 
7 #include "driver.h"
8 
9 #include <stdlib.h>
10 
11 #include "sysmalloc.h"
12 
13 #include "mstrings.h"
14 #include "stdstrings.h"
15 #include "svalue.h"
16 
17 #include "../mudlib/sys/debug_info.h"
18 
19 /* Defines required by the xalloc.c wrapper */
20 /* #undef REPLACE_MALLOC */
21 #define NO_MEM_BLOCK_SIZE
22 #define MEM_THREADSAFE
23 
24 /*-------------------------------------------------------------------------*/
25 
26 static char * heap_start = NULL;
27 static char * heap_end = NULL;
28   /* The start and end of the heap, as we know it.
29    */
30 
31 /*-------------------------------------------------------------------------*/
32 static inline POINTER
mem_alloc(size_t size)33 mem_alloc (size_t size)
34 
35 /* Allocate a memory block for <size> bytes at the source <file>:<line>.
36  * Result is the pointer the memory block, or NULL when out of memory.
37  */
38 
39 {
40     POINTER rc;
41 
42     rc = malloc(size);
43 
44     if (heap_start == NULL || (char *)rc < heap_start)
45         heap_start = rc;
46     if (heap_end == NULL || (char *)rc + size > heap_end)
47         heap_end = rc + size;
48 
49     assert_stack_gap();
50 
51     return rc;
52 } /* mem_alloc() */
53 
54 /*-------------------------------------------------------------------------*/
55 static INLINE void
mem_free(POINTER ptr)56 mem_free (POINTER ptr)
57 
58 /* Return the memoryblock <ptr> to the allocator.
59  */
60 
61 {
62     free(ptr);
63 } /* mem_free() */
64 
65 /*-------------------------------------------------------------------------*/
66 static POINTER
mem_realloc(POINTER p,size_t size)67 mem_realloc (POINTER p, size_t size)
68 
69 /* Reallocate block <p> to the new size of <size> and return the pointer.
70  */
71 
72 {
73     return realloc(p, size);
74 } /* mem_realloc() */
75 
76 /*-------------------------------------------------------------------------*/
77 static INLINE void
mem_mark_permanent(POINTER p UNUSED)78 mem_mark_permanent (POINTER p UNUSED)
79 
80 /* Mark the allocated block at <p> as permanent, ie. it won't be subject
81  * to the GC.
82  */
83 
84 {
85 #   ifdef __MWERKS__
86 #      pragma unused(p)
87 #   endif
88     /* Nothing */
89 } /* mem_mark_permanent() */
90 
91 /*-------------------------------------------------------------------------*/
92 static INLINE void
mem_mark_collectable(POINTER p UNUSED)93 mem_mark_collectable (POINTER p UNUSED)
94 
95 /* Mark the allocated block at <p> as non-permant, ie. it is subject
96  * to the GC.
97  */
98 
99 {
100 #   ifdef __MWERKS__
101 #      pragma unused(p)
102 #   endif
103     /* Nothing */
104 } /* mem_mark_collectable() */
105 
106 /*-------------------------------------------------------------------------*/
107 static INLINE size_t
mem_block_size(POINTER p UNUSED)108 mem_block_size (POINTER p UNUSED)
109 
110 /* Return the size of block <p> (sans internal overhead) in bytes.
111  */
112 
113 {
114 #   ifdef __MWERKS__
115 #      pragma unused(p)
116 #   endif
117    return 0;
118 } /* mem_block_size() */
119 
120 /*-------------------------------------------------------------------------*/
121 static INLINE size_t
mem_overhead(void)122 mem_overhead (void)
123 
124 /* Return the size of each block's overhead in bytes.
125  */
126 
127 {
128    return EXTERN_MALLOC_OVERHEAD;
129 } /* mem_overhead() */
130 
131 /*-------------------------------------------------------------------------*/
132 static INLINE void *
mem_increment_size(void * vp,size_t size)133 mem_increment_size (void *vp, size_t size)
134 
135 /* Try to extent the allocation block for <vp> to hold <size> more bytes.
136  * If this is not possible, return NULL, otherwise return a pointer
137  * to the start of the block extension.
138  */
139 
140 {
141     return NULL;
142 } /* mem_increment_size() */
143 
144 /*-------------------------------------------------------------------------*/
145 void
mem_dump_data(strbuf_t * sbuf)146 mem_dump_data (strbuf_t *sbuf)
147 
148 /* For the status commands and functions: add the smalloc statistic
149  * to the buffer <sbuf>.
150  */
151 
152 {
153     strbuf_add(sbuf, "Using system standard malloc.\n");
154     strbuf_addf(sbuf,
155                 "soft memory limit: %10lu, hard memory limit: %10lu\n\n",
156                 get_memory_limit(MALLOC_SOFT_LIMIT),
157                 get_memory_limit(MALLOC_HARD_LIMIT)
158                );
159 
160 } /* mem_dump_data() */
161 
162 /*-------------------------------------------------------------------------*/
163 void
mem_dinfo_data(svalue_t * svp,int value)164 mem_dinfo_data (svalue_t *svp, int value)
165 
166 /* Fill in the data for debug_info(DINFO_DATA, DID_MEMORY) into the
167  * svalue-block svp.
168  */
169 
170 {
171     if (value == -1)
172         put_ref_string(svp+DID_MEM_NAME, STR_SYSTEM_MALLOC);
173     else if (value == DID_MEM_NAME)
174         put_ref_string(svp, STR_SYSTEM_MALLOC);
175 } /* mem_dinfo_data() */
176 
177 /*-------------------------------------------------------------------------*/
178 void
mem_dump_extdata(strbuf_t * sbuf)179 mem_dump_extdata (strbuf_t *sbuf)
180 
181 /* For the status commands and functions: add the extended smalloc statistic
182  * to the buffer <sbuf>.
183  */
184 
185 {
186     strbuf_add(sbuf, "No detailed blocks statistics available.\n");
187 } /* mem_dump_extdata() */
188 
189 /*-------------------------------------------------------------------------*/
190 Bool
mem_dump_memory(int fd)191 mem_dump_memory (int fd)
192 
193 /* Print the location, size, and (if available) the TRACE information
194  * of all memory blocks to file <fd>, and return TRUE.
195  * If the allocator doesn't support this operation, print nothing
196  * and return FALSE.
197  */
198 
199 {
200     return MY_FALSE;
201 } /* mem_dump_memory() */
202 
203 /*-------------------------------------------------------------------------*/
204 void
mem_consolidate(Bool force UNUSED)205 mem_consolidate (Bool force UNUSED)
206 
207 /* Consolidate the free small blocks, merging them into larger free blocks
208  * where possible, and rebuild the free lists.
209  */
210 
211 {
212 #   ifdef __MWERKS__
213 #      pragma unused(force)
214 #   endif
215     /* Nothing */
216 } /* mem_consolidate() */
217 
218 /*-------------------------------------------------------------------------*/
219 #ifdef MALLOC_EXT_STATISTICS
220 void
mem_update_stats(void)221 mem_update_stats (void)
222 
223 /* Update whatever extended statistics the allocator has. Called every
224  * backend cycle or so to allow for the calculation of averages over time.
225  */
226 
227 {
228     /* Nothing */
229 } /* mem_update_stats() */
230 #endif /* MALLOC_EXT_STATISTICS */
231 
232 /***************************************************************************/
233