📄 os_mem.lst
字号:
ANSI-C/cC++ Compiler for HC08 V-5.0.12 ICG, Oct 6 2000
1: /*
2: *********************************************************************************************************
3: * uC/OS-II
4: * The Real-Time Kernel
5: * MEMORY MANAGEMENT
6: *
7: * (c) Copyright 1992-1998, Jean J. Labrosse, Plantation, FL
8: * All Rights Reserved
9: *
10: * V2.00
11: *
12: * File : OS_MEM.C
13: * By : Jean J. Labrosse
14: *********************************************************************************************************
15: */
16:
17: #include "includes.h"
18:
19: #if OS_MEM_EN && OS_MAX_MEM_PART >= 2
20: /*
21: *********************************************************************************************************
22: * LOCAL GLOBAL VARIABLES
23: *********************************************************************************************************
24: */
25:
26: static OS_MEM *OSMemFreeList; /* Pointer to free list of memory partitions */
27: static OS_MEM OSMemTbl[OS_MAX_MEM_PART];/* Storage for memory partition manager */
28: /*$PAGE*/
29: /*
30: *********************************************************************************************************
31: * CREATE A MEMORY PARTITION
32: *
33: * Description : Create a fixed-sized memory partition that will be managed by uC/OS-II.
34: *
35: * Arguments : addr is the starting address of the memory partition
36: *
37: * nblks is the number of memory blocks to create from the partition.
38: *
39: * blksize is the size (in bytes) of each block in the memory partition.
40: *
41: * err is a pointer to a variable containing an error message which will be set by
42: * this function to either:
43: *
44: * OS_NO_ERR if the memory partition has been created correctly.
45: * OS_MEM_INVALID_PART no free partitions available
46: * OS_MEM_INVALID_BLKS user specified an invalid number of blocks (must be >= 2)
47: * OS_MEM_INVALID_SIZE user specified an invalid block size
48: * (must be greater than the size of a pointer)
49: * Returns : != (OS_MEM *)0 is the partition was created
50: * == (OS_MEM *)0 if the partition was not created because of invalid arguments or, no
51: * free partition is available.
52: *********************************************************************************************************
53: */
54:
55: OS_MEM *OSMemCreate (void *addr, INT32U nblks, INT32U blksize, INT8U *err)
56: {
57: OS_MEM *pmem;
58: INT8U *pblk;
59: void **plink;
60: INT32U i;
61:
62:
63: if (nblks < 2) { /* Must have at least 2 blocks per partition */
64: *err = OS_MEM_INVALID_BLKS;
65: return ((OS_MEM *)0);
66: }
67: if (blksize < sizeof(void *)) { /* Must contain space for at least a pointer */
68: *err = OS_MEM_INVALID_SIZE;
69: return ((OS_MEM *)0);
70: }
71: OS_ENTER_CRITICAL();
72: pmem = OSMemFreeList; /* Get next free memory partition */
73: if (OSMemFreeList != (OS_MEM *)0) { /* See if pool of free partitions was empty */
74: OSMemFreeList = (OS_MEM *)OSMemFreeList->OSMemFreeList;
75: }
76: OS_EXIT_CRITICAL();
77: if (pmem == (OS_MEM *)0) { /* See if we have a memory partition */
78: *err = OS_MEM_INVALID_PART;
79: return ((OS_MEM *)0);
80: }
81: plink = (void **)addr; /* Create linked list of free memory blocks */
82: pblk = (INT8U *)addr + blksize;
83: for (i = 0; i < (nblks - 1); i++) {
84: *plink = (void *)pblk;
85: plink = (void **)pblk;
86: pblk = pblk + blksize;
87: }
88: *plink = (void *)0; /* Last memory block points to NULL */
89: OS_ENTER_CRITICAL();
90: pmem->OSMemAddr = addr; /* Store start address of memory partition */
91: pmem->OSMemFreeList = addr; /* Initialize pointer to pool of free blocks */
92: pmem->OSMemNFree = nblks; /* Store number of free blocks in MCB */
93: pmem->OSMemNBlks = nblks;
94: pmem->OSMemBlkSize = blksize; /* Store block size of each memory blocks */
95: OS_EXIT_CRITICAL();
96: *err = OS_NO_ERR;
97: return (pmem);
98: }
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: *
114: * Returns : A pointer to a memory block if no error is detected
115: * A pointer to NULL if an error is detected
116: *********************************************************************************************************
117: */
118:
119: void *OSMemGet (OS_MEM *pmem, INT8U *err)
120: {
121: void *pblk;
122:
123:
124: OS_ENTER_CRITICAL();
125: if (pmem->OSMemNFree > 0) { /* See if there are any free memory blocks */
126: pblk = pmem->OSMemFreeList; /* Yes, point to next free memory block */
127: pmem->OSMemFreeList = *(void **)pblk; /* Adjust pointer to new free list */
128: pmem->OSMemNFree--; /* One less memory block in this partition */
129: OS_EXIT_CRITICAL();
130: *err = OS_NO_ERR; /* No error */
131: return (pblk); /* Return memory block to caller */
132: } else {
133: OS_EXIT_CRITICAL();
134: *err = OS_MEM_NO_FREE_BLKS; /* No, Notify caller of empty memory partition */
135: return ((void *)0); /* Return NULL pointer to caller */
136: }
137: }
138: /*$PAGE*/
139: /*
140: *********************************************************************************************************
141: * INITIALIZE MEMORY PARTITION MANAGER
142: *
143: * Description : This function is called by uC/OS-II to initialize the memory partition manager. Your
144: * application MUST NOT call this function.
145: *
146: * Arguments : none
147: *
148: * Returns : none
149: *********************************************************************************************************
150: */
151:
152: void OSMemInit (void)
153: {
154: OS_MEM *pmem;
155: INT16U i;
156:
157:
158: pmem = (OS_MEM *)&OSMemTbl[0]; /* Point to memory control block (MCB) */
159: for (i = 0; i < (OS_MAX_MEM_PART - 1); i++) { /* Init. list of free memory partitions */
160: pmem->OSMemFreeList = (void *)&OSMemTbl[i+1]; /* Chain list of free partitions */
161: pmem->OSMemAddr = (void *)0; /* Store start address of memory partition */
162: pmem->OSMemNFree = 0; /* No free blocks */
163: pmem->OSMemNBlks = 0; /* No blocks */
164: pmem->OSMemBlkSize = 0; /* Zero size */
165: pmem++;
166: }
167: OSMemTbl[OS_MAX_MEM_PART - 1].OSMemFreeList = (void *)0;
168: OSMemFreeList = (OS_MEM *)&OSMemTbl[0];
169: }
170: /*$PAGE*/
171: /*
172: *********************************************************************************************************
173: * RELEASE A MEMORY BLOCK
174: *
175: * Description : Returns a memory block to a partition
176: *
177: * Arguments : pmem is a pointer to the memory partition control block
178: *
179: * pblk is a pointer to the memory block being released.
180: *
181: * Returns : OS_NO_ERR if the memory block was inserted into the partition
182: * OS_MEM_FULL if you are returning a memory block to an already FULL memory partition
183: * (You freed more blocks than you allocated!)
184: *********************************************************************************************************
185: */
186:
187: INT8U OSMemPut (OS_MEM *pmem, void *pblk)
188: {
189: OS_ENTER_CRITICAL();
190: if (pmem->OSMemNFree >= pmem->OSMemNBlks) { /* Make sure all blocks not already returned */
191: OS_EXIT_CRITICAL();
192: return (OS_MEM_FULL);
193: }
194: *(void **)pblk = pmem->OSMemFreeList; /* Insert released block into free block list */
195: pmem->OSMemFreeList = pblk;
196: pmem->OSMemNFree++; /* One more memory block in this partition */
197: OS_EXIT_CRITICAL();
198: return (OS_NO_ERR); /* Notify caller that memory block was released */
199: }
200: /*$PAGE*/
201: /*
202: *********************************************************************************************************
203: * QUERY MEMORY PARTITION
204: *
205: * Description : This function is used to determine the number of free memory blocks and the number of
206: * used memory blocks from a memory partition.
207: *
208: * Arguments : pmem is a pointer to the memory partition control block
209: *
210: * pdata is a pointer to a structure that will contain information about the memory
211: * partition.
212: *
213: * Returns : OS_NO_ERR Always returns no error.
214: *********************************************************************************************************
215: */
216:
217: INT8U OSMemQuery (OS_MEM *pmem, OS_MEM_DATA *pdata)
218: {
219: OS_ENTER_CRITICAL();
220: pdata->OSAddr = pmem->OSMemAddr;
221: pdata->OSFreeList = pmem->OSMemFreeList;
222: pdata->OSBlkSize = pmem->OSMemBlkSize;
223: pdata->OSNBlks = pmem->OSMemNBlks;
224: pdata->OSNFree = pmem->OSMemNFree;
225: OS_EXIT_CRITICAL();
226: pdata->OSNUsed = pdata->OSNBlks - pdata->OSNFree;
227: return (OS_NO_ERR);
228: }
229: #endif
230:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -