PageRenderTime 23ms CodeModel.GetById 1ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/os/rt-thread/ftk_mmap_rtthread.c

http://ftk.googlecode.com/
C | 115 lines | 68 code | 18 blank | 29 comment | 14 complexity | d2c41628967c2af26a52fff4b6ba5e92 MD5 | raw file
  1/*
  2 * File: ftk_mmap_rtthread.c
  3 * Author:  Jiao JinXing <jiaojinxing1987@gmail.com>
  4 * Brief:   RT-Thread mmap implemention.
  5 *
  6 * Copyright (c) 2009 - 2010  Jiao JinXing <jiaojinxing1987@gmail.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 * 2010-10-2 Jiao JinXing <jiaojinxing1987@gmail.com> created
 29 *
 30 */
 31
 32#include "ftk_log.h"
 33#include "ftk_mmap.h"
 34#include "ftk_allocator.h"
 35
 36struct _FtkMmap
 37{
 38	int fd;
 39	void* data;
 40	size_t length;
 41};
 42
 43int      ftk_mmap_exist(const char* filename)
 44{
 45	struct stat st = {0};
 46	return_val_if_fail(filename != NULL, 0);
 47
 48	return stat(filename, &st) == 0;
 49}
 50
 51FtkMmap* ftk_mmap_create(const char* filename, size_t offset, size_t size)
 52{
 53	FtkMmap* thiz = NULL;
 54	struct stat st = {0};
 55	ssize_t ret = 0;
 56	return_val_if_fail(filename != NULL, NULL);
 57	return_val_if_fail(stat(filename, &st) == 0, NULL);
 58	return_val_if_fail(offset < st.st_size, NULL);
 59
 60	size = (offset + size) <= st.st_size ? size : st.st_size - offset;
 61
 62	thiz = FTK_ZALLOC(sizeof(FtkMmap));
 63	return_val_if_fail(thiz != NULL, NULL);
 64
 65	thiz->fd = open(filename, O_RDONLY, 0);
 66	if (thiz->fd < 0)
 67	{
 68		FTK_ZFREE(thiz, sizeof(*thiz));
 69		return NULL;
 70	}
 71
 72	thiz->length = size;
 73	thiz->data = FTK_ZALLOC(size);
 74	if(thiz->data == NULL)
 75	{
 76		close(thiz->fd);
 77		FTK_ZFREE(thiz, sizeof(*thiz));
 78		return NULL;
 79	}
 80
 81	ret = read(thiz->fd, thiz->data, size);
 82	close(thiz->fd);
 83	if (ret != size)
 84	{
 85		FTK_ZFREE(thiz->data, size);
 86		FTK_ZFREE(thiz, sizeof(*thiz));
 87		return NULL;
 88	}
 89
 90	return thiz;
 91}
 92
 93void*    ftk_mmap_data(FtkMmap* thiz)
 94{
 95	return_val_if_fail(thiz != NULL, NULL);
 96
 97	return thiz->data;
 98}
 99
100size_t   ftk_mmap_length(FtkMmap* thiz)
101{
102	return_val_if_fail(thiz != NULL, 0);
103
104	return thiz->length;
105}
106
107void     ftk_mmap_destroy(FtkMmap* thiz)
108{
109	if (thiz != NULL)
110	{
111		FTK_ZFREE(thiz->data, thiz->length);
112		FTK_ZFREE(thiz, sizeof(*thiz));
113	}
114}
115