]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/MAI/bios_emulator/scitech/src/pm/common/malloc.c
* Patch by Thomas Frieden, 13 Nov 2002:
[karo-tx-uboot.git] / board / MAI / bios_emulator / scitech / src / pm / common / malloc.c
1 /****************************************************************************
2 *
3 *                   SciTech OS Portability Manager Library
4 *
5 *  ========================================================================
6 *
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
11 *
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.
16 *
17 *    The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
18 *
19 *    The Initial Developer of the Original Code is SciTech Software, Inc.
20 *    All Rights Reserved.
21 *
22 *  ========================================================================
23 *
24 * Language:     ANSI C
25 * Environment:  Any
26 *
27 * Description:  Module for implementing the PM library overrideable memory
28 *               allocator functions.
29 *
30 ****************************************************************************/
31
32 #include "pmapi.h"
33
34 /*--------------------------- Global variables ----------------------------*/
35
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;
40
41 /*----------------------------- Implementation ----------------------------*/
42
43 /****************************************************************************
44 DESCRIPTION:
45 Use local memory allocation routines.
46
47 HEADER:
48 pmapi.h
49
50 PARAMETERS:
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
55
56 REMARKS:
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.
65
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.
71
72 NOTE: This function should be called right at the start of your application,
73       before you initialise any other components or libraries.
74
75 NOTE: Code compiled into Binary Portable DLL's and Drivers automatically
76       end up calling these functions via the BPD C runtime library.
77
78 SEE ALSO:
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))
86 {
87     __PM_malloc = malloc;
88     __PM_calloc = calloc;
89     __PM_realloc = realloc;
90     __PM_free = free;
91 }
92
93 /****************************************************************************
94 DESCRIPTION:
95 Allocate a block of memory.
96
97 HEADER:
98 pmapi.h
99
100 PARAMETERS:
101 size    - Size of block to allocate in bytes
102
103 RETURNS:
104 Pointer to allocated block, or NULL if out of memory.
105
106 REMARKS:
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.
111
112 SEE ALSO:
113 PM_calloc, PM_realloc, PM_free, PM_useLocalMalloc
114 ****************************************************************************/
115 void * PMAPI PM_malloc(
116     size_t size)
117 {
118     return __PM_malloc(size);
119 }
120
121 /****************************************************************************
122 DESCRIPTION:
123 Allocate and clear a large memory block.
124
125 HEADER:
126 pmapi.h
127
128 PARAMETERS:
129 nelem   - number of contiguous size-byte units to allocate
130 size    - size of unit in bytes
131
132 RETURNS:
133 Pointer to allocated memory if successful, NULL if out of memory.
134
135 REMARKS:
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
140 have registered.
141
142 SEE ALSO:
143 PM_malloc, PM_realloc, PM_free, PM_useLocalMalloc
144 ****************************************************************************/
145 void * PMAPI PM_calloc(
146     size_t nelem,
147     size_t size)
148 {
149     return __PM_calloc(nelem,size);
150 }
151
152 /****************************************************************************
153 DESCRIPTION:
154 Re-allocate a block of memory
155
156 HEADER:
157 pmapi.h
158
159 PARAMETERS:
160 ptr     - Pointer to block to resize
161 size    - size of unit in bytes
162
163 RETURNS:
164 Pointer to allocated memory if successful, NULL if out of memory.
165
166 REMARKS:
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
172 have registered.
173
174 SEE ALSO:
175 PM_malloc, PM_calloc, PM_free, PM_useLocalMalloc
176 ****************************************************************************/
177 void * PMAPI PM_realloc(
178     void *ptr,
179     size_t size)
180 {
181     return __PM_realloc(ptr,size);
182 }
183
184 /****************************************************************************
185 DESCRIPTION:
186 Frees a block of memory.
187
188 HEADER:
189 pmapi.h
190
191 PARAMETERS:
192 p   - Pointer to memory block to free
193
194 REMARKS:
195 Frees a block of memory previously allocated with either PM_malloc,
196 PM_calloc or PM_realloc.
197
198 SEE ALSO:
199 PM_malloc, PM_calloc, PM_realloc, PM_useLocalMalloc
200 ****************************************************************************/
201 void PMAPI PM_free(
202     void *p)
203 {
204     __PM_free(p);
205 }