FreeCalypso > hg > ffs-editor
comparison src/nucleus/dmce.c @ 0:92470e5d0b9e
src: partial import from FC Selenite
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 15 May 2020 01:28:16 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:92470e5d0b9e |
---|---|
1 /*************************************************************************/ | |
2 /* */ | |
3 /* Copyright Mentor Graphics Corporation 2002 */ | |
4 /* All Rights Reserved. */ | |
5 /* */ | |
6 /* THIS WORK CONTAINS TRADE SECRET AND PROPRIETARY INFORMATION WHICH IS */ | |
7 /* THE PROPERTY OF MENTOR GRAPHICS CORPORATION OR ITS LICENSORS AND IS */ | |
8 /* SUBJECT TO LICENSE TERMS. */ | |
9 /* */ | |
10 /*************************************************************************/ | |
11 | |
12 /*************************************************************************/ | |
13 /* */ | |
14 /* FILE NAME VERSION */ | |
15 /* */ | |
16 /* dmce.c Nucleus PLUS 1.14 */ | |
17 /* */ | |
18 /* COMPONENT */ | |
19 /* */ | |
20 /* DM - Dynamic Memory Management */ | |
21 /* */ | |
22 /* DESCRIPTION */ | |
23 /* */ | |
24 /* This file contains the error checking routines for the functions */ | |
25 /* in the dynamic memory management component. This permits easy */ | |
26 /* removal of the error checking logic when it is not needed. */ | |
27 /* */ | |
28 /* DATA STRUCTURES */ | |
29 /* */ | |
30 /* None */ | |
31 /* */ | |
32 /* FUNCTIONS */ | |
33 /* */ | |
34 /* DMCE_Create_Memory_Pool Create a dynamic memory pool */ | |
35 /* DMCE_Delete_Memory_Pool Delete a dynamic memory pool */ | |
36 /* DMCE_Allocate_Memory Allocate a memory block from */ | |
37 /* a dynamic memory pool */ | |
38 /* DMCE_Deallocate_Memory Deallocate a memory block */ | |
39 /* from a dynamic memory pool */ | |
40 /* */ | |
41 /* DEPENDENCIES */ | |
42 /* */ | |
43 /* cs_extr.h Common Service functions */ | |
44 /* tc_extr.h Thread Control functions */ | |
45 /* dm_extr.h Partition functions */ | |
46 /* */ | |
47 /* HISTORY */ | |
48 /* */ | |
49 /* DATE REMARKS */ | |
50 /* */ | |
51 /* 03-01-1993 Created initial version 1.0 */ | |
52 /* 04-19-1993 Verified version 1.0 */ | |
53 /* 11-01-1993 Corrected call of actual */ | |
54 /* function to delete a memory */ | |
55 /* pool, resulting in version 1.0a */ | |
56 /* 11-01-1993 Verfied version 1.0a */ | |
57 /* 03-01-1994 Changed name original error */ | |
58 /* checking file and changed */ | |
59 /* function interfaces, resulting */ | |
60 /* in version 1.1 */ | |
61 /* */ | |
62 /* 03-18-1994 Verified version 1.1 */ | |
63 /* 04-17-1996 updated to version 1.2 */ | |
64 /* 03-24-1998 Released version 1.3. */ | |
65 /* 04-17-2002 Released version 1.13m */ | |
66 /* 11-07-2002 Released version 1.14 */ | |
67 /*************************************************************************/ | |
68 #define NU_SOURCE_FILE | |
69 | |
70 | |
71 #include "cs_extr.h" /* Common service functions */ | |
72 #include "tc_extr.h" /* Thread control functions */ | |
73 #include "dm_extr.h" /* Dynamic memory functions */ | |
74 | |
75 | |
76 /*************************************************************************/ | |
77 /* */ | |
78 /* FUNCTION */ | |
79 /* */ | |
80 /* DMCE_Create_Memory_Pool */ | |
81 /* */ | |
82 /* DESCRIPTION */ | |
83 /* */ | |
84 /* This function performs error checking on the parameters supplied */ | |
85 /* to the create dynamic memory pool function. */ | |
86 /* */ | |
87 /* CALLED BY */ | |
88 /* */ | |
89 /* Application */ | |
90 /* */ | |
91 /* CALLS */ | |
92 /* */ | |
93 /* DMC_Create_Memory_Pool Actual create dynamic memory */ | |
94 /* pool function */ | |
95 /* */ | |
96 /* INPUTS */ | |
97 /* */ | |
98 /* pool_ptr Memory pool control block */ | |
99 /* pointer */ | |
100 /* name Memory pool name */ | |
101 /* start_address Starting address of the pool */ | |
102 /* pool_size Number of bytes in the pool */ | |
103 /* min_allocation Minimum allocation size */ | |
104 /* suspend_type Suspension type */ | |
105 /* */ | |
106 /* OUTPUTS */ | |
107 /* */ | |
108 /* NU_INVALID_POOL Indicates the pool control */ | |
109 /* block pointer is invalid */ | |
110 /* NU_INVALID_MEMORY Indicates the starting */ | |
111 /* memory address is NULL */ | |
112 /* NU_INVALID_SIZE Indicates that either the */ | |
113 /* pool size and/or the */ | |
114 /* minimum allocation size is */ | |
115 /* invalid */ | |
116 /* NU_INVALID_SUSPEND Indicate the suspension type */ | |
117 /* is invalid */ | |
118 /* */ | |
119 /* HISTORY */ | |
120 /* */ | |
121 /* DATE REMARKS */ | |
122 /* */ | |
123 /* 03-01-1993 Created initial version 1.0 */ | |
124 /* 04-19-1993 Verified version 1.0 */ | |
125 /* 03-01-1994 Modified function interface, */ | |
126 /* resulting in version 1.1 */ | |
127 /* */ | |
128 /* 03-18-1994 Verified version 1.1 */ | |
129 /* */ | |
130 /*************************************************************************/ | |
131 STATUS DMCE_Create_Memory_Pool(NU_MEMORY_POOL *pool_ptr, CHAR *name, | |
132 VOID *start_address, UNSIGNED pool_size, | |
133 UNSIGNED min_allocation, OPTION suspend_type) | |
134 { | |
135 | |
136 DM_PCB *pool; /* Pool control block ptr */ | |
137 STATUS status; /* Completion status */ | |
138 UNSIGNED adjusted_min; /* Adjusted size of minimum */ | |
139 UNSIGNED adjusted_pool; /* Adjusted size of pool */ | |
140 | |
141 | |
142 /* Move input pool pointer into internal pointer. */ | |
143 pool = (DM_PCB *) pool_ptr; | |
144 | |
145 /* Adjust the minimum allocation size to something that is evenly | |
146 divisible by the number of bytes in an UNSIGNED data type. */ | |
147 adjusted_min = ((min_allocation + sizeof(UNSIGNED) - 1)/sizeof(UNSIGNED)) * | |
148 sizeof(UNSIGNED); | |
149 | |
150 /* Adjust the pool size to something that is evenly divisible by the | |
151 number of bytes in an UNSIGNED data type. */ | |
152 adjusted_pool = ((pool_size + sizeof(UNSIGNED) - 1)/sizeof(UNSIGNED)) * | |
153 sizeof(UNSIGNED); | |
154 | |
155 /* Check for a NULL dynamic memory pool control block pointer or a control | |
156 block that is already created. */ | |
157 if ((pool == NU_NULL) || (pool -> dm_id == DM_DYNAMIC_ID)) | |
158 | |
159 /* Invalid dynamic memory pool control block pointer. */ | |
160 status = NU_INVALID_POOL; | |
161 | |
162 else if (start_address == NU_NULL) | |
163 | |
164 /* Invalid memory pointer. */ | |
165 status = NU_INVALID_MEMORY; | |
166 | |
167 else if ((adjusted_min == 0) || | |
168 ((adjusted_min + (2 * DM_OVERHEAD)) > adjusted_pool)) | |
169 | |
170 /* Pool could not even accommodate one allocation. */ | |
171 status = NU_INVALID_SIZE; | |
172 | |
173 else if ((suspend_type != NU_FIFO) && (suspend_type != NU_PRIORITY)) | |
174 | |
175 /* Invalid suspension type. */ | |
176 status = NU_INVALID_SUSPEND; | |
177 | |
178 else | |
179 | |
180 /* Call the actual service to create the dynamic memory pool. */ | |
181 status = DMC_Create_Memory_Pool(pool_ptr, name, start_address, | |
182 pool_size, min_allocation, suspend_type); | |
183 | |
184 /* Return completion status. */ | |
185 return(status); | |
186 } | |
187 | |
188 | |
189 /*************************************************************************/ | |
190 /* */ | |
191 /* FUNCTION */ | |
192 /* */ | |
193 /* DMCE_Delete_Memory_Pool */ | |
194 /* */ | |
195 /* DESCRIPTION */ | |
196 /* */ | |
197 /* This function performs error checking on the parameters supplied */ | |
198 /* to the delete dynamic memory pool function. */ | |
199 /* */ | |
200 /* CALLED BY */ | |
201 /* */ | |
202 /* Application */ | |
203 /* */ | |
204 /* CALLS */ | |
205 /* */ | |
206 /* DMC_Delete_Memory_Pool Actual function to delete a */ | |
207 /* dynamic memory pool */ | |
208 /* */ | |
209 /* INPUTS */ | |
210 /* */ | |
211 /* pool_ptr Memory pool control block */ | |
212 /* pointer */ | |
213 /* */ | |
214 /* OUTPUTS */ | |
215 /* */ | |
216 /* NU_INVALID_POOL Indicates the pool pointer */ | |
217 /* is invalid */ | |
218 /* */ | |
219 /* HISTORY */ | |
220 /* */ | |
221 /* DATE REMARKS */ | |
222 /* */ | |
223 /* 03-01-1993 Created initial version 1.0 */ | |
224 /* 04-19-1993 Verified version 1.0 */ | |
225 /* 11-01-1993 Corrected call of actual */ | |
226 /* function to delete a memory */ | |
227 /* pool, resulting in version 1.0a */ | |
228 /* 11-01-1993 Verfied version 1.0a */ | |
229 /* 03-01-1994 Modified function interface, */ | |
230 /* resulting in version 1.1 */ | |
231 /* */ | |
232 /* 03-18-1994 Verified version 1.1 */ | |
233 /* */ | |
234 /*************************************************************************/ | |
235 STATUS DMCE_Delete_Memory_Pool(NU_MEMORY_POOL *pool_ptr) | |
236 { | |
237 | |
238 DM_PCB *pool; /* Pool control block ptr */ | |
239 STATUS status; /* Completion status */ | |
240 | |
241 | |
242 /* Move input pool pointer into internal pointer. */ | |
243 pool = (DM_PCB *) pool_ptr; | |
244 | |
245 /* Determine if the dynamic memory pool pointer is valid. */ | |
246 if ((pool) && (pool -> dm_id == DM_DYNAMIC_ID)) | |
247 | |
248 /* Dynamic memory pool pointer is valid, call the function to | |
249 delete it. */ | |
250 status = DMC_Delete_Memory_Pool(pool_ptr); | |
251 | |
252 else | |
253 | |
254 /* Dynamic memory pool pointer is invalid, indicate in | |
255 completion status. */ | |
256 status = NU_INVALID_POOL; | |
257 | |
258 /* Return completion status. */ | |
259 return(status); | |
260 } | |
261 | |
262 | |
263 /*************************************************************************/ | |
264 /* */ | |
265 /* FUNCTION */ | |
266 /* */ | |
267 /* DMCE_Allocate_Memory */ | |
268 /* */ | |
269 /* DESCRIPTION */ | |
270 /* */ | |
271 /* This function performs error checking on the parameters supplied */ | |
272 /* to the allocate memory function. */ | |
273 /* */ | |
274 /* CALLED BY */ | |
275 /* */ | |
276 /* Application */ | |
277 /* */ | |
278 /* CALLS */ | |
279 /* */ | |
280 /* DMC_Allocate_Memory Actual memory allocation */ | |
281 /* function */ | |
282 /* TCCE_Suspend_Error Check for suspension error */ | |
283 /* */ | |
284 /* INPUTS */ | |
285 /* */ | |
286 /* pool_ptr Memory pool pointer */ | |
287 /* return_pointer Pointer to the destination */ | |
288 /* memory pointer */ | |
289 /* size Number of bytes requested */ | |
290 /* suspend Suspension option if full */ | |
291 /* */ | |
292 /* OUTPUTS */ | |
293 /* */ | |
294 /* NU_INVALID_POOL Indicates the supplied pool */ | |
295 /* pointer is invalid */ | |
296 /* NU_INVALID_POINTER Indicates the return pointer */ | |
297 /* is NULL */ | |
298 /* NU_INVALID_SIZE Indicates the size is 0 or */ | |
299 /* larger than the pool */ | |
300 /* NU_INVALID_SUSPEND Invalid suspension requested */ | |
301 /* */ | |
302 /* HISTORY */ | |
303 /* */ | |
304 /* DATE REMARKS */ | |
305 /* */ | |
306 /* 03-01-1993 Created initial version 1.0 */ | |
307 /* 04-19-1993 Verified version 1.0 */ | |
308 /* 03-01-1994 Modified function interface , */ | |
309 /* resulting in version 1.1 */ | |
310 /* */ | |
311 /* 03-18-1994 Verified version 1.1 */ | |
312 /* */ | |
313 /*************************************************************************/ | |
314 STATUS DMCE_Allocate_Memory(NU_MEMORY_POOL *pool_ptr, VOID **return_pointer, | |
315 UNSIGNED size, UNSIGNED suspend) | |
316 { | |
317 | |
318 DM_PCB *pool; /* Pool control block ptr */ | |
319 STATUS status; /* Completion status */ | |
320 | |
321 | |
322 /* Move input pool pointer into internal pointer. */ | |
323 pool = (DM_PCB *) pool_ptr; | |
324 | |
325 /* Determine if dynamic memory pool pointer is invalid. */ | |
326 if (pool == NU_NULL) | |
327 | |
328 /* Dynamic memory pool pointer is invalid, indicate in | |
329 completion status. */ | |
330 status = NU_INVALID_POOL; | |
331 | |
332 else if (pool -> dm_id != DM_DYNAMIC_ID) | |
333 | |
334 /* Dynamic memory pool pointer is invalid, indicate in | |
335 completion status. */ | |
336 status = NU_INVALID_POOL; | |
337 | |
338 else if (return_pointer == NU_NULL) | |
339 | |
340 /* Return pointer is invalid. */ | |
341 status = NU_INVALID_POINTER; | |
342 | |
343 else if ((size == 0) || | |
344 (size > (pool -> dm_pool_size - (2 * DM_OVERHEAD)))) | |
345 | |
346 /* Return the invalid size error. */ | |
347 status = NU_INVALID_SIZE; | |
348 | |
349 else if ((suspend) && (TCCE_Suspend_Error())) | |
350 | |
351 /* Suspension from an non-task thread. */ | |
352 status = NU_INVALID_SUSPEND; | |
353 | |
354 else | |
355 | |
356 /* Parameters are valid, call actual function. */ | |
357 status = DMC_Allocate_Memory(pool_ptr, return_pointer, size, suspend); | |
358 | |
359 /* Return the completion status. */ | |
360 return(status); | |
361 } | |
362 | |
363 | |
364 /*************************************************************************/ | |
365 /* */ | |
366 /* FUNCTION */ | |
367 /* */ | |
368 /* DMCE_Deallocate_Memory */ | |
369 /* */ | |
370 /* DESCRIPTION */ | |
371 /* */ | |
372 /* This function performs error checking on the parameters supplied */ | |
373 /* to the deallocate memory function. */ | |
374 /* */ | |
375 /* CALLED BY */ | |
376 /* */ | |
377 /* Application */ | |
378 /* */ | |
379 /* CALLS */ | |
380 /* */ | |
381 /* DMC_Deallocate_Memory Actual deallocate memory */ | |
382 /* function */ | |
383 /* */ | |
384 /* INPUTS */ | |
385 /* */ | |
386 /* memory Pointer to dynamic memory */ | |
387 /* */ | |
388 /* OUTPUTS */ | |
389 /* */ | |
390 /* NU_INVALID_POINTER Indicates the supplied */ | |
391 /* pointer is invalid */ | |
392 /* */ | |
393 /* HISTORY */ | |
394 /* */ | |
395 /* DATE REMARKS */ | |
396 /* */ | |
397 /* 03-01-1993 Created initial version 1.0 */ | |
398 /* 04-19-1993 Verified version 1.0 */ | |
399 /* 03-01-1994 Modified function interface, */ | |
400 /* resulting in version 1.1 */ | |
401 /* */ | |
402 /* 03-18-1994 Verified version 1.1 */ | |
403 /* */ | |
404 /*************************************************************************/ | |
405 STATUS DMCE_Deallocate_Memory(VOID *memory) | |
406 { | |
407 | |
408 DM_PCB *pool; /* Pool pointer */ | |
409 DM_HEADER *header_ptr; /* Pointer to memory block */ | |
410 STATUS status; /* Completion status */ | |
411 | |
412 | |
413 /* Pickup the associated pool's pointer. It is inside the header of | |
414 each memory block. */ | |
415 header_ptr = (DM_HEADER *) (((BYTE_PTR) memory) - DM_OVERHEAD); | |
416 | |
417 /* Determine if the pointer(s) are NULL. */ | |
418 if ((header_ptr == NU_NULL) || (memory == NU_NULL)) | |
419 | |
420 /* Dynamic memory pointer is invalid. */ | |
421 status = NU_INVALID_POINTER; | |
422 | |
423 /* Determine if dynamic memory pool pointer is invalid. */ | |
424 else if ((pool = header_ptr -> dm_memory_pool) == NU_NULL) | |
425 | |
426 /* Dynamic memory pointer is invalid, indicate in completion | |
427 status. */ | |
428 status = NU_INVALID_POINTER; | |
429 | |
430 else if (pool -> dm_id != DM_DYNAMIC_ID) | |
431 | |
432 /* Dynamic memory pool pointer is invalid, indicate in completion | |
433 status. */ | |
434 status = NU_INVALID_POINTER; | |
435 | |
436 else if (header_ptr -> dm_memory_free) | |
437 | |
438 /* Dynamic memory is free - must not be allocated. */ | |
439 status = NU_INVALID_POINTER; | |
440 | |
441 else | |
442 | |
443 /* Parameters are valid, call actual function. */ | |
444 status = DMC_Deallocate_Memory(memory); | |
445 | |
446 /* Return the completion status. */ | |
447 return(status); | |
448 } | |
449 | |
450 | |
451 | |
452 | |
453 | |
454 | |
455 | |
456 |