| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | #include <bitmap.h> |
|---|
| 20 | #include <slab.h> |
|---|
| 21 | #include <module.h> |
|---|
| 22 | #include <error.h> |
|---|
| 23 | #include <i386/i386.h> |
|---|
| 24 | #include <imports.h> |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | LIST_HEAD(cacheList); |
|---|
| 38 | |
|---|
| 39 | |
|---|
| 40 | #define SLAB_START 0xC0000000 |
|---|
| 41 | #define SLAB_END 0xD0000000 |
|---|
| 42 | |
|---|
| 43 | #define SLAB_TOTAL_BYTES(cache) \ |
|---|
| 44 | (((cache)->numPages*PAGE_SIZE) - (cache)->offset) |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | static struct Cache cacheCache={ |
|---|
| 48 | .list=LIST_HEAD_INIT(cacheCache.list), |
|---|
| 49 | .name="Cache Cache", |
|---|
| 50 | .size=sizeof(struct Cache), |
|---|
| 51 | .numPages = 1, |
|---|
| 52 | .numObjs = (PAGE_SIZE-sizeof(struct Slab))/sizeof(struct Cache), |
|---|
| 53 | .offset = sizeof(struct Slab), |
|---|
| 54 | }; |
|---|
| 55 | |
|---|
| 56 | struct CacheSize |
|---|
| 57 | { |
|---|
| 58 | int size; |
|---|
| 59 | struct Cache* cache; |
|---|
| 60 | }; |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | static struct CacheSize cacheSizes[]={ |
|---|
| 65 | {16,NULL}, |
|---|
| 66 | {32,NULL}, |
|---|
| 67 | {64,NULL}, |
|---|
| 68 | {128,NULL}, |
|---|
| 69 | {256,NULL}, |
|---|
| 70 | {512,NULL}, |
|---|
| 71 | {1024,NULL}, |
|---|
| 72 | {2048,NULL}, |
|---|
| 73 | {4096,NULL}, |
|---|
| 74 | {8192,NULL}, |
|---|
| 75 | {16384,NULL}, |
|---|
| 76 | {32768,NULL}, |
|---|
| 77 | {65536,NULL}, |
|---|
| 78 | {131072,NULL}, |
|---|
| 79 | }; |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | struct Cache* slabCache; |
|---|
| 83 | |
|---|
| 84 | void* malloc(size_t size) |
|---|
| 85 | { |
|---|
| 86 | |
|---|
| 87 | DWORD flags; |
|---|
| 88 | int i; |
|---|
| 89 | void* obj; |
|---|
| 90 | |
|---|
| 91 | IrqSaveFlags(flags); |
|---|
| 92 | |
|---|
| 93 | for (i=0; i<count(cacheSizes); i++) |
|---|
| 94 | if (size <= cacheSizes[i].size) |
|---|
| 95 | |
|---|
| 96 | break; |
|---|
| 97 | |
|---|
| 98 | if (i == count(cacheSizes)) |
|---|
| 99 | { |
|---|
| 100 | KePrint("malloc(%u) failed\n",size); |
|---|
| 101 | IrqRestoreFlags(flags); |
|---|
| 102 | return NULL; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | obj=MemCacheAlloc(cacheSizes[i].cache); |
|---|
| 108 | |
|---|
| 109 | IrqRestoreFlags(flags); |
|---|
| 110 | return obj; |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | SYMBOL_EXPORT(malloc); |
|---|
| 114 | |
|---|
| 115 | |
|---|
| 116 | |
|---|
| 117 | |
|---|
| 118 | |
|---|
| 119 | |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | |
|---|
| 129 | static void SlabFree(struct Cache* cache,struct Slab* slab,void* p) |
|---|
| 130 | { |
|---|
| 131 | |
|---|
| 132 | int i=((DWORD)p-(DWORD)(slab->page+cache->offset))/cache->size; |
|---|
| 133 | |
|---|
| 134 | |
|---|
| 135 | BmapSetBit(slab->bitmap,i,false); |
|---|
| 136 | slab->isFull=0; |
|---|
| 137 | |
|---|
| 138 | if (cache->ctor) |
|---|
| 139 | cache->ctor(p); |
|---|
| 140 | else |
|---|
| 141 | |
|---|
| 142 | ZeroMemory(p,cache->size); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | void free(void* p) |
|---|
| 147 | { |
|---|
| 148 | int i; |
|---|
| 149 | struct Cache* currCache; |
|---|
| 150 | DWORD flags; |
|---|
| 151 | |
|---|
| 152 | if (!p) |
|---|
| 153 | return; |
|---|
| 154 | |
|---|
| 155 | IrqSaveFlags(flags); |
|---|
| 156 | |
|---|
| 157 | for (i=0; i<count(cacheSizes); i++) |
|---|
| 158 | { |
|---|
| 159 | currCache=cacheSizes[i].cache; |
|---|
| 160 | if (!MemCacheFree(currCache,p)) |
|---|
| 161 | { |
|---|
| 162 | IrqRestoreFlags(flags); |
|---|
| 163 | return; |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | KePrint("free did not free %#X (from %#X)\n",p,__builtin_return_address(0)); |
|---|
| 169 | cli(); hlt(); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | SYMBOL_EXPORT(free); |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | |
|---|
| 181 | |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | static inline struct Slab* MemCacheAllocSlab(struct Cache* cache) |
|---|
| 187 | { |
|---|
| 188 | DWORD virtAddr; |
|---|
| 189 | struct Slab* slab; |
|---|
| 190 | |
|---|
| 191 | |
|---|
| 192 | if (cache->size > PAGE_SIZE) |
|---|
| 193 | virtAddr=VirtMapPhysRange(SLAB_START,SLAB_END,PAGE_ALIGN_UP(cache->size) >> PAGE_SHIFT,PAGE_KERNEL | PAGE_RW | PAGE_PRESENT); |
|---|
| 194 | else |
|---|
| 195 | virtAddr=VirtMapPhysPage(SLAB_START,SLAB_END,PAGE_KERNEL | PAGE_RW | PAGE_PRESENT); |
|---|
| 196 | |
|---|
| 197 | |
|---|
| 198 | if (!virtAddr) |
|---|
| 199 | return NULL; |
|---|
| 200 | |
|---|
| 201 | if (cache->options & SLAB_OFFSLAB) |
|---|
| 202 | { |
|---|
| 203 | |
|---|
| 204 | slab=(struct Slab*)MemCacheAlloc(slabCache); |
|---|
| 205 | }else |
|---|
| 206 | slab=(struct Slab*)virtAddr; |
|---|
| 207 | |
|---|
| 208 | |
|---|
| 209 | ZeroMemory(slab,sizeof(struct Slab)); |
|---|
| 210 | slab->page=virtAddr; |
|---|
| 211 | |
|---|
| 212 | return slab; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | static inline void MemCacheConstructSlab(struct Cache* cache,struct Slab* slab) |
|---|
| 216 | { |
|---|
| 217 | int i; |
|---|
| 218 | void* obj; |
|---|
| 219 | |
|---|
| 220 | |
|---|
| 221 | if (cache->ctor) |
|---|
| 222 | { |
|---|
| 223 | for (i=0; i<cache->numObjs; i++) |
|---|
| 224 | { |
|---|
| 225 | obj=(void*)(slab->page+(i*cache->size)+cache->offset); |
|---|
| 226 | cache->ctor(obj); |
|---|
| 227 | } |
|---|
| 228 | }else |
|---|
| 229 | |
|---|
| 230 | ZeroMemory(slab->page+cache->offset,(cache->numPages*PAGE_SIZE)-cache->offset); |
|---|
| 231 | |
|---|
| 232 | if (!cache->slabs) |
|---|
| 233 | { |
|---|
| 234 | |
|---|
| 235 | INIT_LIST_HEAD(&slab->list); |
|---|
| 236 | cache->slabs=slab; |
|---|
| 237 | }else |
|---|
| 238 | |
|---|
| 239 | ListAdd(&slab->list,&cache->slabs->list); |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | |
|---|
| 246 | |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | |
|---|
| 250 | |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | |
|---|
| 254 | |
|---|
| 255 | |
|---|
| 256 | static int MemCacheAddSlab(struct Cache* cache) |
|---|
| 257 | { |
|---|
| 258 | struct Slab* slab; |
|---|
| 259 | |
|---|
| 260 | slab=MemCacheAllocSlab(cache); |
|---|
| 261 | |
|---|
| 262 | if (!slab) |
|---|
| 263 | return -ENOMEM; |
|---|
| 264 | |
|---|
| 265 | MemCacheConstructSlab(cache,slab); |
|---|
| 266 | return 0; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | void* MemCacheAlloc(struct Cache* cache) |
|---|
| 283 | { |
|---|
| 284 | DWORD flags; |
|---|
| 285 | struct Slab* currSlab; |
|---|
| 286 | int i; |
|---|
| 287 | |
|---|
| 288 | if (!cache) |
|---|
| 289 | return NULL; |
|---|
| 290 | |
|---|
| 291 | IrqSaveFlags(flags); |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | if (!cache->slabs) |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | MemCacheAddSlab(cache); |
|---|
| 298 | |
|---|
| 299 | currSlab=cache->slabs; |
|---|
| 300 | |
|---|
| 301 | |
|---|
| 302 | |
|---|
| 303 | while (currSlab) |
|---|
| 304 | { |
|---|
| 305 | if (!currSlab->isFull) |
|---|
| 306 | { |
|---|
| 307 | |
|---|
| 308 | for (i=0; i<cache->numObjs; i++) |
|---|
| 309 | { |
|---|
| 310 | if (!BmapGetBit(currSlab->bitmap,i)) |
|---|
| 311 | { |
|---|
| 312 | BmapSetBit(currSlab->bitmap,i,true); |
|---|
| 313 | if (i == cache->numObjs-1) |
|---|
| 314 | currSlab->isFull=1; |
|---|
| 315 | |
|---|
| 316 | IrqRestoreFlags(flags); |
|---|
| 317 | return (void*)(currSlab->page+cache->offset+(i*cache->size)); |
|---|
| 318 | } |
|---|
| 319 | } |
|---|
| 320 | } |
|---|
| 321 | |
|---|
| 322 | |
|---|
| 323 | if (currSlab->list.prev == &cache->slabs->list) |
|---|
| 324 | MemCacheAddSlab(cache); |
|---|
| 325 | |
|---|
| 326 | currSlab=ListEntry(currSlab->list.prev,struct Slab,list); |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | KePrint("MemCacheAlloc(%#X) - failed to allocate memory\n",cache); |
|---|
| 330 | IrqRestoreFlags(flags); |
|---|
| 331 | |
|---|
| 332 | return NULL; |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | SYMBOL_EXPORT(MemCacheAlloc); |
|---|
| 336 | |
|---|
| 337 | int MemCacheFree(struct Cache* cache,void* obj) |
|---|
| 338 | { |
|---|
| 339 | DWORD flags; |
|---|
| 340 | DWORD addr; |
|---|
| 341 | struct Slab* currSlab; |
|---|
| 342 | int ret=-EFAULT; |
|---|
| 343 | |
|---|
| 344 | if (!obj) |
|---|
| 345 | return -EFAULT; |
|---|
| 346 | |
|---|
| 347 | IrqSaveFlags(flags); |
|---|
| 348 | |
|---|
| 349 | addr=(DWORD)obj; |
|---|
| 350 | currSlab=cache->slabs; |
|---|
| 351 | |
|---|
| 352 | while (currSlab) |
|---|
| 353 | { |
|---|
| 354 | if (addr >= (currSlab->page+cache->offset) && addr < currSlab->page+(cache->numPages*PAGE_SIZE)) |
|---|
| 355 | { |
|---|
| 356 | |
|---|
| 357 | SlabFree(cache,currSlab,obj); |
|---|
| 358 | ret=0; |
|---|
| 359 | goto out; |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | |
|---|
| 363 | if (currSlab->list.prev == &cache->slabs->list) |
|---|
| 364 | goto out; |
|---|
| 365 | |
|---|
| 366 | currSlab=ListEntry(currSlab->list.prev,struct Slab,list); |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | out: |
|---|
| 370 | |
|---|
| 371 | IrqRestoreFlags(flags); |
|---|
| 372 | return ret; |
|---|
| 373 | } |
|---|
| 374 | |
|---|
| 375 | SYMBOL_EXPORT(MemCacheFree); |
|---|
| 376 | |
|---|
| 377 | struct Cache* MemCacheCreate(const char* name,size_t size,Constructor ctor,Destructor dtor,int options) |
|---|
| 378 | { |
|---|
| 379 | struct Cache* retVal; |
|---|
| 380 | |
|---|
| 381 | if (!size) |
|---|
| 382 | return NULL; |
|---|
| 383 | |
|---|
| 384 | if (size < SLAB_MIN_OBJECT_SIZE) |
|---|
| 385 | size=SLAB_MIN_OBJECT_SIZE; |
|---|
| 386 | |
|---|
| 387 | |
|---|
| 388 | retVal=(struct Cache*)MemCacheAlloc(&cacheCache); |
|---|
| 389 | |
|---|
| 390 | |
|---|
| 391 | size&=~0x3; |
|---|
| 392 | |
|---|
| 393 | retVal->size=size; |
|---|
| 394 | |
|---|
| 395 | if (size > PAGE_SIZE) |
|---|
| 396 | retVal->numPages=size >> PAGE_SHIFT; |
|---|
| 397 | else |
|---|
| 398 | retVal->numPages=1; |
|---|
| 399 | |
|---|
| 400 | retVal->name=name; |
|---|
| 401 | retVal->ctor=ctor; |
|---|
| 402 | retVal->dtor=dtor; |
|---|
| 403 | |
|---|
| 404 | if (size >= PAGE_SIZE/4) |
|---|
| 405 | options |= SLAB_OFFSLAB; |
|---|
| 406 | |
|---|
| 407 | retVal->offset=(options & SLAB_OFFSLAB) ? 0 : (sizeof(struct Slab)); |
|---|
| 408 | retVal->numObjs=SLAB_TOTAL_BYTES(retVal)/retVal->size; |
|---|
| 409 | retVal->options=options; |
|---|
| 410 | |
|---|
| 411 | return retVal; |
|---|
| 412 | } |
|---|
| 413 | |
|---|
| 414 | SYMBOL_EXPORT(MemCacheCreate); |
|---|
| 415 | |
|---|
| 416 | DWORD functions[]= |
|---|
| 417 | { |
|---|
| 418 | (DWORD)malloc, |
|---|
| 419 | (DWORD)free, |
|---|
| 420 | (DWORD)MemCacheCreate, |
|---|
| 421 | (DWORD)MemCacheAlloc, |
|---|
| 422 | (DWORD)MemCacheFree, |
|---|
| 423 | }; |
|---|
| 424 | |
|---|
| 425 | int SlabInit() |
|---|
| 426 | { |
|---|
| 427 | int i; |
|---|
| 428 | |
|---|
| 429 | |
|---|
| 430 | ListAdd(&cacheList, &cacheCache.list); |
|---|
| 431 | |
|---|
| 432 | |
|---|
| 433 | |
|---|
| 434 | |
|---|
| 435 | slabCache=MemCacheCreate("Slab cache",sizeof(struct Slab),NULL,NULL,0); |
|---|
| 436 | |
|---|
| 437 | for (i=0; i<count(cacheSizes); i++) |
|---|
| 438 | { |
|---|
| 439 | cacheSizes[i].cache=MemCacheCreate("malloc",cacheSizes[i].size,NULL,NULL,0); |
|---|
| 440 | if (!cacheSizes[i].cache) |
|---|
| 441 | { |
|---|
| 442 | KePrint("Malloc slab allocation failed!\n"); |
|---|
| 443 | return -ENOMEM; |
|---|
| 444 | } |
|---|
| 445 | } |
|---|
| 446 | |
|---|
| 447 | ResolveSetMemFunctions(functions); |
|---|
| 448 | |
|---|
| 449 | return 0; |
|---|
| 450 | } |
|---|
| 451 | |
|---|
| 452 | ModuleInit(SlabInit); |
|---|