source: mainline/generic/include/mm/slab.h@ e3c762cd

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since e3c762cd was 23684b7, checked in by Jakub Jermar <jakub@…>, 19 years ago

Define atomic_t only once in atomic.h
Change the encapsulated counter type to long so that it supports negative values as well.

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[4e147a6]1/*
[a294ad0]2 * Copyright (C) 2006 Ondrej Palkovsky
[4e147a6]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef __SLAB_H__
30#define __SLAB_H__
31
[5c9a08b]32#include <adt/list.h>
[4e147a6]33#include <synch/spinlock.h>
[23684b7]34#include <atomic.h>
[085d973]35#include <mm/frame.h>
[4e147a6]36
[c352c2e]37/** Minimum size to be allocated by malloc */
[214f5bb]38#define SLAB_MIN_MALLOC_W 4
[c352c2e]39
40/** Maximum size to be allocated by malloc */
41#define SLAB_MAX_MALLOC_W 17
42
[4e147a6]43/** Initial Magazine size (TODO: dynamically growing magazines) */
44#define SLAB_MAG_SIZE 4
45
46/** If object size is less, store control structure inside SLAB */
[14e5d88]47#define SLAB_INSIDE_SIZE (PAGE_SIZE >> 3)
[4e147a6]48
[a294ad0]49/** Maximum wasted space we allow for cache */
[c352c2e]50#define SLAB_MAX_BADNESS(cache) ((PAGE_SIZE << (cache)->order) >> 2)
[4e147a6]51
52/* slab_reclaim constants */
53#define SLAB_RECLAIM_ALL 0x1 /**< Reclaim all possible memory, because
54 * we are in memory stress */
55
56/* cache_create flags */
57#define SLAB_CACHE_NOMAGAZINE 0x1 /**< Do not use per-cpu cache */
58#define SLAB_CACHE_SLINSIDE 0x2 /**< Have control structure inside SLAB */
[8e1ea655]59/** We add magazine cache later, if we have this flag */
60#define SLAB_CACHE_MAGDEFERRED (0x4 | SLAB_CACHE_NOMAGAZINE)
[4e147a6]61
62typedef struct {
63 link_t link;
[a294ad0]64 count_t busy; /**< Count of full slots in magazine */
65 count_t size; /**< Number of slots in magazine */
66 void *objs[0]; /**< Slots in magazine */
[4e147a6]67}slab_magazine_t;
68
[8e1ea655]69typedef struct {
70 slab_magazine_t *current;
71 slab_magazine_t *last;
72 SPINLOCK_DECLARE(lock);
73}slab_mag_cache_t;
74
75
[4e147a6]76typedef struct {
77 char *name;
78
79 link_t link;
80 /* Configuration */
[a294ad0]81 size_t size; /**< Size of SLAB position - align_up(sizeof(obj)) */
[4e147a6]82 int (*constructor)(void *obj, int kmflag);
[266294a9]83 int (*destructor)(void *obj);
[a294ad0]84 int flags; /**< Flags changing behaviour of cache */
[4e147a6]85
86 /* Computed values */
[a294ad0]87 __u8 order; /**< Order of frames to be allocated */
88 int objects; /**< Number of objects that fit in */
[4e147a6]89
90 /* Statistics */
[bc504ef2]91 atomic_t allocated_slabs;
92 atomic_t allocated_objs;
[4a5b2b0e]93 atomic_t cached_objs;
[5158549]94 atomic_t magazine_counter; /*<< How many magazines in magazines list */
[4e147a6]95
96 /* Slabs */
[a294ad0]97 link_t full_slabs; /**< List of full slabs */
98 link_t partial_slabs; /**< List of partial slabs */
[428aabf]99 SPINLOCK_DECLARE(slablock);
[4e147a6]100 /* Magazines */
[a294ad0]101 link_t magazines; /**< List o full magazines */
[428aabf]102 SPINLOCK_DECLARE(maglock);
[a294ad0]103
104 /** CPU cache */
[8e1ea655]105 slab_mag_cache_t *mag_cache;
[4e147a6]106}slab_cache_t;
107
[a294ad0]108extern slab_cache_t * slab_cache_create(char *name,
109 size_t size,
110 size_t align,
111 int (*constructor)(void *obj, int kmflag),
[266294a9]112 int (*destructor)(void *obj),
[a294ad0]113 int flags);
114extern void slab_cache_destroy(slab_cache_t *cache);
[4e147a6]115
[a294ad0]116extern void * slab_alloc(slab_cache_t *cache, int flags);
117extern void slab_free(slab_cache_t *cache, void *obj);
118extern count_t slab_reclaim(int flags);
[4e147a6]119
120/** Initialize SLAB subsytem */
[a294ad0]121extern void slab_cache_init(void);
[8e1ea655]122extern void slab_enable_cpucache(void);
[4e147a6]123
124/* KConsole debug */
[a294ad0]125extern void slab_print_list(void);
[4e147a6]126
[c352c2e]127/* Malloc support */
[bb68433]128extern void * malloc(unsigned int size, int flags);
129extern void free(void *obj);
[4e147a6]130#endif
Note: See TracBrowser for help on using the repository browser.