kservice.c 46 KB

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