uffs_os.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. This file is part of UFFS, the Ultra-low-cost Flash File System.
  3. Copyright (C) 2005-2009 Ricky Zheng <ricky_gz_zheng@yahoo.co.nz>
  4. UFFS is free software; you can redistribute it and/or modify it under
  5. the GNU Library General Public License as published by the Free Software
  6. Foundation; either version 2 of the License, or (at your option) any
  7. later version.
  8. UFFS is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. or GNU Library General Public License, as applicable, for more details.
  12. You should have received a copy of the GNU General Public License
  13. and GNU Library General Public License along with UFFS; if not, write
  14. to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  15. Boston, MA 02110-1301, USA.
  16. As a special exception, if other files instantiate templates or use
  17. macros or inline functions from this file, or you compile this file
  18. and link it with other works to produce a work based on this file,
  19. this file does not by itself cause the resulting work to be covered
  20. by the GNU General Public License. However the source code for this
  21. file must still be made available in accordance with section (3) of
  22. the GNU General Public License v2.
  23. This exception does not invalidate any other reasons why a work based
  24. on this file might be covered by the GNU General Public License.
  25. */
  26. /**
  27. * \file uffs_os.c
  28. * \brief Emulation on win32 host
  29. * \author Ricky Zheng
  30. */
  31. #include "uffs_config.h"
  32. #include "uffs/uffs_os.h"
  33. #include "uffs/uffs_public.h"
  34. #include <memory.h>
  35. #include <stdlib.h>
  36. #include <time.h>
  37. #include <stdio.h>
  38. #include <windows.h>
  39. #define PFX "os : "
  40. int uffs_SemCreate(OSSEM *sem)
  41. {
  42. HANDLE mutex = CreateMutex(
  43. NULL, // default security attributes
  44. FALSE, // initially not owned
  45. NULL); // unnamed mutex
  46. if (mutex == NULL) {
  47. printf("Create mutex failed !\n");
  48. return -1;
  49. }
  50. else {
  51. *sem = (OSSEM)mutex;
  52. }
  53. return 0;
  54. }
  55. int uffs_SemWait(OSSEM sem)
  56. {
  57. DWORD result;
  58. result = WaitForSingleObject(
  59. (HANDLE)sem, // handle to mutex
  60. INFINITE); // no time-out interval
  61. return result == WAIT_ABANDONED ? -1 : 0;
  62. }
  63. int uffs_SemSignal(OSSEM sem)
  64. {
  65. return ReleaseMutex((HANDLE)sem) ? 0 : -1;
  66. }
  67. int uffs_SemDelete(OSSEM *sem)
  68. {
  69. if (CloseHandle((HANDLE)(*sem))) {
  70. *sem = 0;
  71. return 0;
  72. }
  73. else
  74. return -1;
  75. }
  76. int uffs_OSGetTaskId(void)
  77. {
  78. return 0;
  79. }
  80. unsigned int uffs_GetCurDateTime(void)
  81. {
  82. // FIXME: return system time, please modify this for your platform !
  83. // or just return 0 if you don't care about file time.
  84. time_t tvalue;
  85. tvalue = time(NULL);
  86. return (unsigned int)tvalue;
  87. }
  88. #if CONFIG_USE_SYSTEM_MEMORY_ALLOCATOR > 0
  89. static void * sys_malloc(struct uffs_DeviceSt *dev, unsigned int size)
  90. {
  91. dev = dev;
  92. uffs_Perror(UFFS_MSG_NORMAL, "system memory alloc %d bytes", size);
  93. return malloc(size);
  94. }
  95. static URET sys_free(struct uffs_DeviceSt *dev, void *p)
  96. {
  97. dev = dev;
  98. free(p);
  99. return U_SUCC;
  100. }
  101. void uffs_MemSetupSystemAllocator(uffs_MemAllocator *allocator)
  102. {
  103. allocator->malloc = sys_malloc;
  104. allocator->free = sys_free;
  105. }
  106. #endif
  107. /* debug message output throught 'printf' */
  108. static void output_dbg_msg(const char *msg);
  109. static struct uffs_DebugMsgOutputSt m_dbg_ops = {
  110. output_dbg_msg,
  111. NULL,
  112. };
  113. static void output_dbg_msg(const char *msg)
  114. {
  115. printf("%s", msg);
  116. }
  117. void uffs_SetupDebugOutput(void)
  118. {
  119. uffs_InitDebugMessageOutput(&m_dbg_ops, UFFS_MSG_NOISY);
  120. }