免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1274 | 回复: 0
打印 上一主题 下一主题

KVM移植之ITRON版 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-05-08 11:56 |只看该作者 |倒序浏览
    前段时间由于工作的需要对KVM做了一些研究和学习,最终的目标是要理解KVM的原理和移植要注意的问题,最终拿出一个移植成功的DEMO来。
    说起JAVA也只是在大学里学过一个月,后来写了一个记事本,后来就没怎么碰JAVA了。这次要学习它的解释器有点突然,不过经过努力最终还是实现了预期的目标。
    因为Itron是日本那边用得比较多的嵌入式操作系统,没有文件系统,只有内存管理和任务管理(具体也不是很了解)。庆幸的是KVM实现得很合理,都是用标准C实现的,所以移植起来费的力气并不是很大。
一,文件管理[重新实现标准库函数(fopen,fread,fgetc,feof,fsize,fseek等)]
    鉴于Itron没有文件系统,而KVM的库和CLASS文件允许以ROM的形式一起编译进KVM,同时KVM的文件操作是基于标准C的,这就为我们的移植提供了方便。比方说我们有Hello.java的JAVA程序,这个Hello.java里有一个Hello的类,这个类只做一件是就是输出"Hello World"。我们把它编译成Hello.class之后把这个class文件用其他工具将其转换成二进制数组并存入一个C变量里:HelloClass[]="…"。
    那么我们得重新实现fopen等函数,当KVM在执行打开文件,读取文件等文件操作时将是对HelloClass等类似的数据进行操作(最后给出了一个例子)。这样的话对loader.c就不需要做什么修改了。
二,内存管理
    由于Itron有自己的内存管理,而我也发现在Itron的系统里有C++的new实现。正好,我将它的new实现移植了过来,实现了我们的malloc,calloc,realloc和free函数.代码在最后。
