kservice.c 48 KB

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