os_mem.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * Copyright (c) 2021, Meco Jianting Man <jiantingman@foxmail.com>
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-09-19 Meco Man first version
  9. */
  10. /*
  11. *********************************************************************************************************
  12. * uC/OS-II
  13. * The Real-Time Kernel
  14. *
  15. * Copyright 1992-2020 Silicon Laboratories Inc. www.silabs.com
  16. *
  17. * SPDX-License-Identifier: APACHE-2.0
  18. *
  19. * This software is subject to an open source license and is distributed by
  20. * Silicon Laboratories Inc. pursuant to the terms of the Apache License,
  21. * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
  22. *
  23. *********************************************************************************************************
  24. */
  25. /*
  26. *********************************************************************************************************
  27. *
  28. * MEMORY MANAGEMENT
  29. *
  30. * Filename : os_mem.c
  31. * Version : V2.93.00
  32. *********************************************************************************************************
  33. */
  34. #include "ucos_ii.h"
  35. #if (OS_MEM_EN > 0u) && (OS_MAX_MEM_PART > 0u)
  36. /*
  37. *********************************************************************************************************
  38. * CREATE A MEMORY PARTITION
  39. *
  40. * Description : Create a fixed-sized memory partition that will be managed by uC/OS-II.
  41. *
  42. * Arguments : addr is the starting address of the memory partition
  43. *
  44. * nblks is the number of memory blocks to create from the partition.
  45. *
  46. * blksize is the size (in bytes) of each block in the memory partition.
  47. *
  48. * perr is a pointer to a variable containing an error message which will be set by
  49. * this function to either:
  50. *
  51. * OS_ERR_NONE if the memory partition has been created correctly.
  52. * OS_ERR_ILLEGAL_CREATE_RUN_TIME if you tried to create a memory partition after
  53. * safety critical operation started.
  54. * OS_ERR_MEM_INVALID_ADDR if you are specifying an invalid address for the memory
  55. * storage of the partition or, the block does not align
  56. * on a pointer boundary
  57. * OS_ERR_MEM_INVALID_PART no free partitions available
  58. * OS_ERR_MEM_INVALID_BLKS user specified an invalid number of blocks (must be >= 2)
  59. * OS_ERR_MEM_INVALID_SIZE user specified an invalid block size
  60. * - must be greater than the size of a pointer
  61. * - must be able to hold an integral number of pointers
  62. * Returns : != (OS_MEM *)0 is the partition was created
  63. * == (OS_MEM *)0 if the partition was not created because of invalid arguments or, no
  64. * free partition is available.
  65. *********************************************************************************************************
  66. */
  67. OS_MEM *OSMemCreate (void *addr,
  68. INT32U nblks,
  69. INT32U blksize,
  70. INT8U *perr)
  71. {
  72. OS_MEM *pmem;
  73. INT8U *pblk;
  74. void **plink;
  75. INT32U loops;
  76. INT32U i;
  77. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  78. OS_CPU_SR cpu_sr = 0u;
  79. #endif
  80. #ifdef OS_SAFETY_CRITICAL
  81. if (perr == (INT8U *)0) {
  82. OS_SAFETY_CRITICAL_EXCEPTION();
  83. return ((OS_MEM *)0);
  84. }
  85. #endif
  86. #ifdef OS_SAFETY_CRITICAL_IEC61508
  87. if (OSSafetyCriticalStartFlag == OS_TRUE) {
  88. OS_SAFETY_CRITICAL_EXCEPTION();
  89. *perr = OS_ERR_ILLEGAL_CREATE_RUN_TIME;
  90. return ((OS_MEM *)0);
  91. }
  92. #endif
  93. #if OS_ARG_CHK_EN > 0u
  94. if (addr == (void *)0) { /* Must pass a valid address for the memory part.*/
  95. *perr = OS_ERR_MEM_INVALID_ADDR;
  96. return ((OS_MEM *)0);
  97. }
  98. if (((INT32U)addr & (sizeof(void *) - 1u)) != 0u){ /* Must be pointer size aligned */
  99. *perr = OS_ERR_MEM_INVALID_ADDR;
  100. return ((OS_MEM *)0);
  101. }
  102. if (nblks < 2u) { /* Must have at least 2 blocks per partition */
  103. *perr = OS_ERR_MEM_INVALID_BLKS;
  104. return ((OS_MEM *)0);
  105. }
  106. if (blksize < sizeof(void *)) { /* Must contain space for at least a pointer */
  107. *perr = OS_ERR_MEM_INVALID_SIZE;
  108. return ((OS_MEM *)0);
  109. }
  110. #endif
  111. OS_ENTER_CRITICAL();
  112. pmem = OSMemFreeList; /* Get next free memory partition */
  113. if (OSMemFreeList != (OS_MEM *)0) { /* See if pool of free partitions was empty */
  114. OSMemFreeList = (OS_MEM *)OSMemFreeList->OSMemFreeList;
  115. }
  116. OS_EXIT_CRITICAL();
  117. if (pmem == (OS_MEM *)0) { /* See if we have a memory partition */
  118. *perr = OS_ERR_MEM_INVALID_PART;
  119. return ((OS_MEM *)0);
  120. }
  121. plink = (void **)addr; /* Create linked list of free memory blocks */
  122. pblk = (INT8U *)addr;
  123. loops = nblks - 1u;
  124. for (i = 0u; i < loops; i++) {
  125. pblk += blksize; /* Point to the FOLLOWING block */
  126. *plink = (void *)pblk; /* Save pointer to NEXT block in CURRENT block */
  127. plink = (void **)pblk; /* Position to NEXT block */
  128. }
  129. *plink = (void *)0; /* Last memory block points to NULL */
  130. pmem->OSMemAddr = addr; /* Store start address of memory partition */
  131. pmem->OSMemFreeList = addr; /* Initialize pointer to pool of free blocks */
  132. pmem->OSMemNFree = nblks; /* Store number of free blocks in MCB */
  133. pmem->OSMemNBlks = nblks;
  134. pmem->OSMemBlkSize = blksize; /* Store block size of each memory blocks */
  135. *perr = OS_ERR_NONE;
  136. return (pmem);
  137. }
  138. /*
  139. *********************************************************************************************************
  140. * GET A MEMORY BLOCK
  141. *
  142. * Description : Get a memory block from a partition
  143. *
  144. * Arguments : pmem is a pointer to the memory partition control block
  145. *
  146. * perr is a pointer to a variable containing an error message which will be set by this
  147. * function to either:
  148. *
  149. * OS_ERR_NONE if the memory partition has been created correctly.
  150. * OS_ERR_MEM_NO_FREE_BLKS if there are no more free memory blocks to allocate to caller
  151. * OS_ERR_MEM_INVALID_PMEM if you passed a NULL pointer for 'pmem'
  152. *
  153. * Returns : A pointer to a memory block if no error is detected
  154. * A pointer to NULL if an error is detected
  155. *********************************************************************************************************
  156. */
  157. void *OSMemGet (OS_MEM *pmem,
  158. INT8U *perr)
  159. {
  160. void *pblk;
  161. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  162. OS_CPU_SR cpu_sr = 0u;
  163. #endif
  164. #ifdef OS_SAFETY_CRITICAL
  165. if (perr == (INT8U *)0) {
  166. OS_SAFETY_CRITICAL_EXCEPTION();
  167. return ((void *)0);
  168. }
  169. #endif
  170. #if OS_ARG_CHK_EN > 0u
  171. if (pmem == (OS_MEM *)0) { /* Must point to a valid memory partition */
  172. *perr = OS_ERR_MEM_INVALID_PMEM;
  173. return ((void *)0);
  174. }
  175. #endif
  176. OS_ENTER_CRITICAL();
  177. if (pmem->OSMemNFree > 0u) { /* See if there are any free memory blocks */
  178. pblk = pmem->OSMemFreeList; /* Yes, point to next free memory block */
  179. pmem->OSMemFreeList = *(void **)pblk; /* Adjust pointer to new free list */
  180. pmem->OSMemNFree--; /* One less memory block in this partition */
  181. OS_EXIT_CRITICAL();
  182. *perr = OS_ERR_NONE; /* No error */
  183. return (pblk); /* Return memory block to caller */
  184. }
  185. OS_EXIT_CRITICAL();
  186. *perr = OS_ERR_MEM_NO_FREE_BLKS; /* No, Notify caller of empty memory partition */
  187. return ((void *)0); /* Return NULL pointer to caller */
  188. }
  189. /*
  190. *********************************************************************************************************
  191. * GET THE NAME OF A MEMORY PARTITION
  192. *
  193. * Description: This function is used to obtain the name assigned to a memory partition.
  194. *
  195. * Arguments : pmem is a pointer to the memory partition
  196. *
  197. * pname is a pointer to a pointer to an ASCII string that will receive the name of the memory partition.
  198. *
  199. * perr is a pointer to an error code that can contain one of the following values:
  200. *
  201. * OS_ERR_NONE if the name was copied to 'pname'
  202. * OS_ERR_MEM_INVALID_PMEM if you passed a NULL pointer for 'pmem'
  203. * OS_ERR_PNAME_NULL You passed a NULL pointer for 'pname'
  204. * OS_ERR_NAME_GET_ISR You called this function from an ISR
  205. *
  206. * Returns : The length of the string or 0 if 'pmem' is a NULL pointer.
  207. *********************************************************************************************************
  208. */
  209. #if OS_MEM_NAME_EN > 0u
  210. INT8U OSMemNameGet (OS_MEM *pmem,
  211. INT8U **pname,
  212. INT8U *perr)
  213. {
  214. INT8U len;
  215. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  216. OS_CPU_SR cpu_sr = 0u;
  217. #endif
  218. #ifdef OS_SAFETY_CRITICAL
  219. if (perr == (INT8U *)0) {
  220. OS_SAFETY_CRITICAL_EXCEPTION();
  221. return (0u);
  222. }
  223. #endif
  224. #if OS_ARG_CHK_EN > 0u
  225. if (pmem == (OS_MEM *)0) { /* Is 'pmem' a NULL pointer? */
  226. *perr = OS_ERR_MEM_INVALID_PMEM;
  227. return (0u);
  228. }
  229. if (pname == (INT8U **)0) { /* Is 'pname' a NULL pointer? */
  230. *perr = OS_ERR_PNAME_NULL;
  231. return (0u);
  232. }
  233. #endif
  234. if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
  235. *perr = OS_ERR_NAME_GET_ISR;
  236. return (0u);
  237. }
  238. OS_ENTER_CRITICAL();
  239. *pname = pmem->OSMemName;
  240. len = OS_StrLen(*pname);
  241. OS_EXIT_CRITICAL();
  242. *perr = OS_ERR_NONE;
  243. return (len);
  244. }
  245. #endif
  246. /*
  247. *********************************************************************************************************
  248. * ASSIGN A NAME TO A MEMORY PARTITION
  249. *
  250. * Description: This function assigns a name to a memory partition.
  251. *
  252. * Arguments : pmem is a pointer to the memory partition
  253. *
  254. * pname is a pointer to an ASCII string that contains the name of the memory partition.
  255. *
  256. * perr is a pointer to an error code that can contain one of the following values:
  257. *
  258. * OS_ERR_NONE if the name was copied to 'pname'
  259. * OS_ERR_MEM_INVALID_PMEM if you passed a NULL pointer for 'pmem'
  260. * OS_ERR_PNAME_NULL You passed a NULL pointer for 'pname'
  261. * OS_ERR_MEM_NAME_TOO_LONG if the name doesn't fit in the storage area
  262. * OS_ERR_NAME_SET_ISR if you called this function from an ISR
  263. *
  264. * Returns : None
  265. *********************************************************************************************************
  266. */
  267. #if OS_MEM_NAME_EN > 0u
  268. void OSMemNameSet (OS_MEM *pmem,
  269. INT8U *pname,
  270. INT8U *perr)
  271. {
  272. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  273. OS_CPU_SR cpu_sr = 0u;
  274. #endif
  275. #ifdef OS_SAFETY_CRITICAL
  276. if (perr == (INT8U *)0) {
  277. OS_SAFETY_CRITICAL_EXCEPTION();
  278. return;
  279. }
  280. #endif
  281. #if OS_ARG_CHK_EN > 0u
  282. if (pmem == (OS_MEM *)0) { /* Is 'pmem' a NULL pointer? */
  283. *perr = OS_ERR_MEM_INVALID_PMEM;
  284. return;
  285. }
  286. if (pname == (INT8U *)0) { /* Is 'pname' a NULL pointer? */
  287. *perr = OS_ERR_PNAME_NULL;
  288. return;
  289. }
  290. #endif
  291. if (OSIntNesting > 0u) { /* See if trying to call from an ISR */
  292. *perr = OS_ERR_NAME_SET_ISR;
  293. return;
  294. }
  295. OS_ENTER_CRITICAL();
  296. pmem->OSMemName = pname;
  297. OS_EXIT_CRITICAL();
  298. *perr = OS_ERR_NONE;
  299. }
  300. #endif
  301. /*
  302. *********************************************************************************************************
  303. * RELEASE A MEMORY BLOCK
  304. *
  305. * Description : Returns a memory block to a partition
  306. *
  307. * Arguments : pmem is a pointer to the memory partition control block
  308. *
  309. * pblk is a pointer to the memory block being released.
  310. *
  311. * Returns : OS_ERR_NONE if the memory block was inserted into the partition
  312. * OS_ERR_MEM_FULL if you are returning a memory block to an already FULL memory
  313. * partition (You freed more blocks than you allocated!)
  314. * OS_ERR_MEM_INVALID_PMEM if you passed a NULL pointer for 'pmem'
  315. * OS_ERR_MEM_INVALID_PBLK if you passed a NULL pointer for the block to release.
  316. *********************************************************************************************************
  317. */
  318. INT8U OSMemPut (OS_MEM *pmem,
  319. void *pblk)
  320. {
  321. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  322. OS_CPU_SR cpu_sr = 0u;
  323. #endif
  324. #if OS_ARG_CHK_EN > 0u
  325. if (pmem == (OS_MEM *)0) { /* Must point to a valid memory partition */
  326. return (OS_ERR_MEM_INVALID_PMEM);
  327. }
  328. if (pblk == (void *)0) { /* Must release a valid block */
  329. return (OS_ERR_MEM_INVALID_PBLK);
  330. }
  331. #endif
  332. OS_ENTER_CRITICAL();
  333. if (pmem->OSMemNFree >= pmem->OSMemNBlks) { /* Make sure all blocks not already returned */
  334. OS_EXIT_CRITICAL();
  335. return (OS_ERR_MEM_FULL);
  336. }
  337. *(void **)pblk = pmem->OSMemFreeList; /* Insert released block into free block list */
  338. pmem->OSMemFreeList = pblk;
  339. pmem->OSMemNFree++; /* One more memory block in this partition */
  340. OS_EXIT_CRITICAL();
  341. return (OS_ERR_NONE); /* Notify caller that memory block was released */
  342. }
  343. /*
  344. *********************************************************************************************************
  345. * QUERY MEMORY PARTITION
  346. *
  347. * Description : This function is used to determine the number of free memory blocks and the number of
  348. * used memory blocks from a memory partition.
  349. *
  350. * Arguments : pmem is a pointer to the memory partition control block
  351. *
  352. * p_mem_data is a pointer to a structure that will contain information about the memory
  353. * partition.
  354. *
  355. * Returns : OS_ERR_NONE if no errors were found.
  356. * OS_ERR_MEM_INVALID_PMEM if you passed a NULL pointer for 'pmem'
  357. * OS_ERR_MEM_INVALID_PDATA if you passed a NULL pointer to the data recipient.
  358. *********************************************************************************************************
  359. */
  360. #if OS_MEM_QUERY_EN > 0u
  361. INT8U OSMemQuery (OS_MEM *pmem,
  362. OS_MEM_DATA *p_mem_data)
  363. {
  364. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  365. OS_CPU_SR cpu_sr = 0u;
  366. #endif
  367. #if OS_ARG_CHK_EN > 0u
  368. if (pmem == (OS_MEM *)0) { /* Must point to a valid memory partition */
  369. return (OS_ERR_MEM_INVALID_PMEM);
  370. }
  371. if (p_mem_data == (OS_MEM_DATA *)0) { /* Must release a valid storage area for the data */
  372. return (OS_ERR_MEM_INVALID_PDATA);
  373. }
  374. #endif
  375. OS_ENTER_CRITICAL();
  376. p_mem_data->OSAddr = pmem->OSMemAddr;
  377. p_mem_data->OSFreeList = pmem->OSMemFreeList;
  378. p_mem_data->OSBlkSize = pmem->OSMemBlkSize;
  379. p_mem_data->OSNBlks = pmem->OSMemNBlks;
  380. p_mem_data->OSNFree = pmem->OSMemNFree;
  381. OS_EXIT_CRITICAL();
  382. p_mem_data->OSNUsed = p_mem_data->OSNBlks - p_mem_data->OSNFree;
  383. return (OS_ERR_NONE);
  384. }
  385. #endif /* OS_MEM_QUERY_EN */
  386. /*
  387. *********************************************************************************************************
  388. * INITIALIZE MEMORY PARTITION MANAGER
  389. *
  390. * Description : This function is called by uC/OS-II to initialize the memory partition manager. Your
  391. * application MUST NOT call this function.
  392. *
  393. * Arguments : none
  394. *
  395. * Returns : none
  396. *
  397. * Note(s) : This function is INTERNAL to uC/OS-II and your application should not call it.
  398. *********************************************************************************************************
  399. */
  400. void OS_MemInit (void)
  401. {
  402. #if OS_MAX_MEM_PART == 1u
  403. OS_MemClr((INT8U *)&OSMemTbl[0], sizeof(OSMemTbl)); /* Clear the memory partition table */
  404. OSMemFreeList = (OS_MEM *)&OSMemTbl[0]; /* Point to beginning of free list */
  405. #if OS_MEM_NAME_EN > 0u
  406. OSMemFreeList->OSMemName = (INT8U *)"?"; /* Unknown name */
  407. #endif
  408. #endif
  409. #if OS_MAX_MEM_PART >= 2u
  410. OS_MEM *pmem;
  411. INT16U i;
  412. OS_MemClr((INT8U *)&OSMemTbl[0], sizeof(OSMemTbl)); /* Clear the memory partition table */
  413. for (i = 0u; i < (OS_MAX_MEM_PART - 1u); i++) { /* Init. list of free memory partitions */
  414. pmem = &OSMemTbl[i]; /* Point to memory control block (MCB) */
  415. pmem->OSMemFreeList = (void *)&OSMemTbl[i + 1u]; /* Chain list of free partitions */
  416. #if OS_MEM_NAME_EN > 0u
  417. pmem->OSMemName = (INT8U *)(void *)"?";
  418. #endif
  419. }
  420. pmem = &OSMemTbl[i];
  421. pmem->OSMemFreeList = (void *)0; /* Initialize last node */
  422. #if OS_MEM_NAME_EN > 0u
  423. pmem->OSMemName = (INT8U *)(void *)"?";
  424. #endif
  425. OSMemFreeList = &OSMemTbl[0]; /* Point to beginning of free list */
  426. #endif
  427. }
  428. #endif /* OS_MEM_EN */