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

490 lines
20 KiB

  1. /*
  2. *********************************************************************************************************
  3. * uC/OS-II
  4. * The Real-Time Kernel
  5. *
  6. * Copyright 1992-2021 Silicon Laboratories Inc. www.silabs.com
  7. *
  8. * SPDX-License-Identifier: APACHE-2.0
  9. *
  10. * This software is subject to an open source license and is distributed by
  11. * Silicon Laboratories Inc. pursuant to the terms of the Apache License,
  12. * Version 2.0 available at www.apache.org/licenses/LICENSE-2.0.
  13. *
  14. *********************************************************************************************************
  15. */
  16. /*
  17. *********************************************************************************************************
  18. *
  19. * MEMORY MANAGEMENT
  20. *
  21. * Filename : os_mem.c
  22. * Version : V2.93.01
  23. *********************************************************************************************************
  24. */
  25. #ifndef OS_MEM_C
  26. #define OS_MEM_C
  27. #define MICRIUM_SOURCE
  28. #ifndef OS_MASTER_FILE
  29. #include <ucos_ii.h>
  30. #endif
  31. #if (OS_MEM_EN > 0u) && (OS_MAX_MEM_PART > 0u)
  32. /*
  33. *********************************************************************************************************
  34. * CREATE A MEMORY PARTITION
  35. *
  36. * Description : Create a fixed-sized memory partition that will be managed by uC/OS-II.
  37. *
  38. * Arguments : addr is the starting address of the memory partition
  39. *
  40. * nblks is the number of memory blocks to create from the partition.
  41. *
  42. * blksize is the size (in bytes) of each block in the memory partition.
  43. *
  44. * perr is a pointer to a variable containing an error message which will be set by
  45. * this function to either:
  46. *
  47. * OS_ERR_NONE if the memory partition has been created correctly.
  48. * OS_ERR_ILLEGAL_CREATE_RUN_TIME if you tried to create a memory partition after
  49. * safety critical operation started.
  50. * OS_ERR_MEM_INVALID_ADDR if you are specifying an invalid address for the memory
  51. * storage of the partition or, the block does not align
  52. * on a pointer boundary
  53. * OS_ERR_MEM_INVALID_PART no free partitions available
  54. * OS_ERR_MEM_INVALID_BLKS user specified an invalid number of blocks (must be >= 2)
  55. * OS_ERR_MEM_INVALID_SIZE user specified an invalid block size
  56. * - must be greater than the size of a pointer
  57. * - must be able to hold an integral number of pointers
  58. * Returns : != (OS_MEM *)0 is the partition was created
  59. * == (OS_MEM *)0 if the partition was not created because of invalid arguments or, no
  60. * free partition is available.
  61. *********************************************************************************************************
  62. */
  63. OS_MEM *OSMemCreate (void *addr,
  64. INT32U nblks,
  65. INT32U blksize,
  66. INT8U *perr)
  67. {
  68. OS_MEM *pmem;
  69. INT8U *pblk;
  70. void **plink;
  71. INT32U loops;
  72. INT32U i;
  73. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  74. OS_CPU_SR cpu_sr = 0u;
  75. #endif
  76. #ifdef OS_SAFETY_CRITICAL
  77. if (perr == (INT8U *)0) {
  78. OS_SAFETY_CRITICAL_EXCEPTION();
  79. return ((OS_MEM *)0);
  80. }
  81. #endif
  82. #ifdef OS_SAFETY_CRITICAL_IEC61508
  83. if (OSSafetyCriticalStartFlag == OS_TRUE) {
  84. OS_SAFETY_CRITICAL_EXCEPTION();
  85. *perr = OS_ERR_ILLEGAL_CREATE_RUN_TIME;
  86. return ((OS_MEM *)0);
  87. }
  88. #endif
  89. #if OS_ARG_CHK_EN > 0u
  90. if (addr == (void *)0) { /* Must pass a valid address for the memory part.*/
  91. *perr = OS_ERR_MEM_INVALID_ADDR;
  92. return ((OS_MEM *)0);
  93. }
  94. if (((INT32U)addr & (sizeof(void *) - 1u)) != 0u){ /* Must be pointer size aligned */
  95. *perr = OS_ERR_MEM_INVALID_ADDR;
  96. return ((OS_MEM *)0);
  97. }
  98. if (nblks < 2u) { /* Must have at least 2 blocks per partition */
  99. *perr = OS_ERR_MEM_INVALID_BLKS;
  100. return ((OS_MEM *)0);
  101. }
  102. if (blksize < sizeof(void *)) { /* Must contain space for at least a pointer */
  103. *perr = OS_ERR_MEM_INVALID_SIZE;
  104. return ((OS_MEM *)0);
  105. }
  106. #endif
  107. OS_ENTER_CRITICAL();
  108. pmem = OSMemFreeList; /* Get next free memory partition */
  109. if (OSMemFreeList != (OS_MEM *)0) { /* See if pool of free partitions was empty */
  110. OSMemFreeList = (OS_MEM *)OSMemFreeList->OSMemFreeList;
  111. }
  112. OS_EXIT_CRITICAL();
  113. if (pmem == (OS_MEM *)0) { /* See if we have a memory partition */
  114. *perr = OS_ERR_MEM_INVALID_PART;
  115. return ((OS_MEM *)0);
  116. }
  117. plink = (void **)addr; /* Create linked list of free memory blocks */
  118. pblk = (INT8U *)addr;
  119. loops = nblks - 1u;
  120. for (i = 0u; i < loops; i++) {
  121. pblk += blksize; /* Point to the FOLLOWING block */
  122. *plink = (void *)pblk; /* Save pointer to NEXT block in CURRENT block */
  123. plink = (void **)pblk; /* Position to NEXT block */
  124. }
  125. *plink = (void *)0; /* Last memory block points to NULL */
  126. pmem->OSMemAddr = addr; /* Store start address of memory partition */
  127. pmem->OSMemFreeList = addr; /* Initialize pointer to pool of free blocks */
  128. pmem->OSMemNFree = nblks; /* Store number of free blocks in MCB */
  129. pmem->OSMemNBlks = nblks;
  130. pmem->OSMemBlkSize = blksize; /* Store block size of each memory blocks */
  131. OS_TRACE_MEM_CREATE(pmem);
  132. *perr = OS_ERR_NONE;
  133. return (pmem);
  134. }
  135. /*
  136. *********************************************************************************************************
  137. * GET A MEMORY BLOCK
  138. *
  139. * Description : Get a memory block from a partition
  140. *
  141. * Arguments : pmem is a pointer to the memory partition control block
  142. *
  143. * perr is a pointer to a variable containing an error message which will be set by this
  144. * function to either:
  145. *
  146. * OS_ERR_NONE if the memory partition has been created correctly.
  147. * OS_ERR_MEM_NO_FREE_BLKS if there are no more free memory blocks to allocate to caller
  148. * OS_ERR_MEM_INVALID_PMEM if you passed a NULL pointer for 'pmem'
  149. *
  150. * Returns : A pointer to a memory block if no error is detected
  151. * A pointer to NULL if an error is detected
  152. *********************************************************************************************************
  153. */
  154. void *OSMemGet (OS_MEM *pmem,
  155. INT8U *perr)
  156. {
  157. void *pblk;
  158. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  159. OS_CPU_SR cpu_sr = 0u;
  160. #endif
  161. #ifdef OS_SAFETY_CRITICAL
  162. if (perr == (INT8U *)0) {
  163. OS_SAFETY_CRITICAL_EXCEPTION();
  164. return ((void *)0);
  165. }
  166. #endif
  167. #if OS_ARG_CHK_EN > 0u
  168. if (pmem == (OS_MEM *)0) { /* Must point to a valid memory partition */
  169. *perr = OS_ERR_MEM_INVALID_PMEM;
  170. return ((void *)0);
  171. }
  172. #endif
  173. OS_TRACE_MEM_GET_ENTER(pmem);
  174. OS_ENTER_CRITICAL();
  175. if (pmem->OSMemNFree > 0u) { /* See if there are any free memory blocks */
  176. pblk = pmem->OSMemFreeList; /* Yes, point to next free memory block */
  177. pmem->OSMemFreeList = *(void **)pblk; /* Adjust pointer to new free list */
  178. pmem->OSMemNFree--; /* One less memory block in this partition */
  179. OS_EXIT_CRITICAL();
  180. *perr = OS_ERR_NONE; /* No error */
  181. OS_TRACE_MEM_GET_EXIT(*perr);
  182. return (pblk); /* Return memory block to caller */
  183. }
  184. OS_EXIT_CRITICAL();
  185. *perr = OS_ERR_MEM_NO_FREE_BLKS; /* No, Notify caller of empty memory partition */
  186. OS_TRACE_MEM_GET_EXIT(*perr);
  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. OS_TRACE_EVENT_NAME_SET(pmem, pname);
  299. *perr = OS_ERR_NONE;
  300. }
  301. #endif
  302. /*
  303. *********************************************************************************************************
  304. * RELEASE A MEMORY BLOCK
  305. *
  306. * Description : Returns a memory block to a partition
  307. *
  308. * Arguments : pmem is a pointer to the memory partition control block
  309. *
  310. * pblk is a pointer to the memory block being released.
  311. *
  312. * Returns : OS_ERR_NONE if the memory block was inserted into the partition
  313. * OS_ERR_MEM_FULL if you are returning a memory block to an already FULL memory
  314. * partition (You freed more blocks than you allocated!)
  315. * OS_ERR_MEM_INVALID_PMEM if you passed a NULL pointer for 'pmem'
  316. * OS_ERR_MEM_INVALID_PBLK if you passed a NULL pointer for the block to release.
  317. *********************************************************************************************************
  318. */
  319. INT8U OSMemPut (OS_MEM *pmem,
  320. void *pblk)
  321. {
  322. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  323. OS_CPU_SR cpu_sr = 0u;
  324. #endif
  325. #if OS_ARG_CHK_EN > 0u
  326. if (pmem == (OS_MEM *)0) { /* Must point to a valid memory partition */
  327. return (OS_ERR_MEM_INVALID_PMEM);
  328. }
  329. if (pblk == (void *)0) { /* Must release a valid block */
  330. return (OS_ERR_MEM_INVALID_PBLK);
  331. }
  332. #endif
  333. OS_TRACE_MEM_PUT_ENTER(pmem, pblk);
  334. OS_ENTER_CRITICAL();
  335. if (pmem->OSMemNFree >= pmem->OSMemNBlks) { /* Make sure all blocks not already returned */
  336. OS_EXIT_CRITICAL();
  337. OS_TRACE_MEM_PUT_EXIT(OS_ERR_MEM_FULL);
  338. return (OS_ERR_MEM_FULL);
  339. }
  340. *(void **)pblk = pmem->OSMemFreeList; /* Insert released block into free block list */
  341. pmem->OSMemFreeList = pblk;
  342. pmem->OSMemNFree++; /* One more memory block in this partition */
  343. OS_EXIT_CRITICAL();
  344. OS_TRACE_MEM_PUT_EXIT(OS_ERR_NONE);
  345. return (OS_ERR_NONE); /* Notify caller that memory block was released */
  346. }
  347. /*
  348. *********************************************************************************************************
  349. * QUERY MEMORY PARTITION
  350. *
  351. * Description : This function is used to determine the number of free memory blocks and the number of
  352. * used memory blocks from a memory partition.
  353. *
  354. * Arguments : pmem is a pointer to the memory partition control block
  355. *
  356. * p_mem_data is a pointer to a structure that will contain information about the memory
  357. * partition.
  358. *
  359. * Returns : OS_ERR_NONE if no errors were found.
  360. * OS_ERR_MEM_INVALID_PMEM if you passed a NULL pointer for 'pmem'
  361. * OS_ERR_MEM_INVALID_PDATA if you passed a NULL pointer to the data recipient.
  362. *********************************************************************************************************
  363. */
  364. #if OS_MEM_QUERY_EN > 0u
  365. INT8U OSMemQuery (OS_MEM *pmem,
  366. OS_MEM_DATA *p_mem_data)
  367. {
  368. #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
  369. OS_CPU_SR cpu_sr = 0u;
  370. #endif
  371. #if OS_ARG_CHK_EN > 0u
  372. if (pmem == (OS_MEM *)0) { /* Must point to a valid memory partition */
  373. return (OS_ERR_MEM_INVALID_PMEM);
  374. }
  375. if (p_mem_data == (OS_MEM_DATA *)0) { /* Must release a valid storage area for the data */
  376. return (OS_ERR_MEM_INVALID_PDATA);
  377. }
  378. #endif
  379. OS_ENTER_CRITICAL();
  380. p_mem_data->OSAddr = pmem->OSMemAddr;
  381. p_mem_data->OSFreeList = pmem->OSMemFreeList;
  382. p_mem_data->OSBlkSize = pmem->OSMemBlkSize;
  383. p_mem_data->OSNBlks = pmem->OSMemNBlks;
  384. p_mem_data->OSNFree = pmem->OSMemNFree;
  385. OS_EXIT_CRITICAL();
  386. p_mem_data->OSNUsed = p_mem_data->OSNBlks - p_mem_data->OSNFree;
  387. return (OS_ERR_NONE);
  388. }
  389. #endif /* OS_MEM_QUERY_EN */
  390. /*
  391. *********************************************************************************************************
  392. * INITIALIZE MEMORY PARTITION MANAGER
  393. *
  394. * Description : This function is called by uC/OS-II to initialize the memory partition manager. Your
  395. * application MUST NOT call this function.
  396. *
  397. * Arguments : none
  398. *
  399. * Returns : none
  400. *
  401. * Note(s) : This function is INTERNAL to uC/OS-II and your application should not call it.
  402. *********************************************************************************************************
  403. */
  404. void OS_MemInit (void)
  405. {
  406. #if OS_MAX_MEM_PART == 1u
  407. OS_MemClr((INT8U *)&OSMemTbl[0], sizeof(OSMemTbl)); /* Clear the memory partition table */
  408. OSMemFreeList = (OS_MEM *)&OSMemTbl[0]; /* Point to beginning of free list */
  409. #if OS_MEM_NAME_EN > 0u
  410. OSMemFreeList->OSMemName = (INT8U *)"?"; /* Unknown name */
  411. #endif
  412. #endif
  413. #if OS_MAX_MEM_PART >= 2u
  414. OS_MEM *pmem;
  415. INT16U i;
  416. OS_MemClr((INT8U *)&OSMemTbl[0], sizeof(OSMemTbl)); /* Clear the memory partition table */
  417. for (i = 0u; i < (OS_MAX_MEM_PART - 1u); i++) { /* Init. list of free memory partitions */
  418. pmem = &OSMemTbl[i]; /* Point to memory control block (MCB) */
  419. pmem->OSMemFreeList = (void *)&OSMemTbl[i + 1u]; /* Chain list of free partitions */
  420. #if OS_MEM_NAME_EN > 0u
  421. pmem->OSMemName = (INT8U *)(void *)"?";
  422. #endif
  423. }
  424. pmem = &OSMemTbl[i];
  425. pmem->OSMemFreeList = (void *)0; /* Initialize last node */
  426. #if OS_MEM_NAME_EN > 0u
  427. pmem->OSMemName = (INT8U *)(void *)"?";
  428. #endif
  429. OSMemFreeList = &OSMemTbl[0]; /* Point to beginning of free list */
  430. #endif
  431. }
  432. #endif /* OS_MEM_EN */
  433. #endif /* OS_MEM_C */