Changeset c352c2e in mainline for generic/src/mm/slab.c


Ignore:
Timestamp:
2006-02-03T23:16:27Z (19 years ago)
Author:
Ondrej Palkovsky <ondrap@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
81e52f2a
Parents:
e1888f9
Message:

Implemented malloc/free as SLABs.

File:
1 edited

Legend:

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

    re1888f9 rc352c2e  
    9393#include <panic.h>
    9494#include <debug.h>
     95#include <bitops.h>
    9596
    9697SPINLOCK_INITIALIZE(slab_cache_lock);
     
    109110 */
    110111static slab_cache_t *slab_extern_cache;
     112/** Caches for malloc */
     113static slab_cache_t *malloc_caches[SLAB_MAX_MALLOC_W-SLAB_MIN_MALLOC_W+1];
     114char *malloc_names[] =  {
     115        "malloc-8","malloc-16","malloc-32","malloc-64","malloc-128",
     116        "malloc-256","malloc-512","malloc-1K","malloc-2K",
     117        "malloc-4K","malloc-8K","malloc-16K","malloc-32K",
     118        "malloc-64K","malloc-128K"
     119};
    111120
    112121/** Slab descriptor */
     
    479488{
    480489        int i;
     490        int pages;
    481491
    482492        memsetb((__address)cache, sizeof(*cache), 0);
     
    508518
    509519        /* Minimum slab order */
    510         cache->order = (cache->size-1) >> PAGE_WIDTH;
     520        pages = ((cache->size-1) >> PAGE_WIDTH) + 1;
     521        cache->order = fnzb(pages);
    511522
    512523        while (badness(cache) > SLAB_MAX_BADNESS(cache)) {
     
    633644        /* Disable interrupts to avoid deadlocks with interrupt handlers */
    634645        ipl = interrupts_disable();
    635        
    636         if (!(cache->flags & SLAB_CACHE_NOMAGAZINE))
     646
     647        if (!(cache->flags & SLAB_CACHE_NOMAGAZINE) && CPU)
    637648                result = magazine_obj_get(cache);
    638649
     
    651662}
    652663
    653 /** Return object to cache  */
    654 void slab_free(slab_cache_t *cache, void *obj)
     664/** Return object to cache, use slab if known  */
     665static void _slab_free(slab_cache_t *cache, void *obj, slab_t *slab)
    655666{
    656667        ipl_t ipl;
     
    659670
    660671        if ((cache->flags & SLAB_CACHE_NOMAGAZINE) \
     672            || !CPU \
    661673            || magazine_obj_put(cache, obj)) {
    662674               
    663675                spinlock_lock(&cache->lock);
    664                 slab_obj_destroy(cache, obj, NULL);
     676                slab_obj_destroy(cache, obj, slab);
    665677                spinlock_unlock(&cache->lock);
    666678        }
    667679        interrupts_restore(ipl);
    668680        atomic_dec(&cache->allocated_objs);
     681}
     682
     683/** Return slab object to cache */
     684void slab_free(slab_cache_t *cache, void *obj)
     685{
     686        _slab_free(cache,obj,NULL);
    669687}
    670688
     
    711729void slab_cache_init(void)
    712730{
     731        int i, size;
     732
    713733        /* Initialize magazine cache */
    714734        _slab_cache_create(&mag_cache,
     
    732752
    733753        /* Initialize structures for malloc */
    734 }
     754        for (i=0, size=(1<<SLAB_MIN_MALLOC_W);
     755             i < (SLAB_MAX_MALLOC_W-SLAB_MIN_MALLOC_W+1);
     756             i++, size <<= 1) {
     757                malloc_caches[i] = slab_cache_create(malloc_names[i],
     758                                                     size, 0,
     759                                                     NULL,NULL,0);
     760        }
     761}
     762
     763/**************************************/
     764/* kalloc/kfree functions             */
     765void * kalloc(unsigned int size, int flags)
     766{
     767        int idx;
     768
     769        ASSERT( size && size <= (1 << SLAB_MAX_MALLOC_W));
     770       
     771        if (size < (1 << SLAB_MIN_MALLOC_W))
     772                size = (1 << SLAB_MIN_MALLOC_W);
     773
     774        idx = fnzb(size-1) - SLAB_MIN_MALLOC_W + 1;
     775
     776        return slab_alloc(malloc_caches[idx], flags);
     777}
     778
     779
     780void kfree(void *obj)
     781{
     782        slab_t *slab = obj2slab(obj);
     783       
     784        _slab_free(slab->cache, obj, slab);
     785}
Note: See TracChangeset for help on using the changeset viewer.