📄 dms.c
字号:
/*************************************************************************/
/* */
/* Copyright Mentor Graphics Corporation 2004 */
/* All Rights Reserved. */
/* */
/* THIS WORK CONTAINS TRADE SECRET AND PROPRIETARY INFORMATION WHICH IS */
/* THE PROPERTY OF MENTOR GRAPHICS CORPORATION OR ITS LICENSORS AND IS */
/* SUBJECT TO LICENSE TERMS. */
/* */
/*************************************************************************/
/*************************************************************************/
/* */
/* FILE NAME VERSION */
/* */
/* dms.c Nucleus PLUS 1.15 */
/* */
/* COMPONENT */
/* */
/* DM - Dynamic Memory Management */
/* */
/* DESCRIPTION */
/* */
/* This file contains the supplemental routines for the Dynamic */
/* Memory Management component. */
/* */
/* DATA STRUCTURES */
/* */
/* None */
/* */
/* FUNCTIONS */
/* */
/* DMS_Allocate_Aligned_Memory Allocate an aligned memory */
/* block from a dynamic */
/* memory pool */
/* DEPENDENCIES */
/* */
/* cs_extr.h Common Service functions */
/* tc_extr.h Thread Control functions */
/* dm_extr.h Partition functions */
/* hi_extr.h History functions */
/* */
/*************************************************************************/
#define NU_SOURCE_FILE
#include "plus/inc/cs_extr.h" /* Common service functions */
#include "plus/inc/tc_extr.h" /* Thread control functions */
#include "plus/inc/dm_extr.h" /* Dynamic memory functions */
#include "plus/inc/hi_extr.h" /* History functions */
/* Define external inner-component global data references. */
/* Define internal component function prototypes. */
VOID DMC_Cleanup(VOID *information);
/*************************************************************************/
/* */
/* FUNCTION */
/* */
/* DMS_Allocate_Aligned_Memory */
/* */
/* DESCRIPTION */
/* */
/* This function allocates memory from the specified dynamic memory */
/* pool. If dynamic memory is currently available, this function */
/* is completed immediately. Otherwise, if there is not enough */
/* memory currently available, task suspension is possible. */
/* */
/* CALLED BY */
/* */
/* Application */
/* */
/* CALLS */
/* */
/* CSC_Place_On_List Place on suspend list */
/* [HIC_Make_History_Entry] Make entry in history log */
/* TCC_Suspend_Task Suspend calling task */
/* TCC_Task_Priority Pickup task priority */
/* [TCT_Check_Stack] Stack checking function */
/* TCT_Current_Thread Pickup current thread pointer*/
/* TCT_Protect Protect memory pool */
/* TCT_Set_Suspend_Protect Save suspend protection */
/* TCT_System_Protect Protect system structures */
/* TCT_Unprotect Release protection */
/* TCT_Unprotect_Specific Release specific protection */
/* */
/* INPUTS */
/* */
/* pool_ptr Memory pool pointer */
/* return_pointer Pointer to the destination */
/* memory pointer */
/* size Number of bytes requested */
/* alignment Required alignment of */
/* destination memory pointer */
/* suspend Suspension option if full */
/* */
/* OUTPUTS */
/* */
/* NU_SUCCESS If service is successful */
/* NU_NO_MEMORY Memory not available */
/* NU_TIMEOUT If timeout on service */
/* NU_POOL_DELETED If memory pool deleted */
/* during suspension */
/* */
/*************************************************************************/
STATUS DMS_Allocate_Aligned_Memory(NU_MEMORY_POOL *pool_ptr,
VOID **return_pointer, UNSIGNED size,
UNSIGNED alignment, UNSIGNED suspend)
{
R1 DM_PCB *pool; /* Pool control block ptr */
R2 DM_SUSPEND *suspend_ptr; /* Pointer to suspend block */
DM_SUSPEND suspend_block; /* Allocate suspension block */
R4 DM_HEADER *memory_ptr; /* Pointer to memory */
R3 DM_HEADER *new_ptr; /* New split block pointer */
UNSIGNED free_size; /* Size of block found */
TC_TCB *task; /* Task pointer */
STATUS status; /* Completion status */
UNSIGNED address; /* Address of start of block */
UNSIGNED split_size; /* Bytes for front split */
UNSIGNED next_aligned; /* Next aligned block addr */
NU_SUPERV_USER_VARIABLES
/* Switch to supervisor mode */
NU_SUPERVISOR_MODE();
/* Move input pool pointer into internal pointer. */
pool = (DM_PCB *) pool_ptr;
#ifdef NU_ENABLE_STACK_CHECK
/* Call stack checking function to check for an overflow condition. */
TCT_Check_Stack();
#endif
#ifdef NU_ENABLE_HISTORY
/* Make an entry that corresponds to this function in the system history
log. */
HIC_Make_History_Entry(NU_ALLOCATE_ALIGNED_ID, (UNSIGNED) pool,
(UNSIGNED) return_pointer, (UNSIGNED) size);
#endif
/* Initialize the status as successful. */
status = NU_SUCCESS;
/* Adjust the request to a size evenly divisible by the number of bytes
in an UNSIGNED data element. Also, check to make sure it is of the
minimum size. */
if (size < pool -> dm_min_allocation)
/* Change size to the minimum allocation. */
size = pool -> dm_min_allocation;
else
/* Insure that size is a multiple of the UNSIGNED size. */
size =
((size + sizeof(UNSIGNED) - 1)/sizeof(UNSIGNED)) * sizeof(UNSIGNED);
/* Adjust the requested alignment to one evenly divisible by the number of
bytes in an UNSIGNED data element. */
alignment =
((alignment + sizeof(UNSIGNED) - 1)/sizeof(UNSIGNED)) * sizeof(UNSIGNED);
/* Protect against simultaneous access to the memory pool. */
TCT_Protect(&(pool -> dm_protect));
/* Search the memory list for the first available block of memory that
satisfies the request. Note that blocks are merged during the
deallocation function. */
memory_ptr = pool -> dm_search_ptr;
do
{
/* Determine if the block is free and if it can satisfy the request. */
if (memory_ptr -> dm_memory_free)
/* Calculate the free block size. */
free_size = (((BYTE_PTR) (memory_ptr -> dm_next_memory)) -
((BYTE_PTR) memory_ptr)) - DM_OVERHEAD;
else
/* There are no free bytes available. */
free_size = 0;
/* Free block may be large enough, now check alignment */
if (free_size >= size)
{
address = ((UNSIGNED)(memory_ptr)) + DM_OVERHEAD;
/* Is this free block, minus the overhead, already aligned? */
if (address % alignment != 0)
{
/* Not aligned, can the free block be split in front? */
next_aligned = address + (alignment - 1);
next_aligned /= alignment;
next_aligned *= alignment;
split_size = next_aligned - address;
/* Is space from start of block to aligned location large enough
to contain 2 DM_OVERHEAD plus pool -> dm_min_allocation? */
if (split_size < ((2 * DM_OVERHEAD) + pool -> dm_min_allocation))
{
/* No, so try to make space for overhead and dm_min_allocation */
next_aligned = address + (2 * DM_OVERHEAD) +
(pool -> dm_min_allocation) + (alignment - 1);
next_aligned /= alignment;
next_aligned *= alignment;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -