kservice.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-16 Bernard the first version
  9. * 2006-05-25 Bernard rewrite vsprintf
  10. * 2006-08-10 Bernard add rt_show_version
  11. * 2010-03-17 Bernard remove rt_strlcpy function
  12. * fix gcc compiling issue.
  13. * 2010-04-15 Bernard remove weak definition on ICCM16C compiler
  14. * 2012-07-18 Arda add the alignment display for signed integer
  15. * 2012-11-23 Bernard fix IAR compiler error.
  16. * 2012-12-22 Bernard fix rt_kprintf issue, which found by Grissiom.
  17. * 2013-06-24 Bernard remove rt_kprintf if RT_USING_CONSOLE is not defined.
  18. * 2013-09-24 aozima make sure the device is in STREAM mode when used by rt_kprintf.
  19. * 2015-07-06 Bernard Add rt_assert_handler routine.
  20. * 2021-02-28 Meco Man add RT_KSERVICE_USING_STDLIB
  21. * 2021-12-20 Meco Man implement rt_strcpy()
  22. * 2022-01-07 Gabriel add __on_rt_assert_hook
  23. * 2022-06-04 Meco Man remove strnlen
  24. * 2022-08-24 Yunjie make rt_memset word-independent to adapt to ti c28x (16bit word)
  25. * 2022-08-30 Yunjie make rt_vsnprintf adapt to ti c28x (16bit int)
  26. * 2023-02-02 Bernard add Smart ID for logo version show
  27. */
  28. #include <rtthread.h>
  29. #include <rthw.h>
  30. #ifdef RT_USING_MODULE
  31. #include <dlmodule.h>
  32. #endif /* RT_USING_MODULE */
  33. #ifdef RT_USING_SMART
  34. #include <lwp.h>
  35. #include <lwp_user_mm.h>
  36. #include <console.h>
  37. #endif
  38. /* use precision */
  39. #define RT_PRINTF_PRECISION
  40. /**
  41. * @addtogroup KernelService
  42. * @{
  43. */
  44. /* global errno in RT-Thread */
  45. static volatile int __rt_errno;
  46. #if defined(RT_USING_DEVICE) && defined(RT_USING_CONSOLE)
  47. static rt_device_t _console_device = RT_NULL;
  48. #endif
  49. rt_weak void rt_hw_us_delay(rt_uint32_t us)
  50. {
  51. (void) us;
  52. RT_DEBUG_LOG(RT_DEBUG_DEVICE, ("rt_hw_us_delay() doesn't support for this board."
  53. "Please consider implementing rt_hw_us_delay() in another file.\n"));
  54. }
  55. static const char* rt_errno_strs[] =
  56. {
  57. "OK",
  58. "ERROR",
  59. "ETIMOUT",
  60. "ERSFULL",
  61. "ERSEPTY",
  62. "ENOMEM",
  63. "ENOSYS",
  64. "EBUSY",
  65. "EIO",
  66. "EINTRPT",
  67. "EINVAL",
  68. "EUNKNOW"
  69. };
  70. /**
  71. * This function return a pointer to a string that contains the
  72. * message of error.
  73. *
  74. * @param error the errorno code
  75. * @return a point to error message string
  76. */
  77. const char *rt_strerror(rt_err_t error)
  78. {
  79. if (error < 0)
  80. error = -error;
  81. return (error > RT_EINVAL + 1) ?
  82. rt_errno_strs[RT_EINVAL + 1] :
  83. rt_errno_strs[error];
  84. }
  85. RTM_EXPORT(rt_strerror);
  86. /**
  87. * This function gets the global errno for the current thread.
  88. *
  89. * @return errno
  90. */
  91. rt_err_t rt_get_errno(void)
  92. {
  93. rt_thread_t tid = RT_NULL;
  94. if (rt_interrupt_get_nest() != 0)
  95. {
  96. /* it's in interrupt context */
  97. return __rt_errno;
  98. }
  99. tid = rt_thread_self();
  100. if (tid == RT_NULL)
  101. {
  102. return __rt_errno;
  103. }
  104. return tid->error;
  105. }
  106. RTM_EXPORT(rt_get_errno);
  107. /**
  108. * This function sets the global errno for the current thread.
  109. *
  110. * @param error is the errno shall be set.
  111. */
  112. void rt_set_errno(rt_err_t error)
  113. {
  114. rt_thread_t tid = RT_NULL;
  115. if (rt_interrupt_get_nest() != 0)
  116. {
  117. /* it's in interrupt context */
  118. __rt_errno = error;
  119. return;
  120. }
  121. tid = rt_thread_self();
  122. if (tid == RT_NULL)
  123. {
  124. __rt_errno = error;
  125. return;
  126. }
  127. tid->error = error;
  128. }
  129. RTM_EXPORT(rt_set_errno);
  130. /**
  131. * This function returns the address of the current thread errno.
  132. *
  133. * @return The errno address.
  134. */
  135. int *_rt_errno(void)
  136. {
  137. rt_thread_t tid = RT_NULL;
  138. if (rt_interrupt_get_nest() != 0)
  139. {
  140. return (int *)&__rt_errno;
  141. }
  142. tid = rt_thread_self();
  143. if (tid != RT_NULL)
  144. {
  145. return (int *) & (tid->error);
  146. }
  147. return (int *)&__rt_errno;
  148. }
  149. RTM_EXPORT(_rt_errno);
  150. #ifndef RT_KSERVICE_USING_STDLIB_MEMORY
  151. /**
  152. * This function will set the content of memory to specified value.
  153. *
  154. * @param s is the address of source memory, point to the memory block to be filled.
  155. *
  156. * @param c is the value to be set. The value is passed in int form, but the function
  157. * uses the unsigned character form of the value when filling the memory block.
  158. *
  159. * @param count number of bytes to be set.
  160. *
  161. * @return The address of source memory.
  162. */
  163. rt_weak void *rt_memset(void *s, int c, rt_ubase_t count)
  164. {
  165. #ifdef RT_KSERVICE_USING_TINY_SIZE
  166. char *xs = (char *)s;
  167. while (count--)
  168. *xs++ = c;
  169. return s;
  170. #else
  171. #define LBLOCKSIZE (sizeof(rt_ubase_t))
  172. #define UNALIGNED(X) ((long)X & (LBLOCKSIZE - 1))
  173. #define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE)
  174. unsigned int i = 0;
  175. char *m = (char *)s;
  176. unsigned long buffer = 0;
  177. unsigned long *aligned_addr = RT_NULL;
  178. unsigned char d = (unsigned int)c & (unsigned char)(-1); /* To avoid sign extension, copy C to an
  179. unsigned variable. (unsigned)((char)(-1))=0xFF for 8bit and =0xFFFF for 16bit: word independent */
  180. RT_ASSERT(LBLOCKSIZE == 2 || LBLOCKSIZE == 4 || LBLOCKSIZE == 8);
  181. if (!TOO_SMALL(count) && !UNALIGNED(s))
  182. {
  183. /* If we get this far, we know that count is large and s is word-aligned. */
  184. aligned_addr = (unsigned long *)s;
  185. /* Store d into each char sized location in buffer so that
  186. * we can set large blocks quickly.
  187. */
  188. for (i = 0; i < LBLOCKSIZE; i++)
  189. {
  190. *(((unsigned char *)&buffer)+i) = d;
  191. }
  192. while (count >= LBLOCKSIZE * 4)
  193. {
  194. *aligned_addr++ = buffer;
  195. *aligned_addr++ = buffer;
  196. *aligned_addr++ = buffer;
  197. *aligned_addr++ = buffer;
  198. count -= 4 * LBLOCKSIZE;
  199. }
  200. while (count >= LBLOCKSIZE)
  201. {
  202. *aligned_addr++ = buffer;
  203. count -= LBLOCKSIZE;
  204. }
  205. /* Pick up the remainder with a bytewise loop. */
  206. m = (char *)aligned_addr;
  207. }
  208. while (count--)
  209. {
  210. *m++ = (char)d;
  211. }
  212. return s;
  213. #undef LBLOCKSIZE
  214. #undef UNALIGNED
  215. #undef TOO_SMALL
  216. #endif /* RT_KSERVICE_USING_TINY_SIZE */
  217. }
  218. RTM_EXPORT(rt_memset);
  219. /**
  220. * This function will copy memory content from source address to destination address.
  221. *
  222. * @param dst is the address of destination memory, points to the copied content.
  223. *
  224. * @param src is the address of source memory, pointing to the data source to be copied.
  225. *
  226. * @param count is the copied length.
  227. *
  228. * @return The address of destination memory
  229. */
  230. rt_weak void *rt_memcpy(void *dst, const void *src, rt_ubase_t count)
  231. {
  232. #ifdef RT_KSERVICE_USING_TINY_SIZE
  233. char *tmp = (char *)dst, *s = (char *)src;
  234. rt_ubase_t len = 0;
  235. if (tmp <= s || tmp > (s + count))
  236. {
  237. while (count--)
  238. *tmp ++ = *s ++;
  239. }
  240. else
  241. {
  242. for (len = count; len > 0; len --)
  243. tmp[len - 1] = s[len - 1];
  244. }
  245. return dst;
  246. #else
  247. #define UNALIGNED(X, Y) \
  248. (((long)X & (sizeof (long) - 1)) | ((long)Y & (sizeof (long) - 1)))
  249. #define BIGBLOCKSIZE (sizeof (long) << 2)
  250. #define LITTLEBLOCKSIZE (sizeof (long))
  251. #define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)
  252. char *dst_ptr = (char *)dst;
  253. char *src_ptr = (char *)src;
  254. long *aligned_dst = RT_NULL;
  255. long *aligned_src = RT_NULL;
  256. rt_ubase_t len = count;
  257. /* If the size is small, or either SRC or DST is unaligned,
  258. then punt into the byte copy loop. This should be rare. */
  259. if (!TOO_SMALL(len) && !UNALIGNED(src_ptr, dst_ptr))
  260. {
  261. aligned_dst = (long *)dst_ptr;
  262. aligned_src = (long *)src_ptr;
  263. /* Copy 4X long words at a time if possible. */
  264. while (len >= BIGBLOCKSIZE)
  265. {
  266. *aligned_dst++ = *aligned_src++;
  267. *aligned_dst++ = *aligned_src++;
  268. *aligned_dst++ = *aligned_src++;
  269. *aligned_dst++ = *aligned_src++;
  270. len -= BIGBLOCKSIZE;
  271. }
  272. /* Copy one long word at a time if possible. */
  273. while (len >= LITTLEBLOCKSIZE)
  274. {
  275. *aligned_dst++ = *aligned_src++;
  276. len -= LITTLEBLOCKSIZE;
  277. }
  278. /* Pick up any residual with a byte copier. */
  279. dst_ptr = (char *)aligned_dst;
  280. src_ptr = (char *)aligned_src;
  281. }
  282. while (len--)
  283. *dst_ptr++ = *src_ptr++;
  284. return dst;
  285. #undef UNALIGNED
  286. #undef BIGBLOCKSIZE
  287. #undef LITTLEBLOCKSIZE
  288. #undef TOO_SMALL
  289. #endif /* RT_KSERVICE_USING_TINY_SIZE */
  290. }
  291. RTM_EXPORT(rt_memcpy);
  292. /**
  293. * This function will move memory content from source address to destination
  294. * address. If the destination memory does not overlap with the source memory,
  295. * the function is the same as memcpy().
  296. *
  297. * @param dest is the address of destination memory, points to the copied content.
  298. *
  299. * @param src is the address of source memory, point to the data source to be copied.
  300. *
  301. * @param n is the copied length.
  302. *
  303. * @return The address of destination memory.
  304. */
  305. void *rt_memmove(void *dest, const void *src, rt_size_t n)
  306. {
  307. char *tmp = (char *)dest, *s = (char *)src;
  308. if (s < tmp && tmp < s + n)
  309. {
  310. tmp += n;
  311. s += n;
  312. while (n--)
  313. *(--tmp) = *(--s);
  314. }
  315. else
  316. {
  317. while (n--)
  318. *tmp++ = *s++;
  319. }
  320. return dest;
  321. }
  322. RTM_EXPORT(rt_memmove);
  323. /**
  324. * This function will compare two areas of memory.
  325. *
  326. * @param cs is a block of memory.
  327. *
  328. * @param ct is another block of memory.
  329. *
  330. * @param count is the size of the area.
  331. *
  332. * @return Compare the results:
  333. * If the result < 0, cs is smaller than ct.
  334. * If the result > 0, cs is greater than ct.
  335. * If the result = 0, cs is equal to ct.
  336. */
  337. rt_int32_t rt_memcmp(const void *cs, const void *ct, rt_size_t count)
  338. {
  339. const unsigned char *su1 = RT_NULL, *su2 = RT_NULL;
  340. int res = 0;
  341. for (su1 = (const unsigned char *)cs, su2 = (const unsigned char *)ct; 0 < count; ++su1, ++su2, count--)
  342. if ((res = *su1 - *su2) != 0)
  343. break;
  344. return res;
  345. }
  346. RTM_EXPORT(rt_memcmp);
  347. #endif /* RT_KSERVICE_USING_STDLIB_MEMORY*/
  348. #ifndef RT_KSERVICE_USING_STDLIB
  349. /**
  350. * This function will return the first occurrence of a string, without the
  351. * terminator '\0'.
  352. *
  353. * @param s1 is the source string.
  354. *
  355. * @param s2 is the find string.
  356. *
  357. * @return The first occurrence of a s2 in s1, or RT_NULL if no found.
  358. */
  359. char *rt_strstr(const char *s1, const char *s2)
  360. {
  361. int l1 = 0, l2 = 0;
  362. l2 = rt_strlen(s2);
  363. if (!l2)
  364. {
  365. return (char *)s1;
  366. }
  367. l1 = rt_strlen(s1);
  368. while (l1 >= l2)
  369. {
  370. l1 --;
  371. if (!rt_memcmp(s1, s2, l2))
  372. {
  373. return (char *)s1;
  374. }
  375. s1 ++;
  376. }
  377. return RT_NULL;
  378. }
  379. RTM_EXPORT(rt_strstr);
  380. /**
  381. * This function will compare two strings while ignoring differences in case
  382. *
  383. * @param a is the string to be compared.
  384. *
  385. * @param b is the string to be compared.
  386. *
  387. * @return Compare the results:
  388. * If the result < 0, a is smaller than a.
  389. * If the result > 0, a is greater than a.
  390. * If the result = 0, a is equal to a.
  391. */
  392. rt_int32_t rt_strcasecmp(const char *a, const char *b)
  393. {
  394. int ca = 0, cb = 0;
  395. do
  396. {
  397. ca = *a++ & 0xff;
  398. cb = *b++ & 0xff;
  399. if (ca >= 'A' && ca <= 'Z')
  400. ca += 'a' - 'A';
  401. if (cb >= 'A' && cb <= 'Z')
  402. cb += 'a' - 'A';
  403. }
  404. while (ca == cb && ca != '\0');
  405. return ca - cb;
  406. }
  407. RTM_EXPORT(rt_strcasecmp);
  408. /**
  409. * This function will copy string no more than n bytes.
  410. *
  411. * @param dst points to the address used to store the copied content.
  412. *
  413. * @param src is the string to be copied.
  414. *
  415. * @param n is the maximum copied length.
  416. *
  417. * @return The address where the copied content is stored.
  418. */
  419. char *rt_strncpy(char *dst, const char *src, rt_size_t n)
  420. {
  421. if (n != 0)
  422. {
  423. char *d = dst;
  424. const char *s = src;
  425. do
  426. {
  427. if ((*d++ = *s++) == 0)
  428. {
  429. /* NUL pad the remaining n-1 bytes */
  430. while (--n != 0)
  431. {
  432. *d++ = 0;
  433. }
  434. break;
  435. }
  436. } while (--n != 0);
  437. }
  438. return (dst);
  439. }
  440. RTM_EXPORT(rt_strncpy);
  441. /**
  442. * This function will copy string.
  443. *
  444. * @param dst points to the address used to store the copied content.
  445. *
  446. * @param src is the string to be copied.
  447. *
  448. * @return The address where the copied content is stored.
  449. */
  450. char *rt_strcpy(char *dst, const char *src)
  451. {
  452. char *dest = dst;
  453. while (*src != '\0')
  454. {
  455. *dst = *src;
  456. dst++;
  457. src++;
  458. }
  459. *dst = '\0';
  460. return dest;
  461. }
  462. RTM_EXPORT(rt_strcpy);
  463. /**
  464. * This function will compare two strings with specified maximum length.
  465. *
  466. * @param cs is the string to be compared.
  467. *
  468. * @param ct is the string to be compared.
  469. *
  470. * @param count is the maximum compare length.
  471. *
  472. * @return Compare the results:
  473. * If the result < 0, cs is smaller than ct.
  474. * If the result > 0, cs is greater than ct.
  475. * If the result = 0, cs is equal to ct.
  476. */
  477. rt_int32_t rt_strncmp(const char *cs, const char *ct, rt_size_t count)
  478. {
  479. signed char __res = 0;
  480. while (count)
  481. {
  482. if ((__res = *cs - *ct++) != 0 || !*cs++)
  483. {
  484. break;
  485. }
  486. count --;
  487. }
  488. return __res;
  489. }
  490. RTM_EXPORT(rt_strncmp);
  491. /**
  492. * This function will compare two strings without specified length.
  493. *
  494. * @param cs is the string to be compared.
  495. *
  496. * @param ct is the string to be compared.
  497. *
  498. * @return Compare the results:
  499. * If the result < 0, cs is smaller than ct.
  500. * If the result > 0, cs is greater than ct.
  501. * If the result = 0, cs is equal to ct.
  502. */
  503. rt_int32_t rt_strcmp(const char *cs, const char *ct)
  504. {
  505. while (*cs && *cs == *ct)
  506. {
  507. cs++;
  508. ct++;
  509. }
  510. return (*cs - *ct);
  511. }
  512. RTM_EXPORT(rt_strcmp);
  513. /**
  514. * This function will return the length of a string, which terminate will
  515. * null character.
  516. *
  517. * @param s is the string
  518. *
  519. * @return The length of string.
  520. */
  521. rt_size_t rt_strlen(const char *s)
  522. {
  523. const char *sc = RT_NULL;
  524. for (sc = s; *sc != '\0'; ++sc) /* nothing */
  525. ;
  526. return sc - s;
  527. }
  528. RTM_EXPORT(rt_strlen);
  529. #endif /* RT_KSERVICE_USING_STDLIB */
  530. /**
  531. * The strnlen() function returns the number of characters in the
  532. * string pointed to by s, excluding the terminating null byte ('\0'),
  533. * but at most maxlen. In doing this, strnlen() looks only at the
  534. * first maxlen characters in the string pointed to by s and never
  535. * beyond s+maxlen.
  536. *
  537. * @param s is the string.
  538. *
  539. * @param maxlen is the max size.
  540. *
  541. * @return The length of string.
  542. */
  543. rt_size_t rt_strnlen(const char *s, rt_ubase_t maxlen)
  544. {
  545. const char *sc;
  546. for (sc = s; *sc != '\0' && (rt_ubase_t)(sc - s) < maxlen; ++sc) /* nothing */
  547. ;
  548. return sc - s;
  549. }
  550. RTM_EXPORT(rt_strnlen);
  551. #ifdef RT_USING_HEAP
  552. /**
  553. * This function will duplicate a string.
  554. *
  555. * @param s is the string to be duplicated.
  556. *
  557. * @return The string address of the copy.
  558. */
  559. char *rt_strdup(const char *s)
  560. {
  561. rt_size_t len = rt_strlen(s) + 1;
  562. char *tmp = (char *)rt_malloc(len);
  563. if (!tmp)
  564. {
  565. return RT_NULL;
  566. }
  567. rt_memcpy(tmp, s, len);
  568. return tmp;
  569. }
  570. RTM_EXPORT(rt_strdup);
  571. #endif /* RT_USING_HEAP */
  572. /**
  573. * This function will show the version of rt-thread rtos
  574. */
  575. void rt_show_version(void)
  576. {
  577. rt_kprintf("\n \\ | /\n");
  578. #ifdef RT_USING_SMART
  579. rt_kprintf("- RT - Thread Smart Operating System\n");
  580. #else
  581. rt_kprintf("- RT - Thread Operating System\n");
  582. #endif
  583. rt_kprintf(" / | \\ %d.%d.%d build %s %s\n",
  584. (rt_int32_t)RT_VERSION_MAJOR, (rt_int32_t)RT_VERSION_MINOR, (rt_int32_t)RT_VERSION_PATCH, __DATE__, __TIME__);
  585. rt_kprintf(" 2006 - 2022 Copyright by RT-Thread team\n");
  586. }
  587. RTM_EXPORT(rt_show_version);
  588. /* private function */
  589. #define _ISDIGIT(c) ((unsigned)((c) - '0') < 10)
  590. /**
  591. * This function will duplicate a string.
  592. *
  593. * @param n is the string to be duplicated.
  594. *
  595. * @param base is support divide instructions value.
  596. *
  597. * @return the duplicated string pointer.
  598. */
  599. #ifdef RT_KPRINTF_USING_LONGLONG
  600. rt_inline int divide(long long *n, int base)
  601. #else
  602. rt_inline int divide(long *n, int base)
  603. #endif /* RT_KPRINTF_USING_LONGLONG */
  604. {
  605. int res;
  606. /* optimized for processor which does not support divide instructions. */
  607. #ifdef RT_KPRINTF_USING_LONGLONG
  608. res = (int)(((unsigned long long)*n) % base);
  609. *n = (long long)(((unsigned long long)*n) / base);
  610. #else
  611. res = (int)(((unsigned long)*n) % base);
  612. *n = (long)(((unsigned long)*n) / base);
  613. #endif
  614. return res;
  615. }
  616. rt_inline int skip_atoi(const char **s)
  617. {
  618. int i = 0;
  619. while (_ISDIGIT(**s))
  620. i = i * 10 + *((*s)++) - '0';
  621. return i;
  622. }
  623. #define ZEROPAD (1 << 0) /* pad with zero */
  624. #define SIGN (1 << 1) /* unsigned/signed long */
  625. #define PLUS (1 << 2) /* show plus */
  626. #define SPACE (1 << 3) /* space if plus */
  627. #define LEFT (1 << 4) /* left justified */
  628. #define SPECIAL (1 << 5) /* 0x */
  629. #define LARGE (1 << 6) /* use 'ABCDEF' instead of 'abcdef' */
  630. static char *print_number(char *buf,
  631. char *end,
  632. #ifdef RT_KPRINTF_USING_LONGLONG
  633. long long num,
  634. #else
  635. long num,
  636. #endif /* RT_KPRINTF_USING_LONGLONG */
  637. int base,
  638. int s,
  639. #ifdef RT_PRINTF_PRECISION
  640. int precision,
  641. #endif /* RT_PRINTF_PRECISION */
  642. int type)
  643. {
  644. char c, sign;
  645. #ifdef RT_KPRINTF_USING_LONGLONG
  646. char tmp[64];
  647. #else
  648. char tmp[32];
  649. #endif /* RT_KPRINTF_USING_LONGLONG */
  650. int precision_bak = precision;
  651. const char *digits;
  652. static const char small_digits[] = "0123456789abcdef";
  653. static const char large_digits[] = "0123456789ABCDEF";
  654. int i, size;
  655. size = s;
  656. digits = (type & LARGE) ? large_digits : small_digits;
  657. if (type & LEFT)
  658. type &= ~ZEROPAD;
  659. c = (type & ZEROPAD) ? '0' : ' ';
  660. /* get sign */
  661. sign = 0;
  662. if (type & SIGN)
  663. {
  664. if (num < 0)
  665. {
  666. sign = '-';
  667. num = -num;
  668. }
  669. else if (type & PLUS)
  670. sign = '+';
  671. else if (type & SPACE)
  672. sign = ' ';
  673. }
  674. #ifdef RT_PRINTF_SPECIAL
  675. if (type & SPECIAL)
  676. {
  677. if (base == 2 || base == 16)
  678. size -= 2;
  679. else if (base == 8)
  680. size--;
  681. }
  682. #endif /* RT_PRINTF_SPECIAL */
  683. i = 0;
  684. if (num == 0)
  685. tmp[i++] = '0';
  686. else
  687. {
  688. while (num != 0)
  689. tmp[i++] = digits[divide(&num, base)];
  690. }
  691. #ifdef RT_PRINTF_PRECISION
  692. if (i > precision)
  693. precision = i;
  694. size -= precision;
  695. #else
  696. size -= i;
  697. #endif /* RT_PRINTF_PRECISION */
  698. if (!(type & (ZEROPAD | LEFT)))
  699. {
  700. if ((sign) && (size > 0))
  701. size--;
  702. while (size-- > 0)
  703. {
  704. if (buf < end)
  705. *buf = ' ';
  706. ++ buf;
  707. }
  708. }
  709. if (sign)
  710. {
  711. if (buf < end)
  712. {
  713. *buf = sign;
  714. }
  715. -- size;
  716. ++ buf;
  717. }
  718. #ifdef RT_PRINTF_SPECIAL
  719. if (type & SPECIAL)
  720. {
  721. if (base == 2)
  722. {
  723. if (buf < end)
  724. *buf = '0';
  725. ++ buf;
  726. if (buf < end)
  727. *buf = 'b';
  728. ++ buf;
  729. }
  730. else if (base == 8)
  731. {
  732. if (buf < end)
  733. *buf = '0';
  734. ++ buf;
  735. }
  736. else if (base == 16)
  737. {
  738. if (buf < end)
  739. *buf = '0';
  740. ++ buf;
  741. if (buf < end)
  742. {
  743. *buf = type & LARGE ? 'X' : 'x';
  744. }
  745. ++ buf;
  746. }
  747. }
  748. #endif /* RT_PRINTF_SPECIAL */
  749. /* no align to the left */
  750. if (!(type & LEFT))
  751. {
  752. while (size-- > 0)
  753. {
  754. if (buf < end)
  755. *buf = c;
  756. ++ buf;
  757. }
  758. }
  759. #ifdef RT_PRINTF_PRECISION
  760. while (i < precision--)
  761. {
  762. if (buf < end)
  763. *buf = '0';
  764. ++ buf;
  765. }
  766. #endif /* RT_PRINTF_PRECISION */
  767. /* put number in the temporary buffer */
  768. while (i-- > 0 && (precision_bak != 0))
  769. {
  770. if (buf < end)
  771. *buf = tmp[i];
  772. ++ buf;
  773. }
  774. while (size-- > 0)
  775. {
  776. if (buf < end)
  777. *buf = ' ';
  778. ++ buf;
  779. }
  780. return buf;
  781. }
  782. /**
  783. * This function will fill a formatted string to buffer.
  784. *
  785. * @param buf is the buffer to save formatted string.
  786. *
  787. * @param size is the size of buffer.
  788. *
  789. * @param fmt is the format parameters.
  790. *
  791. * @param args is a list of variable parameters.
  792. *
  793. * @return The number of characters actually written to buffer.
  794. */
  795. rt_weak int rt_vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list args)
  796. {
  797. #ifdef RT_KPRINTF_USING_LONGLONG
  798. unsigned long long num;
  799. #else
  800. rt_uint32_t num;
  801. #endif /* RT_KPRINTF_USING_LONGLONG */
  802. int i, len;
  803. char *str, *end, c;
  804. const char *s;
  805. rt_uint8_t base; /* the base of number */
  806. rt_uint8_t flags; /* flags to print number */
  807. rt_uint8_t qualifier; /* 'h', 'l', or 'L' for integer fields */
  808. rt_int32_t field_width; /* width of output field */
  809. #ifdef RT_PRINTF_PRECISION
  810. int precision; /* min. # of digits for integers and max for a string */
  811. #endif /* RT_PRINTF_PRECISION */
  812. str = buf;
  813. end = buf + size;
  814. /* Make sure end is always >= buf */
  815. if (end < buf)
  816. {
  817. end = ((char *) - 1);
  818. size = end - buf;
  819. }
  820. for (; *fmt ; ++fmt)
  821. {
  822. if (*fmt != '%')
  823. {
  824. if (str < end)
  825. *str = *fmt;
  826. ++ str;
  827. continue;
  828. }
  829. /* process flags */
  830. flags = 0;
  831. while (1)
  832. {
  833. /* skips the first '%' also */
  834. ++ fmt;
  835. if (*fmt == '-') flags |= LEFT;
  836. else if (*fmt == '+') flags |= PLUS;
  837. else if (*fmt == ' ') flags |= SPACE;
  838. else if (*fmt == '#') flags |= SPECIAL;
  839. else if (*fmt == '0') flags |= ZEROPAD;
  840. else break;
  841. }
  842. /* get field width */
  843. field_width = -1;
  844. if (_ISDIGIT(*fmt)) field_width = skip_atoi(&fmt);
  845. else if (*fmt == '*')
  846. {
  847. ++ fmt;
  848. /* it's the next argument */
  849. field_width = va_arg(args, int);
  850. if (field_width < 0)
  851. {
  852. field_width = -field_width;
  853. flags |= LEFT;
  854. }
  855. }
  856. #ifdef RT_PRINTF_PRECISION
  857. /* get the precision */
  858. precision = -1;
  859. if (*fmt == '.')
  860. {
  861. ++ fmt;
  862. if (_ISDIGIT(*fmt)) precision = skip_atoi(&fmt);
  863. else if (*fmt == '*')
  864. {
  865. ++ fmt;
  866. /* it's the next argument */
  867. precision = va_arg(args, int);
  868. }
  869. if (precision < 0) precision = 0;
  870. }
  871. #endif /* RT_PRINTF_PRECISION */
  872. /* get the conversion qualifier */
  873. qualifier = 0;
  874. #ifdef RT_KPRINTF_USING_LONGLONG
  875. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L')
  876. #else
  877. if (*fmt == 'h' || *fmt == 'l')
  878. #endif /* RT_KPRINTF_USING_LONGLONG */
  879. {
  880. qualifier = *fmt;
  881. ++ fmt;
  882. #ifdef RT_KPRINTF_USING_LONGLONG
  883. if (qualifier == 'l' && *fmt == 'l')
  884. {
  885. qualifier = 'L';
  886. ++ fmt;
  887. }
  888. #endif /* RT_KPRINTF_USING_LONGLONG */
  889. }
  890. /* the default base */
  891. base = 10;
  892. switch (*fmt)
  893. {
  894. case 'c':
  895. if (!(flags & LEFT))
  896. {
  897. while (--field_width > 0)
  898. {
  899. if (str < end) *str = ' ';
  900. ++ str;
  901. }
  902. }
  903. /* get character */
  904. c = (rt_uint8_t)va_arg(args, int);
  905. if (str < end) *str = c;
  906. ++ str;
  907. /* put width */
  908. while (--field_width > 0)
  909. {
  910. if (str < end) *str = ' ';
  911. ++ str;
  912. }
  913. continue;
  914. case 's':
  915. s = va_arg(args, char *);
  916. if (!s) s = "(NULL)";
  917. for (len = 0; (len != field_width) && (s[len] != '\0'); len++);
  918. #ifdef RT_PRINTF_PRECISION
  919. if (precision > 0 && len > precision) len = precision;
  920. #endif /* RT_PRINTF_PRECISION */
  921. if (!(flags & LEFT))
  922. {
  923. while (len < field_width--)
  924. {
  925. if (str < end) *str = ' ';
  926. ++ str;
  927. }
  928. }
  929. for (i = 0; i < len; ++i)
  930. {
  931. if (str < end) *str = *s;
  932. ++ str;
  933. ++ s;
  934. }
  935. while (len < field_width--)
  936. {
  937. if (str < end) *str = ' ';
  938. ++ str;
  939. }
  940. continue;
  941. case 'p':
  942. if (field_width == -1)
  943. {
  944. field_width = sizeof(void *) << 1;
  945. flags |= ZEROPAD;
  946. }
  947. #ifdef RT_PRINTF_PRECISION
  948. str = print_number(str, end,
  949. (long)va_arg(args, void *),
  950. 16, field_width, precision, flags);
  951. #else
  952. str = print_number(str, end,
  953. (long)va_arg(args, void *),
  954. 16, field_width, flags);
  955. #endif /* RT_PRINTF_PRECISION */
  956. continue;
  957. case '%':
  958. if (str < end) *str = '%';
  959. ++ str;
  960. continue;
  961. /* integer number formats - set up the flags and "break" */
  962. case 'b':
  963. base = 2;
  964. break;
  965. case 'o':
  966. base = 8;
  967. break;
  968. case 'X':
  969. flags |= LARGE;
  970. case 'x':
  971. base = 16;
  972. break;
  973. case 'd':
  974. case 'i':
  975. flags |= SIGN;
  976. case 'u':
  977. break;
  978. default:
  979. if (str < end) *str = '%';
  980. ++ str;
  981. if (*fmt)
  982. {
  983. if (str < end) *str = *fmt;
  984. ++ str;
  985. }
  986. else
  987. {
  988. -- fmt;
  989. }
  990. continue;
  991. }
  992. #ifdef RT_KPRINTF_USING_LONGLONG
  993. if (qualifier == 'L') num = va_arg(args, long long);
  994. else if (qualifier == 'l')
  995. #else
  996. if (qualifier == 'l')
  997. #endif /* RT_KPRINTF_USING_LONGLONG */
  998. {
  999. num = va_arg(args, rt_uint32_t);
  1000. if (flags & SIGN) num = (rt_int32_t)num;
  1001. }
  1002. else if (qualifier == 'h')
  1003. {
  1004. num = (rt_uint16_t)va_arg(args, int);
  1005. if (flags & SIGN) num = (rt_int16_t)num;
  1006. }
  1007. else
  1008. {
  1009. num = va_arg(args, rt_uint32_t);
  1010. if (flags & SIGN) num = (rt_int32_t)num;
  1011. }
  1012. #ifdef RT_PRINTF_PRECISION
  1013. str = print_number(str, end, num, base, field_width, precision, flags);
  1014. #else
  1015. str = print_number(str, end, num, base, field_width, flags);
  1016. #endif /* RT_PRINTF_PRECISION */
  1017. }
  1018. if (size > 0)
  1019. {
  1020. if (str < end) *str = '\0';
  1021. else
  1022. {
  1023. end[-1] = '\0';
  1024. }
  1025. }
  1026. /* the trailing null byte doesn't count towards the total
  1027. * ++str;
  1028. */
  1029. return str - buf;
  1030. }
  1031. RTM_EXPORT(rt_vsnprintf);
  1032. /**
  1033. * This function will fill a formatted string to buffer.
  1034. *
  1035. * @param buf is the buffer to save formatted string.
  1036. *
  1037. * @param size is the size of buffer.
  1038. *
  1039. * @param fmt is the format parameters.
  1040. *
  1041. * @return The number of characters actually written to buffer.
  1042. */
  1043. int rt_snprintf(char *buf, rt_size_t size, const char *fmt, ...)
  1044. {
  1045. rt_int32_t n;
  1046. va_list args;
  1047. va_start(args, fmt);
  1048. n = rt_vsnprintf(buf, size, fmt, args);
  1049. va_end(args);
  1050. return n;
  1051. }
  1052. RTM_EXPORT(rt_snprintf);
  1053. /**
  1054. * This function will fill a formatted string to buffer.
  1055. *
  1056. * @param buf is the buffer to save formatted string.
  1057. *
  1058. * @param format is the format parameters.
  1059. *
  1060. * @param arg_ptr is a list of variable parameters.
  1061. *
  1062. * @return The number of characters actually written to buffer.
  1063. */
  1064. int rt_vsprintf(char *buf, const char *format, va_list arg_ptr)
  1065. {
  1066. return rt_vsnprintf(buf, (rt_size_t) - 1, format, arg_ptr);
  1067. }
  1068. RTM_EXPORT(rt_vsprintf);
  1069. /**
  1070. * This function will fill a formatted string to buffer
  1071. *
  1072. * @param buf the buffer to save formatted string.
  1073. *
  1074. * @param format is the format parameters.
  1075. *
  1076. * @return The number of characters actually written to buffer.
  1077. */
  1078. int rt_sprintf(char *buf, const char *format, ...)
  1079. {
  1080. rt_int32_t n;
  1081. va_list arg_ptr;
  1082. va_start(arg_ptr, format);
  1083. n = rt_vsprintf(buf, format, arg_ptr);
  1084. va_end(arg_ptr);
  1085. return n;
  1086. }
  1087. RTM_EXPORT(rt_sprintf);
  1088. #ifdef RT_USING_CONSOLE
  1089. #ifdef RT_USING_DEVICE
  1090. /**
  1091. * This function returns the device using in console.
  1092. *
  1093. * @return Returns the console device pointer or RT_NULL.
  1094. */
  1095. rt_device_t rt_console_get_device(void)
  1096. {
  1097. return _console_device;
  1098. }
  1099. RTM_EXPORT(rt_console_get_device);
  1100. /**
  1101. * This function will set a device as console device.
  1102. * After set a device to console, all output of rt_kprintf will be
  1103. * redirected to this new device.
  1104. *
  1105. * @param name is the name of new console device.
  1106. *
  1107. * @return the old console device handler on successful, or RT_NULL on failure.
  1108. */
  1109. rt_device_t rt_console_set_device(const char *name)
  1110. {
  1111. #ifdef RT_USING_SMART
  1112. rt_device_t new_iodev = RT_NULL, old_iodev = RT_NULL;
  1113. extern void console_init();
  1114. console_init(); /*add line discipline*/
  1115. /* find new console device */
  1116. new_iodev = rt_device_find(name);
  1117. if (new_iodev != RT_NULL)
  1118. {
  1119. if (_console_device != RT_NULL)
  1120. {
  1121. old_iodev = console_set_iodev(new_iodev);
  1122. }
  1123. else
  1124. {
  1125. console_register("console", new_iodev);
  1126. _console_device = rt_device_find("console");
  1127. rt_device_open(_console_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM);
  1128. }
  1129. }
  1130. return old_iodev;
  1131. #else
  1132. rt_device_t new_device, old_device;
  1133. /* save old device */
  1134. old_device = _console_device;
  1135. /* find new console device */
  1136. new_device = rt_device_find(name);
  1137. /* check whether it's a same device */
  1138. if (new_device == old_device) return RT_NULL;
  1139. if (new_device != RT_NULL)
  1140. {
  1141. if (_console_device != RT_NULL)
  1142. {
  1143. /* close old console device */
  1144. rt_device_close(_console_device);
  1145. }
  1146. /* set new console device */
  1147. rt_device_open(new_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM);
  1148. _console_device = new_device;
  1149. }
  1150. return old_device;
  1151. #endif
  1152. }
  1153. RTM_EXPORT(rt_console_set_device);
  1154. #endif /* RT_USING_DEVICE */
  1155. rt_weak void rt_hw_console_output(const char *str)
  1156. {
  1157. /* empty console output */
  1158. }
  1159. RTM_EXPORT(rt_hw_console_output);
  1160. /**
  1161. * This function will put string to the console.
  1162. *
  1163. * @param str is the string output to the console.
  1164. */
  1165. void rt_kputs(const char *str)
  1166. {
  1167. if (!str) return;
  1168. #ifdef RT_USING_DEVICE
  1169. if (_console_device == RT_NULL)
  1170. {
  1171. rt_hw_console_output(str);
  1172. }
  1173. else
  1174. {
  1175. rt_device_write(_console_device, 0, str, rt_strlen(str));
  1176. }
  1177. #else
  1178. rt_hw_console_output(str);
  1179. #endif /* RT_USING_DEVICE */
  1180. }
  1181. /**
  1182. * This function will print a formatted string on system console.
  1183. *
  1184. * @param fmt is the format parameters.
  1185. *
  1186. * @return The number of characters actually written to buffer.
  1187. */
  1188. rt_weak int rt_kprintf(const char *fmt, ...)
  1189. {
  1190. va_list args;
  1191. rt_size_t length;
  1192. static char rt_log_buf[RT_CONSOLEBUF_SIZE];
  1193. va_start(args, fmt);
  1194. /* the return value of vsnprintf is the number of bytes that would be
  1195. * written to buffer had if the size of the buffer been sufficiently
  1196. * large excluding the terminating null byte. If the output string
  1197. * would be larger than the rt_log_buf, we have to adjust the output
  1198. * length. */
  1199. length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
  1200. if (length > RT_CONSOLEBUF_SIZE - 1)
  1201. length = RT_CONSOLEBUF_SIZE - 1;
  1202. #ifdef RT_USING_DEVICE
  1203. if (_console_device == RT_NULL)
  1204. {
  1205. rt_hw_console_output(rt_log_buf);
  1206. }
  1207. else
  1208. {
  1209. rt_device_write(_console_device, 0, rt_log_buf, length);
  1210. }
  1211. #else
  1212. rt_hw_console_output(rt_log_buf);
  1213. #endif /* RT_USING_DEVICE */
  1214. va_end(args);
  1215. return length;
  1216. }
  1217. RTM_EXPORT(rt_kprintf);
  1218. #endif /* RT_USING_CONSOLE */
  1219. #if defined(RT_USING_HEAP) && !defined(RT_USING_USERHEAP)
  1220. #ifdef RT_USING_HOOK
  1221. static void (*rt_malloc_hook)(void *ptr, rt_size_t size);
  1222. static void (*rt_free_hook)(void *ptr);
  1223. /**
  1224. * @addtogroup Hook
  1225. * @{
  1226. */
  1227. /**
  1228. * @brief This function will set a hook function, which will be invoked when a memory
  1229. * block is allocated from heap memory.
  1230. *
  1231. * @param hook the hook function.
  1232. */
  1233. void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size))
  1234. {
  1235. rt_malloc_hook = hook;
  1236. }
  1237. /**
  1238. * @brief This function will set a hook function, which will be invoked when a memory
  1239. * block is released to heap memory.
  1240. *
  1241. * @param hook the hook function
  1242. */
  1243. void rt_free_sethook(void (*hook)(void *ptr))
  1244. {
  1245. rt_free_hook = hook;
  1246. }
  1247. /**@}*/
  1248. #endif /* RT_USING_HOOK */
  1249. #if defined(RT_USING_HEAP_ISR)
  1250. #elif defined(RT_USING_MUTEX)
  1251. static struct rt_mutex _lock;
  1252. #endif
  1253. rt_inline void _heap_lock_init(void)
  1254. {
  1255. #if defined(RT_USING_HEAP_ISR)
  1256. #elif defined(RT_USING_MUTEX)
  1257. rt_mutex_init(&_lock, "heap", RT_IPC_FLAG_PRIO);
  1258. #endif
  1259. }
  1260. rt_inline rt_base_t _heap_lock(void)
  1261. {
  1262. #if defined(RT_USING_HEAP_ISR)
  1263. return rt_hw_interrupt_disable();
  1264. #elif defined(RT_USING_MUTEX)
  1265. if (rt_thread_self())
  1266. return rt_mutex_take(&_lock, RT_WAITING_FOREVER);
  1267. else
  1268. return RT_EOK;
  1269. #else
  1270. rt_enter_critical();
  1271. return RT_EOK;
  1272. #endif
  1273. }
  1274. rt_inline void _heap_unlock(rt_base_t level)
  1275. {
  1276. #if defined(RT_USING_HEAP_ISR)
  1277. rt_hw_interrupt_enable(level);
  1278. #elif defined(RT_USING_MUTEX)
  1279. RT_ASSERT(level == RT_EOK);
  1280. if (rt_thread_self())
  1281. rt_mutex_release(&_lock);
  1282. #else
  1283. rt_exit_critical();
  1284. #endif
  1285. }
  1286. #if defined(RT_USING_SMALL_MEM_AS_HEAP)
  1287. static rt_smem_t system_heap;
  1288. rt_inline void _smem_info(rt_size_t *total,
  1289. rt_size_t *used, rt_size_t *max_used)
  1290. {
  1291. if (total)
  1292. *total = system_heap->total;
  1293. if (used)
  1294. *used = system_heap->used;
  1295. if (max_used)
  1296. *max_used = system_heap->max;
  1297. }
  1298. #define _MEM_INIT(_name, _start, _size) \
  1299. system_heap = rt_smem_init(_name, _start, _size)
  1300. #define _MEM_MALLOC(_size) \
  1301. rt_smem_alloc(system_heap, _size)
  1302. #define _MEM_REALLOC(_ptr, _newsize)\
  1303. rt_smem_realloc(system_heap, _ptr, _newsize)
  1304. #define _MEM_FREE(_ptr) \
  1305. rt_smem_free(_ptr)
  1306. #define _MEM_INFO(_total, _used, _max) \
  1307. _smem_info(_total, _used, _max)
  1308. #elif defined(RT_USING_MEMHEAP_AS_HEAP)
  1309. static struct rt_memheap system_heap;
  1310. void *_memheap_alloc(struct rt_memheap *heap, rt_size_t size);
  1311. void _memheap_free(void *rmem);
  1312. void *_memheap_realloc(struct rt_memheap *heap, void *rmem, rt_size_t newsize);
  1313. #define _MEM_INIT(_name, _start, _size) \
  1314. rt_memheap_init(&system_heap, _name, _start, _size)
  1315. #define _MEM_MALLOC(_size) \
  1316. _memheap_alloc(&system_heap, _size)
  1317. #define _MEM_REALLOC(_ptr, _newsize) \
  1318. _memheap_realloc(&system_heap, _ptr, _newsize)
  1319. #define _MEM_FREE(_ptr) \
  1320. _memheap_free(_ptr)
  1321. #define _MEM_INFO(_total, _used, _max) \
  1322. rt_memheap_info(&system_heap, _total, _used, _max)
  1323. #elif defined(RT_USING_SLAB_AS_HEAP)
  1324. static rt_slab_t system_heap;
  1325. rt_inline void _slab_info(rt_size_t *total,
  1326. rt_size_t *used, rt_size_t *max_used)
  1327. {
  1328. if (total)
  1329. *total = system_heap->total;
  1330. if (used)
  1331. *used = system_heap->used;
  1332. if (max_used)
  1333. *max_used = system_heap->max;
  1334. }
  1335. #define _MEM_INIT(_name, _start, _size) \
  1336. system_heap = rt_slab_init(_name, _start, _size)
  1337. #define _MEM_MALLOC(_size) \
  1338. rt_slab_alloc(system_heap, _size)
  1339. #define _MEM_REALLOC(_ptr, _newsize) \
  1340. rt_slab_realloc(system_heap, _ptr, _newsize)
  1341. #define _MEM_FREE(_ptr) \
  1342. rt_slab_free(system_heap, _ptr)
  1343. #define _MEM_INFO _slab_info
  1344. #else
  1345. #define _MEM_INIT(...)
  1346. #define _MEM_MALLOC(...) RT_NULL
  1347. #define _MEM_REALLOC(...) RT_NULL
  1348. #define _MEM_FREE(...)
  1349. #define _MEM_INFO(...)
  1350. #endif
  1351. /**
  1352. * @brief This function will init system heap.
  1353. *
  1354. * @param begin_addr the beginning address of system page.
  1355. *
  1356. * @param end_addr the end address of system page.
  1357. */
  1358. rt_weak void rt_system_heap_init(void *begin_addr, void *end_addr)
  1359. {
  1360. rt_ubase_t begin_align = RT_ALIGN((rt_ubase_t)begin_addr, RT_ALIGN_SIZE);
  1361. rt_ubase_t end_align = RT_ALIGN_DOWN((rt_ubase_t)end_addr, RT_ALIGN_SIZE);
  1362. RT_ASSERT(end_align > begin_align);
  1363. /* Initialize system memory heap */
  1364. _MEM_INIT("heap", begin_addr, end_align - begin_align);
  1365. /* Initialize multi thread contention lock */
  1366. _heap_lock_init();
  1367. }
  1368. /**
  1369. * @brief Allocate a block of memory with a minimum of 'size' bytes.
  1370. *
  1371. * @param size is the minimum size of the requested block in bytes.
  1372. *
  1373. * @return the pointer to allocated memory or NULL if no free memory was found.
  1374. */
  1375. rt_weak void *rt_malloc(rt_size_t size)
  1376. {
  1377. rt_base_t level;
  1378. void *ptr;
  1379. /* Enter critical zone */
  1380. level = _heap_lock();
  1381. /* allocate memory block from system heap */
  1382. ptr = _MEM_MALLOC(size);
  1383. /* Exit critical zone */
  1384. _heap_unlock(level);
  1385. /* call 'rt_malloc' hook */
  1386. RT_OBJECT_HOOK_CALL(rt_malloc_hook, (ptr, size));
  1387. return ptr;
  1388. }
  1389. RTM_EXPORT(rt_malloc);
  1390. /**
  1391. * @brief This function will change the size of previously allocated memory block.
  1392. *
  1393. * @param rmem is the pointer to memory allocated by rt_malloc.
  1394. *
  1395. * @param newsize is the required new size.
  1396. *
  1397. * @return the changed memory block address.
  1398. */
  1399. rt_weak void *rt_realloc(void *rmem, rt_size_t newsize)
  1400. {
  1401. rt_base_t level;
  1402. void *nptr;
  1403. /* Enter critical zone */
  1404. level = _heap_lock();
  1405. /* Change the size of previously allocated memory block */
  1406. nptr = _MEM_REALLOC(rmem, newsize);
  1407. /* Exit critical zone */
  1408. _heap_unlock(level);
  1409. return nptr;
  1410. }
  1411. RTM_EXPORT(rt_realloc);
  1412. /**
  1413. * @brief This function will contiguously allocate enough space for count objects
  1414. * that are size bytes of memory each and returns a pointer to the allocated
  1415. * memory.
  1416. *
  1417. * @note The allocated memory is filled with bytes of value zero.
  1418. *
  1419. * @param count is the number of objects to allocate.
  1420. *
  1421. * @param size is the size of one object to allocate.
  1422. *
  1423. * @return pointer to allocated memory / NULL pointer if there is an error.
  1424. */
  1425. rt_weak void *rt_calloc(rt_size_t count, rt_size_t size)
  1426. {
  1427. void *p;
  1428. /* allocate 'count' objects of size 'size' */
  1429. p = rt_malloc(count * size);
  1430. /* zero the memory */
  1431. if (p)
  1432. {
  1433. rt_memset(p, 0, count * size);
  1434. }
  1435. return p;
  1436. }
  1437. RTM_EXPORT(rt_calloc);
  1438. /**
  1439. * @brief This function will release the previously allocated memory block by
  1440. * rt_malloc. The released memory block is taken back to system heap.
  1441. *
  1442. * @param rmem the address of memory which will be released.
  1443. */
  1444. rt_weak void rt_free(void *rmem)
  1445. {
  1446. rt_base_t level;
  1447. /* call 'rt_free' hook */
  1448. RT_OBJECT_HOOK_CALL(rt_free_hook, (rmem));
  1449. /* NULL check */
  1450. if (rmem == RT_NULL) return;
  1451. /* Enter critical zone */
  1452. level = _heap_lock();
  1453. _MEM_FREE(rmem);
  1454. /* Exit critical zone */
  1455. _heap_unlock(level);
  1456. }
  1457. RTM_EXPORT(rt_free);
  1458. /**
  1459. * @brief This function will caculate the total memory, the used memory, and
  1460. * the max used memory.
  1461. *
  1462. * @param total is a pointer to get the total size of the memory.
  1463. *
  1464. * @param used is a pointer to get the size of memory used.
  1465. *
  1466. * @param max_used is a pointer to get the maximum memory used.
  1467. */
  1468. rt_weak void rt_memory_info(rt_size_t *total,
  1469. rt_size_t *used,
  1470. rt_size_t *max_used)
  1471. {
  1472. rt_base_t level;
  1473. /* Enter critical zone */
  1474. level = _heap_lock();
  1475. _MEM_INFO(total, used, max_used);
  1476. /* Exit critical zone */
  1477. _heap_unlock(level);
  1478. }
  1479. RTM_EXPORT(rt_memory_info);
  1480. #if defined(RT_USING_SLAB) && defined(RT_USING_SLAB_AS_HEAP)
  1481. void *rt_page_alloc(rt_size_t npages)
  1482. {
  1483. rt_base_t level;
  1484. void *ptr;
  1485. /* Enter critical zone */
  1486. level = _heap_lock();
  1487. /* alloc page */
  1488. ptr = rt_slab_page_alloc(system_heap, npages);
  1489. /* Exit critical zone */
  1490. _heap_unlock(level);
  1491. return ptr;
  1492. }
  1493. void rt_page_free(void *addr, rt_size_t npages)
  1494. {
  1495. rt_base_t level;
  1496. /* Enter critical zone */
  1497. level = _heap_lock();
  1498. /* free page */
  1499. rt_slab_page_free(system_heap, addr, npages);
  1500. /* Exit critical zone */
  1501. _heap_unlock(level);
  1502. }
  1503. #endif
  1504. /**
  1505. * This function allocates a memory block, which address is aligned to the
  1506. * specified alignment size.
  1507. *
  1508. * @param size is the allocated memory block size.
  1509. *
  1510. * @param align is the alignment size.
  1511. *
  1512. * @return The memory block address was returned successfully, otherwise it was
  1513. * returned empty RT_NULL.
  1514. */
  1515. rt_weak void *rt_malloc_align(rt_size_t size, rt_size_t align)
  1516. {
  1517. void *ptr;
  1518. void *align_ptr;
  1519. int uintptr_size;
  1520. rt_size_t align_size;
  1521. /* sizeof pointer */
  1522. uintptr_size = sizeof(void*);
  1523. uintptr_size -= 1;
  1524. /* align the alignment size to uintptr size byte */
  1525. align = ((align + uintptr_size) & ~uintptr_size);
  1526. /* get total aligned size */
  1527. align_size = ((size + uintptr_size) & ~uintptr_size) + align;
  1528. /* allocate memory block from heap */
  1529. ptr = rt_malloc(align_size);
  1530. if (ptr != RT_NULL)
  1531. {
  1532. /* the allocated memory block is aligned */
  1533. if (((rt_ubase_t)ptr & (align - 1)) == 0)
  1534. {
  1535. align_ptr = (void *)((rt_ubase_t)ptr + align);
  1536. }
  1537. else
  1538. {
  1539. align_ptr = (void *)(((rt_ubase_t)ptr + (align - 1)) & ~(align - 1));
  1540. }
  1541. /* set the pointer before alignment pointer to the real pointer */
  1542. *((rt_ubase_t *)((rt_ubase_t)align_ptr - sizeof(void *))) = (rt_ubase_t)ptr;
  1543. ptr = align_ptr;
  1544. }
  1545. return ptr;
  1546. }
  1547. RTM_EXPORT(rt_malloc_align);
  1548. /**
  1549. * This function release the memory block, which is allocated by
  1550. * rt_malloc_align function and address is aligned.
  1551. *
  1552. * @param ptr is the memory block pointer.
  1553. */
  1554. rt_weak void rt_free_align(void *ptr)
  1555. {
  1556. void *real_ptr = RT_NULL;
  1557. /* NULL check */
  1558. if (ptr == RT_NULL) return;
  1559. real_ptr = (void *) * (rt_ubase_t *)((rt_ubase_t)ptr - sizeof(void *));
  1560. rt_free(real_ptr);
  1561. }
  1562. RTM_EXPORT(rt_free_align);
  1563. #endif /* RT_USING_HEAP */
  1564. #ifndef RT_USING_CPU_FFS
  1565. #ifdef RT_USING_TINY_FFS
  1566. const rt_uint8_t __lowest_bit_bitmap[] =
  1567. {
  1568. /* 0 - 7 */ 0, 1, 2, 27, 3, 24, 28, 32,
  1569. /* 8 - 15 */ 4, 17, 25, 31, 29, 12, 32, 14,
  1570. /* 16 - 23 */ 5, 8, 18, 32, 26, 23, 32, 16,
  1571. /* 24 - 31 */ 30, 11, 13, 7, 32, 22, 15, 10,
  1572. /* 32 - 36 */ 6, 21, 9, 20, 19
  1573. };
  1574. /**
  1575. * This function finds the first bit set (beginning with the least significant bit)
  1576. * in value and return the index of that bit.
  1577. *
  1578. * Bits are numbered starting at 1 (the least significant bit). A return value of
  1579. * zero from any of these functions means that the argument was zero.
  1580. *
  1581. * @return return the index of the first bit set. If value is 0, then this function
  1582. * shall return 0.
  1583. */
  1584. int __rt_ffs(int value)
  1585. {
  1586. return __lowest_bit_bitmap[(rt_uint32_t)(value & (value - 1) ^ value) % 37];
  1587. }
  1588. #else
  1589. const rt_uint8_t __lowest_bit_bitmap[] =
  1590. {
  1591. /* 00 */ 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1592. /* 10 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1593. /* 20 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1594. /* 30 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1595. /* 40 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1596. /* 50 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1597. /* 60 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1598. /* 70 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1599. /* 80 */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1600. /* 90 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1601. /* A0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1602. /* B0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1603. /* C0 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1604. /* D0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1605. /* E0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1606. /* F0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
  1607. };
  1608. /**
  1609. * This function finds the first bit set (beginning with the least significant bit)
  1610. * in value and return the index of that bit.
  1611. *
  1612. * Bits are numbered starting at 1 (the least significant bit). A return value of
  1613. * zero from any of these functions means that the argument was zero.
  1614. *
  1615. * @return Return the index of the first bit set. If value is 0, then this function
  1616. * shall return 0.
  1617. */
  1618. int __rt_ffs(int value)
  1619. {
  1620. if (value == 0) return 0;
  1621. if (value & 0xff)
  1622. return __lowest_bit_bitmap[value & 0xff] + 1;
  1623. if (value & 0xff00)
  1624. return __lowest_bit_bitmap[(value & 0xff00) >> 8] + 9;
  1625. if (value & 0xff0000)
  1626. return __lowest_bit_bitmap[(value & 0xff0000) >> 16] + 17;
  1627. return __lowest_bit_bitmap[(value & 0xff000000) >> 24] + 25;
  1628. }
  1629. #endif /* RT_USING_TINY_FFS */
  1630. #endif /* RT_USING_CPU_FFS */
  1631. #ifndef __on_rt_assert_hook
  1632. #define __on_rt_assert_hook(ex, func, line) __ON_HOOK_ARGS(rt_assert_hook, (ex, func, line))
  1633. #endif
  1634. #ifdef RT_DEBUG
  1635. /* RT_ASSERT(EX)'s hook */
  1636. void (*rt_assert_hook)(const char *ex, const char *func, rt_size_t line);
  1637. /**
  1638. * This function will set a hook function to RT_ASSERT(EX). It will run when the expression is false.
  1639. *
  1640. * @param hook is the hook function.
  1641. */
  1642. void rt_assert_set_hook(void (*hook)(const char *ex, const char *func, rt_size_t line))
  1643. {
  1644. rt_assert_hook = hook;
  1645. }
  1646. /**
  1647. * The RT_ASSERT function.
  1648. *
  1649. * @param ex_string is the assertion condition string.
  1650. *
  1651. * @param func is the function name when assertion.
  1652. *
  1653. * @param line is the file line number when assertion.
  1654. */
  1655. void rt_assert_handler(const char *ex_string, const char *func, rt_size_t line)
  1656. {
  1657. volatile char dummy = 0;
  1658. if (rt_assert_hook == RT_NULL)
  1659. {
  1660. #ifdef RT_USING_MODULE
  1661. if (dlmodule_self())
  1662. {
  1663. /* close assertion module */
  1664. dlmodule_exit(-1);
  1665. }
  1666. else
  1667. #endif /*RT_USING_MODULE*/
  1668. {
  1669. rt_kprintf("(%s) assertion failed at function:%s, line number:%d \n", ex_string, func, line);
  1670. while (dummy == 0);
  1671. }
  1672. }
  1673. else
  1674. {
  1675. rt_assert_hook(ex_string, func, line);
  1676. }
  1677. }
  1678. RTM_EXPORT(rt_assert_handler);
  1679. #endif /* RT_DEBUG */
  1680. /**@}*/