kservice.c 48 KB

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