| /** | |
| * @file oss_clib.h | |
| * @brief C¿âÍ·Îļþ | |
| * | |
| * Copyright (C) 2017 Sanechips Technology Co., Ltd. | |
| * | |
| */ | |
| #ifndef _OSS_CLIB_H | |
| #define _OSS_CLIB_H | |
| #define _USE_CLIB | |
| #ifdef _USE_CLIB | |
| /******************************************************************************* | |
| * Include header files * | |
| ******************************************************************************/ | |
| #if defined (_OS_LINUX) | |
| #include <linux/kernel.h> | |
| #include <linux/slab.h> | |
| #include <linux/string.h> | |
| #else | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <assert.h> | |
| #include <time.h> | |
| #include <math.h> | |
| #include <ctype.h> | |
| #endif | |
| #ifdef _OS_WIN | |
| #include <stddef.h> | |
| #endif | |
| #include "oss_pub.h" | |
| #include "oss_compiler.h" | |
| #ifdef __cplusplus | |
| extern "C" | |
| { | |
| #endif | |
| /******************************************************************************* | |
| * Macro definitions * | |
| ******************************************************************************/ | |
| #ifdef _OS_LINUX | |
| /* | |
| * stdio.h | |
| */ | |
| #define printf(fmt, ...) printk(fmt, ##__VA_ARGS__) | |
| /* | |
| * random | |
| */ | |
| #define RAND_MAX (0x7FFFFFFF) | |
| #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)) | |
| # define srand(s) prandom_seed(s) | |
| # define rand() (prandom_u32() & RAND_MAX) | |
| #else | |
| # define srand(s) srandom32(s) | |
| # define rand() (random32() & RAND_MAX) | |
| #endif | |
| /* | |
| * stdlib.h | |
| */ | |
| #define atol(nptr) simple_strtol(nptr, (char **)NULL, 10) | |
| #define atoi(nptr) simple_strtoul(nptr, (char **)NULL, 10) | |
| #define strtol simple_strtol | |
| #define strtoul simple_strtoul | |
| #define malloc(size) kmalloc(size, GFP_ATOMIC) | |
| #define free(ptr) kfree(ptr) | |
| /* | |
| * assert.h | |
| */ | |
| #define assert(exp) zOss_ASSERT(exp) | |
| /* | |
| * time | |
| */ | |
| #define settimeofday sys_settimeofday | |
| /* | |
| * unistd.h | |
| */ | |
| #define exit(nr) do_exit(nr) | |
| #define zOss_Malloc(size) kmalloc(size, GFP_ATOMIC) | |
| #define zOss_MallocEx(size) kmalloc(size, GFP_KERNEL | __GFP_PAGEMODEM) | |
| #define zOss_Realloc(mem_ptr, new_size) krealloc(mem_ptr, new_size, GFP_ATOMIC) | |
| #define zOss_Free(mem_ptr) kfree(mem_ptr) | |
| #define zOss_Memcpy(dest_ptr, src_ptr, size) memcpy(dest_ptr, src_ptr, size) | |
| #define zOss_Memset(dest_ptr, value, size) memset(dest_ptr, value, size) | |
| #define zOss_Memmove(dest_ptr, src_ptr, count) memmove(dest_ptr, src_ptr, count) | |
| #define zOss_Memcmp(f_buf_ptr, s_buf_ptr, count) memcmp(f_buf_ptr, s_buf_ptr, count) | |
| #else | |
| /** | |
| * @brief ¶¯Ì¬ÉêÇëÄڴ溯Êýºê | |
| * @param size Èë²Î£¬ÉêÇëÄÚ´æµÄ´óС | |
| * @return ³É¹¦·µ»Ø·ÖÅäµÄÄÚ´æÖ¸Õ룬ʧ°Ü·µ»Ø¿ÕÖ¸Õë | |
| * @retval void*ÀàÐ͵ÄÖ¸Õë ³É¹¦ | |
| * @retval NULL ʧ°Ü | |
| */ | |
| #define zOss_Malloc(size) mem_malloc(size, ZOSS_FILE, ZOSS_LINE) | |
| /** | |
| * @brief ¶¯Ì¬ÄÚ´æµ÷Õûº¯Êýºê | |
| * @param mem_ptr Èë²Î£¬Òªµ÷ÕûµÄÄÚ´æÖ¸Õë | |
| * @param new_size Èë²Î£¬Òªµ÷ÕûµÄÄÚ´æ´óС | |
| * @return ³É¹¦·µ»Øµ÷ÕûºóµÄÄÚ´æÖ¸Õ룬ʧ°Ü·µ»Ø¿ÕÖ¸Õë | |
| * @retval void*ÀàÐ͵ÄÖ¸Õë ³É¹¦ | |
| * @retval NULL ʧ°Ü | |
| */ | |
| #define zOss_Realloc(mem_ptr, new_size) mem_re_alloc(mem_ptr, new_size, ZOSS_FILE, ZOSS_LINE) | |
| /** | |
| * @brief ÊÍ·ÅÄڴ溯Êýºê | |
| * @param mem_ptr Èë²Î£¬ÒªÊͷŵÄÄÚ´æÖ¸Õë | |
| * @return void | |
| */ | |
| #define zOss_Free(mem_ptr) mem_free_extend(mem_ptr, ZOSS_FILE, ZOSS_LINE) | |
| /** | |
| * @brief Äڴ濽±´º¯Êýºê | |
| * @param dest_ptr Èë²Î£¬Ä¿±êÄÚ´æÖ¸Õë | |
| * @param src_ptr Èë²Î£¬Ô´ÄÚ´æÖ¸Õë | |
| * @param size Èë²Î£¬Òª¿½±´µÄ´óС | |
| * @return ·µ»Ødest_ptrµÄÖ¸Õë | |
| */ | |
| #define zOss_Memcpy(dest_ptr, src_ptr, size) mem_memcpy(dest_ptr, src_ptr, size, ZOSS_FILE, ZOSS_LINE) | |
| /** | |
| * @brief ÄÚ´æ³õʼ»¯º¯Êýºê | |
| * @param dest_ptr Èë²Î£¬Òª³õʼ»¯µÄÄ¿±êÄÚ´æÖ¸Õë | |
| * @param value Èë²Î£¬³õʼ»¯µÄÖµ | |
| * @param size Èë²Î£¬³õʼ»¯µÄÄÚ´æ´óС | |
| * @return ·µ»Ø±»³õʼ»¯ÄÚ´æµÄÖ¸Õ루void*£© | |
| */ | |
| #define zOss_Memset(dest_ptr, value, size) mem_memset(dest_ptr, value, size, ZOSS_FILE, ZOSS_LINE) | |
| /** | |
| * @brief ÄÚ´æ°áÒÆ | |
| * @param dest_ptr Èë²Î£¬Òª°áÒÆµÄÄ¿±êÄÚ´æÖ¸Õë | |
| * @param src_ptr Èë²Î£¬°áÒÆµÄÔ´ÄÚ´æÖ¸Õë | |
| * @param count Èë²Î£¬°áÒÆÄÚ´æ´óС | |
| * @return ·µ»Ødest_ptrµÄÖ¸Õë | |
| */ | |
| #define zOss_Memmove(dest_ptr, src_ptr, count) mem_memmove(dest_ptr, src_ptr, count) | |
| /** | |
| * @brief ÄÚ´æ±È½Ïº¯Êýºê | |
| * @param f_buf_ptr Èë²Î£¬Òª±È½ÏµÄÒ»·½ÄÚ´æÖ¸Õë | |
| * @param s_buf_ptr Èë²Î£¬Òª±È½ÏµÄÁíÒ»·½ÄÚ´æÖ¸Õë | |
| * @param count Èë²Î£¬Òª±È½ÏµÄÄÚ´æ´óС | |
| * @return Ïàͬ·µ»Ø0£¬·ñÔò²»Ïàͬ | |
| */ | |
| #define zOss_Memcmp(f_buf_ptr, s_buf_ptr, count) mem_memcmp(f_buf_ptr, s_buf_ptr, count) | |
| #endif | |
| /******************************************************************************* | |
| * Type definitions * | |
| ******************************************************************************/ | |
| /** | |
| * @brief ZOSS_TIME_TµÄÀàÐͶ¨Òå | |
| */ | |
| typedef UINT32 ZOSS_TIME_T; | |
| /******************************************************************************* | |
| * Global variable declarations * | |
| ******************************************************************************/ | |
| /******************************************************************************* | |
| * Global function declarations * | |
| ******************************************************************************/ | |
| /** | |
| * @brief ·µ»Ø´Ó¹«Ôª1970Äê1ÔÂ1ÈÕµÄUTCʱ¼ä´Ó0ʱ0·Ö0ÃëËãÆðµ½ÏÖÔÚËù¾¹ýµÄÃëÊý | |
| * @param t ³ö²Î£¬±£´æ´Ó¹«Ôª1970Äê1ÔÂ1ÈÕµÄUTCʱ¼ä´Ó0ʱ0·Ö0ÃëËãÆðµ½ÏÖÔÚËù¾¹ýµÄ | |
| ÃëÊý£¬¸Ã²ÎÊý¿ÉΪNULL | |
| * @return ´Ó¹«Ôª1970Äê1ÔÂ1ÈÕµÄUTCʱ¼ä´Ó0ʱ0·Ö0ÃëËãÆðµ½ÏÖÔÚËù¾¹ýµÄÃëÊý | |
| * @note | |
| * @warning | |
| */ | |
| ZOSS_TIME_T zOss_Time(ZOSS_TIME_T *t); | |
| #ifndef DOXYGEN_SHOULD_SKIP_THIS | |
| VOID *zOss_CacheMalloc(UINT32 size); | |
| VOID zOss_CacheFree(VOID *ptr); | |
| /* ÕûÊýת³É×Ö·û */ | |
| char *itoa(int num, char *str, int radix); | |
| /******************************************************************************* | |
| * Inline function implementations * | |
| ******************************************************************************/ | |
| #if defined(_MEM_USE_HEAP) | |
| #if defined (_OS_TOS) | |
| static inline VOID *mem_malloc(UINT32 size, const CHAR *file, UINT32 line) | |
| { | |
| return (VOID *)tos_heap_alloc(size, file, line); | |
| } | |
| static inline VOID *mem_re_alloc(VOID *mem_ptr, UINT32 new_size, const CHAR *file, UINT32 line) | |
| { | |
| return (VOID *)tos_heap_realloc(mem_ptr, new_size, file, line); | |
| } | |
| static inline VOID mem_free_extend(VOID *mem_ptr, const CHAR *file, UINT32 line) | |
| { | |
| (VOID)tos_heap_free(mem_ptr, file, line); | |
| } | |
| #elif defined (_OS_LINUX) | |
| static inline VOID *mem_malloc(UINT32 size, const CHAR *file, UINT32 line) | |
| { | |
| return (VOID *)kmalloc(size, GFP_KERNEL); | |
| } | |
| static inline VOID *mem_re_alloc(VOID *mem_ptr, UINT32 new_size, const CHAR *file, UINT32 line) | |
| { | |
| return (VOID *)krealloc(mem_ptr, new_size, GFP_KERNEL); | |
| } | |
| static inline VOID mem_free_extend(VOID *mem_ptr, const CHAR *file, UINT32 line) | |
| { | |
| (VOID)kfree(mem_ptr); | |
| } | |
| #endif | |
| static inline VOID *mem_memcpy(VOID *dest_ptr, const VOID *src_ptr, UINT32 size, const CHAR *file, UINT32 line) | |
| { | |
| return memcpy(dest_ptr, src_ptr, size); | |
| } | |
| static inline VOID *mem_memset(VOID *dest_ptr, SINT32 value, UINT32 size, const CHAR *file, UINT32 line) | |
| { | |
| return memset(dest_ptr, value, size); | |
| } | |
| static inline VOID *mem_memmove(VOID *dest_ptr, const VOID *src_ptr, UINT32 count) | |
| { | |
| return memmove(dest_ptr, src_ptr, count); | |
| } | |
| static inline SINT32 mem_memcmp(const VOID *f_buf_ptr, const VOID *s_buf_ptr, UINT32 count) | |
| { | |
| return memcmp(f_buf_ptr, s_buf_ptr, count); | |
| } | |
| #else | |
| VOID *mem_malloc(UINT32 size, const CHAR *file, UINT32 line); | |
| VOID *mem_re_alloc(VOID *mem_ptr, UINT32 new_size, const CHAR *file, UINT32 line); | |
| VOID mem_free_extend(VOID *mem_ptr, const CHAR *file, UINT32 line); | |
| VOID *mem_memcpy(VOID *dest_ptr, const VOID *src_ptr, UINT32 size, const CHAR *file, UINT32 line); | |
| VOID *mem_memset(VOID *dest_ptr, SINT32 value, UINT32 size, const CHAR *file, UINT32 line); | |
| VOID *mem_memmove(VOID *dest_ptr, const VOID *src_ptr, UINT32 count); | |
| SINT32 mem_memcmp(const VOID *f_buf_ptr, const VOID *s_buf_ptr, UINT32 count); | |
| #endif | |
| #endif//#ifndef DOXYGEN_SHOULD_SKIP_THIS | |
| #ifdef __cplusplus | |
| } | |
| #endif | |
| #endif //#ifdef _USE_CLIB | |
| #endif //#ifndef _OSS_CLIB_H | |