xref: /nrf52832-nimble/rt-thread/components/dfs/filesystems/uffs/src/platform/posix/uffs_os.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2   This file is part of UFFS, the Ultra-low-cost Flash File System.
3 
4   Copyright (C) 2005-2009 Ricky Zheng <[email protected]>
5 
6   UFFS is free software; you can redistribute it and/or modify it under
7   the GNU Library General Public License as published by the Free Software
8   Foundation; either version 2 of the License, or (at your option) any
9   later version.
10 
11   UFFS is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   or GNU Library General Public License, as applicable, for more details.
15 
16   You should have received a copy of the GNU General Public License
17   and GNU Library General Public License along with UFFS; if not, write
18   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19   Boston, MA  02110-1301, USA.
20 
21   As a special exception, if other files instantiate templates or use
22   macros or inline functions from this file, or you compile this file
23   and link it with other works to produce a work based on this file,
24   this file does not by itself cause the resulting work to be covered
25   by the GNU General Public License. However the source code for this
26   file must still be made available in accordance with section (3) of
27   the GNU General Public License v2.
28 
29   This exception does not invalidate any other reasons why a work based
30   on this file might be covered by the GNU General Public License.
31 */
32 
33 /**
34  * \file uffs_os_posix.c
35  * \brief Emulation on POSIX host. This is just a dumb implementation, does not really create semaphores.
36  * \author Ricky Zheng
37  */
38 
39 #include "uffs_config.h"
40 #include "uffs/uffs_os.h"
41 #include "uffs/uffs_public.h"
42 #include <memory.h>
43 #include <stdlib.h>
44 #include <time.h>
45 #include <stdio.h>
46 #include <pthread.h>
47 
48 #define PFX "os  : "
49 
uffs_SemCreate(OSSEM * sem)50 int uffs_SemCreate(OSSEM *sem)
51 {
52 	pthread_mutex_t *mutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
53 	int ret = -1;
54 
55 	if (mutex) {
56 		ret = pthread_mutex_init(mutex, NULL /* default attr */);
57 		if (ret == 0) {
58 			*sem = (OSSEM *)mutex;
59 		}
60 		else {
61 			free(mutex);
62 		}
63 	}
64 
65 	return ret;
66 }
67 
uffs_SemWait(OSSEM sem)68 int uffs_SemWait(OSSEM sem)
69 {
70 	return pthread_mutex_lock((pthread_mutex_t *)sem);
71 }
72 
uffs_SemSignal(OSSEM sem)73 int uffs_SemSignal(OSSEM sem)
74 {
75 	return pthread_mutex_unlock((pthread_mutex_t *)sem);;
76 }
77 
uffs_SemDelete(OSSEM * sem)78 int uffs_SemDelete(OSSEM *sem)
79 {
80 	pthread_mutex_t *mutex = (pthread_mutex_t *) (*sem);
81 	int ret = -1;
82 
83 	if (mutex) {
84 		ret = pthread_mutex_destroy(mutex);
85 		if (ret == 0) {
86 			free(mutex);
87 			*sem = 0;
88 		}
89 	}
90 	return ret;
91 }
92 
uffs_OSGetTaskId(void)93 int uffs_OSGetTaskId(void)
94 {
95 	//TODO: ... return current task ID ...
96 	return 0;
97 }
98 
uffs_GetCurDateTime(void)99 unsigned int uffs_GetCurDateTime(void)
100 {
101 	// FIXME: return system time, please modify this for your platform !
102 	//			or just return 0 if you don't care about file time.
103 	time_t tvalue;
104 
105 	tvalue = time(NULL);
106 
107 	return (unsigned int)tvalue;
108 }
109 
110 #if CONFIG_USE_SYSTEM_MEMORY_ALLOCATOR > 0
sys_malloc(struct uffs_DeviceSt * dev,unsigned int size)111 static void * sys_malloc(struct uffs_DeviceSt *dev, unsigned int size)
112 {
113 	dev = dev;
114 	uffs_Perror(UFFS_MSG_NORMAL, "system memory alloc %d bytes", size);
115 	return malloc(size);
116 }
117 
sys_free(struct uffs_DeviceSt * dev,void * p)118 static URET sys_free(struct uffs_DeviceSt *dev, void *p)
119 {
120 	dev = dev;
121 	free(p);
122 	return U_SUCC;
123 }
124 
uffs_MemSetupSystemAllocator(uffs_MemAllocator * allocator)125 void uffs_MemSetupSystemAllocator(uffs_MemAllocator *allocator)
126 {
127 	allocator->malloc = sys_malloc;
128 	allocator->free = sys_free;
129 }
130 #endif
131 
132 
133 /* debug message output throught 'printf' */
134 static void output_dbg_msg(const char *msg);
135 static struct uffs_DebugMsgOutputSt m_dbg_ops = {
136 	output_dbg_msg,
137 	NULL,
138 };
139 
output_dbg_msg(const char * msg)140 static void output_dbg_msg(const char *msg)
141 {
142 	printf("%s", msg);
143 }
144 
uffs_SetupDebugOutput(void)145 void uffs_SetupDebugOutput(void)
146 {
147 	uffs_InitDebugMessageOutput(&m_dbg_ops, UFFS_MSG_NOISY);
148 }
149