blob: 9e1c88368b25660230acbf37ca813e30353e666c [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/**
2 * @file oss_clib.h
3 * @brief C¿âÍ·Îļþ
4 *
5 * Copyright (C) 2017 Sanechips Technology Co., Ltd.
6 *
7 */
8#ifndef _OSS_CLIB_H
9#define _OSS_CLIB_H
10
11#define _USE_CLIB
12
13#ifdef _USE_CLIB
14/*******************************************************************************
15 * Include header files *
16 ******************************************************************************/
17#if defined (_OS_LINUX)
18#include <linux/kernel.h>
19#include <linux/slab.h>
20#include <linux/string.h>
21#else
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <assert.h>
26#include <time.h>
27#include <math.h>
28#include <ctype.h>
29#endif
30
31#ifdef _OS_WIN
32#include <stddef.h>
33#endif
34#include "oss_pub.h"
35#include "oss_compiler.h"
36
37#ifdef __cplusplus
38extern "C"
39{
40#endif
41
42/*******************************************************************************
43 * Macro definitions *
44 ******************************************************************************/
45#ifdef _OS_LINUX
46/*
47 * stdio.h
48 */
49#define printf(fmt, ...) printk(fmt, ##__VA_ARGS__)
50
51/*
52 * random
53 */
54#define RAND_MAX (0x7FFFFFFF)
55#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0))
56# define srand(s) prandom_seed(s)
57# define rand() (prandom_u32() & RAND_MAX)
58#else
59# define srand(s) srandom32(s)
60# define rand() (random32() & RAND_MAX)
61#endif
62
63/*
64 * stdlib.h
65 */
66#define atol(nptr) simple_strtol(nptr, (char **)NULL, 10)
67#define atoi(nptr) simple_strtoul(nptr, (char **)NULL, 10)
68
69#define strtol simple_strtol
70#define strtoul simple_strtoul
71
72#define malloc(size) kmalloc(size, GFP_ATOMIC)
73#define free(ptr) kfree(ptr)
74
75/*
76 * assert.h
77 */
78#define assert(exp) zOss_ASSERT(exp)
79
80/*
81 * time
82 */
83#define settimeofday sys_settimeofday
84
85/*
86 * unistd.h
87 */
88#define exit(nr) do_exit(nr)
89
90#define zOss_Malloc(size) kmalloc(size, GFP_ATOMIC)
91#define zOss_MallocEx(size) kmalloc(size, GFP_KERNEL | __GFP_PAGEMODEM)
92#define zOss_Realloc(mem_ptr, new_size) krealloc(mem_ptr, new_size, GFP_ATOMIC)
93#define zOss_Free(mem_ptr) kfree(mem_ptr)
94#define zOss_Memcpy(dest_ptr, src_ptr, size) memcpy(dest_ptr, src_ptr, size)
95#define zOss_Memset(dest_ptr, value, size) memset(dest_ptr, value, size)
96
97#define zOss_Memmove(dest_ptr, src_ptr, count) memmove(dest_ptr, src_ptr, count)
98#define zOss_Memcmp(f_buf_ptr, s_buf_ptr, count) memcmp(f_buf_ptr, s_buf_ptr, count)
99
100#else
101/**
102 * @brief ¶¯Ì¬ÉêÇëÄڴ溯Êýºê
103 * @param size Èë²Î£¬ÉêÇëÄÚ´æµÄ´óС
104 * @return ³É¹¦·µ»Ø·ÖÅäµÄÄÚ´æÖ¸Õ룬ʧ°Ü·µ»Ø¿ÕÖ¸Õë
105 * @retval void*ÀàÐ͵ÄÖ¸Õë ³É¹¦
106 * @retval NULL ʧ°Ü
107 */
108#define zOss_Malloc(size) mem_malloc(size, ZOSS_FILE, ZOSS_LINE)
109
110/**
111 * @brief ¶¯Ì¬ÄÚ´æµ÷Õûº¯Êýºê
112 * @param mem_ptr Èë²Î£¬Òªµ÷ÕûµÄÄÚ´æÖ¸Õë
113 * @param new_size Èë²Î£¬Òªµ÷ÕûµÄÄÚ´æ´óС
114 * @return ³É¹¦·µ»Øµ÷ÕûºóµÄÄÚ´æÖ¸Õ룬ʧ°Ü·µ»Ø¿ÕÖ¸Õë
115 * @retval void*ÀàÐ͵ÄÖ¸Õë ³É¹¦
116 * @retval NULL ʧ°Ü
117 */
118#define zOss_Realloc(mem_ptr, new_size) mem_re_alloc(mem_ptr, new_size, ZOSS_FILE, ZOSS_LINE)
119
120/**
121 * @brief ÊÍ·ÅÄڴ溯Êýºê
122 * @param mem_ptr Èë²Î£¬ÒªÊͷŵÄÄÚ´æÖ¸Õë
123 * @return void
124 */
125#define zOss_Free(mem_ptr) mem_free_extend(mem_ptr, ZOSS_FILE, ZOSS_LINE)
126
127/**
128 * @brief Äڴ濽±´º¯Êýºê
129 * @param dest_ptr Èë²Î£¬Ä¿±êÄÚ´æÖ¸Õë
130 * @param src_ptr Èë²Î£¬Ô´ÄÚ´æÖ¸Õë
131 * @param size Èë²Î£¬Òª¿½±´µÄ´óС
132 * @return ·µ»Ødest_ptrµÄÖ¸Õë
133 */
134#define zOss_Memcpy(dest_ptr, src_ptr, size) mem_memcpy(dest_ptr, src_ptr, size, ZOSS_FILE, ZOSS_LINE)
135
136/**
137 * @brief ÄÚ´æ³õʼ»¯º¯Êýºê
138 * @param dest_ptr Èë²Î£¬Òª³õʼ»¯µÄÄ¿±êÄÚ´æÖ¸Õë
139 * @param value Èë²Î£¬³õʼ»¯µÄÖµ
140 * @param size Èë²Î£¬³õʼ»¯µÄÄÚ´æ´óС
141 * @return ·µ»Ø±»³õʼ»¯ÄÚ´æµÄÖ¸Õ루void*£©
142 */
143#define zOss_Memset(dest_ptr, value, size) mem_memset(dest_ptr, value, size, ZOSS_FILE, ZOSS_LINE)
144
145/**
146 * @brief ÄÚ´æ°áÒÆ
147 * @param dest_ptr Èë²Î£¬Òª°áÒÆµÄÄ¿±êÄÚ´æÖ¸Õë
148 * @param src_ptr Èë²Î£¬°áÒÆµÄÔ´ÄÚ´æÖ¸Õë
149 * @param count Èë²Î£¬°áÒÆÄÚ´æ´óС
150 * @return ·µ»Ødest_ptrµÄÖ¸Õë
151 */
152#define zOss_Memmove(dest_ptr, src_ptr, count) mem_memmove(dest_ptr, src_ptr, count)
153
154/**
155 * @brief ÄÚ´æ±È½Ïº¯Êýºê
156 * @param f_buf_ptr Èë²Î£¬Òª±È½ÏµÄÒ»·½ÄÚ´æÖ¸Õë
157 * @param s_buf_ptr Èë²Î£¬Òª±È½ÏµÄÁíÒ»·½ÄÚ´æÖ¸Õë
158 * @param count Èë²Î£¬Òª±È½ÏµÄÄÚ´æ´óС
159 * @return Ïàͬ·µ»Ø0£¬·ñÔò²»Ïàͬ
160 */
161#define zOss_Memcmp(f_buf_ptr, s_buf_ptr, count) mem_memcmp(f_buf_ptr, s_buf_ptr, count)
162
163#endif
164
165/*******************************************************************************
166 * Type definitions *
167 ******************************************************************************/
168
169/**
170 * @brief ZOSS_TIME_TµÄÀàÐͶ¨Òå
171 */
172typedef UINT32 ZOSS_TIME_T;
173
174/*******************************************************************************
175 * Global variable declarations *
176 ******************************************************************************/
177
178
179/*******************************************************************************
180 * Global function declarations *
181 ******************************************************************************/
182/**
183 * @brief ·µ»Ø´Ó¹«Ôª1970Äê1ÔÂ1ÈÕµÄUTCʱ¼ä´Ó0ʱ0·Ö0ÃëËãÆðµ½ÏÖÔÚËù¾­¹ýµÄÃëÊý
184 * @param t ³ö²Î£¬±£´æ´Ó¹«Ôª1970Äê1ÔÂ1ÈÕµÄUTCʱ¼ä´Ó0ʱ0·Ö0ÃëËãÆðµ½ÏÖÔÚËù¾­¹ýµÄ
185 ÃëÊý£¬¸Ã²ÎÊý¿ÉΪNULL
186 * @return ´Ó¹«Ôª1970Äê1ÔÂ1ÈÕµÄUTCʱ¼ä´Ó0ʱ0·Ö0ÃëËãÆðµ½ÏÖÔÚËù¾­¹ýµÄÃëÊý
187 * @note
188 * @warning
189 */
190ZOSS_TIME_T zOss_Time(ZOSS_TIME_T *t);
191
192#ifndef DOXYGEN_SHOULD_SKIP_THIS
193
194VOID *zOss_CacheMalloc(UINT32 size);
195
196VOID zOss_CacheFree(VOID *ptr);
197
198/* ÕûÊýת³É×Ö·û */
199char *itoa(int num, char *str, int radix);
200
201/*******************************************************************************
202 * Inline function implementations *
203 ******************************************************************************/
204#if defined(_MEM_USE_HEAP)
205
206#if defined (_OS_TOS)
207static inline VOID *mem_malloc(UINT32 size, const CHAR *file, UINT32 line)
208{
209 return (VOID *)tos_heap_alloc(size, file, line);
210}
211
212static inline VOID *mem_re_alloc(VOID *mem_ptr, UINT32 new_size, const CHAR *file, UINT32 line)
213{
214 return (VOID *)tos_heap_realloc(mem_ptr, new_size, file, line);
215}
216
217static inline VOID mem_free_extend(VOID *mem_ptr, const CHAR *file, UINT32 line)
218{
219 (VOID)tos_heap_free(mem_ptr, file, line);
220}
221#elif defined (_OS_LINUX)
222static inline VOID *mem_malloc(UINT32 size, const CHAR *file, UINT32 line)
223{
224 return (VOID *)kmalloc(size, GFP_KERNEL);
225}
226
227static inline VOID *mem_re_alloc(VOID *mem_ptr, UINT32 new_size, const CHAR *file, UINT32 line)
228{
229 return (VOID *)krealloc(mem_ptr, new_size, GFP_KERNEL);
230}
231
232static inline VOID mem_free_extend(VOID *mem_ptr, const CHAR *file, UINT32 line)
233{
234 (VOID)kfree(mem_ptr);
235}
236#endif
237
238static inline VOID *mem_memcpy(VOID *dest_ptr, const VOID *src_ptr, UINT32 size, const CHAR *file, UINT32 line)
239{
240 return memcpy(dest_ptr, src_ptr, size);
241}
242
243static inline VOID *mem_memset(VOID *dest_ptr, SINT32 value, UINT32 size, const CHAR *file, UINT32 line)
244{
245 return memset(dest_ptr, value, size);
246}
247
248static inline VOID *mem_memmove(VOID *dest_ptr, const VOID *src_ptr, UINT32 count)
249{
250 return memmove(dest_ptr, src_ptr, count);
251}
252
253static inline SINT32 mem_memcmp(const VOID *f_buf_ptr, const VOID *s_buf_ptr, UINT32 count)
254{
255 return memcmp(f_buf_ptr, s_buf_ptr, count);
256}
257
258#else
259
260VOID *mem_malloc(UINT32 size, const CHAR *file, UINT32 line);
261VOID *mem_re_alloc(VOID *mem_ptr, UINT32 new_size, const CHAR *file, UINT32 line);
262VOID mem_free_extend(VOID *mem_ptr, const CHAR *file, UINT32 line);
263VOID *mem_memcpy(VOID *dest_ptr, const VOID *src_ptr, UINT32 size, const CHAR *file, UINT32 line);
264VOID *mem_memset(VOID *dest_ptr, SINT32 value, UINT32 size, const CHAR *file, UINT32 line);
265VOID *mem_memmove(VOID *dest_ptr, const VOID *src_ptr, UINT32 count);
266SINT32 mem_memcmp(const VOID *f_buf_ptr, const VOID *s_buf_ptr, UINT32 count);
267
268#endif
269
270#endif//#ifndef DOXYGEN_SHOULD_SKIP_THIS
271
272#ifdef __cplusplus
273}
274#endif
275
276#endif //#ifdef _USE_CLIB
277
278#endif //#ifndef _OSS_CLIB_H
279