Merge pull request #586 from DiUS/ram-saving

Reclaim ~1.5k RAM by tuning down SPIFFS cache, use dynamic DNS table names
This commit is contained in:
Vowstar 2015-08-05 23:19:24 +08:00
commit a1bb3a6ad8
8 changed files with 70 additions and 30 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

@ -173,7 +173,7 @@ struct dns_table_entry {
u8_t seqno; u8_t seqno;
u8_t err; u8_t err;
u32_t ttl; u32_t ttl;
char name[DNS_MAX_NAME_LENGTH]; char *name;
ip_addr_t ipaddr; ip_addr_t ipaddr;
/* pointer to callback on DNS query done */ /* pointer to callback on DNS query done */
dns_found_callback found; dns_found_callback found;
@ -673,6 +673,8 @@ dns_check_entry(u8_t i)
if (pEntry->found) if (pEntry->found)
(*pEntry->found)(pEntry->name, NULL, pEntry->arg); (*pEntry->found)(pEntry->name, NULL, pEntry->arg);
/* flush this entry */ /* flush this entry */
mem_free (pEntry->name);
pEntry->name = NULL;
pEntry->state = DNS_STATE_UNUSED; pEntry->state = DNS_STATE_UNUSED;
pEntry->found = NULL; pEntry->found = NULL;
break; break;
@ -697,6 +699,8 @@ dns_check_entry(u8_t i)
if (--pEntry->ttl == 0) { if (--pEntry->ttl == 0) {
LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": flush\n", pEntry->name)); LWIP_DEBUGF(DNS_DEBUG, ("dns_check_entry: \"%s\": flush\n", pEntry->name));
/* flush this entry */ /* flush this entry */
mem_free (pEntry->name);
pEntry->name = NULL;
pEntry->state = DNS_STATE_UNUSED; pEntry->state = DNS_STATE_UNUSED;
pEntry->found = NULL; pEntry->found = NULL;
} }
@ -839,6 +843,8 @@ responseerr:
(*pEntry->found)(pEntry->name, NULL, pEntry->arg); (*pEntry->found)(pEntry->name, NULL, pEntry->arg);
} }
/* flush this entry */ /* flush this entry */
mem_free (pEntry->name);
pEntry->name = NULL;
pEntry->state = DNS_STATE_UNUSED; pEntry->state = DNS_STATE_UNUSED;
pEntry->found = NULL; pEntry->found = NULL;
@ -898,11 +904,16 @@ dns_enqueue(const char *name, dns_found_callback found, void *callback_arg)
LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS entry %"U16_F"\n", name, (u16_t)(i))); LWIP_DEBUGF(DNS_DEBUG, ("dns_enqueue: \"%s\": use DNS entry %"U16_F"\n", name, (u16_t)(i)));
/* fill the entry */ /* fill the entry */
namelen = LWIP_MIN(strlen(name), DNS_MAX_NAME_LENGTH-1);
char *namebuf = (char *)mem_zalloc (namelen);
if (!namebuf)
return ERR_MEM;
pEntry->state = DNS_STATE_NEW; pEntry->state = DNS_STATE_NEW;
pEntry->seqno = dns_seqno++; pEntry->seqno = dns_seqno++;
pEntry->found = found; pEntry->found = found;
pEntry->arg = callback_arg; pEntry->arg = callback_arg;
namelen = LWIP_MIN(strlen(name), DNS_MAX_NAME_LENGTH-1); mem_free (pEntry->name);
pEntry->name = namebuf;
MEMCPY(pEntry->name, name, namelen); MEMCPY(pEntry->name, name, namelen);
pEntry->name[namelen] = 0; pEntry->name[namelen] = 0;

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

@ -220,7 +220,7 @@ uint32_t platform_flash_read( void *to, uint32_t fromaddr, uint32_t size )
#else // #ifindef INTERNAL_FLASH_READ_UNIT_SIZE #else // #ifindef INTERNAL_FLASH_READ_UNIT_SIZE
uint32_t temp, rest, ssize = size; uint32_t temp, rest, ssize = size;
unsigned i; unsigned i;
char tmpdata[ INTERNAL_FLASH_READ_UNIT_SIZE ]; char tmpdata[ INTERNAL_FLASH_READ_UNIT_SIZE ] __attribute__ ((aligned(INTERNAL_FLASH_READ_UNIT_SIZE)));
uint8_t *pto = ( uint8_t* )to; uint8_t *pto = ( uint8_t* )to;
const uint32_t blksize = INTERNAL_FLASH_READ_UNIT_SIZE; const uint32_t blksize = INTERNAL_FLASH_READ_UNIT_SIZE;
const uint32_t blkmask = INTERNAL_FLASH_READ_UNIT_SIZE - 1; const uint32_t blkmask = INTERNAL_FLASH_READ_UNIT_SIZE - 1;

