Reduced SPIFFS cache, freeing ~0.5k RAM.

Also made the cache on/off configurable via user_config.h. Uncached writes
are not a very good idea, but for read-only deployments a further ~0.5k RAM
can be gained by disabling the cache.

Tweaked the file.read() workhorse to read large chunks at a time rather
than use getc(), to compensate for potential unavailability of cache.
This commit is contained in:
Johny Mattsson 2015-07-31 16:29:29 +10:00
parent 00e1e6bcbe
commit 4a47813e20
5 changed files with 22 additions and 24 deletions

View File

@ -47,6 +47,8 @@
// #define BUILD_WOFS 1 // #define BUILD_WOFS 1
#define BUILD_SPIFFS 1 #define BUILD_SPIFFS 1
#define SPIFFS_CACHE 1
// #define LUA_NUMBER_INTEGRAL // #define LUA_NUMBER_INTEGRAL
#define LUA_OPTRAM #define LUA_OPTRAM

View File

@ -196,11 +196,10 @@ static int file_fsinfo( lua_State* L )
// g_read() // g_read()
static int file_g_read( lua_State* L, int n, int16_t end_char ) static int file_g_read( lua_State* L, int n, int16_t end_char )
{ {
if(n< 0 || n>LUAL_BUFFERSIZE) if(n <= 0 || n > LUAL_BUFFERSIZE)
n = LUAL_BUFFERSIZE; n = LUAL_BUFFERSIZE;
if(end_char < 0 || end_char >255) if(end_char < 0 || end_char >255)
end_char = EOF; end_char = EOF;
int ec = (int)end_char;
luaL_Buffer b; luaL_Buffer b;
if((FS_OPEN_OK - 1)==file_fd) if((FS_OPEN_OK - 1)==file_fd)
@ -208,27 +207,22 @@ static int file_g_read( lua_State* L, int n, int16_t end_char )
luaL_buffinit(L, &b); luaL_buffinit(L, &b);
char *p = luaL_prepbuffer(&b); char *p = luaL_prepbuffer(&b);
int c = EOF; int i;
int i = 0;
do{ n = fs_read(file_fd, p, n);
c = fs_getc(file_fd); for (i = 0; i < n; ++i)
if(c==EOF){ if (p[i] == end_char)
{
++i;
break; break;
} }
p[i++] = (char)(0xFF & c);
}while((c!=EOF) && (c!=ec) && (i<n) );
#if 0
if(i>0 && p[i-1] == '\n')
i--; /* do not include `eol' */
#endif
if(i==0){ if(i==0){
luaL_pushresult(&b); /* close buffer */ luaL_pushresult(&b); /* close buffer */
return (lua_objlen(L, -1) > 0); /* check whether read something */ return (lua_objlen(L, -1) > 0); /* check whether read something */
} }
fs_seek(file_fd, -(n - i), SEEK_CUR);
luaL_addsize(&b, i); luaL_addsize(&b, i);
luaL_pushresult(&b); /* close buffer */ luaL_pushresult(&b); /* close buffer */
return 1; /* read at least an `eol' */ return 1; /* read at least an `eol' */

View File

@ -8,7 +8,9 @@ spiffs fs;
static u8_t spiffs_work_buf[LOG_PAGE_SIZE*2]; static u8_t spiffs_work_buf[LOG_PAGE_SIZE*2];
static u8_t spiffs_fds[32*4]; static u8_t spiffs_fds[32*4];
static u8_t spiffs_cache[(LOG_PAGE_SIZE+32)*4]; #if SPIFFS_CACHE
static u8_t spiffs_cache[(LOG_PAGE_SIZE+32)*2];
#endif
static s32_t my_spiffs_read(u32_t addr, u32_t size, u8_t *dst) { static s32_t my_spiffs_read(u32_t addr, u32_t size, u8_t *dst) {
platform_flash_read(dst, addr, size); platform_flash_read(dst, addr, size);
@ -62,8 +64,12 @@ void myspiffs_mount() {
spiffs_work_buf, spiffs_work_buf,
spiffs_fds, spiffs_fds,
sizeof(spiffs_fds), sizeof(spiffs_fds),
#if SPIFFS_CACHE
spiffs_cache, spiffs_cache,
sizeof(spiffs_cache), sizeof(spiffs_cache),
#else
0, 0,
#endif
// myspiffs_check_callback); // myspiffs_check_callback);
0); 0);
NODE_DBG("mount res: %i\n", res); NODE_DBG("mount res: %i\n", res);

View File

@ -8,16 +8,10 @@
#ifndef SPIFFS_CONFIG_H_ #ifndef SPIFFS_CONFIG_H_
#define SPIFFS_CONFIG_H_ #define SPIFFS_CONFIG_H_
// ----------- 8< ------------ #include "user_config.h"
// Following includes are for the linux test build of spiffs
// These may/should/must be removed/altered/replaced in your target
// #include "params_test.h"
#include "c_stdio.h" #include "c_stdio.h"
#include "c_stdlib.h" #include "c_stdint.h"
#include "c_string.h" #include "c_string.h"
#include "c_stddef.h"
#include "c_types.h"
// ----------- >8 ------------
typedef sint32_t s32_t; typedef sint32_t s32_t;
typedef uint32_t u32_t; typedef uint32_t u32_t;
@ -67,6 +61,8 @@ typedef uint8_t u8_t;
#ifndef SPIFFS_CACHE_STATS #ifndef SPIFFS_CACHE_STATS
#define SPIFFS_CACHE_STATS 0 #define SPIFFS_CACHE_STATS 0
#endif #endif
#else
#define SPIFFS_CACHE_WR 0
#endif #endif
// Always check header of each accessed page to ensure consistent state. // Always check header of each accessed page to ensure consistent state.

View File

@ -405,7 +405,7 @@ typedef struct {
// page header, part of each page except object lookup pages // page header, part of each page except object lookup pages
// NB: this is always aligned when the data page is an object index, // NB: this is always aligned when the data page is an object index,
// as in this case struct spiffs_page_object_ix is used // as in this case struct spiffs_page_object_ix is used
typedef struct __attribute(( packed )) { typedef struct __attribute(( packed, aligned(4) )) {
// object id // object id
spiffs_obj_id obj_id; spiffs_obj_id obj_id;
// object span index // object span index