kservice.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848
  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. rt_device_open(new_device, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM);
  1106. _console_device = new_device;
  1107. }
  1108. return old_device;
  1109. }
  1110. RTM_EXPORT(rt_console_set_device);
  1111. #endif /* RT_USING_DEVICE */
  1112. RT_WEAK void rt_hw_console_output(const char *str)
  1113. {
  1114. /* empty console output */
  1115. }
  1116. RTM_EXPORT(rt_hw_console_output);
  1117. /**
  1118. * This function will put string to the console.
  1119. *
  1120. * @param str is the string output to the console.
  1121. */
  1122. void rt_kputs(const char *str)
  1123. {
  1124. if (!str) return;
  1125. #ifdef RT_USING_DEVICE
  1126. if (_console_device == RT_NULL)
  1127. {
  1128. rt_hw_console_output(str);
  1129. }
  1130. else
  1131. {
  1132. rt_device_write(_console_device, 0, str, rt_strlen(str));
  1133. }
  1134. #else
  1135. rt_hw_console_output(str);
  1136. #endif /* RT_USING_DEVICE */
  1137. }
  1138. /**
  1139. * This function will print a formatted string on system console.
  1140. *
  1141. * @param fmt is the format parameters.
  1142. *
  1143. * @return The number of characters actually written to buffer.
  1144. */
  1145. RT_WEAK int rt_kprintf(const char *fmt, ...)
  1146. {
  1147. va_list args;
  1148. rt_size_t length;
  1149. static char rt_log_buf[RT_CONSOLEBUF_SIZE];
  1150. va_start(args, fmt);
  1151. /* the return value of vsnprintf is the number of bytes that would be
  1152. * written to buffer had if the size of the buffer been sufficiently
  1153. * large excluding the terminating null byte. If the output string
  1154. * would be larger than the rt_log_buf, we have to adjust the output
  1155. * length. */
  1156. length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
  1157. if (length > RT_CONSOLEBUF_SIZE - 1)
  1158. length = RT_CONSOLEBUF_SIZE - 1;
  1159. #ifdef RT_USING_DEVICE
  1160. if (_console_device == RT_NULL)
  1161. {
  1162. rt_hw_console_output(rt_log_buf);
  1163. }
  1164. else
  1165. {
  1166. rt_device_write(_console_device, 0, rt_log_buf, length);
  1167. }
  1168. #else
  1169. rt_hw_console_output(rt_log_buf);
  1170. #endif /* RT_USING_DEVICE */
  1171. va_end(args);
  1172. return length;
  1173. }
  1174. RTM_EXPORT(rt_kprintf);
  1175. #endif /* RT_USING_CONSOLE */
  1176. #if defined(RT_USING_HEAP) && !defined(RT_USING_USERHEAP)
  1177. #ifdef RT_USING_HOOK
  1178. static void (*rt_malloc_hook)(void *ptr, rt_size_t size);
  1179. static void (*rt_free_hook)(void *ptr);
  1180. /**
  1181. * @addtogroup Hook
  1182. */
  1183. /**@{*/
  1184. /**
  1185. * @brief This function will set a hook function, which will be invoked when a memory
  1186. * block is allocated from heap memory.
  1187. *
  1188. * @param hook the hook function.
  1189. */
  1190. void rt_malloc_sethook(void (*hook)(void *ptr, rt_size_t size))
  1191. {
  1192. rt_malloc_hook = hook;
  1193. }
  1194. /**
  1195. * @brief This function will set a hook function, which will be invoked when a memory
  1196. * block is released to heap memory.
  1197. *
  1198. * @param hook the hook function
  1199. */
  1200. void rt_free_sethook(void (*hook)(void *ptr))
  1201. {
  1202. rt_free_hook = hook;
  1203. }
  1204. /**@}*/
  1205. #endif /* RT_USING_HOOK */
  1206. #if defined(RT_USING_HEAP_ISR)
  1207. #elif defined(RT_USING_MUTEX)
  1208. static struct rt_mutex _lock;
  1209. #endif
  1210. rt_inline void _heap_lock_init(void)
  1211. {
  1212. #if defined(RT_USING_HEAP_ISR)
  1213. #elif defined(RT_USING_MUTEX)
  1214. rt_mutex_init(&_lock, "heap", RT_IPC_FLAG_PRIO);
  1215. #endif
  1216. }
  1217. rt_inline rt_base_t _heap_lock(void)
  1218. {
  1219. #if defined(RT_USING_HEAP_ISR)
  1220. return rt_hw_interrupt_disable();
  1221. #elif defined(RT_USING_MUTEX)
  1222. if (rt_thread_self())
  1223. return rt_mutex_take(&_lock, RT_WAITING_FOREVER);
  1224. else
  1225. return RT_EOK;
  1226. #else
  1227. rt_enter_critical();
  1228. return RT_EOK;
  1229. #endif
  1230. }
  1231. rt_inline void _heap_unlock(rt_base_t level)
  1232. {
  1233. #if defined(RT_USING_HEAP_ISR)
  1234. rt_hw_interrupt_enable(level);
  1235. #elif defined(RT_USING_MUTEX)
  1236. RT_ASSERT(level == RT_EOK);
  1237. if (rt_thread_self())
  1238. rt_mutex_release(&_lock);
  1239. #else
  1240. rt_exit_critical();
  1241. #endif
  1242. }
  1243. #if defined(RT_USING_SMALL_MEM_AS_HEAP)
  1244. static rt_smem_t system_heap;
  1245. rt_inline void _smem_info(rt_size_t *total,
  1246. rt_size_t *used, rt_size_t *max_used)
  1247. {
  1248. if (total)
  1249. *total = system_heap->total;
  1250. if (used)
  1251. *used = system_heap->used;
  1252. if (max_used)
  1253. *max_used = system_heap->max;
  1254. }
  1255. #define _MEM_INIT(_name, _start, _size) \
  1256. system_heap = rt_smem_init(_name, _start, _size)
  1257. #define _MEM_MALLOC(_size) \
  1258. rt_smem_alloc(system_heap, _size)
  1259. #define _MEM_REALLOC(_ptr, _newsize)\
  1260. rt_smem_realloc(system_heap, _ptr, _newsize)
  1261. #define _MEM_FREE(_ptr) \
  1262. rt_smem_free(_ptr)
  1263. #define _MEM_INFO(_total, _used, _max) \
  1264. _smem_info(_total, _used, _max)
  1265. #elif defined(RT_USING_MEMHEAP_AS_HEAP)
  1266. static struct rt_memheap system_heap;
  1267. void *_memheap_alloc(struct rt_memheap *heap, rt_size_t size);
  1268. void _memheap_free(void *rmem);
  1269. void *_memheap_realloc(struct rt_memheap *heap, void *rmem, rt_size_t newsize);
  1270. #define _MEM_INIT(_name, _start, _size) \
  1271. rt_memheap_init(&system_heap, _name, _start, _size)
  1272. #define _MEM_MALLOC(_size) \
  1273. _memheap_alloc(&system_heap, _size)
  1274. #define _MEM_REALLOC(_ptr, _newsize) \
  1275. _memheap_realloc(&system_heap, _ptr, _newsize)
  1276. #define _MEM_FREE(_ptr) \
  1277. _memheap_free(_ptr)
  1278. #define _MEM_INFO(_total, _used, _max) \
  1279. rt_memheap_info(&system_heap, _total, _used, _max)
  1280. #elif defined(RT_USING_SLAB_AS_HEAP)
  1281. static rt_slab_t system_heap;
  1282. rt_inline void _slab_info(rt_size_t *total,
  1283. rt_size_t *used, rt_size_t *max_used)
  1284. {
  1285. if (total)
  1286. *total = system_heap->total;
  1287. if (used)
  1288. *used = system_heap->used;
  1289. if (max_used)
  1290. *max_used = system_heap->max;
  1291. }
  1292. #define _MEM_INIT(_name, _start, _size) \
  1293. system_heap = rt_slab_init(_name, _start, _size)
  1294. #define _MEM_MALLOC(_size) \
  1295. rt_slab_alloc(system_heap, _size)
  1296. #define _MEM_REALLOC(_ptr, _newsize) \
  1297. rt_slab_realloc(system_heap, _ptr, _newsize)
  1298. #define _MEM_FREE(_ptr) \
  1299. rt_slab_free(system_heap, _ptr)
  1300. #define _MEM_INFO _slab_info
  1301. #else
  1302. #define _MEM_INIT(...)
  1303. #define _MEM_MALLOC(...) RT_NULL
  1304. #define _MEM_REALLOC(...) RT_NULL
  1305. #define _MEM_FREE(...)
  1306. #define _MEM_INFO(...)
  1307. #endif
  1308. /**
  1309. * @brief This function will init system heap.
  1310. *
  1311. * @param begin_addr the beginning address of system page.
  1312. *
  1313. * @param end_addr the end address of system page.
  1314. */
  1315. RT_WEAK void rt_system_heap_init(void *begin_addr, void *end_addr)
  1316. {
  1317. rt_ubase_t begin_align = RT_ALIGN((rt_ubase_t)begin_addr, RT_ALIGN_SIZE);
  1318. rt_ubase_t end_align = RT_ALIGN_DOWN((rt_ubase_t)end_addr, RT_ALIGN_SIZE);
  1319. RT_ASSERT(end_align > begin_align);
  1320. /* Initialize system memory heap */
  1321. _MEM_INIT("heap", begin_addr, end_align - begin_align);
  1322. /* Initialize multi thread contention lock */
  1323. _heap_lock_init();
  1324. }
  1325. /**
  1326. * @brief Allocate a block of memory with a minimum of 'size' bytes.
  1327. *
  1328. * @param size is the minimum size of the requested block in bytes.
  1329. *
  1330. * @return the pointer to allocated memory or NULL if no free memory was found.
  1331. */
  1332. RT_WEAK void *rt_malloc(rt_size_t size)
  1333. {
  1334. rt_base_t level;
  1335. void *ptr;
  1336. /* Enter critical zone */
  1337. level = _heap_lock();
  1338. /* allocate memory block from system heap */
  1339. ptr = _MEM_MALLOC(size);
  1340. /* Exit critical zone */
  1341. _heap_unlock(level);
  1342. /* call 'rt_malloc' hook */
  1343. RT_OBJECT_HOOK_CALL(rt_malloc_hook, (ptr, size));
  1344. return ptr;
  1345. }
  1346. RTM_EXPORT(rt_malloc);
  1347. /**
  1348. * @brief This function will change the size of previously allocated memory block.
  1349. *
  1350. * @param rmem is the pointer to memory allocated by rt_malloc.
  1351. *
  1352. * @param newsize is the required new size.
  1353. *
  1354. * @return the changed memory block address.
  1355. */
  1356. RT_WEAK void *rt_realloc(void *rmem, rt_size_t newsize)
  1357. {
  1358. rt_base_t level;
  1359. void *nptr;
  1360. /* Enter critical zone */
  1361. level = _heap_lock();
  1362. /* Change the size of previously allocated memory block */
  1363. nptr = _MEM_REALLOC(rmem, newsize);
  1364. /* Exit critical zone */
  1365. _heap_unlock(level);
  1366. return nptr;
  1367. }
  1368. RTM_EXPORT(rt_realloc);
  1369. /**
  1370. * @brief This function will contiguously allocate enough space for count objects
  1371. * that are size bytes of memory each and returns a pointer to the allocated
  1372. * memory.
  1373. *
  1374. * @note The allocated memory is filled with bytes of value zero.
  1375. *
  1376. * @param count is the number of objects to allocate.
  1377. *
  1378. * @param size is the size of one object to allocate.
  1379. *
  1380. * @return pointer to allocated memory / NULL pointer if there is an error.
  1381. */
  1382. RT_WEAK void *rt_calloc(rt_size_t count, rt_size_t size)
  1383. {
  1384. void *p;
  1385. /* allocate 'count' objects of size 'size' */
  1386. p = rt_malloc(count * size);
  1387. /* zero the memory */
  1388. if (p)
  1389. {
  1390. rt_memset(p, 0, count * size);
  1391. }
  1392. return p;
  1393. }
  1394. RTM_EXPORT(rt_calloc);
  1395. /**
  1396. * @brief This function will release the previously allocated memory block by
  1397. * rt_malloc. The released memory block is taken back to system heap.
  1398. *
  1399. * @param rmem the address of memory which will be released.
  1400. */
  1401. RT_WEAK void rt_free(void *rmem)
  1402. {
  1403. rt_base_t level;
  1404. /* call 'rt_free' hook */
  1405. RT_OBJECT_HOOK_CALL(rt_free_hook, (rmem));
  1406. /* NULL check */
  1407. if (rmem == RT_NULL) return;
  1408. /* Enter critical zone */
  1409. level = _heap_lock();
  1410. _MEM_FREE(rmem);
  1411. /* Exit critical zone */
  1412. _heap_unlock(level);
  1413. }
  1414. RTM_EXPORT(rt_free);
  1415. /**
  1416. * @brief This function will caculate the total memory, the used memory, and
  1417. * the max used memory.
  1418. *
  1419. * @param total is a pointer to get the total size of the memory.
  1420. *
  1421. * @param used is a pointer to get the size of memory used.
  1422. *
  1423. * @param max_used is a pointer to get the maximum memory used.
  1424. */
  1425. RT_WEAK void rt_memory_info(rt_size_t *total,
  1426. rt_size_t *used,
  1427. rt_size_t *max_used)
  1428. {
  1429. rt_base_t level;
  1430. /* Enter critical zone */
  1431. level = _heap_lock();
  1432. _MEM_INFO(total, used, max_used);
  1433. /* Exit critical zone */
  1434. _heap_unlock(level);
  1435. }
  1436. RTM_EXPORT(rt_memory_info);
  1437. #if defined(RT_USING_SLAB) && defined(RT_USING_SLAB_AS_HEAP)
  1438. void *rt_page_alloc(rt_size_t npages)
  1439. {
  1440. rt_base_t level;
  1441. void *ptr;
  1442. /* Enter critical zone */
  1443. level = _heap_lock();
  1444. /* alloc page */
  1445. ptr = rt_slab_page_alloc(system_heap, npages);
  1446. /* Exit critical zone */
  1447. _heap_unlock(level);
  1448. return ptr;
  1449. }
  1450. void rt_page_free(void *addr, rt_size_t npages)
  1451. {
  1452. rt_base_t level;
  1453. /* Enter critical zone */
  1454. level = _heap_lock();
  1455. /* free page */
  1456. rt_slab_page_free(system_heap, addr, npages);
  1457. /* Exit critical zone */
  1458. _heap_unlock(level);
  1459. }
  1460. #endif
  1461. /**
  1462. * This function allocates a memory block, which address is aligned to the
  1463. * specified alignment size.
  1464. *
  1465. * @param size is the allocated memory block size.
  1466. *
  1467. * @param align is the alignment size.
  1468. *
  1469. * @return The memory block address was returned successfully, otherwise it was
  1470. * returned empty RT_NULL.
  1471. */
  1472. RT_WEAK void *rt_malloc_align(rt_size_t size, rt_size_t align)
  1473. {
  1474. void *ptr;
  1475. void *align_ptr;
  1476. int uintptr_size;
  1477. rt_size_t align_size;
  1478. /* sizeof pointer */
  1479. uintptr_size = sizeof(void*);
  1480. uintptr_size -= 1;
  1481. /* align the alignment size to uintptr size byte */
  1482. align = ((align + uintptr_size) & ~uintptr_size);
  1483. /* get total aligned size */
  1484. align_size = ((size + uintptr_size) & ~uintptr_size) + align;
  1485. /* allocate memory block from heap */
  1486. ptr = rt_malloc(align_size);
  1487. if (ptr != RT_NULL)
  1488. {
  1489. /* the allocated memory block is aligned */
  1490. if (((rt_ubase_t)ptr & (align - 1)) == 0)
  1491. {
  1492. align_ptr = (void *)((rt_ubase_t)ptr + align);
  1493. }
  1494. else
  1495. {
  1496. align_ptr = (void *)(((rt_ubase_t)ptr + (align - 1)) & ~(align - 1));
  1497. }
  1498. /* set the pointer before alignment pointer to the real pointer */
  1499. *((rt_ubase_t *)((rt_ubase_t)align_ptr - sizeof(void *))) = (rt_ubase_t)ptr;
  1500. ptr = align_ptr;
  1501. }
  1502. return ptr;
  1503. }
  1504. RTM_EXPORT(rt_malloc_align);
  1505. /**
  1506. * This function release the memory block, which is allocated by
  1507. * rt_malloc_align function and address is aligned.
  1508. *
  1509. * @param ptr is the memory block pointer.
  1510. */
  1511. RT_WEAK void rt_free_align(void *ptr)
  1512. {
  1513. void *real_ptr;
  1514. /* NULL check */
  1515. if (ptr == RT_NULL) return;
  1516. real_ptr = (void *) * (rt_ubase_t *)((rt_ubase_t)ptr - sizeof(void *));
  1517. rt_free(real_ptr);
  1518. }
  1519. RTM_EXPORT(rt_free_align);
  1520. #endif /* RT_USING_HEAP */
  1521. #ifndef RT_USING_CPU_FFS
  1522. #ifdef RT_USING_TINY_FFS
  1523. const rt_uint8_t __lowest_bit_bitmap[] =
  1524. {
  1525. /* 0 - 7 */ 0, 1, 2, 27, 3, 24, 28, 32,
  1526. /* 8 - 15 */ 4, 17, 25, 31, 29, 12, 32, 14,
  1527. /* 16 - 23 */ 5, 8, 18, 32, 26, 23, 32, 16,
  1528. /* 24 - 31 */ 30, 11, 13, 7, 32, 22, 15, 10,
  1529. /* 32 - 36 */ 6, 21, 9, 20, 19
  1530. };
  1531. /**
  1532. * This function finds the first bit set (beginning with the least significant bit)
  1533. * in value and return the index of that bit.
  1534. *
  1535. * Bits are numbered starting at 1 (the least significant bit). A return value of
  1536. * zero from any of these functions means that the argument was zero.
  1537. *
  1538. * @return return the index of the first bit set. If value is 0, then this function
  1539. * shall return 0.
  1540. */
  1541. int __rt_ffs(int value)
  1542. {
  1543. return __lowest_bit_bitmap[(rt_uint32_t)(value & (value - 1) ^ value) % 37];
  1544. }
  1545. #else
  1546. const rt_uint8_t __lowest_bit_bitmap[] =
  1547. {
  1548. /* 00 */ 0, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1549. /* 10 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1550. /* 20 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1551. /* 30 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1552. /* 40 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1553. /* 50 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1554. /* 60 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1555. /* 70 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1556. /* 80 */ 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1557. /* 90 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1558. /* A0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1559. /* B0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1560. /* C0 */ 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1561. /* D0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1562. /* E0 */ 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0,
  1563. /* F0 */ 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
  1564. };
  1565. /**
  1566. * This function finds the first bit set (beginning with the least significant bit)
  1567. * in value and return the index of that bit.
  1568. *
  1569. * Bits are numbered starting at 1 (the least significant bit). A return value of
  1570. * zero from any of these functions means that the argument was zero.
  1571. *
  1572. * @return Return the index of the first bit set. If value is 0, then this function
  1573. * shall return 0.
  1574. */
  1575. int __rt_ffs(int value)
  1576. {
  1577. if (value == 0) return 0;
  1578. if (value & 0xff)
  1579. return __lowest_bit_bitmap[value & 0xff] + 1;
  1580. if (value & 0xff00)
  1581. return __lowest_bit_bitmap[(value & 0xff00) >> 8] + 9;
  1582. if (value & 0xff0000)
  1583. return __lowest_bit_bitmap[(value & 0xff0000) >> 16] + 17;
  1584. return __lowest_bit_bitmap[(value & 0xff000000) >> 24] + 25;
  1585. }
  1586. #endif /* RT_USING_TINY_FFS */
  1587. #endif /* RT_USING_CPU_FFS */
  1588. #ifndef __on_rt_assert_hook
  1589. #define __on_rt_assert_hook(ex, func, line) __ON_HOOK_ARGS(rt_assert_hook, (ex, func, line))
  1590. #endif
  1591. #ifdef RT_DEBUG
  1592. /* RT_ASSERT(EX)'s hook */
  1593. void (*rt_assert_hook)(const char *ex, const char *func, rt_size_t line);
  1594. /**
  1595. * This function will set a hook function to RT_ASSERT(EX). It will run when the expression is false.
  1596. *
  1597. * @param hook is the hook function.
  1598. */
  1599. void rt_assert_set_hook(void (*hook)(const char *ex, const char *func, rt_size_t line))
  1600. {
  1601. rt_assert_hook = hook;
  1602. }
  1603. /**
  1604. * The RT_ASSERT function.
  1605. *
  1606. * @param ex_string is the assertion condition string.
  1607. *
  1608. * @param func is the function name when assertion.
  1609. *
  1610. * @param line is the file line number when assertion.
  1611. */
  1612. void rt_assert_handler(const char *ex_string, const char *func, rt_size_t line)
  1613. {
  1614. volatile char dummy = 0;
  1615. if (rt_assert_hook == RT_NULL)
  1616. {
  1617. #ifdef RT_USING_MODULE
  1618. if (dlmodule_self())
  1619. {
  1620. /* close assertion module */
  1621. dlmodule_exit(-1);
  1622. }
  1623. else
  1624. #endif /*RT_USING_MODULE*/
  1625. {
  1626. rt_kprintf("(%s) assertion failed at function:%s, line number:%d \n", ex_string, func, line);
  1627. while (dummy == 0);
  1628. }
  1629. }
  1630. else
  1631. {
  1632. rt_assert_hook(ex_string, func, line);
  1633. }
  1634. }
  1635. RTM_EXPORT(rt_assert_handler);
  1636. #endif /* RT_DEBUG */
  1637. /**@}*/