到这里KVM的移植工作基本完成,接下来要做的就是一些优化工作。以上只是一种移植到没有文件系统的操作系统上的解决方案,也是我能想到的最好最快的方案。因为在存储空间有限的嵌入式系统里,有一个文件系统当然会省很多事,但是文件系统的实现或移植并不是一件乘简单的事情。我们当初也考虑过用MSDOS的文件系统,但要在短时间内拿出DEMO来并非易事,所以没有采用。而文件系统在管理文件时也是将文件写在FLASH中,然后对FLASH进行管理,与其这样还不如静态连接入KVM,我觉得采用这套方案是最佳的。可能会有些不便,但是对于应用不多的嵌入式系统来说还是很值得的。
/*
* Copyright ?2003 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*/
/*=========================================================================
* KVM
*=========================================================================
* SYSTEM: KVM
* SUBSYSTEM: Itron-specific functions needed by the virtual machine
* FILE: Hello.h
* AUTHOR: yangyoufa(blueshycool@gmail.com) (Itron port @2007/04/16)
*=======================================================================*/
#ifndef __ITRON_HELLO_H__
#define __ITRON_HELLO_H__
static char Helloclass[] = {
   
     0xCA, 0xFE, 0xBA, 0xBE, 0x00, 0x03, 0x00, 0x2D,
     0x00, 0x1D, 0x0A, 0x00, 0x06, 0x00, 0x0F, 0x09,
     0x00, 0x10, 0x00, 0x11, 0x08, 0x00, 0x12, 0x0A,
     0x00, 0x13, 0x00, 0x14, 0x07, 0x00, 0x15, 0x07,
     0x00, 0x16, 0x01, 0x00, 0x06, 0x3C, 0x69, 0x6E,
     0x69, 0x74, 0x3E, 0x01, 0x00, 0x03, 0x28, 0x29,
     0x56, 0x01, 0x00, 0x04, 0x43, 0x6F, 0x64, 0x65,
     0x01, 0x00, 0x0F, 0x4C, 0x69, 0x6E, 0x65, 0x4E,
     0x75, 0x6D, 0x62, 0x65, 0x72, 0x54, 0x61, 0x62,
     0x6C, 0x65, 0x01, 0x00, 0x04, 0x6D, 0x61, 0x69,
     0x6E, 0x01, 0x00, 0x16, 0x28, 0x5B, 0x4C, 0x6A,
     0x61, 0x76, 0x61, 0x2F, 0x6C, 0x61, 0x6E, 0x67,
     0x2F, 0x53, 0x74, 0x72, 0x69, 0x6E, 0x67, 0x3B,
     0x29, 0x56, 0x01, 0x00, 0x0A, 0x53, 0x6F, 0x75,
     0x72, 0x63, 0x65, 0x46, 0x69, 0x6C, 0x65, 0x01,
     0x00, 0x0A, 0x48, 0x65, 0x6C, 0x6C, 0x6F, 0x2E,
     0x6A, 0x61, 0x76, 0x61, 0x0C, 0x00, 0x07, 0x00,
     0x08, 0x07, 0x00, 0x17, 0x0C, 0x00, 0x18, 0x00,
     0x19, 0x01, 0x00, 0x0D, 0x48, 0x65, 0x6C, 0x6C,
     0x6F, 0x20, 0x57, 0x6F, 0x72, 0x6C, 0x64, 0x21,
     0x0A, 0x07, 0x00, 0x1A, 0x0C, 0x00, 0x1B, 0x00,
     0x1C, 0x01, 0x00, 0x05, 0x48, 0x65, 0x6C, 0x6C,
     0x6F, 0x01, 0x00, 0x10, 0x6A, 0x61, 0x76, 0x61,
     0x2F, 0x6C, 0x61, 0x6E, 0x67, 0x2F, 0x4F, 0x62,
     0x6A, 0x65, 0x63, 0x74, 0x01, 0x00, 0x10, 0x6A,
     0x61, 0x76, 0x61, 0x2F, 0x6C, 0x61, 0x6E, 0x67,
     0x2F, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6D, 0x01,
     0x00, 0x03, 0x6F, 0x75, 0x74, 0x01, 0x00, 0x15,
     0x4C, 0x6A, 0x61, 0x76, 0x61, 0x2F, 0x69, 0x6F,
     0x2F, 0x50, 0x72, 0x69, 0x6E, 0x74, 0x53, 0x74,
     0x72, 0x65, 0x61, 0x6D, 0x3B, 0x01, 0x00, 0x13,
     0x6A, 0x61, 0x76, 0x61, 0x2F, 0x69, 0x6F, 0x2F,
     0x50, 0x72, 0x69, 0x6E, 0x74, 0x53, 0x74, 0x72,
     0x65, 0x61, 0x6D, 0x01, 0x00, 0x07, 0x70, 0x72,
     0x69, 0x6E, 0x74, 0x6C, 0x6E, 0x01, 0x00, 0x15,
     0x28, 0x4C, 0x6A, 0x61, 0x76, 0x61, 0x2F, 0x6C,
     0x61, 0x6E, 0x67, 0x2F, 0x53, 0x74, 0x72, 0x69,
     0x6E, 0x67, 0x3B, 0x29, 0x56, 0x00, 0x21, 0x00,
     0x05, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
     0x02, 0x00, 0x01, 0x00, 0x07, 0x00, 0x08, 0x00,
     0x01, 0x00, 0x09, 0x00, 0x00, 0x00, 0x1D, 0x00,
     0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x05, 0x2A,
     0xB7, 0x00, 0x01, 0xB1, 0x00, 0x00, 0x00, 0x01,
     0x00, 0x0A, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01,
     0x00, 0x00, 0x00, 0x01, 0x00, 0x09, 0x00, 0x0B,
     0x00, 0x0C, 0x00, 0x01, 0x00, 0x09, 0x00, 0x00,
     0x00, 0x25, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00,
     0x00, 0x09, 0xB2, 0x00, 0x02, 0x12, 0x03, 0xB6,
     0x00, 0x04, 0xB1, 0x00, 0x00, 0x00, 0x01, 0x00,
     0x0A, 0x00, 0x00, 0x00, 0x0A, 0x00, 0x02, 0x00,
     0x00, 0x00, 0x03, 0x00, 0x08, 0x00, 0x04, 0x00,
     0x01, 0x00, 0x0D, 0x00, 0x00, 0x00, 0x02, 0x00,
     0x0E };
