kservice.c 44 KB

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