⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jam_list.c

📁 java 1.1 gemini 08_16
💻 C
字号:
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2001
*
*****************************************************************************/

/*****************************************************************************
 *
 * Filename:
 * ---------
 *   jam_list.c
 *
 * Project:
 * --------
 *   Maui_Software
 *
 * Description:
 * ------------
 *   This file implements JAM list function
 *
 * Author:
 * -------
 * -------
 *
 *============================================================================
 *             HISTORY
 * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *------------------------------------------------------------------------------
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 * removed!
 * removed!
 * removed!
 *
 *------------------------------------------------------------------------------
 * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
 *============================================================================
 ****************************************************************************/

/*************************************************************************
 * Include Statements
 *************************************************************************/
#include "jal.h"
#include "jam_internal.h"
#include "jvm_internal.h"

#include "stack_common.h"
#include "stack_msgs.h"
#include "task_main_func.h"
#include "app_ltlcom.h"
#include "lcd_ip_cqueue.h"
#include "stack_types.h"
#include "task_config.h"
#include "syscomp_config.h"
#include "custom_config.h"
#include "custom_util.h"
#include "stack_init.h"
#include "stack_ltlcom.h"
#include "stack_msgs.h"
#include "app_buff_alloc.h"
#include "jam_msg_handler.h"
#include "machine_md.h"

/*************************************************************************
 * Global Definition
 *************************************************************************/
/* Byte boundary for word alignment - Assumes word is 4-bytes */
#define ALIGNMENT                               (0x00000003)
/* Constant to verify a header's validity */
#define MAGIC                                   (0xCAFE)

/*
 * Total memory allocate from Java runtime memory pool, can be accessed 
 * * by these pointers. Meanwhile, they are used to release memory when
 * * clean up.
 */
mids_listfile_struct *saved_mids_listfile_ptr;

typedef struct listmem_struct_t
{
    unsigned short magic;   /* magic number */
    char free;              /* 1 == block is free, 0 == block is used */
    char reserved;
    unsigned int size;      /* size of block */
} listmem_struct, *listmem_hdr;

/* mids list memory pool */
long midslist_mem_pool[MIDSLIST_MEM_POOL_SIZE / sizeof(long)];
static char *midslist_mem_start;
static char *midslist_mem_end;

/**************************************************************************
 * List memory management
 **************************************************************************/


/*****************************************************************************
 * FUNCTION
 *  jam_listmem_initialize
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  
 *****************************************************************************/
kal_int32 jam_listmem_initialize()
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    listmem_hdr header;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    midslist_mem_start = (char*)midslist_mem_pool;
    midslist_mem_end = midslist_mem_start + MIDSLIST_MEM_POOL_SIZE;

    header = (listmem_hdr) midslist_mem_start;
    header->magic = MAGIC;
    header->free = 1;
    header->size = MIDSLIST_MEM_POOL_SIZE - sizeof(listmem_struct);

    return 0;
}


/*****************************************************************************
 * FUNCTION
 *  jam_listmem_finalize
 * DESCRIPTION
 *  
 * PARAMETERS
 *  void
 * RETURNS
 *  
 *****************************************************************************/
int jam_listmem_finalize()
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    listmem_hdr header;
    char *work;
    int count, size;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    count = 0;
    size = 0;

    for (work = midslist_mem_start; work < midslist_mem_end; work += header->size + sizeof(listmem_struct))
    {

        header = (listmem_hdr) work;

        if (header->magic != MAGIC)
        {
            return -1;
        }
        else if (header->free != 1)
        {
            /* possible leak */
            jam_listmem_free((void*)((char*)header + sizeof(listmem_struct)));
            count += 1;
            size += header->size;
        }
    }
    return count;
}


/*****************************************************************************
 * FUNCTION
 *  jam_listmem_malloc
 * DESCRIPTION
 *  
 * PARAMETERS
 *  size        [IN]        
 * RETURNS
 *  void
 *****************************************************************************/
