guowenxue
2020-08-22 4f9a1657573f0a1ce24e63ad618ab60d238287b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
 * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
 *
 * Copyright (C) 2002-2011 Aleph One Ltd.
 *   for Toby Churchill Ltd and Brightstar Engineering
 *
 * Created by Charles Manning <charles@aleph1.co.uk>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */
 
/* 
 * Example OS glue functions for running on a Linux/POSIX system.
 */
 
#include "yaffscfg.h"
#include "yaffs_guts.h"
#include "yaffsfs.h"
#include "yaffs_trace.h"
#include "yportenv.h"
 
 
/*
 * yaffsfs_SetError() and yaffsfs_GetError()
 * Do whatever to set the system error.
 * yaffsfs_GetError() just fetches the last error.
 */
static int errno;
 
static int yaffsfs_lastError;
 
void yaffsfs_SetError(int err)
{
    //Do whatever to set error
    yaffsfs_lastError = err;
    errno = err;
}
 
int yaffsfs_GetLastError(void)
{
    return yaffsfs_lastError;
}
 
/*
 * yaffsfs_CheckMemRegion()
 * Check that access to an address is valid.
 * This can check memory is in bounds and is writable etc.
 *
 * Returns 0 if ok, negative if not.
 */
int yaffsfs_CheckMemRegion(const void *addr, size_t size, int write_request)
{
    if(!addr)
        return -1;
    return 0;
}
 
/*
 * yaffsfs_Lock()
 * yaffsfs_Unlock()
 * A single mechanism to lock and unlock yaffs. Hook up to a mutex or whatever.
 * Here are two examples, one using POSIX pthreads, the other doing nothing.
 */
 
#ifdef CONFIG_YAFFS_USE_PTHREADS
#include <pthread.h>
static pthread_mutex_t mutex1;
 
 
void yaffsfs_Lock(void)
{
    pthread_mutex_lock( &mutex1 );
}
 
void yaffsfs_Unlock(void)
{
    pthread_mutex_unlock( &mutex1 );
}
 
void yaffsfs_LockInit(void)
{
    pthread_mutex_init( &mutex1, NULL);
}
 
#else
 
void yaffsfs_Lock(void)
{
}
 
void yaffsfs_Unlock(void)
{
}
 
void yaffsfs_LockInit(void)
{
}
#endif
 
/*
 * yaffsfs_CurrentTime() retrns a 32-bit timestamp.
 * 
 * Can return 0 if your system does not care about time.
 */
 
u32 yaffsfs_CurrentTime(void)
{
    return 0;
    //return time(NULL);
}
 
 
/*
 * yaffsfs_malloc()
 * yaffsfs_free()
 *
 * Functions to allocate and free memory.
 */
 
#ifdef CONFIG_YAFFS_TEST_MALLOC
 
static int yaffs_kill_alloc = 0;
static size_t total_malloced = 0;
static size_t malloc_limit = 0 & 6000000;
 
void *yaffsfs_malloc(size_t size)
{
    void * this;
    if(yaffs_kill_alloc)
        return NULL;
    if(malloc_limit && malloc_limit <(total_malloced + size) )
        return NULL;
 
    this = malloc(size);
    if(this)
        total_malloced += size;
    return this;
}
 
#else
 
void *yaffsfs_malloc(size_t size)
{
    return malloc(size);
}
 
#endif
 
void yaffsfs_free(void *ptr)
{
    free(ptr);
}
 
void yaffsfs_OSInitialisation(void)
{
    yaffsfs_LockInit();
}
 
/*
 * yaffs_bug_fn()
 * Function to report a bug.
 */
 
void yaffs_bug_fn(const char *file_name, int line_no)
{
    printf("yaffs bug detected %s:%d\n",
        file_name, line_no);
}