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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c352c2e was c352c2e, checked in by Ondrej Palkovsky <ondrap@…>, 19 years ago

Implemented malloc/free as SLABs.

  • Property mode set to 100644
File size: 4.0 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
32#include <list.h>
33#include <synch/spinlock.h>
[bc504ef2]34#include <arch/atomic.h>
[4e147a6]35
[c352c2e]36/** Minimum size to be allocated by malloc */
37#define SLAB_MIN_MALLOC_W 3
38
39/** Maximum size to be allocated by malloc */
40#define SLAB_MAX_MALLOC_W 17
41
[4e147a6]42/** Initial Magazine size (TODO: dynamically growing magazines) */
43#define SLAB_MAG_SIZE 4
44
45/** If object size is less, store control structure inside SLAB */
[14e5d88]46#define SLAB_INSIDE_SIZE (PAGE_SIZE >> 3)
[4e147a6]47
[a294ad0]48/** Maximum wasted space we allow for cache */
[c352c2e]49#define SLAB_MAX_BADNESS(cache) ((PAGE_SIZE << (cache)->order) >> 2)
[4e147a6]50
51/* slab_reclaim constants */
52#define SLAB_RECLAIM_ALL 0x1 /**< Reclaim all possible memory, because
53 * we are in memory stress */
54
55/* cache_create flags */
56#define SLAB_CACHE_NOMAGAZINE 0x1 /**< Do not use per-cpu cache */
57#define SLAB_CACHE_SLINSIDE 0x2 /**< Have control structure inside SLAB */
58
59typedef struct {
60 link_t link;
[a294ad0]61 count_t busy; /**< Count of full slots in magazine */
62 count_t size; /**< Number of slots in magazine */
63 void *objs[0]; /**< Slots in magazine */
[4e147a6]64}slab_magazine_t;
65
66typedef struct {
67 char *name;
68
69 SPINLOCK_DECLARE(lock);
70 link_t link;
71 /* Configuration */
[a294ad0]72 size_t size; /**< Size of SLAB position - align_up(sizeof(obj)) */
[4e147a6]73 int (*constructor)(void *obj, int kmflag);
74 void (*destructor)(void *obj);
[a294ad0]75 int flags; /**< Flags changing behaviour of cache */
[4e147a6]76
77 /* Computed values */
[a294ad0]78 __u8 order; /**< Order of frames to be allocated */
79 int objects; /**< Number of objects that fit in */
[4e147a6]80
81 /* Statistics */
[bc504ef2]82 atomic_t allocated_slabs;
83 atomic_t allocated_objs;
[4a5b2b0e]84 atomic_t cached_objs;
[4e147a6]85
86 /* Slabs */
[a294ad0]87 link_t full_slabs; /**< List of full slabs */
88 link_t partial_slabs; /**< List of partial slabs */
[4e147a6]89 /* Magazines */
[a294ad0]90 link_t magazines; /**< List o full magazines */
91
92 /** CPU cache */
[4e147a6]93 struct {
94 slab_magazine_t *current;
95 slab_magazine_t *last;
96 SPINLOCK_DECLARE(lock);
97 }mag_cache[0];
98}slab_cache_t;
99
[a294ad0]100extern slab_cache_t * slab_cache_create(char *name,
101 size_t size,
102 size_t align,
103 int (*constructor)(void *obj, int kmflag),
104 void (*destructor)(void *obj),
105 int flags);
106extern void slab_cache_destroy(slab_cache_t *cache);
[4e147a6]107
[a294ad0]108extern void * slab_alloc(slab_cache_t *cache, int flags);
109extern void slab_free(slab_cache_t *cache, void *obj);
110extern count_t slab_reclaim(int flags);
[4e147a6]111
112/** Initialize SLAB subsytem */
[a294ad0]113extern void slab_cache_init(void);
[4e147a6]114
115/* KConsole debug */
[a294ad0]116extern void slab_print_list(void);
[4e147a6]117
[c352c2e]118/* Malloc support */
119extern void * kalloc(unsigned int size, int flags);
120extern void kfree(void *obj);
121
[4e147a6]122#endif
Note: See TracBrowser for help on using the repository browser.