📄 guialloc.lst
字号:
C51 COMPILER V8.05a GUIALLOC 04/11/2008 14:18:53 PAGE 1
C51 COMPILER V8.05a, COMPILATION OF MODULE GUIALLOC
OBJECT MODULE PLACED IN GUIAlloc.obj
COMPILER INVOKED BY: D:\Program Files\keil\C51\BIN\C51.EXE gui\Core\GUIAlloc.c LARGE BROWSE MDU_F120 DEBUG OBJECTEXTEND
-PRINT(.\GUIAlloc.lst) OBJECT(GUIAlloc.obj)
line level source
1 /*
2 *********************************************************************************************************
3 * uC/GUI
4 * Universal graphic software for embedded applications
5 *
6 * (c) Copyright 2002, Micrium Inc., Weston, FL
7 * (c) Copyright 2002, SEGGER Microcontroller Systeme GmbH
8 *
9 * 礐/GUI is protected by international copyright laws. Knowledge of the
10 * source code may not be used to write a similar product. This file may
11 * only be used in accordance with a license and should not be redistributed
12 * in any way. We appreciate your understanding and fairness.
13 *
14 ----------------------------------------------------------------------
15 File : GUIAlloc.C
16 Purpose : emWin dynamic memory management
17 ----------------------------------------------------------------------
18 Version-Date---Author-Explanation
19 ----------------------------------------------------------------------
20 1.00 000107 RS First version
21 ----------------------------------------------------------------------
22 Known problems:
23 None.
24 ----------------------------------------------------------------------
25 Open issues:
26 None.
27 ----------------------------------------------------------------------
28 Todo:
29 Nothing.
30
31 */
32
33
34 #include <stddef.h> /* needed for definition of NULL */
35 #include <string.h> /* for memcpy, memset */
36
37 #include "gui\Core\GUI_Protected.h"
38 #include "gui\Core\GUIDebug.h"
39
40 #if GUI_ALLOC_SIZE==0
#error GUI_ALLOC_SIZE needs to be > 0 when using this module
#endif
43 /*
44 *****************************************************************
45 *
46 * Config defaults
47 *
48 *****************************************************************
49 */
50
51 /* Permit automatic defragmentation when necessary */
52 #ifndef GUI_ALLOC_AUTDEFRAG
53 #define GUI_ALLOC_AUTDEFRAG 1
54 #endif
C51 COMPILER V8.05a GUIALLOC 04/11/2008 14:18:53 PAGE 2
55
56 #ifndef GUI_BLOCK_ALIGN
57 #define GUI_BLOCK_ALIGN 2 /* 2 means 4 bytes, 1 means 2 bytes */
58 /* 1 can be used on 16-bit CPUs and
59 CPUs which do not require aligned
60 32-bit values (such as x86) */
61 #endif
62
63 #ifndef GUI_MAXBLOCKS
64 #define GUI_MAXBLOCKS (2+GUI_ALLOC_SIZE/32)
65 #endif
66
67
68 /*
69 *****************************************************************
70 *
71 * Internal types and declarations
72 *
73 *****************************************************************
74 */
75 #if GUI_ALLOC_SIZE <32767
76 #define tALLOCINT I16
77 #else
#define tALLOCINT I32
#endif
80
81 #if GUI_MAXBLOCKS >= 256
#define HANDLE U16
#else
84 #define HANDLE U8
85 #endif
86
87
88
89
90 typedef struct {
91 tALLOCINT Off; /* Offset of memory area */
92 tALLOCINT Size; /* usable size of allocated block */
93 HANDLE Next; /* next handle in linked list */
94 HANDLE Prev;
95 } tBlock;
96
97 /****************************************************************
98 *
99 * Static data
100 *
101 *****************************************************************
102 */
103
104 GUI_HEAP GUI_Heap; /* Public for debugging only */
105 static tBlock aBlock[GUI_MAXBLOCKS];
106
107 struct {
108 int NumUsedBlocks, NumFreeBlocks, NumFreeBlocksMin; /* For statistical purposes only */
109 tALLOCINT NumUsedBytes, NumFreeBytes, NumFreeBytesMin;
110 } GUI_ALLOC;
111
112 static char IsInitialized =0;
113
114 /*
115 ********************************************************************
116 *
C51 COMPILER V8.05a GUIALLOC 04/11/2008 14:18:53 PAGE 3
117 * Macros for internal use
118 *
119 ********************************************************************
120 */
121
122 #define Min(v0,v1) ((v0>v1) ? v1 : v0)
123 #define Max(v0,v1) ((v0>v1) ? v0 : v1)
124 #define ASSIGN_IF_LESS(v0,v1) if (v1<v0) v0=v1
125 #define HMEM2PTR(hMem) (void*)&GUI_Heap.abHeap[aBlock[hMem].Off]
126
127
128 /*
129 ********************************************************************
130 *
131 * Internal routines
132 *
133 ********************************************************************
134 */
135
136
137 /*
138 *************************************************
139 *
140 * Size2LegalSize
141 *
142 *************************************************
143
144 returns: Legal allocation size
145 */
146
147 static int Size2LegalSize(int size) {
148 1 return (size + ((1<<GUI_BLOCK_ALIGN)-1)) & ~((1<<GUI_BLOCK_ALIGN)-1);
149 1 }
150
151 /*
152 *************************************************
153 *
154 * FindFreeHandle
155 *
156 *************************************************
157
158 returns: Free handle
159 */
160
161 static GUI_HMEM FindFreeHandle(void) {
162 1 int i;
163 1 for (i=1; i< GUI_MAXBLOCKS; i++) {
164 2 if (aBlock[i].Size ==0)
165 2 return i;
166 2 }
167 1 GUI_DEBUG_ERROROUT1("Insufficient memory handles configured (GUI_MAXBLOCKS == %d (See GUIConf.h))", GUI_
-MAXBLOCKS);
168 1 return GUI_HMEM_NULL;
169 1 }
170
171
172 /*
173 *************************************************
174 *
175 * Find hole in heap area
176 *
177 *************************************************
C51 COMPILER V8.05a GUIALLOC 04/11/2008 14:18:53 PAGE 4
178
179 returns: Offset to the memory hole (if available)
180 -1 if not available
181 */
182 static GUI_HMEM FindHole(int Size) {
183 1 int i, iNext;
184 1 for (i=0; (iNext = aBlock[i].Next) != 0; i = iNext) {
185 2 int NumFreeBytes = aBlock[iNext].Off- (aBlock[i].Off+aBlock[i].Size);
186 2 if (NumFreeBytes>=Size)
187 2 return i;
188 2 }
189 1 /* Check last block */
190 1 if (GUI_ALLOC_SIZE - (aBlock[i].Off+aBlock[i].Size) >= Size)
191 1 return i;
192 1 return -1;
193 1 }
194
195 /*
196 *************************************************
197 *
198 * Create hole in heap area
199 *
200 *************************************************
201
202 returns: Offset to the memory hole (if available)
203 -1 if not available
204 */
205 static GUI_HMEM CreateHole(int Size) {
206 1 int i, iNext;
207 1 int r = -1;
208 1 for (i=0; (iNext =aBlock[i].Next) !=0; i= iNext) {
209 2 int NumFreeBytes = aBlock[iNext].Off- (aBlock[i].Off+aBlock[i].Size);
210 2 if (NumFreeBytes < Size) {
211 3 int NumBytesBeforeBlock = aBlock[iNext].Off - (aBlock[i].Off+aBlock[i].Size);
212 3 if (NumBytesBeforeBlock) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -