Changeset 8565a42 in mainline for kernel/generic/src/mm/slab.c


Ignore:
Timestamp:
2018-03-02T20:34:50Z (7 years ago)
Author:
GitHub <noreply@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a1a81f69, d5e5fd1
Parents:
3061bc1 (diff), 34e1206 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:34:50)
git-committer:
GitHub <noreply@…> (2018-03-02 20:34:50)
Message:

Remove all trailing whitespace, everywhere.

See individual commit messages for details.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/mm/slab.c

    r3061bc1 r8565a42  
    186186{
    187187        size_t zone = 0;
    188        
     188
    189189        uintptr_t data_phys =
    190190            frame_alloc_generic(cache->frames, flags, 0, &zone);
    191191        if (!data_phys)
    192192                return NULL;
    193        
     193
    194194        void *data = (void *) PA2KA(data_phys);
    195        
     195
    196196        slab_t *slab;
    197197        size_t fsize;
    198        
     198
    199199        if (!(cache->flags & SLAB_CACHE_SLINSIDE)) {
    200200                slab = slab_alloc(slab_extern_cache, flags);
     
    207207                slab = data + fsize - sizeof(*slab);
    208208        }
    209        
     209
    210210        /* Fill in slab structures */
    211211        size_t i;
    212212        for (i = 0; i < cache->frames; i++)
    213213                frame_set_parent(ADDR2PFN(KA2PA(data)) + i, slab, zone);
    214        
     214
    215215        slab->start = data;
    216216        slab->available = cache->objects;
    217217        slab->nextavail = 0;
    218218        slab->cache = cache;
    219        
     219
    220220        for (i = 0; i < cache->objects; i++)
    221221                *((size_t *) (slab->start + i * cache->size)) = i + 1;
    222        
     222
    223223        atomic_inc(&cache->allocated_slabs);
    224224        return slab;
     
    235235        if (!(cache->flags & SLAB_CACHE_SLINSIDE))
    236236                slab_free(slab_extern_cache, slab);
    237        
     237
    238238        atomic_dec(&cache->allocated_slabs);
    239        
     239
    240240        return cache->frames;
    241241}
     
    263263        if (!slab)
    264264                slab = obj2slab(obj);
    265        
     265
    266266        assert(slab->cache == cache);
    267        
     267
    268268        size_t freed = 0;
    269        
     269
    270270        if (cache->destructor)
    271271                freed = cache->destructor(obj);
    272        
     272
    273273        irq_spinlock_lock(&cache->slablock, true);
    274274        assert(slab->available < cache->objects);
    275        
     275
    276276        *((size_t *) obj) = slab->nextavail;
    277277        slab->nextavail = (obj - slab->start) / cache->size;
    278278        slab->available++;
    279        
     279
    280280        /* Move it to correct list */
    281281        if (slab->available == cache->objects) {
     
    283283                list_remove(&slab->link);
    284284                irq_spinlock_unlock(&cache->slablock, true);
    285                
     285
    286286                return freed + slab_space_free(cache, slab);
    287287        } else if (slab->available == 1) {
     
    290290                list_prepend(&slab->link, &cache->partial_slabs);
    291291        }
    292        
     292
    293293        irq_spinlock_unlock(&cache->slablock, true);
    294294        return freed;
     
    303303{
    304304        irq_spinlock_lock(&cache->slablock, true);
    305        
     305
    306306        slab_t *slab;
    307        
     307
    308308        if (list_empty(&cache->partial_slabs)) {
    309309                /*
     
    319319                if (!slab)
    320320                        return NULL;
    321                
     321
    322322                irq_spinlock_lock(&cache->slablock, true);
    323323        } else {
     
    326326                list_remove(&slab->link);
    327327        }
    328        
     328
    329329        void *obj = slab->start + slab->nextavail * cache->size;
    330330        slab->nextavail = *((size_t *) obj);
    331331        slab->available--;
    332        
     332
    333333        if (!slab->available)
    334334                list_prepend(&slab->link, &cache->full_slabs);
    335335        else
    336336                list_prepend(&slab->link, &cache->partial_slabs);
    337        
     337
    338338        irq_spinlock_unlock(&cache->slablock, true);
    339        
     339
    340340        if ((cache->constructor) && (cache->constructor(obj, flags) != EOK)) {
    341341                /* Bad, bad, construction failed */
     
    343343                return NULL;
    344344        }
    345        
     345
    346346        return obj;
    347347}
     
    361361        slab_magazine_t *mag = NULL;
    362362        link_t *cur;
    363        
     363
    364364        irq_spinlock_lock(&cache->maglock, true);
    365365        if (!list_empty(&cache->magazines)) {
     
    368368                else
    369369                        cur = list_last(&cache->magazines);
    370                
     370
    371371                mag = list_get_instance(cur, slab_magazine_t, link);
    372372                list_remove(&mag->link);
     
    385385{
    386386        irq_spinlock_lock(&cache->maglock, true);
    387        
     387
    388388        list_prepend(&mag->link, &cache->magazines);
    389389        atomic_inc(&cache->magazine_counter);
    390        
     390
    391391        irq_spinlock_unlock(&cache->maglock, true);
    392392}
     
    402402        size_t i;
    403403        size_t frames = 0;
    404        
     404
    405405        for (i = 0; i < mag->busy; i++) {
    406406                frames += slab_obj_destroy(cache, mag->objs[i], NULL);
    407407                atomic_dec(&cache->cached_objs);
    408408        }
    409        
     409
    410410        slab_free(&mag_cache, mag);
    411        
     411
    412412        return frames;
    413413}
     
    420420        slab_magazine_t *cmag = cache->mag_cache[CPU->id].current;
    421421        slab_magazine_t *lastmag = cache->mag_cache[CPU->id].last;
    422        
     422
    423423        assert(irq_spinlock_locked(&cache->mag_cache[CPU->id].lock));
    424        
     424
    425425        if (cmag) { /* First try local CPU magazines */
    426426                if (cmag->busy)
    427427                        return cmag;
    428                
     428
    429429                if ((lastmag) && (lastmag->busy)) {
    430430                        cache->mag_cache[CPU->id].current = lastmag;
     
    433433                }
    434434        }
    435        
     435
    436436        /* Local magazines are empty, import one from magazine list */
    437437        slab_magazine_t *newmag = get_mag_from_cache(cache, 1);
    438438        if (!newmag)
    439439                return NULL;
    440        
     440
    441441        if (lastmag)
    442442                magazine_destroy(cache, lastmag);
    443        
     443
    444444        cache->mag_cache[CPU->id].last = cmag;
    445445        cache->mag_cache[CPU->id].current = newmag;
    446        
     446
    447447        return newmag;
    448448}
     
    457457        if (!CPU)
    458458                return NULL;
    459        
     459
    460460        irq_spinlock_lock(&cache->mag_cache[CPU->id].lock, true);
    461        
     461
    462462        slab_magazine_t *mag = get_full_current_mag(cache);
    463463        if (!mag) {
     
    465465                return NULL;
    466466        }
    467        
     467
    468468        void *obj = mag->objs[--mag->busy];
    469469        irq_spinlock_unlock(&cache->mag_cache[CPU->id].lock, true);
    470        
     470
    471471        atomic_dec(&cache->cached_objs);
    472        
     472
    473473        return obj;
    474474}
     
    487487        slab_magazine_t *cmag = cache->mag_cache[CPU->id].current;
    488488        slab_magazine_t *lastmag = cache->mag_cache[CPU->id].last;
    489        
     489
    490490        assert(irq_spinlock_locked(&cache->mag_cache[CPU->id].lock));
    491        
     491
    492492        if (cmag) {
    493493                if (cmag->busy < cmag->size)
    494494                        return cmag;
    495                
     495
    496496                if ((lastmag) && (lastmag->busy < lastmag->size)) {
    497497                        cache->mag_cache[CPU->id].last = cmag;
     
    500500                }
    501501        }
    502        
     502
    503503        /* current | last are full | nonexistent, allocate new */
    504        
     504
    505505        /*
    506506         * We do not want to sleep just because of caching,
     
    513513        if (!newmag)
    514514                return NULL;
    515        
     515
    516516        newmag->size = SLAB_MAG_SIZE;
    517517        newmag->busy = 0;
    518        
     518
    519519        /* Flush last to magazine list */
    520520        if (lastmag)
    521521                put_mag_to_cache(cache, lastmag);
    522        
     522
    523523        /* Move current as last, save new as current */
    524524        cache->mag_cache[CPU->id].last = cmag;
    525525        cache->mag_cache[CPU->id].current = newmag;
    526        
     526
    527527        return newmag;
    528528}
     
    537537        if (!CPU)
    538538                return -1;
    539        
     539
    540540        irq_spinlock_lock(&cache->mag_cache[CPU->id].lock, true);
    541        
     541
    542542        slab_magazine_t *mag = make_empty_current_mag(cache);
    543543        if (!mag) {
     
    545545                return -1;
    546546        }
    547        
     547
    548548        mag->objs[mag->busy++] = obj;
    549        
     549
    550550        irq_spinlock_unlock(&cache->mag_cache[CPU->id].lock, true);
    551        
     551
    552552        atomic_inc(&cache->cached_objs);
    553        
     553
    554554        return 0;
    555555}
     
    578578        size_t objects = comp_objects(cache);
    579579        size_t ssize = FRAMES2SIZE(cache->frames);
    580        
     580
    581581        if (cache->flags & SLAB_CACHE_SLINSIDE)
    582582                ssize -= sizeof(slab_t);
    583        
     583
    584584        return ssize - objects * cache->size;
    585585}
     
    591591{
    592592        assert(_slab_initialized >= 2);
    593        
     593
    594594        cache->mag_cache = slab_alloc(&slab_mag_cache, FRAME_ATOMIC);
    595595        if (!cache->mag_cache)
    596596                return false;
    597        
     597
    598598        size_t i;
    599599        for (i = 0; i < config.cpu_count; i++) {
     
    602602                    "slab.cache.mag_cache[].lock");
    603603        }
    604        
     604
    605605        return true;
    606606}
     
    614614{
    615615        assert(size > 0);
    616        
     616
    617617        memsetb(cache, sizeof(*cache), 0);
    618618        cache->name = name;
    619        
     619
    620620        if (align < sizeof(sysarg_t))
    621621                align = sizeof(sysarg_t);
    622        
     622
    623623        size = ALIGN_UP(size, align);
    624        
     624
    625625        cache->size = size;
    626626        cache->constructor = constructor;
    627627        cache->destructor = destructor;
    628628        cache->flags = flags;
    629        
     629
    630630        list_initialize(&cache->full_slabs);
    631631        list_initialize(&cache->partial_slabs);
    632632        list_initialize(&cache->magazines);
    633        
     633
    634634        irq_spinlock_initialize(&cache->slablock, "slab.cache.slablock");
    635635        irq_spinlock_initialize(&cache->maglock, "slab.cache.maglock");
    636        
     636
    637637        if (!(cache->flags & SLAB_CACHE_NOMAGAZINE))
    638638                (void) make_magcache(cache);
    639        
     639
    640640        /* Compute slab sizes, object counts in slabs etc. */
    641641        if (cache->size < SLAB_INSIDE_SIZE)
    642642                cache->flags |= SLAB_CACHE_SLINSIDE;
    643        
     643
    644644        /* Minimum slab frames */
    645645        cache->frames = SIZE2FRAMES(cache->size);
    646        
     646
    647647        while (badness(cache) > SLAB_MAX_BADNESS(cache))
    648648                cache->frames <<= 1;
    649        
     649
    650650        cache->objects = comp_objects(cache);
    651        
     651
    652652        /* If info fits in, put it inside */
    653653        if (badness(cache) > sizeof(slab_t))
    654654                cache->flags |= SLAB_CACHE_SLINSIDE;
    655        
     655
    656656        /* Add cache to cache list */
    657657        irq_spinlock_lock(&slab_cache_lock, true);
     
    670670        _slab_cache_create(cache, name, size, align, constructor, destructor,
    671671            flags);
    672        
     672
    673673        return cache;
    674674}
     
    685685        if (cache->flags & SLAB_CACHE_NOMAGAZINE)
    686686                return 0; /* Nothing to do */
    687        
     687
    688688        /*
    689689         * We count up to original magazine count to avoid
     
    691691         */
    692692        atomic_count_t magcount = atomic_get(&cache->magazine_counter);
    693        
     693
    694694        slab_magazine_t *mag;
    695695        size_t frames = 0;
    696        
     696
    697697        while ((magcount--) && (mag = get_mag_from_cache(cache, 0))) {
    698698                frames += magazine_destroy(cache, mag);
     
    700700                        break;
    701701        }
    702        
     702
    703703        if (flags & SLAB_RECLAIM_ALL) {
    704704                /* Free cpu-bound magazines */
     
    707707                for (i = 0; i < config.cpu_count; i++) {
    708708                        irq_spinlock_lock(&cache->mag_cache[i].lock, true);
    709                        
     709
    710710                        mag = cache->mag_cache[i].current;
    711711                        if (mag)
    712712                                frames += magazine_destroy(cache, mag);
    713713                        cache->mag_cache[i].current = NULL;
    714                        
     714
    715715                        mag = cache->mag_cache[i].last;
    716716                        if (mag)
    717717                                frames += magazine_destroy(cache, mag);
    718718                        cache->mag_cache[i].last = NULL;
    719                        
     719
    720720                        irq_spinlock_unlock(&cache->mag_cache[i].lock, true);
    721721                }
    722722        }
    723        
     723
    724724        return frames;
    725725}
     
    731731{
    732732        ipl_t ipl = interrupts_disable();
    733        
     733
    734734        if ((cache->flags & SLAB_CACHE_NOMAGAZINE) ||
    735735            (magazine_obj_put(cache, obj)))
    736736                slab_obj_destroy(cache, obj, slab);
    737        
     737
    738738        interrupts_restore(ipl);
    739739        atomic_dec(&cache->allocated_objs);
     
    753753        list_remove(&cache->link);
    754754        irq_spinlock_unlock(&slab_cache_lock, true);
    755        
     755
    756756        /*
    757757         * Do not lock anything, we assume the software is correct and
     
    759759         *
    760760         */
    761        
     761
    762762        /* Destroy all magazines */
    763763        _slab_reclaim(cache, SLAB_RECLAIM_ALL);
    764        
     764
    765765        /* All slabs must be empty */
    766766        if ((!list_empty(&cache->full_slabs)) ||
    767767            (!list_empty(&cache->partial_slabs)))
    768768                panic("Destroying cache that is not empty.");
    769        
     769
    770770        if (!(cache->flags & SLAB_CACHE_NOMAGAZINE)) {
    771771                slab_t *mag_slab = obj2slab(cache->mag_cache);
    772772                _slab_free(mag_slab->cache, cache->mag_cache, mag_slab);
    773773        }
    774        
     774
    775775        slab_free(&slab_cache_cache, cache);
    776776}
     
    783783        /* Disable interrupts to avoid deadlocks with interrupt handlers */
    784784        ipl_t ipl = interrupts_disable();
    785        
     785
    786786        void *result = NULL;
    787        
     787
    788788        if (!(cache->flags & SLAB_CACHE_NOMAGAZINE))
    789789                result = magazine_obj_get(cache);
    790        
     790
    791791        if (!result)
    792792                result = slab_obj_create(cache, flags);
    793        
     793
    794794        interrupts_restore(ipl);
    795        
     795
    796796        if (result)
    797797                atomic_inc(&cache->allocated_objs);
    798        
     798
    799799        return result;
    800800}
     
    812812{
    813813        irq_spinlock_lock(&slab_cache_lock, true);
    814        
     814
    815815        size_t frames = 0;
    816816        list_foreach(slab_cache_list, link, slab_cache_t, cache) {
    817817                frames += _slab_reclaim(cache, flags);
    818818        }
    819        
     819
    820820        irq_spinlock_unlock(&slab_cache_lock, true);
    821        
     821
    822822        return frames;
    823823}
     
    828828        printf("[cache name      ] [size  ] [pages ] [obj/pg] [slabs ]"
    829829            " [cached] [alloc ] [ctl]\n");
    830        
     830
    831831        size_t skip = 0;
    832832        while (true) {
     
    853853                 * statistics.
    854854                 */
    855                
     855
    856856                irq_spinlock_lock(&slab_cache_lock, true);
    857                
     857
    858858                link_t *cur;
    859859                size_t i;
     
    861861                    (i < skip) && (cur != &slab_cache_list.head);
    862862                    i++, cur = cur->next);
    863                
     863
    864864                if (cur == &slab_cache_list.head) {
    865865                        irq_spinlock_unlock(&slab_cache_lock, true);
    866866                        break;
    867867                }
    868                
     868
    869869                skip++;
    870                
     870
    871871                slab_cache_t *cache = list_get_instance(cur, slab_cache_t, link);
    872                
     872
    873873                const char *name = cache->name;
    874874                size_t frames = cache->frames;
     
    879879                long allocated_objs = atomic_get(&cache->allocated_objs);
    880880                unsigned int flags = cache->flags;
    881                
     881
    882882                irq_spinlock_unlock(&slab_cache_lock, true);
    883                
     883
    884884                printf("%-18s %8zu %8zu %8zu %8ld %8ld %8ld %-5s\n",
    885885                    name, size, frames, objects, allocated_slabs,
     
    896896            sizeof(uintptr_t), NULL, NULL, SLAB_CACHE_NOMAGAZINE |
    897897            SLAB_CACHE_SLINSIDE);
    898        
     898
    899899        /* Initialize slab_cache cache */
    900900        _slab_cache_create(&slab_cache_cache, "slab_cache_cache",
    901901            sizeof(slab_cache_cache), sizeof(uintptr_t), NULL, NULL,
    902902            SLAB_CACHE_NOMAGAZINE | SLAB_CACHE_SLINSIDE);
    903        
     903
    904904        /* Initialize external slab cache */
    905905        slab_extern_cache = slab_cache_create("slab_t", sizeof(slab_t), 0,
    906906            NULL, NULL, SLAB_CACHE_SLINSIDE | SLAB_CACHE_MAGDEFERRED);
    907        
     907
    908908        /* Initialize structures for malloc */
    909909        size_t i;
    910910        size_t size;
    911        
     911
    912912        for (i = 0, size = (1 << SLAB_MIN_MALLOC_W);
    913913            i < (SLAB_MAX_MALLOC_W - SLAB_MIN_MALLOC_W + 1);
     
    916916                    NULL, NULL, SLAB_CACHE_MAGDEFERRED);
    917917        }
    918        
     918
    919919#ifdef CONFIG_DEBUG
    920920        _slab_initialized = 1;
     
    934934        _slab_initialized = 2;
    935935#endif
    936        
     936
    937937        _slab_cache_create(&slab_mag_cache, "slab_mag_cache",
    938938            sizeof(slab_mag_cache_t) * config.cpu_count, sizeof(uintptr_t),
    939939            NULL, NULL, SLAB_CACHE_NOMAGAZINE | SLAB_CACHE_SLINSIDE);
    940        
     940
    941941        irq_spinlock_lock(&slab_cache_lock, false);
    942        
     942
    943943        list_foreach(slab_cache_list, link, slab_cache_t, slab) {
    944944                if ((slab->flags & SLAB_CACHE_MAGDEFERRED) !=
    945945                    SLAB_CACHE_MAGDEFERRED)
    946946                        continue;
    947                
     947
    948948                (void) make_magcache(slab);
    949949                slab->flags &= ~SLAB_CACHE_MAGDEFERRED;
    950950        }
    951        
     951
    952952        irq_spinlock_unlock(&slab_cache_lock, false);
    953953}
     
    957957        assert(_slab_initialized);
    958958        assert(size <= (1 << SLAB_MAX_MALLOC_W));
    959        
     959
    960960        if (size < (1 << SLAB_MIN_MALLOC_W))
    961961                size = (1 << SLAB_MIN_MALLOC_W);
    962        
     962
    963963        uint8_t idx = fnzb(size - 1) - SLAB_MIN_MALLOC_W + 1;
    964        
     964
    965965        return slab_alloc(malloc_caches[idx], flags);
    966966}
     
    970970        assert(_slab_initialized);
    971971        assert(size <= (1 << SLAB_MAX_MALLOC_W));
    972        
     972
    973973        void *new_ptr;
    974        
     974
    975975        if (size > 0) {
    976976                if (size < (1 << SLAB_MIN_MALLOC_W))
    977977                        size = (1 << SLAB_MIN_MALLOC_W);
    978978                uint8_t idx = fnzb(size - 1) - SLAB_MIN_MALLOC_W + 1;
    979                
     979
    980980                new_ptr = slab_alloc(malloc_caches[idx], flags);
    981981        } else
    982982                new_ptr = NULL;
    983        
     983
    984984        if ((new_ptr != NULL) && (ptr != NULL)) {
    985985                slab_t *slab = obj2slab(ptr);
    986986                memcpy(new_ptr, ptr, min(size, slab->cache->size));
    987987        }
    988        
     988
    989989        if (ptr != NULL)
    990990                free(ptr);
    991        
     991
    992992        return new_ptr;
    993993}
     
    997997        if (!ptr)
    998998                return;
    999        
     999
    10001000        slab_t *slab = obj2slab(ptr);
    10011001        _slab_free(slab->cache, ptr, slab);
Note: See TracChangeset for help on using the changeset viewer.