void *jam_listmem_malloc(int size)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    int allocate_size;
    void *loc = NULL;
    listmem_hdr tmp_header = NULL;
    char *temp = NULL, *mem_ptr;
    char *mem_pool_start = midslist_mem_start;
    char *mem_pool_end = midslist_mem_end;
    listmem_hdr header;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (size <= 0)
    {
        return NULL;
    }

    /* 4bytes alignment */
    allocate_size = (size + ALIGNMENT) & 0xfffffffc;

    /* find a free slot */
    for (mem_ptr = mem_pool_start; mem_ptr < mem_pool_end; mem_ptr += header->size + sizeof(listmem_struct))
    {

        header = (listmem_hdr) mem_ptr;
        if (header->magic != MAGIC)
        {
            sprintf(_kvmLogStr, "J2ME Error: Memory corruption at 0x%08x", (int)mem_ptr);
            Kprintf();
            ASSERT(0);
            return ((void*)0);
        }
        else
        {
            while (1)
            {
                /* coalescing */
                if (header->free == 1)
                {
                    /* if current block is free */
                    temp = (char*)header;
                    temp += header->size + sizeof(listmem_struct);
                    tmp_header = (listmem_hdr) temp;

                    if ((tmp_header->free == 1) && (tmp_header->magic == MAGIC) && (temp < mem_pool_end))
                    {
                        /* and the next block is free too */
                        /* then coalesce */
                        header->size += tmp_header->size + sizeof(listmem_struct);
                    }
                    else
                    {
                        break;
                    }
                }
                else
                {
                    break;
                }
            }

            if ((header->free == 1) && (header->size >= (unsigned)allocate_size))
            {

                if (header->size > (allocate_size + sizeof(listmem_struct) + 4))
                {
                    listmem_hdr next_header;

                    /* split block */
                    next_header = (listmem_hdr) ((char*)mem_ptr + allocate_size + sizeof(listmem_struct));
                    next_header->magic = MAGIC;
                    next_header->free = 1;
                    next_header->size = header->size - allocate_size - sizeof(listmem_struct);
                    header->size = allocate_size;
                }
                header->free = 0;
                loc = (void*)((char*)header + sizeof(listmem_struct));
                return (loc);
            }
        }
    }

    sprintf(_kvmLogStr, "J2ME Error: list memory not available, size:%d", allocate_size);
    Kprintf();

    return ((void*)0);
}


/*****************************************************************************
 * FUNCTION
 *  jam_listmem_free
 * DESCRIPTION
 *  
 * PARAMETERS
 *  ptr     [?]     
 * RETURNS
 *  void
 *****************************************************************************/
void jam_listmem_free(void *ptr)
{
    /*----------------------------------------------------------------*/
    /* Local Variables                                                */
    /*----------------------------------------------------------------*/
    listmem_hdr header;
    char *mem_pool_start = midslist_mem_start;
    char *mem_pool_end = midslist_mem_end;

    /*----------------------------------------------------------------*/
    /* Code Body                                                      */
    /*----------------------------------------------------------------*/
    if (ptr == NULL)
    {
        Kputs("J2ME Warning: Attempt to free NULL pointer\n");
    }
    else if (((char*)ptr > mem_pool_end) || ((char*)ptr < mem_pool_start))
    {
        sprintf(_kvmLogStr, "J2ME Error: Attempt to free memory out of scope: 0x%08x", (int)ptr);
        Kprintf();
    }
    else
    {
        header = (listmem_hdr) ((char*)ptr - sizeof(listmem_struct));

        if (header->magic != MAGIC)
        {
            sprintf(_kvmLogStr, "J2ME Error: Attempt to free corrupted memory: 0x%08x", (int)ptr);
            Kprintf();
        }
        else if (header->free != 0)
        {
            sprintf(_kvmLogStr, "J2ME Warning: Attempt to free memory twice: 0x%08x", (int)ptr);
            Kprintf();
        }
        else
        {
            header->free = 1;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -