PageRenderTime 19ms CodeModel.GetById 9ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/src/os/sylixos/ftk_mmap_sylixos.c

http://ftk.googlecode.com/
C | 112 lines | 63 code | 20 blank | 29 comment | 15 complexity | f2f8326d42be5e3aee73a00bb47541d5 MD5 | raw file
  1/*
  2 * File: ftk_mmap_sylixos.c
  3 * Author:  Han.hui <sylixos@gmail.com>
  4 * Brief:   sylixos mmap implemention.
  5 *
  6 * Copyright (c) 2009 - 2010  Li XianJing <xianjimli@hotmail.com>
  7 *
  8 * Licensed under the Academic Free License version 2.1
  9 *
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 23 */
 24
 25/*
 26 * History:
 27 * ================================================================
 28 * 2011-05-15 Han.hui <sylixos@gmail.com> created
 29 *
 30 */
 31
 32#include "ftk_log.h"

 33#include "ftk_mmap.h"

 34#include "ftk_allocator.h"

 35#include <sys/mman.h>

 36
 37struct _FtkMmap
 38{
 39    int fd;
 40    void* data;
 41    size_t length;
 42};
 43
 44int      ftk_mmap_exist(const char* filename)
 45{
 46    struct stat st = {0};
 47    return_val_if_fail(filename != NULL, 0);
 48
 49    return stat(filename, &st) == 0;
 50}
 51
 52FtkMmap* ftk_mmap_create(const char* filename, size_t offset, size_t size)
 53{
 54    FtkMmap* thiz = NULL;
 55    struct stat st = {0};
 56    return_val_if_fail(filename != NULL, NULL);
 57    if(stat(filename, &st) != 0)
 58    {
 59        return NULL;
 60    }
 61    return_val_if_fail(offset < st.st_size, NULL);
 62
 63    size = (offset + size) <= st.st_size ? size : st.st_size - offset;
 64
 65    thiz = FTK_ZALLOC(sizeof(FtkMmap));
 66    return_val_if_fail(thiz != NULL, NULL);
 67
 68    thiz->fd = open(filename, O_RDONLY);
 69    if(thiz->fd > 0)
 70    {
 71        thiz->length = size;
 72        thiz->data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, thiz->fd, offset);
 73    }
 74
 75    if(thiz->data == NULL || thiz->data == MAP_FAILED)
 76    {
 77        close(thiz->fd);
 78        FTK_ZFREE(thiz, sizeof(*thiz));
 79        ftk_logd("%s mmap %s failed.\n", __func__, filename);
 80    }
 81
 82    return thiz;
 83}
 84
 85void*    ftk_mmap_data(FtkMmap* thiz)
 86{
 87    return_val_if_fail(thiz != NULL, NULL);
 88
 89    return thiz->data;
 90}
 91
 92size_t   ftk_mmap_length(FtkMmap* thiz)
 93{
 94    return_val_if_fail(thiz != NULL, 0);
 95
 96    return thiz->length;
 97}
 98
 99void     ftk_mmap_destroy(FtkMmap* thiz)
100{
101    if(thiz != NULL)
102    {
103        close(thiz->fd);
104        munmap(thiz->data, thiz->length);
105
106        FTK_ZFREE(thiz, sizeof(*thiz));
107    }
108
109    return;
110}
111
112