#endif /* __ITRON_HELLO_H__ */
/*
* Copyright ?2003 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*/
/*=========================================================================
* KVM
*=========================================================================
* SYSTEM: KVM
* SUBSYSTEM: Itron-specific functions needed by the virtual machine
* FILE: classes.h
* AUTHOR: yangyoufa(blueshycool@gmail.com) (Itron port @2007/04/16)
*=======================================================================*/
#ifndef __ITRON_CLASSES_H__
#define __ITRON_CLASSES_H__
#include "classes/hello.class.h"
/**
* The type of file, may be class and (jar or zip)
*
**/
typedef enum file_type {
    ITRON_UNKNOW = -1,
    ITRON_CLASS = 0,
    ITRON_JAR
} file_type_t;
/**
* File entry of class or jar package
*
**/
typedef struct file_entry {
    const char* filename;
    UI32 size;
    void* data;
    file_type_t type;
} file_entry_t;
/**
* All entry of files
**/
static file_entry_t CLASS_FILES[] = {
    {"./Hello.class", sizeof(Helloclass), &Helloclass, ITRON_CLASS},
    {"./HelloHello.class", sizeof(HelloHelloclass), &HelloHelloclass, ITRON_CLASS},
    {NULL, 0, "", ITRON_UNKNOW}
};
#define CLASS_FILES_SIZE (sizeof(CLASS_FILES))
#if 1
#define CLASS_FILES_LEN ((CLASS_FILES_SIZE)/(sizeof(file_entry_t)) - 1 )
#else
#define CLASS_FILES_LEN 1
#endif
#endif /* __ITRON_CLASSES_H__ */
/*
* Copyright ?2003 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*/
/*=========================================================================
* KVM
*=========================================================================
* SYSTEM: KVM
* SUBSYSTEM: Itron-specific functions needed by the virtual machine
* FILE: itron_io.h
* AUTHOR: yangyoufa(blueshycool@gmail.com) (Itron port @2007/04/13)
*=======================================================================*/
#ifndef __ITRON_IO_H__
#define __ITRON_IO_H__
#if 0 /* FILE structure */
typedef struct _iobuf {
    unsigned char *_io_next;    /* next character to be read from buffer or
                 next position to put a character in buffer */
    unsigned char *_io_base;    /* start of buffer */
    long _io_left;        /* number of characters left in buffer */
    unsigned _io_channel:14;    /* OS I/O channel number */ /* GWW: was short */
    unsigned _io_ivbuf: 1;    /* the read buffer differs from disk (ungetc called) */
    unsigned _io_append: 1;    /* should do seeks to eof before writing */
    char _io_tmp;        /* used by ungetc on unbuffered files */
    unsigned _io_buffering:2;    /* _IONBF,_IOFBF,_IOLBF */
    unsigned _io_eof: 1;     /* have read end of file */
    unsigned _io_error:1;    /* have detected io error in file */
    unsigned _io_stdio_buffer:1; /* buffer for this file created by stdio */
    unsigned _io_readable:1;    /* file may be read at this time */
    unsigned _io_writable:1;    /* file may be written at this time */
    unsigned _io_readwrite:1;    /* file opened for both reading and writing */
#if __CHAR_BIT > 8
    unsigned _io_binary:1; /* file opened in binary mode */
    unsigned _io_text:1; /* file opened in text mode */
#endif
} FILE;
#endif
/*=========================================================================
* Include files
*=======================================================================*/
/* For macros or definitions */
#include
#include
/* RTOS Interface */
#include "mmac/rtos.h"
FILE *itron_fopen(const char *path, const char *mode);
int itron_fclose(FILE *stream);
UI32 itron_fread(void *ptr, UI32 size, UI32 nmemb, FILE *stream);
UI32 itron_fwrite(const void *ptr, UI32 size, UI32 nmemb, FILE* stream);
int itron_fseek(FILE *stream, long offset, int whence);
long itron_ftell(FILE *stream);
int itron_getc(FILE *stream);
#endif /* __ITRON_IO_H__ */
/*
* Copyright ?2003 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*/
/*=========================================================================
* KVM
*=========================================================================
* SYSTEM: KVM
* SUBSYSTEM: Itron-specific functions needed by the virtual machine
* FILE: itron_io.c
* AUTHOR: yangyoufa(blueshycool@gmail.com) (Itron port @2007/04/13)
*=======================================================================*/
/*=========================================================================
* Include files
*=======================================================================*/
#include "itron_io.h"
#include "itron_mem.h"
#include "classes.h"
#include
static file_entry_t* find_entry(const char* path) {
    int i;
    if(path == NULL) {
    return NULL;
    }
   
    for(i=0; i_io_base = fp->_io_next = file->data;
   
     fp->_io_left = file->size;
     /*
     fp->_io_channel = 0;
     fp->_io_ivbuf = 0;
     fp->_io_append     = 0;
     fp->_io_tmp        = 0;
     fp->_io_buffering    = 0;
     fp->_io_eof        = 0;
     fp->_io_error    = 0;   
     fp->_io_stdio_buffer= 0;
     */
     /* Read only */
     fp->_io_readable = 1;
     /*
     fp->_io_writable = 0;
     fp->_io_readwrite = 0;
     */
     /* Binary only */
     #if __CHAR_BIT > 8
     fp->_io_binary = 1;
     /*fp->_io_text = 0;*/
            #endif
         
     return fp;
    } else {
     return NULL;
    }
    } else {
    return NULL;
    }
}
int itron_fclose(FILE *stream) {
    if(stream != NULL) {
    free(stream);
    }
    return 0;
}
UI32 itron_fread(void *ptr, UI32 size, UI32 nmemb, FILE *stream) {
    UI32 length;
    FILE* fp = stream;
   
    if(ptr==NULL || size_io_eof) { /* Not eof */
    if(length _io_left) {
     memcpy(ptr, fp->_io_next, length);
     fp->_io_left -= length;
     fp->_io_next += length;
    } else {
     memcpy(ptr, fp->_io_next, fp->_io_left);
    }
    if(length >= fp->_io_left) {
     fp->_io_eof = 1;
     if(length > fp->_io_left) {
        return 0; /* Or other value */
     }
    }
    } else {
    return -1;
    }
    return nmemb;
}
UI32 itron_fwrite(const void *ptr, UI32 size, UI32 nmemb, FILE* stream) {
}
int itron_fseek(FILE *stream, long offset, int whence) {
    FILE* fp = stream;
    UI32 size;
    if(fp != NULL) {
    size = fp->_io_left + (fp->_io_next - fp->_io_base);
    switch(whence) {
     case SEEK_SET: {/* errors? */
        if(offset _io_next = (fp->_io_base + offset);
        if(fp->io_next >= fp->_io_base &&
            fp->_io_next _io_base + size) {
         fp->_io_left = size - offset;
         return 0;
        }
     }
     break;
     case SEEK_CUR: {
        fp->_io_next += offset;
        if(fp->io_next >= fp->_io_base &&
            fp->_io_next _io_base + size) {
         if(offset >= 0) {
            fp->_io_left -= offset;
         } else {
            fp->_io_left += offset;
         }
         return 0;
        }
     }
     break;
     case SEEK_END: {
        if(offset > 0) {
         return -1;
        }
        fp->_io_next -= offset;
        if(fp->io_next >= fp->_io_base &&
            fp->_io_next _io_base + size) {
         fp->_io_left += (-1*offset);
         return 0;
        }
     }
     break;
    default:
     break;
    }
    }
   
    return -1;
}
long itron_ftell(FILE *stream) {
    FILE* fp = stream;
    if(fp != NULL) {
    return (fp->_io_next - fp->_io_base);
    }
    return -1;
}
int itron_getc(FILE *stream) {
    char ch;
    FILE* fp = stream;
   
    if(fp != NULL && fp->_io_left) { /* Best */
    ch = (char)(*(fp->_io_next));
    fp->_io_next++;
    fp->_io_left--;
    if(!fp->_io_left) {
     fp->_io_eof = 1;
    }
    return ch;
    }
    return EOF;
}
void itron_rewind(FILE *stream) {
    FILE* fp = stream;
    UI32 size = fp->_io_left + (fp->_io_next - fp->_io_base);
   
    if(fp == NULL) {
    return ;
    }
   
    fp->_io_next = fp->_io_base;
    fp->_io_left = size;
    fp->_io_eof = 0;
/*fp->_io_channel = 0;
  fp->_io_ivbuf = 0;
  fp->_io_append     = 0;
  fp->_io_tmp        = 0;
  fp->_io_buffering    = 0;
  fp->_io_eof        = 0;
  fp->_io_error    = 0;   
  fp->_io_stdio_buffer= 0;
*/
/* Read only */
fp->_io_readable = 1;
/*
fp->_io_writable = 0;
fp->_io_readwrite = 0;
*/
/* Binary only */
#if __CHAR_BIT > 8
fp->_io_binary = 1;
/*fp->_io_text = 0;*/
#endif
}
UI32 itron_fsize(FILE* fp) {
    if(fp != NULL) {
        return (fp->_io_left + (fp->_io_next - fp->_io_base));
    }
    return -1;
}
/* 当然我们得对global.h做些相应的修改,就是将标准库函数的调用指向我们自己的实现如:
#ifdef ITRON /* Reimplemention of file I/O functions (Add @2007/04/16 by yangyoufa) */
#define fopen itron_fopen
#define fclose itron_fclose
#define fread itron_fread
#define fwrite itron_fwrite
#define fseek itron_fseek
#define ftell itron_ftell
#define getc itron_getc
#define rewind itron_rewind
#define malloc itron_malloc
#define calloc itron_calloc
#define realloc itron_realloc
#define free itron_free
#endif
*/
/*
* Copyright ?2003 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*/
/*=========================================================================
* KVM
*=========================================================================
* SYSTEM: KVM
* SUBSYSTEM: Itron-specific functions needed by the virtual machine
* FILE: itron_mem.h
* AUTHOR: yangyoufa(blueshycool@gmail.com) (Itron port @2007/04/16)
*=======================================================================*/
#ifndef __ITRON_MEM_H__
#define __ITRON_MEM_H__
/*=========================================================================
* Include files
*=======================================================================*/
#include "mmac/rtos.h" /* RTOS Interface */
/*=========================================================================
* FUNCTION:    allocates memory for an array of nmemb elements of size
*                bytes each and returns a pointer to the allocated memory.
*                The memory is set to zero.
* TYPE: Memory control
* OVERVIEW:   
* INTERFACE
* parameters:    nmemb    members
*                    size    the size of array
* returns:        If success returns a pointer to the start address of
*                    the array, or NULL when fail.
*=======================================================================*/
void *itron_calloc(UI32 nmemb, UI32 size);
/*=========================================================================
* FUNCTION:    allocates size bytes and returns a pointer to the
*                allocated memory. The memory is not cleared.
*
* TYPE: Memory control
* OVERVIEW:   
* INTERFACE
* parameters:    size    the size of array
* returns:        If success returns a pointer to the start address of
*                    the array, or NULL when fail.
*=======================================================================*/
void *itron_malloc(UI32 size);
/*=========================================================================
* FUNCTION:    frees the memory space pointed to by ptr, which must have
*                been returned by a previous call to malloc(), calloc() or
*                realloc(). Other- wise, or if free(ptr) has already
*                been called before, undefined behaviour occurs.
*                If ptr is NULL, no operation is performed.
*
* TYPE: Memory control
* OVERVIEW:   
* INTERFACE
* parameters:    ptr The start address of memory will be freed.
* returns:        
*=======================================================================*/
void itron_free(void *ptr);
/*=========================================================================
* FUNCTION:    changes the size of the memory block pointed to by ptr to
*                size bytes. The contents will be unchanged to the minimum
*                of the old and new sizes; newly allocated memory will be
*                uninitialized. If ptr is NULL, the call is equivalent to
*                malloc(size); if size is equal to zero, the call is
*                equivalent to free(ptr). Unless ptr is NULL, it must have
*                been returned by an earlier call to malloc(), calloc() or
*                realloc().
*
* TYPE: Memory control
* OVERVIEW:   
* INTERFACE
* parameters:    ptr The start address of memory will be freed.
*                    size the new size of array
* returns:        
*=======================================================================*/
void *itron_realloc(void *ptr, UI32 size);
#endif /* __ITRON_IO_H__ */
/*
* Copyright ?2003 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*/
/*=========================================================================
* KVM
*=========================================================================
* SYSTEM: KVM
* SUBSYSTEM: Itron-specific functions needed by the virtual machine
* FILE: itron_mem.h
* AUTHOR: yangyoufa(blueshycool@gmail.com) (Itron port @2007/04/16)
*=======================================================================*/
/*=========================================================================
* Include files
*=======================================================================*/
#include "itron_mem.h"
#include "mmac/rtos.h" /* RTOS Interface */
/****************************************************************/
/* MACROS/DEFINES */
/****************************************************************/
#ifdef ITRON
#define ASSERT(x) assert(x)
#else
#define ASSERT(x)
#endif
#define DYNAMIC_POOL_SIZE (0x80000)
#define POOL_OVERHEAD_SIZE (0x20)
#define ALLOC_OVERHEAD_SIZE (0x00)
#define MIN_BLOCK_ALLOCATION (0)
extern MMAC_RTOS_MEMORY_POOL RTOS_systemHeap;
MMAC_RTOS_MEMORY_POOL localPool = 0;
static void* itron_getmem (UI32 sizeInBytes)
{
    MMAC_RTOS_RESULT_CODE result;
    void * memoryBlock;
    char * pFreeMemory;
#if 0
    if (0 == localPool)
    {
        result = MMAC_RTOS_AllocMemory (RTOS_systemHeap,
                                        DYNAMIC_POOL_SIZE +
                                        POOL_OVERHEAD_SIZE +
                                        ALLOC_OVERHEAD_SIZE,
                                        ((void**)&pFreeMemory));
        ASSERT (MMAC_RTOS_OK == result);
        localPool = (MMAC_RTOS_MEMORY_POOL) pFreeMemory;
        pFreeMemory += POOL_OVERHEAD_SIZE;
        result = MMAC_RTOS_CreateMemoryPool ((I8*)"kvmHeap",
                                             pFreeMemory,
                                             DYNAMIC_POOL_SIZE,
                                             MIN_BLOCK_ALLOCATION,
                                             localPool);
        ASSERT (MMAC_RTOS_OK == result);
    }
   
    result = MMAC_RTOS_AllocMemory (localPool, sizeInBytes, (void**)&memoryBlock);
    if (MMAC_RTOS_OK != result)
    {
        memoryBlock = NULL;
    }
#endif
    result = MMAC_RTOS_AllocMemory (RTOS_systemHeap, sizeInBytes, (void**)&memoryBlock);
    ASSERT (MMAC_RTOS_OK == result);
#if (MMAC_ICE_OPERATOR_RESULT_DEBUG_ENABLE == MMAC_YES)
    MMAC_DEBUG_Print(MMAC_DEBUG_MSG, "localGetMemory: memoryBlock = 0x%x\n\r", memoryBlock);
#endif
    return memoryBlock;
}
static void itron_freemem (void * memPtr)
{
    MMAC_RTOS_RESULT_CODE result;
#if (MMAC_ICE_OPERATOR_FUNCTION_DEBUG_ENABLE == MMAC_YES)
    MMAC_DEBUG_Print(MMAC_DEBUG_MSG, "localFreeMemory: memPtr = 0x%x\n\r", memPtr);
#endif
    if(memPtr != NULL)
    {
     result = MMAC_RTOS_FreeMemory (memPtr);
        ASSERT (MMAC_RTOS_OK == result);
    }
    return;
}
/*=========================================================================
* FUNCTION:    allocates memory for an array of nmemb elements of size
*                bytes each and returns a pointer to the allocated memory.
*                The memory is set to zero.
* TYPE: Memory control
* OVERVIEW:   
* INTERFACE
* parameters:    nmemb    members
*                    size    the size of array
* returns:        If success returns a pointer to the start address of
*                    the array, or NULL when fail.
*=======================================================================*/
void *itron_calloc(UI32 nmemb, UI32 size) {
    void* mem;
    UI32 length;
    if(nmemb<=0 || size<=0) {
    return NULL;
    }
    length = nmemb * size;
    mem = itron_getmem(length );
    if(mem != NULL) {
    memset(mem, 0x00, length);
    return mem;
    }
    return NULL;
}
/*=========================================================================
* FUNCTION:    allocates size bytes and returns a pointer to the
*                allocated memory. The memory is not cleared.
*
* TYPE: Memory control
* OVERVIEW:   
* INTERFACE
* parameters:    size    the size of array
* returns:        If success returns a pointer to the start address of
*                    the array, or NULL when fail.
*=======================================================================*/
void *itron_malloc(UI32 size) {
    if(size<=0) {
    return NULL;
    }
    return itron_getmem(size );
}
/*=========================================================================
* FUNCTION:    frees the memory space pointed to by ptr, which must have
*                been returned by a previous call to malloc(), calloc() or
*                realloc(). Other- wise, or if free(ptr) has already
*                been called before, undefined behaviour occurs.
*                If ptr is NULL, no operation is performed.
*
* TYPE: Memory control
* OVERVIEW:   
* INTERFACE
* parameters:    ptr The start address of memory will be freed.
* returns:        
*=======================================================================*/
void itron_free(void *ptr) {
    if(ptr != NULL) {
    itron_freemem(ptr);
    }
}
/*=========================================================================
* FUNCTION:    changes the size of the memory block pointed to by ptr to
*                size bytes. The contents will be unchanged to the minimum
*                of the old and new sizes; newly allocated memory will be
*                uninitialized. If ptr is NULL, the call is equivalent to
*                malloc(size); if size is equal to zero, the call is
*                equivalent to free(ptr). Unless ptr is NULL, it must have
*                been returned by an earlier call to malloc(), calloc() or
*                realloc().
*
* TYPE: Memory control
* OVERVIEW:   
* INTERFACE
* parameters:    ptr The start address of memory will be freed.
*                    size the new size of array
* returns:        
*=======================================================================*/
void *itron_realloc(void *ptr, UI32 size) {
    return realloc(ptr, size); /* Not support current */
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/18584/showart_294251.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP