📄 os_mem.lst
字号:
C51 COMPILER V6.23a OS_MEM 12/09/2004 16:50:25 PAGE 1
C51 COMPILER V6.23a, COMPILATION OF MODULE OS_MEM
OBJECT MODULE PLACED IN OS_MEM.OBJ
COMPILER INVOKED BY: C:\Program Files\Keil\C51\BIN\C51.EXE OS_MEM.C LARGE BROWSE DEBUG OBJECTEXTEND
stmt level source
1 /*
2 *********************************************************************************************************
3 * uC/OS-II
4 * The Real-Time Kernel
5 * MEMORY MANAGEMENT
6 *
7 * (c) Copyright 1992-2002, Jean J. Labrosse, Weston, FL
8 * All Rights Reserved
9 *
10 * File : OS_MEM.C
11 * By : Jean J. Labrosse
12 *********************************************************************************************************
13 */
14
15 #ifndef OS_MASTER_FILE
16 #include "includes.h"
17 #endif
18
19 #if (OS_MEM_EN > 0) && (OS_MAX_MEM_PART > 0)
20 /*
21 *********************************************************************************************************
22 * CREATE A MEMORY PARTITION
23 *
24 * Description : Create a fixed-sized memory partition that will be managed by uC/OS-II.
25 *
26 * Arguments : addr is the starting address of the memory partition
27 *
28 * nblks is the number of memory blocks to create from the partition.
29 *
30 * blksize is the size (in bytes) of each block in the memory partition.
31 *
32 * err is a pointer to a variable containing an error message which will be set by
33 * this function to either:
34 *
35 * OS_NO_ERR if the memory partition has been created correctly.
36 * OS_MEM_INVALID_ADDR you are specifying an invalid address for the memory
37 * storage of the partition.
38 * OS_MEM_INVALID_PART no free partitions available
39 * OS_MEM_INVALID_BLKS user specified an invalid number of blocks (must be >= 2)
40 * OS_MEM_INVALID_SIZE user specified an invalid block size
41 * (must be greater than the size of a pointer)
42 * Returns : != (OS_MEM *)0 is the partition was created
43 * == (OS_MEM *)0 if the partition was not created because of invalid arguments or, no
44 * free partition is available.
45 *********************************************************************************************************
46 */
47
48 OS_MEM *OSMemCreate (void *addr, INT32U nblks, INT32U blksize, INT8U *err) KCREENTRANT
49 {
50 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
53 1 OS_MEM *pmem;
54 1 INT8U *pblk;
55 1 void **plink;
C51 COMPILER V6.23a OS_MEM 12/09/2004 16:50:25 PAGE 2
56 1 INT32U i;
57 1
58 1
59 1 #if OS_ARG_CHK_EN > 0
60 1 if (addr == (void *)0) { /* Must pass a valid address for the memory part. */
61 2 *err = OS_MEM_INVALID_ADDR;
62 2 return ((OS_MEM *)0);
63 2 }
64 1 if (nblks < 2) { /* Must have at least 2 blocks per partition */
65 2 *err = OS_MEM_INVALID_BLKS;
66 2 return ((OS_MEM *)0);
67 2 }
68 1 if (blksize < sizeof(void *)) { /* Must contain space for at least a pointer */
69 2 *err = OS_MEM_INVALID_SIZE;
70 2 return ((OS_MEM *)0);
71 2 }
72 1 #endif
73 1 OS_ENTER_CRITICAL();
74 1 pmem = OSMemFreeList; /* Get next free memory partition */
75 1 if (OSMemFreeList != (OS_MEM *)0) { /* See if pool of free partitions was empty */
76 2 OSMemFreeList = (OS_MEM *)OSMemFreeList->OSMemFreeList;
77 2 }
78 1 OS_EXIT_CRITICAL();
79 1 if (pmem == (OS_MEM *)0) { /* See if we have a memory partition */
80 2 *err = OS_MEM_INVALID_PART;
81 2 return ((OS_MEM *)0);
82 2 }
83 1 plink = (void **)addr; /* Create linked list of free memory blocks */
84 1 pblk = (INT8U *)addr + blksize;
85 1 for (i = 0; i < (nblks - 1); i++) {
86 2 *plink = (void *)pblk;
87 2 plink = (void **)pblk;
88 2 pblk = pblk + blksize;
89 2 }
90 1 *plink = (void *)0; /* Last memory block points to NULL */
91 1 pmem->OSMemAddr = addr; /* Store start address of memory partition */
92 1 pmem->OSMemFreeList = addr; /* Initialize pointer to pool of free blocks */
93 1 pmem->OSMemNFree = nblks; /* Store number of free blocks in MCB */
94 1 pmem->OSMemNBlks = nblks;
95 1 pmem->OSMemBlkSize = blksize; /* Store block size of each memory blocks */
96 1 *err = OS_NO_ERR;
97 1 return (pmem);
98 1 }
99 /*$PAGE*/
100 /*
101 *********************************************************************************************************
102 * GET A MEMORY BLOCK
103 *
104 * Description : Get a memory block from a partition
105 *
106 * Arguments : pmem is a pointer to the memory partition control block
107 *
108 * err is a pointer to a variable containing an error message which will be set by this
109 * function to either:
110 *
111 * OS_NO_ERR if the memory partition has been created correctly.
112 * OS_MEM_NO_FREE_BLKS if there are no more free memory blocks to allocate to caller
113 * OS_MEM_INVALID_PMEM if you passed a NULL pointer for 'pmem'
114 *
115 * Returns : A pointer to a memory block if no error is detected
116 * A pointer to NULL if an error is detected
117 *********************************************************************************************************
C51 COMPILER V6.23a OS_MEM 12/09/2004 16:50:25 PAGE 3
118 */
119
120 void *OSMemGet (OS_MEM *pmem, INT8U *err) KCREENTRANT
121 {
122 1 #if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
125 1 void *pblk;
126 1
127 1
128 1 #if OS_ARG_CHK_EN > 0
129 1 if (pmem == (OS_MEM *)0) { /* Must point to a valid memory partition */
130 2 *err = OS_MEM_INVALID_PMEM;
131 2 return ((OS_MEM *)0);
132 2 }
133 1 #endif
134 1 OS_ENTER_CRITICAL();
135 1 if (pmem->OSMemNFree > 0) { /* See if there are any free memory blocks */
136 2 pblk = pmem->OSMemFreeList; /* Yes, point to next free memory block */
137 2 pmem->OSMemFreeList = *(void **)pblk; /* Adjust pointer to new free list */
138 2 pmem->OSMemNFree--; /* One less memory block in this partition */
139 2 OS_EXIT_CRITICAL();
140 2 *err = OS_NO_ERR; /* No error */
141 2 return (pblk); /* Return memory block to caller */
142 2 }
143 1 OS_EXIT_CRITICAL();
144 1 *err = OS_MEM_NO_FREE_BLKS; /* No, Notify caller of empty memory partition */
145 1 return ((void *)0); /* Return NULL pointer to caller */
146 1 }
147 /*$PAGE*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -