You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

memset_s.c 21 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #define SECUREC_INLINE_DO_MEMSET 1
  17. #include "securecutil.h"
  18. #ifndef SECUREC_MEMSET_WITH_PERFORMANCE
  19. #define SECUREC_MEMSET_WITH_PERFORMANCE 0
  20. #endif
  21. #define SECUREC_MEMSET_PARAM_OK(dest, destMax, count) (SECUREC_LIKELY((count) <= (destMax) && \
  22. (dest) != NULL && (destMax) <= SECUREC_MEM_MAX_LEN))
  23. #if SECUREC_WITH_PERFORMANCE_ADDONS || SECUREC_MEMSET_WITH_PERFORMANCE
  24. /*
  25. * Determine whether the address is 8-byte aligned, use static to increase performance
  26. * return 0 is aligned
  27. */
  28. static int SecIsAddrAligned8(const void *addr, const void *zeroAddr)
  29. {
  30. return (int)(((size_t)((const char*)addr - (const char*)zeroAddr)) & 7); /* use 7 to check aligned 8 */
  31. }
  32. /* use union to clear strict-aliasing warning */
  33. typedef union {
  34. SecStrBuf32 buf32;
  35. SecStrBuf31 buf31;
  36. SecStrBuf30 buf30;
  37. SecStrBuf29 buf29;
  38. SecStrBuf28 buf28;
  39. SecStrBuf27 buf27;
  40. SecStrBuf26 buf26;
  41. SecStrBuf25 buf25;
  42. SecStrBuf24 buf24;
  43. SecStrBuf23 buf23;
  44. SecStrBuf22 buf22;
  45. SecStrBuf21 buf21;
  46. SecStrBuf20 buf20;
  47. SecStrBuf19 buf19;
  48. SecStrBuf18 buf18;
  49. SecStrBuf17 buf17;
  50. SecStrBuf16 buf16;
  51. SecStrBuf15 buf15;
  52. SecStrBuf14 buf14;
  53. SecStrBuf13 buf13;
  54. SecStrBuf12 buf12;
  55. SecStrBuf11 buf11;
  56. SecStrBuf10 buf10;
  57. SecStrBuf9 buf9;
  58. SecStrBuf8 buf8;
  59. SecStrBuf7 buf7;
  60. SecStrBuf6 buf6;
  61. SecStrBuf5 buf5;
  62. SecStrBuf4 buf4;
  63. SecStrBuf3 buf3;
  64. SecStrBuf2 buf2;
  65. SecStrBuf1 buf1;
  66. } SecStrBuf32Union;
  67. /* C standard initializes the first member of the consortium. */
  68. static const SecStrBuf32 g_allZero = {{
  69. '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
  70. '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
  71. '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0',
  72. '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'
  73. }};
  74. static const SecStrBuf32 g_allFF = {{
  75. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  76. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  77. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  78. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
  79. }};
  80. static const SecStrBuf32Union *SecStrictAliasingCast(const SecStrBuf32 *buf)
  81. {
  82. return (const SecStrBuf32Union *)buf;
  83. }
  84. #ifndef SECUREC_MEMSET_THRESHOLD_SIZE
  85. #define SECUREC_MEMSET_THRESHOLD_SIZE 32UL
  86. #endif
  87. #define SECUREC_UNALIGNED_SET do { \
  88. char *pcDest = (char *)dest; \
  89. switch (count) { \
  90. case 32: \
  91. *(pcDest++) = (char)c; \
  92. /* fall-through */ /* FALLTHRU */ \
  93. case 31: \
  94. *(pcDest++) = (char)c; \
  95. /* fall-through */ /* FALLTHRU */ \
  96. case 30: \
  97. *(pcDest++) = (char)c; \
  98. /* fall-through */ /* FALLTHRU */ \
  99. case 29: \
  100. *(pcDest++) = (char)c; \
  101. /* fall-through */ /* FALLTHRU */ \
  102. case 28: \
  103. *(pcDest++) = (char)c; \
  104. /* fall-through */ /* FALLTHRU */ \
  105. case 27: \
  106. *(pcDest++) = (char)c; \
  107. /* fall-through */ /* FALLTHRU */ \
  108. case 26: \
  109. *(pcDest++) = (char)c; \
  110. /* fall-through */ /* FALLTHRU */ \
  111. case 25: \
  112. *(pcDest++) = (char)c; \
  113. /* fall-through */ /* FALLTHRU */ \
  114. case 24: \
  115. *(pcDest++) = (char)c; \
  116. /* fall-through */ /* FALLTHRU */ \
  117. case 23: \
  118. *(pcDest++) = (char)c; \
  119. /* fall-through */ /* FALLTHRU */ \
  120. case 22: \
  121. *(pcDest++) = (char)c; \
  122. /* fall-through */ /* FALLTHRU */ \
  123. case 21: \
  124. *(pcDest++) = (char)c; \
  125. /* fall-through */ /* FALLTHRU */ \
  126. case 20: \
  127. *(pcDest++) = (char)c; \
  128. /* fall-through */ /* FALLTHRU */ \
  129. case 19: \
  130. *(pcDest++) = (char)c; \
  131. /* fall-through */ /* FALLTHRU */ \
  132. case 18: \
  133. *(pcDest++) = (char)c; \
  134. /* fall-through */ /* FALLTHRU */ \
  135. case 17: \
  136. *(pcDest++) = (char)c; \
  137. /* fall-through */ /* FALLTHRU */ \
  138. case 16: \
  139. *(pcDest++) = (char)c; \
  140. /* fall-through */ /* FALLTHRU */ \
  141. case 15: \
  142. *(pcDest++) = (char)c; \
  143. /* fall-through */ /* FALLTHRU */ \
  144. case 14: \
  145. *(pcDest++) = (char)c; \
  146. /* fall-through */ /* FALLTHRU */ \
  147. case 13: \
  148. *(pcDest++) = (char)c; \
  149. /* fall-through */ /* FALLTHRU */ \
  150. case 12: \
  151. *(pcDest++) = (char)c; \
  152. /* fall-through */ /* FALLTHRU */ \
  153. case 11: \
  154. *(pcDest++) = (char)c; \
  155. /* fall-through */ /* FALLTHRU */ \
  156. case 10: \
  157. *(pcDest++) = (char)c; \
  158. /* fall-through */ /* FALLTHRU */ \
  159. case 9: \
  160. *(pcDest++) = (char)c; \
  161. /* fall-through */ /* FALLTHRU */ \
  162. case 8: \
  163. *(pcDest++) = (char)c; \
  164. /* fall-through */ /* FALLTHRU */ \
  165. case 7: \
  166. *(pcDest++) = (char)c; \
  167. /* fall-through */ /* FALLTHRU */ \
  168. case 6: \
  169. *(pcDest++) = (char)c; \
  170. /* fall-through */ /* FALLTHRU */ \
  171. case 5: \
  172. *(pcDest++) = (char)c; \
  173. /* fall-through */ /* FALLTHRU */ \
  174. case 4: \
  175. *(pcDest++) = (char)c; \
  176. /* fall-through */ /* FALLTHRU */ \
  177. case 3: \
  178. *(pcDest++) = (char)c; \
  179. /* fall-through */ /* FALLTHRU */ \
  180. case 2: \
  181. *(pcDest++) = (char)c; \
  182. /* fall-through */ /* FALLTHRU */ \
  183. case 1: \
  184. *(pcDest++) = (char)c; \
  185. /* fall-through */ /* FALLTHRU */ \
  186. default: \
  187. break; \
  188. } \
  189. } SECUREC_WHILE_ZERO
  190. #define SECUREC_ALIGNED_SET_OPT_ZERO_FF do { \
  191. switch (c) { \
  192. case 0: \
  193. switch (count) { \
  194. case 1: \
  195. *(SecStrBuf1 *)dest = *(const SecStrBuf1 *)(&((SecStrictAliasingCast(&g_allZero))->buf1)); \
  196. break; \
  197. case 2: \
  198. *(SecStrBuf2 *)dest = *(const SecStrBuf2 *)(&((SecStrictAliasingCast(&g_allZero))->buf2)); \
  199. break; \
  200. case 3: \
  201. *(SecStrBuf3 *)dest = *(const SecStrBuf3 *)(&((SecStrictAliasingCast(&g_allZero))->buf3)); \
  202. break; \
  203. case 4: \
  204. *(SecStrBuf4 *)dest = *(const SecStrBuf4 *)(&((SecStrictAliasingCast(&g_allZero))->buf4)); \
  205. break; \
  206. case 5: \
  207. *(SecStrBuf5 *)dest = *(const SecStrBuf5 *)(&((SecStrictAliasingCast(&g_allZero))->buf5)); \
  208. break; \
  209. case 6: \
  210. *(SecStrBuf6 *)dest = *(const SecStrBuf6 *)(&((SecStrictAliasingCast(&g_allZero))->buf6)); \
  211. break; \
  212. case 7: \
  213. *(SecStrBuf7 *)dest = *(const SecStrBuf7 *)(&((SecStrictAliasingCast(&g_allZero))->buf7)); \
  214. break; \
  215. case 8: \
  216. *(SecStrBuf8 *)dest = *(const SecStrBuf8 *)(&((SecStrictAliasingCast(&g_allZero))->buf8)); \
  217. break; \
  218. case 9: \
  219. *(SecStrBuf9 *)dest = *(const SecStrBuf9 *)(&((SecStrictAliasingCast(&g_allZero))->buf9)); \
  220. break; \
  221. case 10: \
  222. *(SecStrBuf10 *)dest = *(const SecStrBuf10 *)(&((SecStrictAliasingCast(&g_allZero))->buf10)); \
  223. break; \
  224. case 11: \
  225. *(SecStrBuf11 *)dest = *(const SecStrBuf11 *)(&((SecStrictAliasingCast(&g_allZero))->buf11)); \
  226. break; \
  227. case 12: \
  228. *(SecStrBuf12 *)dest = *(const SecStrBuf12 *)(&((SecStrictAliasingCast(&g_allZero))->buf12)); \
  229. break; \
  230. case 13: \
  231. *(SecStrBuf13 *)dest = *(const SecStrBuf13 *)(&((SecStrictAliasingCast(&g_allZero))->buf13)); \
  232. break; \
  233. case 14: \
  234. *(SecStrBuf14 *)dest = *(const SecStrBuf14 *)(&((SecStrictAliasingCast(&g_allZero))->buf14)); \
  235. break; \
  236. case 15: \
  237. *(SecStrBuf15 *)dest = *(const SecStrBuf15 *)(&((SecStrictAliasingCast(&g_allZero))->buf15)); \
  238. break; \
  239. case 16: \
  240. *(SecStrBuf16 *)dest = *(const SecStrBuf16 *)(&((SecStrictAliasingCast(&g_allZero))->buf16)); \
  241. break; \
  242. case 17: \
  243. *(SecStrBuf17 *)dest = *(const SecStrBuf17 *)(&((SecStrictAliasingCast(&g_allZero))->buf17)); \
  244. break; \
  245. case 18: \
  246. *(SecStrBuf18 *)dest = *(const SecStrBuf18 *)(&((SecStrictAliasingCast(&g_allZero))->buf18)); \
  247. break; \
  248. case 19: \
  249. *(SecStrBuf19 *)dest = *(const SecStrBuf19 *)(&((SecStrictAliasingCast(&g_allZero))->buf19)); \
  250. break; \
  251. case 20: \
  252. *(SecStrBuf20 *)dest = *(const SecStrBuf20 *)(&((SecStrictAliasingCast(&g_allZero))->buf20)); \
  253. break; \
  254. case 21: \
  255. *(SecStrBuf21 *)dest = *(const SecStrBuf21 *)(&((SecStrictAliasingCast(&g_allZero))->buf21)); \
  256. break; \
  257. case 22: \
  258. *(SecStrBuf22 *)dest = *(const SecStrBuf22 *)(&((SecStrictAliasingCast(&g_allZero))->buf22)); \
  259. break; \
  260. case 23: \
  261. *(SecStrBuf23 *)dest = *(const SecStrBuf23 *)(&((SecStrictAliasingCast(&g_allZero))->buf23)); \
  262. break; \
  263. case 24: \
  264. *(SecStrBuf24 *)dest = *(const SecStrBuf24 *)(&((SecStrictAliasingCast(&g_allZero))->buf24)); \
  265. break; \
  266. case 25: \
  267. *(SecStrBuf25 *)dest = *(const SecStrBuf25 *)(&((SecStrictAliasingCast(&g_allZero))->buf25)); \
  268. break; \
  269. case 26: \
  270. *(SecStrBuf26 *)dest = *(const SecStrBuf26 *)(&((SecStrictAliasingCast(&g_allZero))->buf26)); \
  271. break; \
  272. case 27: \
  273. *(SecStrBuf27 *)dest = *(const SecStrBuf27 *)(&((SecStrictAliasingCast(&g_allZero))->buf27)); \
  274. break; \
  275. case 28: \
  276. *(SecStrBuf28 *)dest = *(const SecStrBuf28 *)(&((SecStrictAliasingCast(&g_allZero))->buf28)); \
  277. break; \
  278. case 29: \
  279. *(SecStrBuf29 *)dest = *(const SecStrBuf29 *)(&((SecStrictAliasingCast(&g_allZero))->buf29)); \
  280. break; \
  281. case 30: \
  282. *(SecStrBuf30 *)dest = *(const SecStrBuf30 *)(&((SecStrictAliasingCast(&g_allZero))->buf30)); \
  283. break; \
  284. case 31: \
  285. *(SecStrBuf31 *)dest = *(const SecStrBuf31 *)(&((SecStrictAliasingCast(&g_allZero))->buf31)); \
  286. break; \
  287. case 32: \
  288. *(SecStrBuf32 *)dest = *(const SecStrBuf32 *)(&((SecStrictAliasingCast(&g_allZero))->buf32)); \
  289. break; \
  290. default: \
  291. break; \
  292. } \
  293. break; \
  294. case 0xFF: \
  295. switch (count) { \
  296. case 1: \
  297. *(SecStrBuf1 *)dest = *(const SecStrBuf1 *)(&((SecStrictAliasingCast(&g_allFF))->buf1)); \
  298. break; \
  299. case 2: \
  300. *(SecStrBuf2 *)dest = *(const SecStrBuf2 *)(&((SecStrictAliasingCast(&g_allFF))->buf2)); \
  301. break; \
  302. case 3: \
  303. *(SecStrBuf3 *)dest = *(const SecStrBuf3 *)(&((SecStrictAliasingCast(&g_allFF))->buf3)); \
  304. break; \
  305. case 4: \
  306. *(SecStrBuf4 *)dest = *(const SecStrBuf4 *)(&((SecStrictAliasingCast(&g_allFF))->buf4)); \
  307. break; \
  308. case 5: \
  309. *(SecStrBuf5 *)dest = *(const SecStrBuf5 *)(&((SecStrictAliasingCast(&g_allFF))->buf5)); \
  310. break; \
  311. case 6: \
  312. *(SecStrBuf6 *)dest = *(const SecStrBuf6 *)(&((SecStrictAliasingCast(&g_allFF))->buf6)); \
  313. break; \
  314. case 7: \
  315. *(SecStrBuf7 *)dest = *(const SecStrBuf7 *)(&((SecStrictAliasingCast(&g_allFF))->buf7)); \
  316. break; \
  317. case 8: \
  318. *(SecStrBuf8 *)dest = *(const SecStrBuf8 *)(&((SecStrictAliasingCast(&g_allFF))->buf8)); \
  319. break; \
  320. case 9: \
  321. *(SecStrBuf9 *)dest = *(const SecStrBuf9 *)(&((SecStrictAliasingCast(&g_allFF))->buf9)); \
  322. break; \
  323. case 10: \
  324. *(SecStrBuf10 *)dest = *(const SecStrBuf10 *)(&((SecStrictAliasingCast(&g_allFF))->buf10)); \
  325. break; \
  326. case 11: \
  327. *(SecStrBuf11 *)dest = *(const SecStrBuf11 *)(&((SecStrictAliasingCast(&g_allFF))->buf11)); \
  328. break; \
  329. case 12: \
  330. *(SecStrBuf12 *)dest = *(const SecStrBuf12 *)(&((SecStrictAliasingCast(&g_allFF))->buf12)); \
  331. break; \
  332. case 13: \
  333. *(SecStrBuf13 *)dest = *(const SecStrBuf13 *)(&((SecStrictAliasingCast(&g_allFF))->buf13)); \
  334. break; \
  335. case 14: \
  336. *(SecStrBuf14 *)dest = *(const SecStrBuf14 *)(&((SecStrictAliasingCast(&g_allFF))->buf14)); \
  337. break; \
  338. case 15: \
  339. *(SecStrBuf15 *)dest = *(const SecStrBuf15 *)(&((SecStrictAliasingCast(&g_allFF))->buf15)); \
  340. break; \
  341. case 16: \
  342. *(SecStrBuf16 *)dest = *(const SecStrBuf16 *)(&((SecStrictAliasingCast(&g_allFF))->buf16)); \
  343. break; \
  344. case 17: \
  345. *(SecStrBuf17 *)dest = *(const SecStrBuf17 *)(&((SecStrictAliasingCast(&g_allFF))->buf17)); \
  346. break; \
  347. case 18: \
  348. *(SecStrBuf18 *)dest = *(const SecStrBuf18 *)(&((SecStrictAliasingCast(&g_allFF))->buf18)); \
  349. break; \
  350. case 19: \
  351. *(SecStrBuf19 *)dest = *(const SecStrBuf19 *)(&((SecStrictAliasingCast(&g_allFF))->buf19)); \
  352. break; \
  353. case 20: \
  354. *(SecStrBuf20 *)dest = *(const SecStrBuf20 *)(&((SecStrictAliasingCast(&g_allFF))->buf20)); \
  355. break; \
  356. case 21: \
  357. *(SecStrBuf21 *)dest = *(const SecStrBuf21 *)(&((SecStrictAliasingCast(&g_allFF))->buf21)); \
  358. break; \
  359. case 22: \
  360. *(SecStrBuf22 *)dest = *(const SecStrBuf22 *)(&((SecStrictAliasingCast(&g_allFF))->buf22)); \
  361. break; \
  362. case 23: \
  363. *(SecStrBuf23 *)dest = *(const SecStrBuf23 *)(&((SecStrictAliasingCast(&g_allFF))->buf23)); \
  364. break; \
  365. case 24: \
  366. *(SecStrBuf24 *)dest = *(const SecStrBuf24 *)(&((SecStrictAliasingCast(&g_allFF))->buf24)); \
  367. break; \
  368. case 25: \
  369. *(SecStrBuf25 *)dest = *(const SecStrBuf25 *)(&((SecStrictAliasingCast(&g_allFF))->buf25)); \
  370. break; \
  371. case 26: \
  372. *(SecStrBuf26 *)dest = *(const SecStrBuf26 *)(&((SecStrictAliasingCast(&g_allFF))->buf26)); \
  373. break; \
  374. case 27: \
  375. *(SecStrBuf27 *)dest = *(const SecStrBuf27 *)(&((SecStrictAliasingCast(&g_allFF))->buf27)); \
  376. break; \
  377. case 28: \
  378. *(SecStrBuf28 *)dest = *(const SecStrBuf28 *)(&((SecStrictAliasingCast(&g_allFF))->buf28)); \
  379. break; \
  380. case 29: \
  381. *(SecStrBuf29 *)dest = *(const SecStrBuf29 *)(&((SecStrictAliasingCast(&g_allFF))->buf29)); \
  382. break; \
  383. case 30: \
  384. *(SecStrBuf30 *)dest = *(const SecStrBuf30 *)(&((SecStrictAliasingCast(&g_allFF))->buf30)); \
  385. break; \
  386. case 31: \
  387. *(SecStrBuf31 *)dest = *(const SecStrBuf31 *)(&((SecStrictAliasingCast(&g_allFF))->buf31)); \
  388. break; \
  389. case 32: \
  390. *(SecStrBuf32 *)dest = *(const SecStrBuf32 *)(&((SecStrictAliasingCast(&g_allFF))->buf32)); \
  391. break; \
  392. default: \
  393. break; \
  394. } \
  395. break; \
  396. default: \
  397. SECUREC_UNALIGNED_SET; \
  398. } /* END switch */ \
  399. } SECUREC_WHILE_ZERO
  400. #endif
  401. /*
  402. * Handling errors
  403. */
  404. static errno_t SecMemsetError(void *dest, size_t destMax, int c, size_t count)
  405. {
  406. if (destMax == 0 || destMax > SECUREC_MEM_MAX_LEN) {
  407. SECUREC_ERROR_INVALID_RANGE("memset_s");
  408. return ERANGE;
  409. }
  410. if (dest == NULL) {
  411. SECUREC_ERROR_INVALID_PARAMTER("memset_s");
  412. return EINVAL;
  413. }
  414. if (count > destMax) {
  415. (void)memset(dest, c, destMax); /* set entire buffer to value c */
  416. SECUREC_ERROR_INVALID_RANGE("memset_s");
  417. return ERANGE_AND_RESET;
  418. }
  419. return EOK;
  420. }
  421. #if SECUREC_WITH_PERFORMANCE_ADDONS || SECUREC_MEMSET_WITH_PERFORMANCE
  422. /*
  423. * Performance optimization
  424. */
  425. static void SecDoMemsetOpt(void *dest, int c, size_t count)
  426. {
  427. if (count > SECUREC_MEMSET_THRESHOLD_SIZE) {
  428. SecDoMemset(dest, c, count);
  429. } else {
  430. if (SECUREC_ADDR_ALIGNED_8(dest)) {
  431. /* use struct assignment */
  432. SECUREC_ALIGNED_SET_OPT_ZERO_FF;
  433. } else {
  434. SECUREC_UNALIGNED_SET;
  435. }
  436. }
  437. return;
  438. }
  439. #endif
  440. /*
  441. * <FUNCTION DESCRIPTION>
  442. * The memset_s function copies the value of c (converted to an unsigned char)
  443. * into each of the first count characters of the object pointed to by dest.
  444. *
  445. * <INPUT PARAMETERS>
  446. * dest Pointer to destination.
  447. * destMax The size of the buffer.
  448. * c Character to set.
  449. * count Number of characters.
  450. *
  451. * <OUTPUT PARAMETERS>
  452. * dest buffer is uptdated.
  453. *
  454. * <RETURN VALUE>
  455. * EOK Success
  456. * EINVAL dest == NULL and destMax != 0 and destMax <= SECUREC_MEM_MAX_LEN
  457. * ERANGE destMax is 0 or destMax > SECUREC_MEM_MAX_LEN
  458. * ERANGE_AND_RESET count > destMax and destMax != 0 and destMax <= SECUREC_MEM_MAX_LEN and dest != NULL
  459. *
  460. * if return ERANGE_AND_RESET then fill dest to c ,fill length is destMax
  461. */
  462. errno_t memset_s(void *dest, size_t destMax, int c, size_t count)
  463. {
  464. if (SECUREC_MEMSET_PARAM_OK(dest, destMax, count)) {
  465. #if SECUREC_MEMSET_WITH_PERFORMANCE
  466. SecDoMemsetOpt(dest, c, count);
  467. #else
  468. SecDoMemset(dest, c, count);
  469. #endif
  470. return EOK;
  471. } else {
  472. /* meet some runtime violation, return error code */
  473. return SecMemsetError(dest, destMax, c, count);
  474. }
  475. }
  476. #if SECUREC_IN_KERNEL
  477. EXPORT_SYMBOL(memset_s);
  478. #endif
  479. #if SECUREC_WITH_PERFORMANCE_ADDONS
  480. /*
  481. * Performance optimization
  482. */
  483. errno_t memset_sOptAsm(void *dest, size_t destMax, int c, size_t count)
  484. {
  485. if (SECUREC_MEMSET_PARAM_OK(dest, destMax, count)) {
  486. SecDoMemsetOpt(dest, c, count);
  487. return EOK;
  488. }
  489. /* meet some runtime violation, return error code */
  490. return SecMemsetError(dest, destMax, c, count);
  491. }
  492. /*
  493. * Performance optimization
  494. */
  495. errno_t memset_sOptTc(void *dest, size_t destMax, int c, size_t count)
  496. {
  497. if (SECUREC_LIKELY(count <= destMax && dest != NULL)) {
  498. SecDoMemsetOpt(dest, c, count);
  499. return EOK;
  500. }
  501. /* meet some runtime violation, return error code */
  502. return SecMemsetError(dest, destMax, c, count);
  503. }
  504. #endif

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示