📄 main.c
字号:
/**************************************************************************** * main.c * * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * 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. * ****************************************************************************//**************************************************************************** * Compilation Switches ****************************************************************************//**************************************************************************** * Included Files ****************************************************************************/#include <nuttx/config.h>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sched.h>#include <nuttx/init.h>#include "ostest.h"/**************************************************************************** * Definitions ****************************************************************************/#define PRIORITY 100#define NARGS 4/* The task_create task size can be specified in the defconfig file */#ifdef CONFIG_EXAMPLES_OSTEST_STACKSIZE# define STACKSIZE CONFIG_EXAMPLES_OSTEST_STACKSIZE#else# define STACKSIZE 8192#endif/* The number of times to execute the test can be specified in the defconfig * file. */#ifndef CONFIG_EXAMPLES_OSTEST_LOOPS# define CONFIG_EXAMPLES_OSTEST_LOOPS 1#endif/**************************************************************************** * Private Data ****************************************************************************/static const char arg1[] = "Arg1";static const char arg2[] = "Arg2";static const char arg3[] = "Arg3";static const char arg4[] = "Arg4";#if CONFIG_NFILE_DESCRIPTORS > 0static const char write_data1[] = "stdio_test: write fd=1\n";static const char write_data2[] = "stdio_test: write fd=2\n";#endif#ifdef SDCC/* I am not yet certain why SDCC does not like the following * initializer. It involves some issues with 2- vs 3-byte * pointer types. */static const char *g_argv[NARGS+1];#elsestatic const char *g_argv[NARGS+1] = { arg1, arg2, arg3, arg4, NULL };#endif#ifndef CONFIG_DISABLE_SIGNALSstatic struct mallinfo g_mmbefore;static struct mallinfo g_mmprevious;static struct mallinfo g_mmafter;#endif#ifndef CONFIG_DISABLE_ENVIRONconst char g_var1_name[] = "Variable1";const char g_var1_value[] = "GoodValue1";const char g_var2_name[] = "Variable2";const char g_var2_value[] = "GoodValue2";const char g_var3_name[] = "Variable3";const char g_var3_value[] = "GoodValue3";const char g_bad_value1[] = "BadValue1";const char g_bad_value2[] = "BadValue2";const char g_putenv_value[] = "Variable1=BadValue3";#endif/**************************************************************************** * Private Functions ****************************************************************************//**************************************************************************** * Name: show_memory_usage ****************************************************************************/#ifndef CONFIG_DISABLE_SIGNALSstatic void show_memory_usage(struct mallinfo *mmbefore, struct mallinfo *mmafter){ printf("VARIABLE BEFORE AFTER\n"); printf("======== ======== ========\n"); printf("arena %8x %8x\n", mmbefore->arena, mmafter->arena); printf("ordblks %8d %8d\n", mmbefore->ordblks, mmafter->ordblks); printf("mxordblk %8x %8x\n", mmbefore->mxordblk, mmafter->mxordblk); printf("uordblks %8x %8x\n", mmbefore->uordblks, mmafter->uordblks); printf("fordblks %8x %8x\n", mmbefore->fordblks, mmafter->fordblks);}#else# define show_memory_usage(mm1, mm2)#endif/**************************************************************************** * Name: check_test_memory_usage ****************************************************************************/#ifndef CONFIG_DISABLE_SIGNALSstatic void check_test_memory_usage(void){ /* Wait a little bit to let any threads terminate */ usleep(500*1000); /* Get the current memory usage */#ifdef CONFIG_CAN_PASS_STRUCTS g_mmafter = mallinfo();#else (void)mallinfo(&g_mmafter);#endif /* Show the change from the previous time */ printf("\nEnd of test memory usage:\n"); show_memory_usage(&g_mmprevious, &g_mmafter); /* Set up for the next test */#ifdef CONFIG_CAN_PASS_STRUCTS g_mmprevious = g_mmafter;#else memcpy(&g_mmprevious, &g_mmafter, sizeof(struct mallinfo));#endif}#else# define check_test_memory_usage()#endif/**************************************************************************** * Name: show_environment ****************************************************************************/#ifndef CONFIG_DISABLE_ENVIRONstatic void show_variable(const char *var_name, const char *exptd_value, boolean var_valid){ char *actual_value = getenv(var_name); if (actual_value) { if (var_valid) { if (strcmp(actual_value, exptd_value) == 0) { printf("show_variable: Variable=%s has value=%s\n", var_name, exptd_value); } else { printf("show_variable: ERROR Variable=%s has the wrong value\n", var_name); printf("show_variable: found=%s expected=%s\n", actual_value, exptd_value); } } else { printf("show_variable: ERROR Variable=%s has a value when it should not\n", var_name); printf("show_variable: value=%s\n", actual_value); } } else if (var_valid) { printf("show_variable: ERROR Variable=%s has no value\n", var_name); printf("show_variable: Should have had value=%s\n", exptd_value); } else { printf("show_variable: Variable=%s has no value\n", var_name); }}static void show_environment(boolean var1_valid, boolean var2_valid, boolean var3_valid){ show_variable( g_var1_name, g_var1_value, var1_valid); show_variable( g_var2_name, g_var2_value, var2_valid); show_variable( g_var3_name, g_var3_value, var3_valid);}#else# define show_environment()#endif/**************************************************************************** * Name: user_main ****************************************************************************/static int user_main(int argc, char *argv[]){ int i; /* Sample the memory usage now */#ifndef CONFIG_DISABLE_SIGNALS usleep(500*1000);#ifdef CONFIG_CAN_PASS_STRUCTS g_mmbefore = mallinfo(); g_mmprevious = g_mmbefore;#else (void)mallinfo(&g_mmbefore); memcpy(&g_mmprevious, &g_mmbefore, sizeof(struct mallinfo));#endif#endif printf("\nuser_main: Begin argument test\n"); printf("user_main: Started with argc=%d\n", argc); /* Verify passed arguments */ if (argc != NARGS + 1) { printf("user_main: Error expected argc=%d got argc=%d\n", NARGS+1, argc); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -