1 /****************************************************************************
3 * SciTech OS Portability Manager Library
5 * ========================================================================
7 * The contents of this file are subject to the SciTech MGL Public
8 * License Version 1.0 (the "License"); you may not use this file
9 * except in compliance with the License. You may obtain a copy of
10 * the License at http://www.scitechsoft.com/mgl-license.txt
12 * Software distributed under the License is distributed on an
13 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * rights and limitations under the License.
17 * The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
19 * The Initial Developer of the Original Code is SciTech Software, Inc.
20 * All Rights Reserved.
22 * ========================================================================
27 * Description: Module for implementing the PM library overrideable memory
28 * allocator functions.
30 ****************************************************************************/
34 /*--------------------------- Global variables ----------------------------*/
36 void * (*__PM_malloc)(size_t size) = malloc;
37 void * (*__PM_calloc)(size_t nelem,size_t size) = calloc;
38 void * (*__PM_realloc)(void *ptr,size_t size) = realloc;
39 void (*__PM_free)(void *p) = free;
41 /*----------------------------- Implementation ----------------------------*/
43 /****************************************************************************
45 Use local memory allocation routines.
51 malloc - Pointer to new malloc routine to use
52 calloc - Pointer to new caalloc routine to use
53 realloc - Pointer to new realloc routine to use
54 free - Pointer to new free routine to use
57 Tells the PM library to use a set of user specified memory allocation
58 routines instead of using the normal malloc/calloc/realloc/free standard
59 C library functions. This is useful if you wish to use a third party
60 debugging malloc library or perhaps a set of faster memory allocation
61 functions with the PM library, or any apps that use the PM library (such as
62 the MGL). Once you have registered your memory allocation routines, all
63 calls to PM_malloc, PM_calloc, PM_realloc and PM_free will be revectored to
64 your local memory allocation routines.
66 This is also useful if you need to keep track of just how much physical
67 memory your program has been using. You can use the PM_availableMemory
68 function to find out how much physical memory is available when the program
69 starts, and then you can use your own local memory allocation routines to
70 keep track of how much memory has been used and freed.
72 NOTE: This function should be called right at the start of your application,
73 before you initialise any other components or libraries.
75 NOTE: Code compiled into Binary Portable DLL's and Drivers automatically
76 end up calling these functions via the BPD C runtime library.
79 PM_malloc, PM_calloc, PM_realloc, PM_free, PM_availableMemory
80 ****************************************************************************/
81 void PMAPI PM_useLocalMalloc(
82 void * (*malloc)(size_t size),
83 void * (*calloc)(size_t nelem,size_t size),
84 void * (*realloc)(void *ptr,size_t size),
85 void (*free)(void *p))
89 __PM_realloc = realloc;
93 /****************************************************************************
95 Allocate a block of memory.
101 size - Size of block to allocate in bytes
104 Pointer to allocated block, or NULL if out of memory.
107 Allocates a block of memory of length size. If you have changed the memory
108 allocation routines with the PM_useLocalMalloc function, then calls to this
109 function will actually make calls to the local memory allocation routines
110 that you have registered.
113 PM_calloc, PM_realloc, PM_free, PM_useLocalMalloc
114 ****************************************************************************/
115 void * PMAPI PM_malloc(
118 return __PM_malloc(size);
121 /****************************************************************************
123 Allocate and clear a large memory block.
129 nelem - number of contiguous size-byte units to allocate
130 size - size of unit in bytes
133 Pointer to allocated memory if successful, NULL if out of memory.
136 Allocates a block of memory of length (size * nelem), and clears the
137 allocated area with zeros (0). If you have changed the memory allocation
138 routines with the PM_useLocalMalloc function, then calls to this function
139 will actually make calls to the local memory allocation routines that you
143 PM_malloc, PM_realloc, PM_free, PM_useLocalMalloc
144 ****************************************************************************/
145 void * PMAPI PM_calloc(
149 return __PM_calloc(nelem,size);
152 /****************************************************************************
154 Re-allocate a block of memory
160 ptr - Pointer to block to resize
161 size - size of unit in bytes
164 Pointer to allocated memory if successful, NULL if out of memory.
167 This function reallocates a block of memory that has been previously been
168 allocated to the new of size. The new size may be smaller or larger than
169 the original block of memory. If you have changed the memory allocation
170 routines with the PM_useLocalMalloc function, then calls to this function
171 will actually make calls to the local memory allocation routines that you
175 PM_malloc, PM_calloc, PM_free, PM_useLocalMalloc
176 ****************************************************************************/
177 void * PMAPI PM_realloc(
181 return __PM_realloc(ptr,size);
184 /****************************************************************************
186 Frees a block of memory.
192 p - Pointer to memory block to free
195 Frees a block of memory previously allocated with either PM_malloc,
196 PM_calloc or PM_realloc.
199 PM_malloc, PM_calloc, PM_realloc, PM_useLocalMalloc
200 ****************************************************************************/