You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
4.9 KiB
178 lines
4.9 KiB
|
3 days ago
|
#ifndef COMMON_H
|
||
|
|
|
||
|
|
#define COMMON_H
|
||
|
|
|
||
|
|
#define rd_inline static __inline
|
||
|
|
|
||
|
|
#define RD_SUCCESS 0 //成功
|
||
|
|
#define RD_FAILURE -1 //失败
|
||
|
|
#define RD_NULL -2 //对象为空
|
||
|
|
#define RD_INVALUE -3 //非法参数
|
||
|
|
|
||
|
|
/* 跨平台弱符号宏定义 */
|
||
|
|
#if defined(__GNUC__) || defined(__clang__) || defined(__SUNPRO_CC) || defined(__SUNPRO_C)
|
||
|
|
/* GCC, Clang, SunPro 系列 */
|
||
|
|
#define WEAK __attribute__((weak))
|
||
|
|
|
||
|
|
#elif defined(__IAR_SYSTEMS_ICC__)
|
||
|
|
/* IAR Embedded Workbench */
|
||
|
|
#define WEAK __weak
|
||
|
|
|
||
|
|
#elif defined(__CC_ARM) || defined(__ARMCC_VERSION)
|
||
|
|
/* Keil MDK / ARM Compiler */
|
||
|
|
#define WEAK __weak
|
||
|
|
|
||
|
|
#elif defined(_MSC_VER)
|
||
|
|
/* Microsoft Visual C++ */
|
||
|
|
/* 或者干脆定义为空,但需要确保不会重复定义 */
|
||
|
|
#define WEAK
|
||
|
|
|
||
|
|
#else
|
||
|
|
/* 其他未知编译器,保守处理 */
|
||
|
|
#error "Weak symbol not supported on this compiler"
|
||
|
|
#define WEAK
|
||
|
|
/* 可以添加警告:#warning "Weak symbol not supported on this compiler" */
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#include "elog.h"
|
||
|
|
|
||
|
|
#ifdef __RTTHREAD__
|
||
|
|
#include <rtthread.h>
|
||
|
|
#include <shell.h>
|
||
|
|
#define RD_PRINTF rt_kprintf
|
||
|
|
#define RD_SPRINTF rt_sprintf
|
||
|
|
#define RD_SNPRINTF rt_snprintf
|
||
|
|
#define RD_VSNPRINTF rt_vsnprintf
|
||
|
|
#define RD_GETCHAR finsh_getchar
|
||
|
|
#define RD_STRLEN rt_strlen
|
||
|
|
#define RD_STRCMP rt_strcmp
|
||
|
|
#define RD_STRNCMP rt_strncmp
|
||
|
|
#define RD_STRCPY rt_strcpy
|
||
|
|
#define RD_MEMCPY rt_memcpy
|
||
|
|
#define RD_STRNCPY rt_strncpy
|
||
|
|
#define RD_MEMSET rt_memset
|
||
|
|
#define RD_MEMCMP rt_memcmp
|
||
|
|
#define RD_MEMMOVE rt_memmove
|
||
|
|
// ... RT 实现
|
||
|
|
#else
|
||
|
|
#include <stdio.h>
|
||
|
|
#define RD_PRINTF printf
|
||
|
|
#define RD_SPRINTF sprintf
|
||
|
|
#define RD_SNPRINTF snprintf
|
||
|
|
#define RD_VSNPRINTF vsnprintf
|
||
|
|
#include <stdlib.h>
|
||
|
|
#define RD_GETCHAR getchar
|
||
|
|
#include <string.h>
|
||
|
|
#define RD_STRLEN strlen
|
||
|
|
#define RD_STRCMP strcmp
|
||
|
|
#define RD_STRNCMP strncmp
|
||
|
|
#define RD_STRCPY strcpy
|
||
|
|
#define RD_STRNCPY strncpy
|
||
|
|
#define RD_MEMCPY memcpy
|
||
|
|
#define RD_MEMSET memset
|
||
|
|
#define RD_MEMCMP memcmp
|
||
|
|
#define RD_MEMMOVE memmove
|
||
|
|
// ... Linux 实现
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#define RD_ALIGN_SIZE 4//四字节对齐
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @def RD_ALIGN(size, align)
|
||
|
|
* Return the most contiguous size aligned at specified width. RD_ALIGN(13, 4)
|
||
|
|
* would return 16.
|
||
|
|
*/
|
||
|
|
#define RD_ALIGN(size, align) (((size) + (align) - 1) & ~((align) - 1))
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @def RD_ALIGN_DOWN(size, align)
|
||
|
|
* Return the down number of aligned at specified width. RD_ALIGN_DOWN(13, 4)
|
||
|
|
* would return 12.
|
||
|
|
*/
|
||
|
|
#define RD_ALIGN_DOWN(size, align) ((size) & ~((align) - 1))
|
||
|
|
|
||
|
|
extern void* Rd_MemPoolMalloc(size_t size);
|
||
|
|
extern void Rd_MemPoolFree(void* ptr);
|
||
|
|
extern void* Rd_MemPoolCalloc(size_t n, size_t size);
|
||
|
|
extern void* Rd_MemPoolRealloc(void* ptr, size_t new_size);
|
||
|
|
|
||
|
|
#define RD_MALLOC Rd_MemPoolMalloc
|
||
|
|
#define RD_CALLOC Rd_MemPoolCalloc
|
||
|
|
#define RD_REALLOC Rd_MemPoolRealloc
|
||
|
|
#define RD_FREE Rd_MemPoolFree
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Rd_String2Array
|
||
|
|
* @note 将字符串按照一定的分割符分割为字符串数组
|
||
|
|
* @param _pSrc: 输入的字符串
|
||
|
|
* @param _pRes[]: 输出的字符串数组
|
||
|
|
* @param _iNum: 字符串的数组元素数
|
||
|
|
* @param _Ptr: 分割符
|
||
|
|
* @retval 0成功,非0失败
|
||
|
|
*/
|
||
|
|
int Rd_String2Array(char *_pSrc, char *_pRes[], int _iNum, char *_Ptr);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @brief Rd_Array2String
|
||
|
|
* @note 将字符串数组用一定的连接符拼接成字符串
|
||
|
|
* @param _pRes: 存放输出的字符串
|
||
|
|
* @param _iLen: 字符串长度
|
||
|
|
* @param _pSrc[]: 输入的字符串数组
|
||
|
|
* @param _iNum: 字符串的数组元素数
|
||
|
|
* @param _Ptr: 连接符
|
||
|
|
* @retval 0成功,非0失败
|
||
|
|
*/
|
||
|
|
int Rd_Array2String(char *_pRes, int _iLen, char *_pSrc[], int _iNum, char *_Ptr);
|
||
|
|
int Rd_FileExists(const char *_psPath);
|
||
|
|
int Rd_RegularFileExists(const char *_psPath);
|
||
|
|
|
||
|
|
#ifdef BUILD_LOG
|
||
|
|
extern void Rd_ElogEnableStrong(void);
|
||
|
|
#endif
|
||
|
|
#ifdef BUILD_PLATFORM_LINUX
|
||
|
|
extern void Rd_LinuxEnableStrong(void);
|
||
|
|
extern void Rd_ThreadEnableStrong(void);
|
||
|
|
#endif
|
||
|
|
#ifdef BUILD_PLATFORM_WIN32
|
||
|
|
extern void Rd_Win32EnableStrong(void);
|
||
|
|
#endif
|
||
|
|
#ifdef BUILD_BSPMCU
|
||
|
|
extern void Rd_lstm32EnableStrong(void);
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#ifdef BUILD_LOG
|
||
|
|
#define INIT_LOG_PART() do { Rd_ElogEnableStrong(); } while(0)
|
||
|
|
#else
|
||
|
|
#define INIT_LOG_PART() do {} while(0)
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#ifdef BUILD_PLATFORM_LINUX
|
||
|
|
#define INIT_LINUX_PART() do { Rd_LinuxEnableStrong(); Rd_ThreadEnableStrong();} while(0)
|
||
|
|
#else
|
||
|
|
#define INIT_LINUX_PART() do {} while(0)
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#ifdef BUILD_PLATFORM_WIN32
|
||
|
|
#define INIT_WIN32_PART() do { Rd_Win32EnableStrong(); } while(0)
|
||
|
|
#else
|
||
|
|
#define INIT_WIN32_PART() do {} while(0)
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#ifdef BUILD_BSPMCU
|
||
|
|
#define INIT_BSPMCU_PART() do { Rd_lstm32EnableStrong(); } while(0)
|
||
|
|
#else
|
||
|
|
#define INIT_BSPMCU_PART() do {} while(0)
|
||
|
|
#endif
|
||
|
|
|
||
|
|
#define RD_INIT() \
|
||
|
|
do { \
|
||
|
|
INIT_LOG_PART(); \
|
||
|
|
extern void Rd_MemPoolInit_Auto(void); \
|
||
|
|
Rd_MemPoolInit_Auto(); \
|
||
|
|
INIT_LINUX_PART(); \
|
||
|
|
INIT_WIN32_PART(); \
|
||
|
|
INIT_BSPMCU_PART(); \
|
||
|
|
} while(0)
|
||
|
|
|
||
|
|
#endif /* end of include guard: COMMON_H */
|