Changeset 46c1234 in mainline for kernel/generic/src/mm/slab.c
- Timestamp:
- 2008-06-21T09:13:04Z (16 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 1e9d0e3
- Parents:
- 23cdc921
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/mm/slab.c
r23cdc921 r46c1234 168 168 * 169 169 */ 170 static slab_t * 170 static slab_t *slab_space_alloc(slab_cache_t *cache, int flags) 171 171 { 172 172 void *data; … … 180 180 return NULL; 181 181 } 182 if (! 182 if (!(cache->flags & SLAB_CACHE_SLINSIDE)) { 183 183 slab = slab_alloc(slab_extern_cache, flags); 184 184 if (!slab) { … … 201 201 202 202 for (i = 0; i < cache->objects; i++) 203 *((int *) (slab->start + i*cache->size)) = i +1;203 *((int *) (slab->start + i*cache->size)) = i + 1; 204 204 205 205 atomic_inc(&cache->allocated_slabs); … … 240 240 * @return Number of freed pages 241 241 */ 242 static count_t slab_obj_destroy(slab_cache_t *cache, void *obj, 243 slab_t *slab) 242 static count_t slab_obj_destroy(slab_cache_t *cache, void *obj, slab_t *slab) 244 243 { 245 244 int freed = 0; … … 257 256 258 257 *((int *)obj) = slab->nextavail; 259 slab->nextavail = (obj - slab->start) /cache->size;258 slab->nextavail = (obj - slab->start) / cache->size; 260 259 slab->available++; 261 260 … … 282 281 * @return Object address or null 283 282 */ 284 static void * 283 static void *slab_obj_create(slab_cache_t *cache, int flags) 285 284 { 286 285 slab_t *slab; … … 302 301 spinlock_lock(&cache->slablock); 303 302 } else { 304 slab = list_get_instance(cache->partial_slabs.next, slab_t, link); 303 slab = list_get_instance(cache->partial_slabs.next, slab_t, 304 link); 305 305 list_remove(&slab->link); 306 306 } … … 333 333 * @param first If true, return first, else last mag 334 334 */ 335 static slab_magazine_t * get_mag_from_cache(slab_cache_t *cache, 336 int first) 335 static slab_magazine_t *get_mag_from_cache(slab_cache_t *cache, int first) 337 336 { 338 337 slab_magazine_t *mag = NULL; … … 369 368 * @return Number of freed pages 370 369 */ 371 static count_t magazine_destroy(slab_cache_t *cache, 372 slab_magazine_t *mag) 370 static count_t magazine_destroy(slab_cache_t *cache, slab_magazine_t *mag) 373 371 { 374 372 unsigned int i; … … 390 388 * Assume cpu_magazine lock is held 391 389 */ 392 static slab_magazine_t * 390 static slab_magazine_t *get_full_current_mag(slab_cache_t *cache) 393 391 { 394 392 slab_magazine_t *cmag, *lastmag, *newmag; … … 424 422 * @return Pointer to object or NULL if not available 425 423 */ 426 static void * 424 static void *magazine_obj_get(slab_cache_t *cache) 427 425 { 428 426 slab_magazine_t *mag; … … 459 457 * 460 458 */ 461 static slab_magazine_t * 459 static slab_magazine_t *make_empty_current_mag(slab_cache_t *cache) 462 460 { 463 461 slab_magazine_t *cmag,*lastmag,*newmag; … … 531 529 { 532 530 if (cache->flags & SLAB_CACHE_SLINSIDE) 533 return ((PAGE_SIZE << cache->order) - sizeof(slab_t)) / cache->size; 531 return ((PAGE_SIZE << cache->order) - sizeof(slab_t)) / 532 cache->size; 534 533 else 535 534 return (PAGE_SIZE << cache->order) / cache->size; … … 558 557 ASSERT(_slab_initialized >= 2); 559 558 560 cache->mag_cache = malloc(sizeof(slab_mag_cache_t) * config.cpu_count,0); 559 cache->mag_cache = malloc(sizeof(slab_mag_cache_t) * config.cpu_count, 560 0); 561 561 for (i = 0; i < config.cpu_count; i++) { 562 562 memsetb(&cache->mag_cache[i], sizeof(cache->mag_cache[i]), 0); 563 spinlock_initialize(&cache->mag_cache[i].lock, "slab_maglock_cpu"); 563 spinlock_initialize(&cache->mag_cache[i].lock, 564 "slab_maglock_cpu"); 564 565 } 565 566 } … … 567 568 /** Initialize allocated memory as a slab cache */ 568 569 static void 569 _slab_cache_create(slab_cache_t *cache, 570 char *name, 571 size_t size, 572 size_t align, 573 int (*constructor)(void *obj, int kmflag), 574 int (*destructor)(void *obj), 575 int flags) 570 _slab_cache_create(slab_cache_t *cache, char *name, size_t size, size_t align, 571 int (*constructor)(void *obj, int kmflag), int (*destructor)(void *obj), 572 int flags) 576 573 { 577 574 int pages; … … 596 593 spinlock_initialize(&cache->slablock, "slab_lock"); 597 594 spinlock_initialize(&cache->maglock, "slab_maglock"); 598 if (! 595 if (!(cache->flags & SLAB_CACHE_NOMAGAZINE)) 599 596 make_magcache(cache); 600 597 … … 609 606 cache->order = 0; 610 607 else 611 cache->order = fnzb(pages -1)+1;608 cache->order = fnzb(pages - 1) + 1; 612 609 613 610 while (badness(cache) > SLAB_MAX_BADNESS(cache)) { … … 630 627 631 628 /** Create slab cache */ 632 slab_cache_t * slab_cache_create(char *name, 633 size_t size, 634 size_t align, 635 int (*constructor)(void *obj, int kmflag), 636 int (*destructor)(void *obj), 637 int flags) 629 slab_cache_t * 630 slab_cache_create(char *name, size_t size, size_t align, 631 int (*constructor)(void *obj, int kmflag), int (*destructor)(void *obj), 632 int flags) 638 633 { 639 634 slab_cache_t *cache; … … 641 636 cache = slab_alloc(&slab_cache_cache, 0); 642 637 _slab_cache_create(cache, name, size, align, constructor, destructor, 643 638 flags); 644 639 return cache; 645 640 } … … 665 660 */ 666 661 magcount = atomic_get(&cache->magazine_counter); 667 while (magcount-- && (mag=get_mag_from_cache(cache, 0))) {662 while (magcount-- && (mag=get_mag_from_cache(cache, 0))) { 668 663 frames += magazine_destroy(cache,mag); 669 664 if (!(flags & SLAB_RECLAIM_ALL) && frames) … … 718 713 719 714 /* All slabs must be empty */ 720 if (!list_empty(&cache->full_slabs) \721 ||!list_empty(&cache->partial_slabs))715 if (!list_empty(&cache->full_slabs) || 716 !list_empty(&cache->partial_slabs)) 722 717 panic("Destroying cache that is not empty."); 723 718 … … 727 722 } 728 723 729 /** Allocate new object from cache - if no flags given, always returns 730 memory */ 731 void * slab_alloc(slab_cache_t *cache, int flags) 724 /** Allocate new object from cache - if no flags given, always returns memory */ 725 void *slab_alloc(slab_cache_t *cache, int flags) 732 726 { 733 727 ipl_t ipl; … … 758 752 ipl = interrupts_disable(); 759 753 760 if ((cache->flags & SLAB_CACHE_NOMAGAZINE) \ 761 || magazine_obj_put(cache, obj)) { 762 754 if ((cache->flags & SLAB_CACHE_NOMAGAZINE) || 755 magazine_obj_put(cache, obj)) { 763 756 slab_obj_destroy(cache, obj, slab); 764 757 … … 787 780 */ 788 781 789 for (cur = slab_cache_list.next;cur!=&slab_cache_list; cur=cur->next) { 782 for (cur = slab_cache_list.next; cur != &slab_cache_list; 783 cur = cur->next) { 790 784 cache = list_get_instance(cur, slab_cache_t, link); 791 785 frames += _slab_reclaim(cache, flags); … … 807 801 ipl = interrupts_disable(); 808 802 spinlock_lock(&slab_cache_lock); 809 printf("slab name size pages obj/pg slabs cached allocated ctl\n"); 810 printf("---------------- -------- ------ ------ ------ ------ --------- ---\n"); 811 812 for (cur = slab_cache_list.next; cur != &slab_cache_list; cur = cur->next) { 803 printf("slab name size pages obj/pg slabs cached allocated" 804 " ctl\n"); 805 printf("---------------- -------- ------ ------ ------ ------ ---------" 806 " ---\n"); 807 808 for (cur = slab_cache_list.next; cur != &slab_cache_list; 809 cur = cur->next) { 813 810 cache = list_get_instance(cur, slab_cache_t, link); 814 811 815 812 printf("%-16s %8" PRIs " %6d %6u %6ld %6ld %9ld %-3s\n", 816 cache->name, cache->size, (1 << cache->order), cache->objects, 817 atomic_get(&cache->allocated_slabs), atomic_get(&cache->cached_objs), 818 atomic_get(&cache->allocated_objs), cache->flags & SLAB_CACHE_SLINSIDE ? "in" : "out"); 813 cache->name, cache->size, (1 << cache->order), 814 cache->objects, atomic_get(&cache->allocated_slabs), 815 atomic_get(&cache->cached_objs), 816 atomic_get(&cache->allocated_objs), 817 cache->flags & SLAB_CACHE_SLINSIDE ? "in" : "out"); 819 818 } 820 819 spinlock_unlock(&slab_cache_lock); … … 827 826 828 827 /* Initialize magazine cache */ 829 _slab_cache_create(&mag_cache, 830 "slab_magazine", 831 sizeof(slab_magazine_t) + SLAB_MAG_SIZE * sizeof(void*), 832 sizeof(uintptr_t), 833 NULL, NULL, 834 SLAB_CACHE_NOMAGAZINE | SLAB_CACHE_SLINSIDE); 828 _slab_cache_create(&mag_cache, "slab_magazine", 829 sizeof(slab_magazine_t) + SLAB_MAG_SIZE * sizeof(void*), 830 sizeof(uintptr_t), NULL, NULL, SLAB_CACHE_NOMAGAZINE | 831 SLAB_CACHE_SLINSIDE); 835 832 /* Initialize slab_cache cache */ 836 _slab_cache_create(&slab_cache_cache, 837 "slab_cache", 838 sizeof(slab_cache_cache), 839 sizeof(uintptr_t), 840 NULL, NULL, 841 SLAB_CACHE_NOMAGAZINE | SLAB_CACHE_SLINSIDE); 833 _slab_cache_create(&slab_cache_cache, "slab_cache", 834 sizeof(slab_cache_cache), sizeof(uintptr_t), NULL, NULL, 835 SLAB_CACHE_NOMAGAZINE | SLAB_CACHE_SLINSIDE); 842 836 /* Initialize external slab cache */ 843 slab_extern_cache = slab_cache_create("slab_extern", 844 sizeof(slab_t), 845 0, NULL, NULL, 846 SLAB_CACHE_SLINSIDE | SLAB_CACHE_MAGDEFERRED); 837 slab_extern_cache = slab_cache_create("slab_extern", sizeof(slab_t), 0, 838 NULL, NULL, SLAB_CACHE_SLINSIDE | SLAB_CACHE_MAGDEFERRED); 847 839 848 840 /* Initialize structures for malloc */ 849 for (i=0, size=(1 << SLAB_MIN_MALLOC_W); 850 i < (SLAB_MAX_MALLOC_W - SLAB_MIN_MALLOC_W + 1); 851 i++, size <<= 1) { 852 malloc_caches[i] = slab_cache_create(malloc_names[i], 853 size, 0, 854 NULL,NULL, SLAB_CACHE_MAGDEFERRED); 841 for (i = 0, size = (1 << SLAB_MIN_MALLOC_W); 842 i < (SLAB_MAX_MALLOC_W - SLAB_MIN_MALLOC_W + 1); 843 i++, size <<= 1) { 844 malloc_caches[i] = slab_cache_create(malloc_names[i], size, 0, 845 NULL, NULL, SLAB_CACHE_MAGDEFERRED); 855 846 } 856 847 #ifdef CONFIG_DEBUG … … 877 868 spinlock_lock(&slab_cache_lock); 878 869 879 for (cur=slab_cache_list.next; cur != &slab_cache_list;cur=cur->next){ 870 for (cur = slab_cache_list.next; cur != &slab_cache_list; 871 cur = cur->next){ 880 872 s = list_get_instance(cur, slab_cache_t, link); 881 if ((s->flags & SLAB_CACHE_MAGDEFERRED) != SLAB_CACHE_MAGDEFERRED) 873 if ((s->flags & SLAB_CACHE_MAGDEFERRED) != 874 SLAB_CACHE_MAGDEFERRED) 882 875 continue; 883 876 make_magcache(s); … … 890 883 /**************************************/ 891 884 /* kalloc/kfree functions */ 892 void * 885 void *malloc(unsigned int size, int flags) 893 886 { 894 887 ASSERT(_slab_initialized); … … 903 896 } 904 897 905 void * 898 void *realloc(void *ptr, unsigned int size, int flags) 906 899 { 907 900 ASSERT(_slab_initialized);
Note:
See TracChangeset
for help on using the changeset viewer.