kservice.c 43 KB

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