kservice.c 44 KB

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