Ignore:
Timestamp:
Apr 5, 2019, 9:26:53 PM (5 years ago)
Author:
coas-nagasima
Message:

mbed関連を更新
シリアルドライバをmbedのHALを使うよう変更
ファイルディスクリプタの処理を更新

File:
1 edited

Legend:

Unmodified
Added
Removed
  • asp3_tinet_ecnl_arm/trunk/ntshell/src/io_stub.c

    r352 r374  
    11/*
    22 *  TOPPERS ECHONET Lite Communication Middleware
    3  * 
     3 *
    44 *  Copyright (C) 2017 Cores Co., Ltd. Japan
    5  * 
     5 *
    66 *  上記著作権者は,以下の(1)~(4)の条件を満たす場合に限り,本ソフトウェ
    77 *  ア(本ソフトウェアを改変したものを含む.以下同じ)を使用・複製・改
     
    2626 *      由に基づく請求からも,上記著作権者およびTOPPERSプロジェクトを
    2727 *      免責すること.
    28  * 
     28 *
    2929 *  本ソフトウェアは,無保証で提供されているものである.上記著作権者お
    3030 *  よびTOPPERSプロジェクトは,本ソフトウェアに関して,特定の使用目的
     
    3232 *  アの利用により直接的または間接的に生じたいかなる損害に関しても,そ
    3333 *  の責任を負わない.
    34  * 
     34 *
    3535 *  @(#) $Id$
    3636 */
     
    4343#include <sil.h>
    4444#include <string.h>
     45#include "syssvc/serial.h"
    4546#include "syssvc/syslog.h"
    4647#include "socket_stub.h"
     
    4950#include "core/ntlibc.h"
    5051#include "kernel_cfg.h"
     52#include "target_syssvc.h"
    5153
    5254int fresult2errno(FRESULT res)
     
    7779}
    7880
    79 int shell_open(const char * path, int flags, void *arg)
    80 {
    81         FRESULT res;
    82         struct _IO_FILE *fp;
     81static int file_close(struct SHELL_FILE *fp);
     82static size_t file_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
     83static size_t file_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
     84static off_t file_seek(struct SHELL_FILE *fp, off_t ofs, int org);
     85static int file_ioctl(struct SHELL_FILE *fp, int req, void *arg);
     86static bool_t file_readable(struct SHELL_FILE *fp);
     87static void file_delete(struct SHELL_FILE *fp);
     88
     89static int dir_close(struct SHELL_FILE *fp);
     90static size_t dir_read(struct SHELL_FILE *fp, unsigned char *data, size_t len);
     91static size_t dir_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len);
     92static off_t dir_seek(struct SHELL_FILE *fp, off_t ofs, int org);
     93static int dir_ioctl(struct SHELL_FILE *fp, int req, void *arg);
     94static bool_t dir_readable(struct SHELL_FILE *fp);
     95static void dir_delete(struct SHELL_FILE *fp);
     96
     97IO_TYPE IO_TYPE_FILE = { file_close, file_read, file_write, file_seek, file_ioctl, file_readable, file_delete };
     98IO_TYPE IO_TYPE_DIR = { dir_close, dir_read, dir_write, dir_seek, dir_ioctl, dir_readable, dir_delete };
     99
     100int shell_open(const char *path, int flags, void *arg)
     101{
     102        FRESULT res;
     103        struct SHELL_FILE *fp;
    83104
    84105        if (flags & O_DIRECTORY) {
    85                 fp = new_dir_fd(0);
     106                fp = new_fp(&IO_TYPE_DIR, 0, 0);
    86107                if (fp == NULL)
    87108                        return -ENOMEM;
    88109
    89                 DIR *dir = &fp->pdir->dir;
     110                fp->exinf = malloc(sizeof(struct SHELL_DIR));
     111                memset(fp->exinf, 0, sizeof(struct SHELL_DIR));
     112
     113                FATFS_DIR *dir = &((struct SHELL_DIR *)fp->exinf)->dir;
    90114                FRESULT res;
    91115                if ((res = f_opendir(dir, path)) != FR_OK) {
     
    95119        }
    96120
    97         fp = new_file_fd(0);
     121        fp = new_fp(&IO_TYPE_FILE, 0, 1);
    98122        if (fp == NULL)
    99123                return -ENOMEM;
     124
     125        fp->exinf = malloc(sizeof(FIL));
     126        memset(fp->exinf, 0, sizeof(FIL));
    100127
    101128        BYTE fmd = 0;
     
    133160        }
    134161
    135         if ((res = f_open(fp->pfile, path, fmd)) == FR_OK) {
     162        if ((res = f_open((FIL *)fp->exinf, path, fmd)) == FR_OK) {
    136163                fp->handle = fp->fd;
    137164                return fp->fd;
     
    141168}
    142169
    143 int file_close(struct _IO_FILE *fp)
    144 {
    145         FRESULT res;
    146 
    147         if ((res = f_close(fp->pfile)) == FR_OK) {
     170int file_close(struct SHELL_FILE *fp)
     171{
     172        FRESULT res;
     173
     174        if ((res = f_close((FIL *)fp->exinf)) == FR_OK) {
    148175                return 0;
    149176        }
     
    152179}
    153180
    154 size_t file_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
     181size_t file_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)
    155182{
    156183        unsigned int ret = 0;
    157184        FRESULT res;
    158185
    159         if ((res = f_read(fp->pfile, data, len, &ret)) != FR_OK)
     186        if ((res = f_read((FIL *)fp->exinf, data, len, &ret)) != FR_OK)
    160187                return -EIO;
    161188
     
    163190}
    164191
    165 size_t file_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
     192size_t file_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)
    166193{
    167194        unsigned int ret = 0;
    168195        FRESULT res;
    169196
    170         if ((res = f_write(fp->pfile, data, len, &ret)) != FR_OK)
     197        if ((res = f_write((FIL *)fp->exinf, data, len, &ret)) != FR_OK)
    171198                return -EIO;
    172199
     
    174201}
    175202
    176 off_t file_seek(struct _IO_FILE *fp, off_t ptr, int dir)
     203off_t file_seek(struct SHELL_FILE *fp, off_t ptr, int dir)
    177204{
    178205        switch (dir) {
     
    191218
    192219        FRESULT res;
    193         if ((res = f_seek(fp->pfile, ptr, dir)) != FR_OK)
     220        if ((res = f_seek((FIL *)fp->exinf, ptr, dir)) != FR_OK)
    194221                return -EIO;
    195222
    196         return fp->pfile->fptr;
    197 }
    198 
    199 int file_ioctl(struct _IO_FILE *fp, int req, void *arg)
     223        return ((FIL *)fp->exinf)->fptr;
     224}
     225
     226int file_ioctl(struct SHELL_FILE *fp, int req, void *arg)
    200227{
    201228        DRESULT res;
    202229
    203         if ((res = disk_ioctl(fp->pfile->fs->drv, req, arg) != RES_OK))
     230        if ((res = disk_ioctl(((FIL *)fp->exinf)->fs->drv, req, arg) != RES_OK))
    204231                return -EINVAL;
    205232
     
    207234}
    208235
     236bool_t file_readable(struct SHELL_FILE *fp)
     237{
     238        return fp->readevt_w != fp->readevt_r;
     239}
     240
     241void file_delete(struct SHELL_FILE *fp)
     242{
     243        free((FIL *)fp->exinf);
     244        fp->exinf = NULL;
     245}
     246
    209247int shell_close(int fd)
    210248{
    211         struct _IO_FILE *fp = fd_to_fp(fd);
    212         if (fp == NULL)
    213                 return -EBADF;
    214 
    215         int ret = fp->close(fp);
     249        struct SHELL_FILE *fp = fd_to_fp(fd);
     250        if (fp == NULL)
     251                return -EBADF;
     252
     253        int ret = fp->type->close(fp);
    216254
    217255        delete_fp(fp);
     
    222260ssize_t shell_read(int fd, void *data, size_t len)
    223261{
    224         struct _IO_FILE *fp = fd_to_fp(fd);
    225         if (fp == NULL)
    226                 return -EBADF;
    227 
    228         return fp->read(fp, (unsigned char *)data, len);
     262        struct SHELL_FILE *fp = fd_to_fp(fd);
     263        if (fp == NULL)
     264                return -EBADF;
     265
     266        return fp->type->read(fp, (unsigned char *)data, len);
    229267}
    230268
     
    232270{
    233271        int result = 0;
    234         struct _IO_FILE *fp = fd_to_fp(fd);
     272        struct SHELL_FILE *fp = fd_to_fp(fd);
    235273        if (fp == NULL)
    236274                return -EBADF;
     
    238276        const struct iovec *end = &iov[iovcnt];
    239277        for (; iov < end; iov++) {
    240                 result += fp->read(fp, (unsigned char *)iov->iov_base, iov->iov_len);
     278                result += fp->type->read(fp, (unsigned char *)iov->iov_base, iov->iov_len);
    241279        }
    242280
     
    246284ssize_t shell_write(int fd, const void *data, size_t len)
    247285{
    248         struct _IO_FILE *fp = fd_to_fp(fd);
    249         if (fp == NULL)
    250                 return -EBADF;
    251 
    252         return fp->write(fp, (unsigned char *)data, len);
     286        struct SHELL_FILE *fp = fd_to_fp(fd);
     287        if (fp == NULL)
     288                return -EBADF;
     289
     290        return fp->type->write(fp, (unsigned char *)data, len);
    253291}
    254292
     
    256294{
    257295        int result = 0;
    258         struct _IO_FILE *fp = fd_to_fp(fd);
     296        struct SHELL_FILE *fp = fd_to_fp(fd);
    259297        if (fp == NULL)
    260298                return -EBADF;
     
    262300        const struct iovec *end = &iov[iovcnt];
    263301        for (; iov < end; iov++) {
    264                 result += fp->write(fp, (unsigned char *)iov->iov_base, iov->iov_len);
     302                result += fp->type->write(fp, (unsigned char *)iov->iov_base, iov->iov_len);
    265303        }
    266304
     
    270308int shell_llseek(int fd, off_t ptr, off_t *result, int dir)
    271309{
    272         struct _IO_FILE *fp = fd_to_fp(fd);
    273         if (fp == NULL)
    274                 return -EBADF;
    275 
    276         off_t ret = fp->seek(fp, ptr, dir);
     310        struct SHELL_FILE *fp = fd_to_fp(fd);
     311        if (fp == NULL)
     312                return -EBADF;
     313
     314        off_t ret = fp->type->seek(fp, ptr, dir);
    277315        if (ret < 0)
    278316                return ret;
     
    284322int shell_fstat(int fd, struct stat * st)
    285323{
    286         struct _IO_FILE *fp = fd_to_fp(fd);
     324        struct SHELL_FILE *fp = fd_to_fp(fd);
    287325        if (fp == NULL)
    288326                return -EBADF;
     
    296334int shell_fsync(int fd)
    297335{
    298         struct _IO_FILE *fp = fd_to_fp(fd);
     336        struct SHELL_FILE *fp = fd_to_fp(fd);
    299337        if (fp == NULL)
    300338                return -EBADF;
     
    304342int shell_ftruncate(int fd, off_t length)
    305343{
    306         struct _IO_FILE *fp = fd_to_fp(fd);
    307         if (fp == NULL)
    308                 return -EBADF;
    309 
    310         FRESULT res;
    311         if ((res = f_truncate(fp->pfile)) != FR_OK)
     344        struct SHELL_FILE *fp = fd_to_fp(fd);
     345        if (fp == NULL)
     346                return -EBADF;
     347
     348        FRESULT res;
     349        if ((res = f_truncate((FIL *)fp->exinf)) != FR_OK)
    312350                return fresult2errno(res);
    313351
     
    320358}
    321359
     360extern IO_TYPE IO_TYPE_SIO;
     361
    322362int sio_tcgetattr(int fd, struct termios *termios)
    323363{
    324         extern ntstdio_t ntstdio;
    325 
    326         if (fd == STDIN_FILENO) {
    327                 memset(termios, 0, sizeof(*termios));
    328 
    329                 if (ntstdio.option & NTSTDIO_OPTION_LINE_ECHO) {
    330                         termios->c_lflag |= ECHO;
     364        struct SHELL_FILE *fp = fd_to_fp(fd);
     365        if ((fp == NULL) || (fp->type != &IO_TYPE_SIO))
     366                return -EBADF;
     367
     368        ntstdio_t *ntstdio = (ntstdio_t *)fp->exinf;
     369
     370        memset(termios, 0, sizeof(*termios));
     371
     372        if (ntstdio->option & NTSTDIO_OPTION_LINE_ECHO) {
     373                termios->c_lflag |= ECHO;
     374        }
     375        else {
     376                termios->c_lflag &= ~ECHO;
     377        }
     378        if (ntstdio->option & NTSTDIO_OPTION_CANON) {
     379                termios->c_lflag |= ICANON;
     380        }
     381        else {
     382                termios->c_lflag &= ~ICANON;
     383        }
     384        if (ntstdio->option & NTSTDIO_OPTION_LF_CR) {
     385                termios->c_iflag |= INLCR;
     386        }
     387        else {
     388                termios->c_iflag &= ~INLCR;
     389        }
     390        if (ntstdio->option & NTSTDIO_OPTION_LF_CRLF) {
     391                termios->c_oflag |= ONLCR;
     392        }
     393        else {
     394                termios->c_oflag &= ~ONLCR;
     395        }
     396
     397        return 0;
     398}
     399
     400int sio_tcsetattr(int fd, int optional_actions, const struct termios *termios)
     401{
     402        struct SHELL_FILE *fp = fd_to_fp(fd);
     403        if ((fp == NULL) || (fp->type != &IO_TYPE_SIO))
     404                return -EBADF;
     405
     406        ntstdio_t *ntstdio = (ntstdio_t *)fp->exinf;
     407
     408        if (optional_actions == TCSANOW) {
     409                if (termios->c_lflag & ECHO) {
     410                        ntstdio->option |= NTSTDIO_OPTION_LINE_ECHO;
    331411                }
    332412                else {
    333                         termios->c_lflag &= ~ECHO;
    334                 }
    335                 if (ntstdio.option & NTSTDIO_OPTION_CANON) {
    336                         termios->c_lflag |= ICANON;
     413                        ntstdio->option &= ~NTSTDIO_OPTION_LINE_ECHO;
     414                }
     415                if (termios->c_lflag & ICANON) {
     416                        ntstdio->option |= NTSTDIO_OPTION_CANON;
    337417                }
    338418                else {
    339                         termios->c_lflag &= ~ICANON;
    340                 }
    341                 if (ntstdio.option & NTSTDIO_OPTION_LF_CR) {
    342                         termios->c_iflag |= INLCR;
     419                        ntstdio->option &= ~NTSTDIO_OPTION_CANON;
     420                }
     421                if (termios->c_iflag & INLCR) {
     422                        ntstdio->option |= NTSTDIO_OPTION_LF_CR;
    343423                }
    344424                else {
    345                         termios->c_iflag &= ~INLCR;
    346                 }
    347                 if (ntstdio.option & NTSTDIO_OPTION_LF_CRLF) {
    348                         termios->c_oflag |= ONLCR;
     425                        ntstdio->option &= ~NTSTDIO_OPTION_LF_CR;
     426                }
     427                if (termios->c_oflag & ONLCR) {
     428                        ntstdio->option |= NTSTDIO_OPTION_LF_CRLF;
    349429                }
    350430                else {
    351                         termios->c_oflag &= ~ONLCR;
     431                        ntstdio->option &= ~NTSTDIO_OPTION_LF_CRLF;
    352432                }
    353433                return 0;
    354434        }
    355         shell_abort();
    356         return 0;
    357 }
    358 
    359 int sio_tcsetattr(int fd, int optional_actions, const struct termios *termios)
    360 {
    361         extern ntstdio_t ntstdio;
    362 
    363         if ((fd == STDIN_FILENO) && (optional_actions == TCSANOW)) {
    364                 if (termios->c_lflag & ECHO) {
    365                         ntstdio.option |= NTSTDIO_OPTION_LINE_ECHO;
    366                 }
    367                 else {
    368                         ntstdio.option &= ~NTSTDIO_OPTION_LINE_ECHO;
    369                 }
    370                 if (termios->c_lflag & ICANON) {
    371                         ntstdio.option |= NTSTDIO_OPTION_CANON;
    372                 }
    373                 else {
    374                         ntstdio.option &= ~NTSTDIO_OPTION_CANON;
    375                 }
    376                 if (termios->c_iflag & INLCR) {
    377                         ntstdio.option |= NTSTDIO_OPTION_LF_CR;
    378                 }
    379                 else{
    380                         ntstdio.option &= ~NTSTDIO_OPTION_LF_CR;
    381                 }
    382                 if (termios->c_oflag & ONLCR) {
    383                         ntstdio.option |= NTSTDIO_OPTION_LF_CRLF;
    384                 }
    385                 else {
    386                         ntstdio.option &= ~NTSTDIO_OPTION_LF_CRLF;
    387                 }
    388                 return 0;
    389         }
     435
    390436        shell_abort();
    391437        return 0;
     
    396442        FILINFO fi;
    397443        FRESULT ret;
    398 #if _USE_LFN
    399         static char lfn[_MAX_LFN + 1];   /* Buffer to store the LFN */
     444#if FF_USE_LFN
     445        static char lfn[FF_MAX_LFN + 1];   /* Buffer to store the LFN */
    400446        fi.lfname = lfn;
    401447        fi.lfsize = sizeof lfn;
    402448#endif
    403449        if (strcmp(path, ".") == 0) {
    404                 char cwd[_MAX_LFN];
     450                char cwd[FF_MAX_LFN];
    405451                if ((ret = f_getcwd(cwd, sizeof(cwd))) != FR_OK) {
    406452                        return fresult2errno(ret);
     
    426472        st->st_mtim.tv_nsec = 0;
    427473        st->st_mtim.tv_sec = fi.fdate + fi.ftime;
    428         st->st_mode  = (S_IRUSR | S_IRGRP | S_IROTH);
     474        st->st_mode = (S_IRUSR | S_IRGRP | S_IROTH);
    429475        st->st_mode |= (fi.fattrib & AM_RDO) ? 0 : (S_IWUSR | S_IWGRP | S_IWOTH);
    430476        st->st_mode |= (fi.fattrib & (AM_DIR | AM_VOL)) ? S_IFDIR : S_IFREG;
     
    485531        BYTE mask = AM_RDO | AM_SYS; // AM_ARC, AM_HID
    486532
    487         if(mode & S_IREAD) {
    488                 if((mode & S_IWRITE) == 0) {
     533        if (mode & S_IREAD) {
     534                if ((mode & S_IWRITE) == 0) {
    489535                        attr |= AM_RDO;
    490536                }
     
    494540        }
    495541
    496         if((res = f_chmod(path, attr, mask)) != FR_OK) {
     542        if ((res = f_chmod(path, attr, mask)) != FR_OK) {
    497543                return fresult2errno(res);
    498544        }
     
    507553        BYTE mask = AM_RDO | AM_SYS; // AM_ARC, AM_HID
    508554
    509         if(mode & S_IREAD) {
    510                 if((mode & S_IWRITE) == 0) {
     555        if (mode & S_IREAD) {
     556                if ((mode & S_IWRITE) == 0) {
    511557                        attr |= AM_RDO;
    512558                }
     
    516562        }
    517563
    518         if((res = f_chmod(path, attr, mask)) != FR_OK) {
     564        if ((res = f_chmod(path, attr, mask)) != FR_OK) {
    519565                return fresult2errno(res);
    520566        }
     
    526572{
    527573        FRESULT ret;
    528         if((ret = f_getcwd(buf, size)) != FR_OK) {
     574        if ((ret = f_getcwd(buf, size)) != FR_OK) {
    529575                return NULL;
    530576        }
     
    549595}
    550596
    551 int dir_close(struct _IO_FILE *fp)
    552 {
    553         FRESULT res;
    554         if ((res = f_closedir(&fp->pdir->dir)) != FR_OK) {
     597int dir_close(struct SHELL_FILE *fp)
     598{
     599        FRESULT res;
     600        if ((res = f_closedir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) {
    555601                return fresult2errno(res);
    556602        }
     
    564610                return -EINVAL;
    565611
    566         struct _IO_FILE *fp = fd_to_fp(fd);
     612        struct SHELL_FILE *fp = fd_to_fp(fd);
    567613        if (fp == NULL)
    568614                return -EBADF;
    569615
    570616        FILINFO fno;
    571 #if _USE_LFN
    572         static char lfn[_MAX_LFN + 1];   /* Buffer to store the LFN */
     617#if FF_USE_LFN
     618        static char lfn[FF_MAX_LFN + 1];   /* Buffer to store the LFN */
    573619        fno.lfname = lfn;
    574620        fno.lfsize = sizeof lfn;
    575621#endif
    576622        FRESULT res;
    577         if ((res = f_readdir(&fp->pdir->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {
     623        if ((res = f_readdir(&((struct SHELL_DIR *)fp->exinf)->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {
    578624                return fresult2errno(res);
    579625        }
    580626
    581627        memset(de, 0, sizeof(*de));
    582 #if _USE_LFN
     628#if FF_USE_LFN
    583629        ntlibc_strlcpy(de->d_name, *fno.lfname ? fno.lfname : fno.fname, sizeof(de->d_name));
    584630#else
     
    589635}
    590636
    591 size_t dir_read(struct _IO_FILE *fp, unsigned char *data, size_t len)
     637size_t dir_read(struct SHELL_FILE *fp, unsigned char *data, size_t len)
    592638{
    593639        return -EPERM;
    594640}
    595641
    596 size_t dir_write(struct _IO_FILE *fp, const unsigned char *data, size_t len)
     642size_t dir_write(struct SHELL_FILE *fp, const unsigned char *data, size_t len)
    597643{
    598644        return -EPERM;
    599645}
    600646
    601 off_t dir_seek(struct _IO_FILE *fp, off_t ptr, int dir)
     647off_t dir_seek(struct SHELL_FILE *fp, off_t ptr, int dir)
    602648{
    603649        FRESULT res;
     
    607653
    608654        if (ptr == 0) {
    609                 if ((res = f_rewinddir(&fp->pdir->dir)) != FR_OK) {
     655                if ((res = f_rewinddir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) {
    610656                        return fresult2errno(res);
    611657                }
     
    613659        else {
    614660                FILINFO fno;
    615 #if _USE_LFN
    616                 static char lfn[_MAX_LFN + 1];   /* Buffer to store the LFN */
     661#if FF_USE_LFN
     662                static char lfn[FF_MAX_LFN + 1];   /* Buffer to store the LFN */
    617663                fno.lfname = lfn;
    618664                fno.lfsize = sizeof lfn;
    619665#endif
    620                 if ((res = f_rewinddir(&fp->pdir->dir)) != FR_OK) {
     666                if ((res = f_rewinddir(&((struct SHELL_DIR *)fp->exinf)->dir)) != FR_OK) {
    621667                        return fresult2errno(res);
    622668                }
    623669
    624670                for (int i = 0; i < ptr; i++) {
    625                         if ((res = f_readdir(&fp->pdir->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {
     671                        if ((res = f_readdir(&((struct SHELL_DIR *)fp->exinf)->dir, &fno)) != FR_OK || fno.fname[0] == '\0') {
    626672                                return fresult2errno(res);
    627673                        }
     
    632678}
    633679
    634 int dir_ioctl(struct _IO_FILE *fp, int req, void *arg)
     680int dir_ioctl(struct SHELL_FILE *fp, int req, void *arg)
    635681{
    636682        return -EINVAL;
     683}
     684
     685bool_t dir_readable(struct SHELL_FILE *fp)
     686{
     687        return fp->readevt_w != fp->readevt_r;
     688}
     689
     690void dir_delete(struct SHELL_FILE *fp)
     691{
     692        free((struct SHELL_DIR *)fp->exinf);
     693        fp->exinf = NULL;
    637694}
    638695
     
    687744{
    688745        //if ((addr >= (void *)&__HeapBase) && (addr + len < (void *)&__HeapLimit)) {
    689                 return 0;
    690         //}
    691         //return -1;
    692 }
    693 
    694 #include "tlsf.h"
    695 
    696 static tlsf_t sys_tlsf;
    697 static pool_t sys_pool;
    698 
    699 void sys_init(void)
    700 {
    701         sys_tlsf = tlsf_create(&__HeapBase);
    702         if (sys_tlsf == NULL)
    703                 return;
    704 
    705         sys_pool = tlsf_add_pool(sys_tlsf, ((uint8_t *)&__HeapBase) + tlsf_size(), ((intptr_t)&__HeapLimit - (intptr_t)&__HeapBase) - tlsf_size());
    706 }
    707 
    708 void sys_fini(void)
    709 {
    710         tlsf_destroy(sys_tlsf);
    711 }
    712 
    713 void *malloc(size_t size)
    714 {
    715         void *result;
    716         wai_sem(SEM_MALLOC);
    717         result = tlsf_malloc(sys_tlsf, size);
    718         sig_sem(SEM_MALLOC);
    719         if (result == NULL)
    720                 tlsf_check_pool(sys_pool);
    721         return result;
    722 }
    723 
    724 void *calloc(size_t size, size_t count)
    725 {
    726         void *result;
    727         wai_sem(SEM_MALLOC);
    728         result = tlsf_malloc(sys_tlsf, count * size);
    729         sig_sem(SEM_MALLOC);
    730         if (result != NULL)
    731                 memset(result, 0, count * size);
    732         else
    733                 tlsf_check_pool(sys_pool);
    734         return result;
    735 }
    736 
    737 void *realloc(void *ptr, size_t size)
    738 {
    739         void *result;
    740         wai_sem(SEM_MALLOC);
    741         result = tlsf_realloc(sys_tlsf, ptr, size);
    742         sig_sem(SEM_MALLOC);
    743         if (result == NULL)
    744                 tlsf_check_pool(sys_pool);
    745         return result;
    746 }
    747 
    748 void free(void *ptr)
    749 {
    750         wai_sem(SEM_MALLOC);
    751         tlsf_free(sys_tlsf, ptr);
    752         sig_sem(SEM_MALLOC);
    753 }
    754 
     746        return 0;
     747//}
     748//return -1;
     749}
Note: See TracChangeset for help on using the changeset viewer.