/src/os/linux/ftk_mmap_linux.c

http://ftk.googlecode.com/ · C · 112 lines · 63 code · 20 blank · 29 comment · 15 complexity · 2b6ff8bf8ad673ce72aab562cda95ebf MD5 · raw file

  1. /*
  2. * File: ftk_mmap_linux.c
  3. * Author: Li XianJing <xianjimli@hotmail.com>
  4. * Brief: linux 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. * History:
  26. * ================================================================
  27. * 2009-11-20 Li XianJing <xianjimli@hotmail.com> created
  28. *
  29. */
  30. #include "ftk_log.h"
  31. #include "ftk_mmap.h"
  32. #include "ftk_allocator.h"
  33. #include <sys/mman.h>
  34. struct _FtkMmap
  35. {
  36. int fd;
  37. void* data;
  38. size_t length;
  39. };
  40. int ftk_mmap_exist(const char* filename)
  41. {
  42. struct stat st = {0};
  43. return_val_if_fail(filename != NULL, 0);
  44. return stat(filename, &st) == 0;
  45. }
  46. FtkMmap* ftk_mmap_create(const char* filename, size_t offset, size_t size)
  47. {
  48. FtkMmap* thiz = NULL;
  49. struct stat st = {0};
  50. return_val_if_fail(filename != NULL, NULL);
  51. if(stat(filename, &st) != 0)
  52. {
  53. return NULL;
  54. }
  55. return_val_if_fail(offset < st.st_size, NULL);
  56. size = (offset + size) <= st.st_size ? size : st.st_size - offset;
  57. thiz = FTK_ZALLOC(sizeof(FtkMmap));
  58. return_val_if_fail(thiz != NULL, NULL);
  59. thiz->fd = open(filename, O_RDONLY);
  60. if(thiz->fd > 0)
  61. {
  62. thiz->length = size;
  63. thiz->data = mmap(NULL, size, PROT_READ, MAP_PRIVATE, thiz->fd, offset);
  64. }
  65. if(thiz->data == NULL || thiz->data == MAP_FAILED)
  66. {
  67. close(thiz->fd);
  68. FTK_ZFREE(thiz, sizeof(*thiz));
  69. ftk_logd("%s mmap %s failed.\n", __func__, filename);
  70. }
  71. return thiz;
  72. }
  73. void* ftk_mmap_data(FtkMmap* thiz)
  74. {
  75. return_val_if_fail(thiz != NULL, NULL);
  76. return thiz->data;
  77. }
  78. size_t ftk_mmap_length(FtkMmap* thiz)
  79. {
  80. return_val_if_fail(thiz != NULL, 0);
  81. return thiz->length;
  82. }
  83. void ftk_mmap_destroy(FtkMmap* thiz)
  84. {
  85. if(thiz != NULL)
  86. {
  87. close(thiz->fd);
  88. munmap(thiz->data, thiz->length);
  89. FTK_ZFREE(thiz, sizeof(*thiz));
  90. }
  91. return;
  92. }