View File

@ -460,13 +460,19 @@ spi_data_type platform_spi_send_recv( unsigned id, spi_data_type data )
// **************************************************************************** // ****************************************************************************
// Flash access functions // Flash access functions
/*
* Assumptions:
* > toaddr is INTERNAL_FLASH_WRITE_UNIT_SIZE aligned
* > size is a multiple of INTERNAL_FLASH_WRITE_UNIT_SIZE
*/
uint32_t platform_s_flash_write( const void *from, uint32_t toaddr, uint32_t size ) uint32_t platform_s_flash_write( const void *from, uint32_t toaddr, uint32_t size )
{ {
toaddr -= INTERNAL_FLASH_START_ADDRESS; toaddr -= INTERNAL_FLASH_START_ADDRESS;
SpiFlashOpResult r; SpiFlashOpResult r;
const uint32_t blkmask = INTERNAL_FLASH_WRITE_UNIT_SIZE - 1; const uint32_t blkmask = INTERNAL_FLASH_WRITE_UNIT_SIZE - 1;
uint32_t *apbuf = NULL; uint32_t *apbuf = NULL;
if( ((uint32_t)from) & blkmask ){ uint32_t fromaddr = (uint32_t)from;
if( (fromaddr & blkmask ) || (fromaddr >= INTERNAL_FLASH_START_ADDRESS)) {
apbuf = (uint32_t *)c_malloc(size); apbuf = (uint32_t *)c_malloc(size);
if(!apbuf) if(!apbuf)
return 0; return 0;
@ -484,12 +490,37 @@ uint32_t platform_s_flash_write( const void *from, uint32_t toaddr, uint32_t siz
} }
} }
/*
* Assumptions:
* > fromaddr is INTERNAL_FLASH_READ_UNIT_SIZE aligned
* > size is a multiple of INTERNAL_FLASH_READ_UNIT_SIZE
*/
uint32_t platform_s_flash_read( void *to, uint32_t fromaddr, uint32_t size ) uint32_t platform_s_flash_read( void *to, uint32_t fromaddr, uint32_t size )
{ {
if (size==0)
return 0;
fromaddr -= INTERNAL_FLASH_START_ADDRESS; fromaddr -= INTERNAL_FLASH_START_ADDRESS;
SpiFlashOpResult r; SpiFlashOpResult r;
WRITE_PERI_REG(0x60000914, 0x73); WRITE_PERI_REG(0x60000914, 0x73);
r = flash_read(fromaddr, (uint32 *)to, size);
const uint32_t blkmask = (INTERNAL_FLASH_READ_UNIT_SIZE - 1);
if( ((uint32_t)to) & blkmask )
{
uint32_t size2=size-INTERNAL_FLASH_READ_UNIT_SIZE;
uint32* to2=(uint32*)((((uint32_t)to)&(~blkmask))+INTERNAL_FLASH_READ_UNIT_SIZE);
r = flash_read(fromaddr, to2, size2);
if(SPI_FLASH_RESULT_OK == r)
{
os_memmove(to,to2,size2);
char back[ INTERNAL_FLASH_READ_UNIT_SIZE ] __attribute__ ((aligned(INTERNAL_FLASH_READ_UNIT_SIZE)));
r=flash_read(fromaddr+size2,(uint32*)back,INTERNAL_FLASH_READ_UNIT_SIZE);
os_memcpy((uint8_t*)to+size2,back,INTERNAL_FLASH_READ_UNIT_SIZE);
}
}
else
r = flash_read(fromaddr, (uint32 *)to, size);
if(SPI_FLASH_RESULT_OK == r) if(SPI_FLASH_RESULT_OK == r)
return size; return size;
else{ else{

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

@ -415,7 +415,7 @@ typedef struct __attribute(( packed )) {
} spiffs_page_header; } spiffs_page_header;
// object index header page header // object index header page header
typedef struct __attribute(( packed, aligned(4) )) typedef struct __attribute(( packed ))
{ {
// common page header // common page header
spiffs_page_header p_hdr; spiffs_page_header p_hdr;
@ -428,7 +428,7 @@ typedef struct __attribute(( packed, aligned(4) ))
} spiffs_page_object_ix_header; } spiffs_page_object_ix_header;
// object index page header // object index page header
typedef struct __attribute(( packed, aligned(4) )) { typedef struct __attribute(( packed )) {
spiffs_page_header p_hdr; spiffs_page_header p_hdr;
} spiffs_page_object_ix; } spiffs_page_object_ix;