├── README.md └── cqy_malloc ├── Makefile ├── main.c ├── mem_malloc.c ├── mem_malloc.h └── test /README.md: -------------------------------------------------------------------------------- 1 | # mem_malloc 2 | 3 | $ cd cqy_malloc 4 | 5 | $ make 6 | 7 | $ ./test 8 | ``` 9 | ------------mem_info-------------- 10 | sizeof(mem_block)=12 11 | MEM_START = 134525056(0x804b080) 12 | MEM_END = 134525184(0x804b100) 13 | MEM_SIZE = 128(0x80) 14 | ---------------------------------- 15 | ------test_malloc------- 16 | p = 0x804b0f6, i=1, id=1, size=10 17 | f6 b0 04 08 0a 00 00 00 01 00 00 00 18 | 00 00 00 00 00 00 00 00 00 00 00 00 19 | 00 00 00 00 00 00 00 00 00 00 00 00 20 | 00 00 00 00 00 00 00 00 00 00 00 00 21 | 00 00 00 00 00 00 00 00 00 00 00 00 22 | 00 00 00 00 00 00 00 00 00 00 00 00 23 | 00 00 00 00 00 00 00 00 00 00 00 00 24 | 00 00 00 00 00 00 00 00 00 00 00 00 25 | 00 00 00 00 00 00 00 00 00 00 00 00 26 | 00 00 00 00 00 00 00 00 00 00 01 01 27 | 01 01 01 01 01 01 01 01 28 | ------test_malloc------- 29 | p = 0x804b0ee, i=2, id=2, size=8 30 | f6 b0 04 08 0a 00 00 00 01 00 00 00 31 | ee b0 04 08 08 00 00 00 02 00 00 00 32 | 00 00 00 00 00 00 00 00 00 00 00 00 33 | 00 00 00 00 00 00 00 00 00 00 00 00 34 | 00 00 00 00 00 00 00 00 00 00 00 00 35 | 00 00 00 00 00 00 00 00 00 00 00 00 36 | 00 00 00 00 00 00 00 00 00 00 00 00 37 | 00 00 00 00 00 00 00 00 00 00 00 00 38 | 00 00 00 00 00 00 00 00 00 00 00 00 39 | 00 00 02 02 02 02 02 02 02 02 01 01 40 | 01 01 01 01 01 01 01 01 41 | ------test_malloc------- 42 | p = 0x804b0da, i=3, id=3, size=20 43 | f6 b0 04 08 0a 00 00 00 01 00 00 00 44 | ee b0 04 08 08 00 00 00 02 00 00 00 45 | da b0 04 08 14 00 00 00 03 00 00 00 46 | 00 00 00 00 00 00 00 00 00 00 00 00 47 | 00 00 00 00 00 00 00 00 00 00 00 00 48 | 00 00 00 00 00 00 00 00 00 00 00 00 49 | 00 00 00 00 00 00 00 00 00 00 00 00 50 | 00 00 00 00 00 00 03 03 03 03 03 03 51 | 03 03 03 03 03 03 03 03 03 03 03 03 52 | 03 03 02 02 02 02 02 02 02 02 01 01 53 | 01 01 01 01 01 01 01 01 54 | ------test_free------- 55 | i=2, id = 2 56 | f6 b0 04 08 0a 00 00 00 01 00 00 00 57 | e2 b0 04 08 14 00 00 00 03 00 00 00 58 | 00 00 00 00 00 00 00 00 00 00 00 00 59 | 00 00 00 00 00 00 00 00 00 00 00 00 60 | 00 00 00 00 00 00 00 00 00 00 00 00 61 | 00 00 00 00 00 00 00 00 00 00 00 00 62 | 00 00 00 00 00 00 00 00 00 00 00 00 63 | 00 00 00 00 00 00 00 00 00 00 00 00 64 | 00 00 03 03 03 03 03 03 03 03 03 03 65 | 03 03 03 03 03 03 03 03 03 03 01 01 66 | 01 01 01 01 01 01 01 01 67 | ------test_malloc------- 68 | p = 0x804b0d8, i=4, id=4, size=10 69 | f6 b0 04 08 0a 00 00 00 01 00 00 00 70 | e2 b0 04 08 14 00 00 00 03 00 00 00 71 | d8 b0 04 08 0a 00 00 00 04 00 00 00 72 | 00 00 00 00 00 00 00 00 00 00 00 00 73 | 00 00 00 00 00 00 00 00 00 00 00 00 74 | 00 00 00 00 00 00 00 00 00 00 00 00 75 | 00 00 00 00 00 00 00 00 00 00 00 00 76 | 00 00 00 00 04 04 04 04 04 04 04 04 77 | 04 04 03 03 03 03 03 03 03 03 03 03 78 | 03 03 03 03 03 03 03 03 03 03 01 01 79 | 01 01 01 01 01 01 01 01 80 | ------test_free------- 81 | i=1, id = 1 82 | ec b0 04 08 14 00 00 00 03 00 00 00 83 | e2 b0 04 08 0a 00 00 00 04 00 00 00 84 | 00 00 00 00 00 00 00 00 00 00 00 00 85 | 00 00 00 00 00 00 00 00 00 00 00 00 86 | 00 00 00 00 00 00 00 00 00 00 00 00 87 | 00 00 00 00 00 00 00 00 00 00 00 00 88 | 00 00 00 00 00 00 00 00 00 00 00 00 89 | 00 00 00 00 00 00 00 00 00 00 00 00 90 | 00 00 04 04 04 04 04 04 04 04 04 04 91 | 03 03 03 03 03 03 03 03 03 03 03 03 92 | 03 03 03 03 03 03 03 03 93 | ------test_malloc------- 94 | p = 0x804b0ce, i=5, id=5, size=20 95 | ec b0 04 08 14 00 00 00 03 00 00 00 96 | e2 b0 04 08 0a 00 00 00 04 00 00 00 97 | ce b0 04 08 14 00 00 00 05 00 00 00 98 | 00 00 00 00 00 00 00 00 00 00 00 00 99 | 00 00 00 00 00 00 00 00 00 00 00 00 100 | 00 00 00 00 00 00 00 00 00 00 00 00 101 | 00 00 00 00 00 00 05 05 05 05 05 05 102 | 05 05 05 05 05 05 05 05 05 05 05 05 103 | 05 05 04 04 04 04 04 04 04 04 04 04 104 | 03 03 03 03 03 03 03 03 03 03 03 03 105 | 03 03 03 03 03 03 03 03 106 | ------test_malloc------- 107 | p = 0x804b0c4, i=6, id=6, size=10 108 | ec b0 04 08 14 00 00 00 03 00 00 00 109 | e2 b0 04 08 0a 00 00 00 04 00 00 00 110 | ce b0 04 08 14 00 00 00 05 00 00 00 111 | c4 b0 04 08 0a 00 00 00 06 00 00 00 112 | 00 00 00 00 00 00 00 00 00 00 00 00 113 | 00 00 00 00 00 00 00 00 06 06 06 06 114 | 06 06 06 06 06 06 05 05 05 05 05 05 115 | 05 05 05 05 05 05 05 05 05 05 05 05 116 | 05 05 04 04 04 04 04 04 04 04 04 04 117 | 03 03 03 03 03 03 03 03 03 03 03 03 118 | 03 03 03 03 03 03 03 03 119 | ------test_malloc------- 120 | malloc --- fail 121 | size=10 122 | ec b0 04 08 14 00 00 00 03 00 00 00 123 | e2 b0 04 08 0a 00 00 00 04 00 00 00 124 | ce b0 04 08 14 00 00 00 05 00 00 00 125 | c4 b0 04 08 0a 00 00 00 06 00 00 00 126 | 00 00 00 00 00 00 00 00 00 00 00 00 127 | 00 00 00 00 00 00 00 00 06 06 06 06 128 | 06 06 06 06 06 06 05 05 05 05 05 05 129 | 05 05 05 05 05 05 05 05 05 05 05 05 130 | 05 05 04 04 04 04 04 04 04 04 04 04 131 | 03 03 03 03 03 03 03 03 03 03 03 03 132 | 03 03 03 03 03 03 03 03 133 | ------test_free------- 134 | i=6, id = 6 135 | ec b0 04 08 14 00 00 00 03 00 00 00 136 | e2 b0 04 08 0a 00 00 00 04 00 00 00 137 | ce b0 04 08 14 00 00 00 05 00 00 00 138 | 00 00 00 00 00 00 00 00 00 00 00 00 139 | 00 00 00 00 00 00 00 00 00 00 00 00 140 | 00 00 00 00 00 00 00 00 00 00 00 00 141 | 00 00 00 00 00 00 05 05 05 05 05 05 142 | 05 05 05 05 05 05 05 05 05 05 05 05 143 | 05 05 04 04 04 04 04 04 04 04 04 04 144 | 03 03 03 03 03 03 03 03 03 03 03 03 145 | 03 03 03 03 03 03 03 03 146 | ------test_malloc------- 147 | p = 0x804b0c1, i=8, id=6, size=13 148 | ec b0 04 08 14 00 00 00 03 00 00 00 149 | e2 b0 04 08 0a 00 00 00 04 00 00 00 150 | ce b0 04 08 14 00 00 00 05 00 00 00 151 | c1 b0 04 08 0d 00 00 00 06 00 00 00 152 | 00 00 00 00 00 00 00 00 00 00 00 00 153 | 00 00 00 00 00 08 08 08 08 08 08 08 154 | 08 08 08 08 08 08 05 05 05 05 05 05 155 | 05 05 05 05 05 05 05 05 05 05 05 05 156 | 05 05 04 04 04 04 04 04 04 04 04 04 157 | 03 03 03 03 03 03 03 03 03 03 03 03 158 | 03 03 03 03 03 03 03 03 159 | ------test_buffer------- 160 | i=5, id = 5, size=20 161 | ec b0 04 08 14 00 00 00 03 00 00 00 162 | e2 b0 04 08 0a 00 00 00 04 00 00 00 163 | ce b0 04 08 14 00 00 00 05 00 00 00 164 | c1 b0 04 08 0d 00 00 00 06 00 00 00 165 | 00 00 00 00 00 00 00 00 00 00 00 00 166 | 00 00 00 00 00 08 08 08 08 08 08 08 167 | 08 08 08 08 08 08 f5 f5 f5 f5 f5 f5 168 | f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 169 | f5 f5 04 04 04 04 04 04 04 04 04 04 170 | 03 03 03 03 03 03 03 03 03 03 03 03 171 | 03 03 03 03 03 03 03 03 172 | ------test_free------- 173 | i=4, id = 4 174 | ec b0 04 08 14 00 00 00 03 00 00 00 175 | d8 b0 04 08 14 00 00 00 05 00 00 00 176 | cb b0 04 08 0d 00 00 00 06 00 00 00 177 | 00 00 00 00 00 00 00 00 00 00 00 00 178 | 00 00 00 00 00 00 00 00 00 00 00 00 179 | 00 00 00 00 00 00 00 00 00 00 00 00 180 | 00 00 00 08 08 08 08 08 08 08 08 08 181 | 08 08 08 08 f5 f5 f5 f5 f5 f5 f5 f5 182 | f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 183 | 03 03 03 03 03 03 03 03 03 03 03 03 184 | 03 03 03 03 03 03 03 03 185 | ------test_buffer------- 186 | i=3, id = 3, size=20 187 | ec b0 04 08 14 00 00 00 03 00 00 00 188 | d8 b0 04 08 14 00 00 00 05 00 00 00 189 | cb b0 04 08 0d 00 00 00 06 00 00 00 190 | 00 00 00 00 00 00 00 00 00 00 00 00 191 | 00 00 00 00 00 00 00 00 00 00 00 00 192 | 00 00 00 00 00 00 00 00 00 00 00 00 193 | 00 00 00 08 08 08 08 08 08 08 08 08 194 | 08 08 08 08 f5 f5 f5 f5 f5 f5 f5 f5 195 | f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 196 | f3 f3 f3 f3 f3 f3 f3 f3 f3 f3 f3 f3 197 | f3 f3 f3 f3 f3 f3 f3 f3 198 | ------test_malloc------- 199 | p = 0x804b0bc, i=9, id=7, size=15 200 | ec b0 04 08 14 00 00 00 03 00 00 00 201 | d8 b0 04 08 14 00 00 00 05 00 00 00 202 | cb b0 04 08 0d 00 00 00 06 00 00 00 203 | bc b0 04 08 0f 00 00 00 07 00 00 00 204 | 00 00 00 00 00 00 00 00 00 00 00 00 205 | 09 09 09 09 09 09 09 09 09 09 09 09 206 | 09 09 09 08 08 08 08 08 08 08 08 08 207 | 08 08 08 08 f5 f5 f5 f5 f5 f5 f5 f5 208 | f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 209 | f3 f3 f3 f3 f3 f3 f3 f3 f3 f3 f3 f3 210 | f3 f3 f3 f3 f3 f3 f3 f3 211 | ------test_malloc------- 212 | malloc --- fail 213 | size=15 214 | ec b0 04 08 14 00 00 00 03 00 00 00 215 | d8 b0 04 08 14 00 00 00 05 00 00 00 216 | cb b0 04 08 0d 00 00 00 06 00 00 00 217 | bc b0 04 08 0f 00 00 00 07 00 00 00 218 | 00 00 00 00 00 00 00 00 00 00 00 00 219 | 09 09 09 09 09 09 09 09 09 09 09 09 220 | 09 09 09 08 08 08 08 08 08 08 08 08 221 | 08 08 08 08 f5 f5 f5 f5 f5 f5 f5 f5 222 | f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 223 | f3 f3 f3 f3 f3 f3 f3 f3 f3 f3 f3 f3 224 | f3 f3 f3 f3 f3 f3 f3 f3 225 | ------test_realloc------- 226 | i=3, id = 3, size=10 227 | f6 b0 04 08 0a 00 00 00 03 00 00 00 228 | e2 b0 04 08 14 00 00 00 05 00 00 00 229 | d5 b0 04 08 0d 00 00 00 06 00 00 00 230 | c6 b0 04 08 0f 00 00 00 07 00 00 00 231 | 00 00 00 00 00 00 00 00 00 00 00 00 232 | 00 00 00 00 00 00 00 00 00 00 09 09 233 | 09 09 09 09 09 09 09 09 09 09 09 09 234 | 09 08 08 08 08 08 08 08 08 08 08 08 235 | 08 08 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 236 | f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 a3 a3 237 | a3 a3 a3 a3 a3 a3 a3 a3 238 | ------test_realloc------- 239 | i=8, id = 6, size=15 240 | -----ok----- 241 | f6 b0 04 08 0a 00 00 00 03 00 00 00 242 | e2 b0 04 08 14 00 00 00 05 00 00 00 243 | d3 b0 04 08 0f 00 00 00 06 00 00 00 244 | c4 b0 04 08 0f 00 00 00 07 00 00 00 245 | 00 00 00 00 00 00 00 00 00 00 00 00 246 | 00 00 00 00 00 00 00 00 09 09 09 09 247 | 09 09 09 09 09 09 09 09 09 09 09 a8 248 | a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 249 | a8 a8 f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 250 | f5 f5 f5 f5 f5 f5 f5 f5 f5 f5 a3 a3 251 | a3 a3 a3 a3 a3 a3 a3 a3 252 | ------test_realloc------- 253 | i=5, id = 5, size=30 254 | -----ok----- 255 | f6 b0 04 08 0a 00 00 00 03 00 00 00 256 | d8 b0 04 08 1e 00 00 00 05 00 00 00 257 | c9 b0 04 08 0f 00 00 00 06 00 00 00 258 | ba b0 04 08 0f 00 00 00 07 00 00 00 259 | 00 00 00 00 00 00 00 00 00 00 09 09 260 | 09 09 09 09 09 09 09 09 09 09 09 09 261 | 09 a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 262 | a8 a8 a8 a8 a5 a5 a5 a5 a5 a5 a5 a5 263 | a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 264 | a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a3 a3 265 | a3 a3 a3 a3 a3 a3 a3 a3 266 | ------test_realloc------- 267 | i=8, id = 6, size=20 268 | -----ok----- 269 | f6 b0 04 08 0a 00 00 00 03 00 00 00 270 | d8 b0 04 08 1e 00 00 00 05 00 00 00 271 | c4 b0 04 08 14 00 00 00 06 00 00 00 272 | b5 b0 04 08 0f 00 00 00 07 00 00 00 273 | 00 00 00 00 00 09 09 09 09 09 09 09 274 | 09 09 09 09 09 09 09 09 a8 a8 a8 a8 275 | a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 276 | a8 a8 a8 a8 a5 a5 a5 a5 a5 a5 a5 a5 277 | a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 278 | a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a3 a3 279 | a3 a3 a3 a3 a3 a3 a3 a3 280 | ------test_realloc------- 281 | i=9, id = 7, size=20 282 | -----ok----- 283 | f6 b0 04 08 0a 00 00 00 03 00 00 00 284 | d8 b0 04 08 1e 00 00 00 05 00 00 00 285 | c4 b0 04 08 14 00 00 00 06 00 00 00 286 | b0 b0 04 08 14 00 00 00 07 00 00 00 287 | a9 a9 a9 a9 a9 a9 a9 a9 a9 a9 a9 a9 288 | a9 a9 a9 a9 a9 a9 a9 a9 a8 a8 a8 a8 289 | a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 290 | a8 a8 a8 a8 a5 a5 a5 a5 a5 a5 a5 a5 291 | a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 292 | a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a3 a3 293 | a3 a3 a3 a3 a3 a3 a3 a3 294 | ------test_realloc------- 295 | i=9, id = 7, size=13 296 | f6 b0 04 08 0a 00 00 00 03 00 00 00 297 | d8 b0 04 08 1e 00 00 00 05 00 00 00 298 | c4 b0 04 08 14 00 00 00 06 00 00 00 299 | b7 b0 04 08 0d 00 00 00 07 00 00 00 300 | 00 00 00 00 00 00 00 a9 a9 a9 a9 a9 301 | a9 a9 a9 a9 a9 a9 a9 a9 a8 a8 a8 a8 302 | a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 303 | a8 a8 a8 a8 a5 a5 a5 a5 a5 a5 a5 a5 304 | a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 305 | a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a3 a3 306 | a3 a3 a3 a3 a3 a3 a3 a3 307 | ------test_free------- 308 | i=0, id = 0 309 | ------test_free------- 310 | i=1, id = 1 311 | ------test_free------- 312 | i=2, id = 2 313 | ------test_free------- 314 | i=3, id = 3 315 | e2 b0 04 08 1e 00 00 00 05 00 00 00 316 | ce b0 04 08 14 00 00 00 06 00 00 00 317 | c1 b0 04 08 0d 00 00 00 07 00 00 00 318 | 00 00 00 00 00 00 00 00 00 00 00 00 319 | 00 00 00 00 00 00 00 00 00 00 00 00 320 | 00 00 00 00 00 a9 a9 a9 a9 a9 a9 a9 321 | a9 a9 a9 a9 a9 a9 a8 a8 a8 a8 a8 a8 322 | a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 323 | a8 a8 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 324 | a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 a5 325 | a5 a5 a5 a5 a5 a5 a5 a5 326 | ------test_free------- 327 | i=4, id = 4 328 | ------test_free------- 329 | i=5, id = 5 330 | ec b0 04 08 14 00 00 00 06 00 00 00 331 | df b0 04 08 0d 00 00 00 07 00 00 00 332 | 00 00 00 00 00 00 00 00 00 00 00 00 333 | 00 00 00 00 00 00 00 00 00 00 00 00 334 | 00 00 00 00 00 00 00 00 00 00 00 00 335 | 00 00 00 00 00 00 00 00 00 00 00 00 336 | 00 00 00 00 00 00 00 00 00 00 00 00 337 | 00 00 00 00 00 00 00 00 00 00 00 a9 338 | a9 a9 a9 a9 a9 a9 a9 a9 a9 a9 a9 a9 339 | a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 a8 340 | a8 a8 a8 a8 a8 a8 a8 a8 341 | ------test_free------- 342 | i=6, id = 6 343 | f3 b0 04 08 0d 00 00 00 07 00 00 00 344 | 00 00 00 00 00 00 00 00 00 00 00 00 345 | 00 00 00 00 00 00 00 00 00 00 00 00 346 | 00 00 00 00 00 00 00 00 00 00 00 00 347 | 00 00 00 00 00 00 00 00 00 00 00 00 348 | 00 00 00 00 00 00 00 00 00 00 00 00 349 | 00 00 00 00 00 00 00 00 00 00 00 00 350 | 00 00 00 00 00 00 00 00 00 00 00 00 351 | 00 00 00 00 00 00 00 00 00 00 00 00 352 | 00 00 00 00 00 00 00 a9 a9 a9 a9 a9 353 | a9 a9 a9 a9 a9 a9 a9 a9 354 | ------test_free------- 355 | i=7, id = 0 356 | ------test_free------- 357 | i=8, id = 6 358 | ------test_free------- 359 | i=9, id = 7 360 | 00 00 00 00 00 00 00 00 00 00 00 00 361 | 00 00 00 00 00 00 00 00 00 00 00 00 362 | 00 00 00 00 00 00 00 00 00 00 00 00 363 | 00 00 00 00 00 00 00 00 00 00 00 00 364 | 00 00 00 00 00 00 00 00 00 00 00 00 365 | 00 00 00 00 00 00 00 00 00 00 00 00 366 | 00 00 00 00 00 00 00 00 00 00 00 00 367 | 00 00 00 00 00 00 00 00 00 00 00 00 368 | 00 00 00 00 00 00 00 00 00 00 00 00 369 | 00 00 00 00 00 00 00 00 00 00 00 00 370 | 00 00 00 00 00 00 00 00 371 | ``` 372 | -------------------------------------------------------------------------------- /cqy_malloc/Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | gcc -o test main.c mem_malloc.c -I. 3 | clean: 4 | rm test 5 | -------------------------------------------------------------------------------- /cqy_malloc/main.c: -------------------------------------------------------------------------------- 1 | 2 | #include "mem_malloc.h" 3 | 4 | char mem_id[10]={0}; 5 | 6 | void test_malloc(int i, int size){ 7 | printf("------test_malloc-------\n"); 8 | mem_id[i] = mem_malloc(size); 9 | if(mem_id[i] == 0){ 10 | printf("malloc --- fail\n"); 11 | printf("size=%d\n", size); 12 | }else{ 13 | char *p = mem_buffer(mem_id[i]); 14 | memset(p, i, size); 15 | printf("p = 0x%x, i=%d, id=%d, size=%d\n", (int)p, i, mem_id[i], size); 16 | } 17 | print_mem_hex(MEM_SIZE); 18 | } 19 | 20 | void test_buffer(int i, int size){ 21 | printf("------test_buffer-------\n"); 22 | printf("i=%d, id = %d, size=%d\n", i, mem_id[i], size); 23 | char *p = mem_buffer(mem_id[i]); 24 | if(p != NULL){ 25 | memset(p, 0xf0+i, size); 26 | print_mem_hex(MEM_SIZE); 27 | }else{ 28 | printf("test_buffer---fail\n"); 29 | } 30 | } 31 | 32 | void test_realloc(int i, int size){ 33 | printf("------test_realloc-------\n"); 34 | printf("i=%d, id = %d, size=%d\n", i, mem_id[i], size); 35 | int ret = mem_realloc(mem_id[i], size); 36 | if(ret){ 37 | char *p = mem_buffer(mem_id[i]); 38 | memset(p, 0xa0+i, size); 39 | print_mem_hex(MEM_SIZE); 40 | }else{ 41 | printf("test_realloc---fail\n"); 42 | } 43 | } 44 | 45 | void test_free(int i){ 46 | printf("------test_free-------\n"); 47 | printf("i=%d, id = %d\n", i, mem_id[i]); 48 | if(mem_free(mem_id[i])) 49 | print_mem_hex( MEM_SIZE); 50 | } 51 | 52 | void main(void){ 53 | 54 | print_mem_info(); 55 | test_malloc(1, 10); 56 | test_malloc(2, 8); 57 | test_malloc(3, 20); 58 | 59 | test_free(2); 60 | 61 | test_malloc(4, 10); 62 | 63 | test_free(1); 64 | 65 | test_malloc(5, 20); 66 | test_malloc(6, 10); 67 | test_malloc(7, 10); 68 | 69 | test_free(6); 70 | 71 | test_malloc(8, 13); 72 | 73 | test_buffer(5, 20); 74 | 75 | test_free(4); 76 | 77 | test_buffer(3, 20); 78 | 79 | test_malloc(9, 15); 80 | test_malloc(10, 15); 81 | 82 | test_realloc(3, 10); 83 | test_realloc(8, 15); 84 | test_realloc(5, 30); 85 | test_realloc(8, 20); 86 | test_realloc(9, 20); 87 | test_realloc(9, 13); 88 | 89 | for(int i=0; i<10; i++) 90 | test_free(i); 91 | } 92 | 93 | -------------------------------------------------------------------------------- /cqy_malloc/mem_malloc.c: -------------------------------------------------------------------------------- 1 | 2 | #include "mem_malloc.h" 3 | 4 | static unsigned int sum = 0; 5 | static char mem[MEM_SIZE]; 6 | 7 | #define DEBUG_EN 0 8 | #define MEM_START &mem[0] 9 | #define MEM_END &mem[MEM_SIZE] 10 | #define BLK_SIZE sizeof(mem_block) 11 | 12 | void print_mem_info(void){ 13 | printf("------------mem_info--------------\n"); 14 | printf("sizeof(mem_block)=%d\n", BLK_SIZE); 15 | printf("MEM_START = %d(0x%x)\n", (int)MEM_START, (int)MEM_START); 16 | printf("MEM_END = %d(0x%x)\n", (int)MEM_END, (int)MEM_END); 17 | printf("MEM_SIZE = %d(0x%x)\n", (int)MEM_SIZE, (int)MEM_SIZE); 18 | printf("----------------------------------\n"); 19 | } 20 | 21 | void print_hex(char *data, int len){ 22 | for(int i=0; imem_ptr-(MEM_START + BLK_SIZE*sum); 40 | if(all_size <= free_blk){ 41 | tmp_blk.mem_ptr = ptr_blk->mem_ptr - msize; 42 | tmp_blk.mem_size = msize; 43 | tmp_blk.mem_index = ptr_blk->mem_index + 1; 44 | memcpy(MEM_START + BLK_SIZE*sum, &tmp_blk, BLK_SIZE); 45 | sum = sum + 1; 46 | #if DEBUG_EN 47 | printf("mem_ptr = 0x%x\n", (int)tmp_blk.mem_ptr); 48 | printf("mem_size = 0x%x\n", tmp_blk.mem_size); 49 | printf("mem_index = 0x%x\n", tmp_blk.mem_index); 50 | #endif 51 | return tmp_blk.mem_index; 52 | } 53 | }else{ 54 | if(all_size <= MEM_SIZE){ 55 | tmp_blk.mem_ptr = MEM_END - msize; 56 | tmp_blk.mem_size = msize; 57 | tmp_blk.mem_index = 1; 58 | memcpy(MEM_START, &tmp_blk, BLK_SIZE); 59 | sum = 1; 60 | #if DEBUG_EN 61 | printf("mem_ptr = 0x%x\n", (int)tmp_blk.mem_ptr); 62 | printf("mem_size = 0x%x\n", tmp_blk.mem_size); 63 | printf("mem_index = 0x%x\n", tmp_blk.mem_index); 64 | #endif 65 | return 1; 66 | } 67 | } 68 | return 0; 69 | } 70 | 71 | int mem_realloc(int id, unsigned int msize){ 72 | for(int i=0; imem_index){ 75 | int free_blk = (char *)ptr_blk->mem_ptr-(MEM_START + BLK_SIZE*sum); 76 | int old_size = ptr_blk->mem_size; 77 | int offset = msize - old_size; 78 | int move_size = 0; 79 | int n = sum - i; 80 | mem_block *ptr_tmp; 81 | if(offset == 0){ 82 | return 0; 83 | }else if(offset < 0){ 84 | offset = old_size - msize; 85 | for(int j=1; jmem_size; 88 | } 89 | if(n == 1){ 90 | ptr_tmp = (mem_block *)(MEM_START + BLK_SIZE*i); 91 | } 92 | move_size += msize; 93 | char *dst_addr = ptr_tmp->mem_ptr + move_size + offset - 1; 94 | char *src_addr = ptr_tmp->mem_ptr + move_size - 1; 95 | for(int j=move_size; j>0; j--){ 96 | *dst_addr-- = *src_addr--; 97 | } 98 | memset(src_addr, 0, offset+1); 99 | for(int j=0; jmem_ptr += offset; 102 | if(j == 0){ 103 | ptr_tmp->mem_size = msize; 104 | } 105 | } 106 | return 1; 107 | }else{ 108 | if(offset <= free_blk){ 109 | for(int j=1; jmem_size; 112 | } 113 | if(n == 1){ 114 | ptr_tmp = (mem_block *)(MEM_START + BLK_SIZE*i); 115 | } 116 | move_size += old_size; 117 | char *dst_addr = ptr_tmp->mem_ptr - offset; 118 | char *src_addr = ptr_tmp->mem_ptr; 119 | for(int j=0; jmem_ptr -= offset; 125 | if(j == 0){ 126 | ptr_tmp->mem_size = msize; 127 | } 128 | } 129 | return 1; 130 | } 131 | } 132 | } 133 | } 134 | return 0; 135 | } 136 | 137 | void *mem_buffer(int id){ 138 | for(int i=0; imem_index){ 141 | return ptr_blk->mem_ptr; 142 | } 143 | } 144 | return NULL; 145 | } 146 | 147 | int mem_free(int id){ 148 | for(int i=0; imem_index){ 151 | mem_block *ptr_old; 152 | if(i != (sum-1)){ 153 | int offset = ptr_blk->mem_size; 154 | int move_size = 0; 155 | int n = sum - i; 156 | mem_block *ptr_tmp; 157 | for(int j=1; jmem_size; 160 | } 161 | //memmove(); 162 | char *dst_addr = ptr_tmp->mem_ptr + move_size + offset - 1; 163 | char *src_addr = ptr_tmp->mem_ptr + move_size - 1; 164 | for(int j=move_size; j>0; j--){ 165 | *dst_addr-- = *src_addr--; 166 | } 167 | memset(src_addr, 0, offset+1); 168 | for(int j=0; j<(n-1); j++){ 169 | ptr_tmp = (mem_block *)(MEM_START + BLK_SIZE*(i+j)); 170 | ptr_old = (mem_block *)(MEM_START + BLK_SIZE*(i+j+1)); 171 | memcpy(ptr_tmp, ptr_old, BLK_SIZE); 172 | ptr_tmp->mem_ptr += offset; 173 | } 174 | }else{ 175 | ptr_old = (mem_block *)(MEM_START + BLK_SIZE*i); 176 | memset(ptr_old->mem_ptr, 0, ptr_old->mem_size); 177 | } 178 | memset(ptr_old, 0, BLK_SIZE); 179 | sum = sum - 1; 180 | return 1; 181 | } 182 | } 183 | return 0; 184 | } 185 | 186 | -------------------------------------------------------------------------------- /cqy_malloc/mem_malloc.h: -------------------------------------------------------------------------------- 1 | #ifndef __MEM_MALLOC_H__ 2 | #define __MEM_MALLOC_H__ 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #pragma pack(1) 15 | typedef struct mem_block{ 16 | void *mem_ptr; 17 | unsigned int mem_size; 18 | unsigned int mem_index; 19 | }mem_block; 20 | #pragma pack() 21 | 22 | #define MEM_SIZE 128 23 | 24 | 25 | void print_mem_info(void); 26 | void print_hex(char *data, int len); 27 | void print_mem_hex(int size); 28 | int mem_malloc(unsigned int msize); 29 | int mem_realloc(int id, unsigned int msize); 30 | void *mem_buffer(int id); 31 | int mem_free(int id); 32 | 33 | 34 | #ifdef __cplusplus 35 | } 36 | #endif 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /cqy_malloc/test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chenqy2018/mem_malloc/cfedc88d2ee54b4a3e3cd83f914994452c98ac50/cqy_malloc/test --------------------------------------------------------------------------------