2011-03-18 19:31:26 +01:00
|
|
|
/****************************************************************************
|
2011-05-12 16:49:46 +02:00
|
|
|
* apps/system/free/free.c
|
2011-03-18 19:31:26 +01:00
|
|
|
*
|
|
|
|
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
|
2012-09-13 18:58:49 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
2011-03-18 19:31:26 +01:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-10-04 01:36:54 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
2011-03-18 19:31:26 +01:00
|
|
|
#include <nuttx/config.h>
|
2011-05-07 21:22:15 +02:00
|
|
|
#include <nuttx/progmem.h>
|
2011-03-18 19:31:26 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2012-10-04 01:36:54 +02:00
|
|
|
|
|
|
|
/* TODO Max block size only works on uniform prog mem */
|
|
|
|
|
|
|
|
static void free_getprogmeminfo(struct mallinfo * mem)
|
2011-05-07 21:22:15 +02:00
|
|
|
{
|
2012-10-04 01:36:54 +02:00
|
|
|
uint16_t page = 0, stpage = 0xFFFF;
|
|
|
|
uint16_t pagesize = 0;
|
|
|
|
int status;
|
|
|
|
|
|
|
|
mem->arena = 0;
|
|
|
|
mem->fordblks = 0;
|
|
|
|
mem->uordblks = 0;
|
|
|
|
mem->mxordblk = 0;
|
|
|
|
|
|
|
|
for (status=0, page=0; status >= 0; page++)
|
|
|
|
{
|
|
|
|
status = up_progmem_ispageerased(page);
|
|
|
|
pagesize = up_progmem_pagesize(page);
|
|
|
|
|
|
|
|
mem->arena += pagesize;
|
|
|
|
|
|
|
|
/* Is this beginning of new free space section */
|
|
|
|
|
|
|
|
if (status == 0)
|
|
|
|
{
|
|
|
|
if (stpage == 0xFFFF) stpage = page;
|
|
|
|
mem->fordblks += pagesize;
|
2011-05-07 21:22:15 +02:00
|
|
|
}
|
2012-10-04 01:36:54 +02:00
|
|
|
else if (status != 0)
|
|
|
|
{
|
|
|
|
mem->uordblks += pagesize;
|
|
|
|
|
|
|
|
if (stpage != 0xFFFF && up_progmem_isuniform())
|
|
|
|
{
|
|
|
|
stpage = page - stpage;
|
|
|
|
if (stpage > mem->mxordblk)
|
|
|
|
{
|
|
|
|
mem->mxordblk = stpage;
|
|
|
|
}
|
|
|
|
stpage = 0xFFFF;
|
2011-05-07 21:22:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-03-18 19:31:26 +01:00
|
|
|
|
2012-10-04 01:36:54 +02:00
|
|
|
mem->mxordblk *= pagesize;
|
|
|
|
}
|
2011-03-18 19:31:26 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
2011-05-07 21:22:15 +02:00
|
|
|
* Public Functions
|
2011-03-18 19:31:26 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int free_main(int argc, char **argv)
|
|
|
|
{
|
2011-05-07 21:22:15 +02:00
|
|
|
struct mallinfo data;
|
|
|
|
struct mallinfo prog;
|
2011-03-18 19:31:26 +01:00
|
|
|
|
|
|
|
#ifdef CONFIG_CAN_PASS_STRUCTS
|
2011-05-07 21:22:15 +02:00
|
|
|
data = mallinfo();
|
2011-03-18 19:31:26 +01:00
|
|
|
#else
|
2011-05-07 21:22:15 +02:00
|
|
|
(void)mallinfo(&data);
|
2011-03-18 19:31:26 +01:00
|
|
|
#endif
|
|
|
|
|
2011-05-07 21:22:15 +02:00
|
|
|
free_getprogmeminfo(&prog);
|
|
|
|
|
|
|
|
printf(" total used free largest\n");
|
|
|
|
printf("Data: %11d%11d%11d%11d\n",
|
|
|
|
data.arena, data.uordblks, data.fordblks, data.mxordblk);
|
|
|
|
printf("Prog: %11d%11d%11d%11d\n",
|
|
|
|
prog.arena, prog.uordblks, prog.fordblks, prog.mxordblk);
|
2011-03-18 19:31:26 +01:00
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|