kservice.c 45 KB

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