Subversion Repositories HelenOS

Rev

Rev 762 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

/*
 * Copyright (C) 2005 Ondrej Palkovsky
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 * - The name of the author may not be used to endorse or promote products
 *   derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef __SLAB_H__
#define __SLAB_H__

#include <list.h>
#include <synch/spinlock.h>

/** Initial Magazine size (TODO: dynamically growing magazines) */
#define SLAB_MAG_SIZE  4

/** If object size is less, store control structure inside SLAB */
#define SLAB_INSIDE_SIZE   (PAGE_SIZE / 6)

/* slab_alloc constants */
#define SLAB_ATOMIC       0x1 /**< Do not sleep when no free memory, 
                   may return NULL */
#define SLAB_NO_RECLAIM   0x2 /**< Do not try to call slab_reclaim, if no
                 free memory is found - avoid deadlock */

/* slab_reclaim constants */
#define SLAB_RECLAIM_ALL  0x1 /**< Reclaim all possible memory, because
                   *   we are in memory stress */

/* cache_create flags */
#define SLAB_CACHE_NOMAGAZINE 0x1 /**< Do not use per-cpu cache */
#define SLAB_CACHE_SLINSIDE   0x2 /**< Have control structure inside SLAB */

typedef struct {
    link_t link;
    count_t busy;
    count_t size;
    void *objs[0];
}slab_magazine_t;

typedef struct {
    char *name;

    SPINLOCK_DECLARE(lock);
    link_t link;
    /* Configuration */
    size_t size;
    size_t align;
    int (*constructor)(void *obj, int kmflag);
    void (*destructor)(void *obj);
    int flags;

    /* Computed values */
    int pages;
    int objects;

    /* Statistics */

    /* Slabs */
    link_t full_slabs;
    link_t partial_slabs;
    /* Magazines  */
    link_t magazines;
    /* CPU cache */
    struct {
        slab_magazine_t *current;
        slab_magazine_t *last;
        SPINLOCK_DECLARE(lock);
    }mag_cache[0];
}slab_cache_t;

typedef struct {
    slab_cache_t *cache; /**< Pointer to parent cache */
    void *start;       /**< Start address of first available item */
    count_t available; /**< Count of available items in this slab */
    index_t nextavail; /**< The index of next available item */
}slab_slab_t;


slab_cache_t * slab_cache_create(char *name,
                 size_t size,
                 size_t align,
                 int (*constructor)(void *obj, int kmflag),
                 void (*destructor)(void *obj),
                 int flags);
void slab_cache_destroy(slab_cache_t *cache);

void * slab_alloc(slab_cache_t *cache, int flags);
void slab_free(slab_cache_t *cache, void *obj);
count_t slab_reclaim(int flags);

/** Initialize SLAB subsytem */
void slab_cache_init(void);

/* KConsole debug */
void slab_print_list(void);

#endif