source: mainline/kernel/generic/include/mm/slab.h

Last change on this file was 31e15be, checked in by jxsvoboda <5887334+jxsvoboda@…>, 4 years ago

kernel: deprecate atomic_t

  • Property mode set to 100644
File size: 4.1 KB
RevLine 
[4e147a6]1/*
[df4ed85]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
[174156fd]29/** @addtogroup kernel_generic_mm
[b45c443]30 * @{
31 */
32/** @file
33 */
34
[06e1e95]35#ifndef KERN_SLAB_H_
36#define KERN_SLAB_H_
[4e147a6]37
[5c9a08b]38#include <adt/list.h>
[4e147a6]39#include <synch/spinlock.h>
[23684b7]40#include <atomic.h>
[085d973]41#include <mm/frame.h>
[4e147a6]42
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 */
[c3ebc47]47#define SLAB_INSIDE_SIZE (PAGE_SIZE >> 3)
[4e147a6]48
[a294ad0]49/** Maximum wasted space we allow for cache */
[c3ebc47]50#define SLAB_MAX_BADNESS(cache) \
[b0c2075]51 (FRAMES2SIZE((cache)->frames) >> 2)
[4e147a6]52
53/* slab_reclaim constants */
[80bcaed]54
55/** Reclaim all possible memory, because we are in memory stress */
[5f0f29ce]56#define SLAB_RECLAIM_ALL 0x01
[4e147a6]57
58/* cache_create flags */
[80bcaed]59
60/** Do not use per-cpu cache */
[c3ebc47]61#define SLAB_CACHE_NOMAGAZINE 0x01
[80bcaed]62/** Have control structure inside SLAB */
[c3ebc47]63#define SLAB_CACHE_SLINSIDE 0x02
[80bcaed]64/** We add magazine cache later, if we have this flag */
[c3ebc47]65#define SLAB_CACHE_MAGDEFERRED (0x04 | SLAB_CACHE_NOMAGAZINE)
[4e147a6]66
67typedef struct {
68 link_t link;
[98000fb]69 size_t busy; /**< Count of full slots in magazine */
70 size_t size; /**< Number of slots in magazine */
[c3ebc47]71 void *objs[]; /**< Slots in magazine */
[f3272e98]72} slab_magazine_t;
[4e147a6]73
[8e1ea655]74typedef struct {
75 slab_magazine_t *current;
76 slab_magazine_t *last;
[25ebfbd]77 IRQ_SPINLOCK_DECLARE(lock);
[f3272e98]78} slab_mag_cache_t;
[8e1ea655]79
[4e147a6]80typedef struct {
[a000878c]81 const char *name;
[a35b458]82
[4e147a6]83 link_t link;
[a35b458]84
[4e147a6]85 /* Configuration */
[a35b458]86
[80bcaed]87 /** Size of slab position - align_up(sizeof(obj)) */
88 size_t size;
[a35b458]89
[b7fd2a0]90 errno_t (*constructor)(void *obj, unsigned int kmflag);
[da1bafb]91 size_t (*destructor)(void *obj);
[a35b458]92
[80bcaed]93 /** Flags changing behaviour of cache */
[da1bafb]94 unsigned int flags;
[a35b458]95
[4e147a6]96 /* Computed values */
[b0c2075]97 size_t frames; /**< Number of frames to be allocated */
[da1bafb]98 size_t objects; /**< Number of objects that fit in */
[a35b458]99
[4e147a6]100 /* Statistics */
[31e15be]101 atomic_size_t allocated_slabs;
102 atomic_size_t allocated_objs;
103 atomic_size_t cached_objs;
[80bcaed]104 /** How many magazines in magazines list */
[31e15be]105 atomic_size_t magazine_counter;
[a35b458]106
[4e147a6]107 /* Slabs */
[55b77d9]108 list_t full_slabs; /**< List of full slabs */
109 list_t partial_slabs; /**< List of partial slabs */
[ddb56be]110 IRQ_SPINLOCK_DECLARE(slablock);
[c3ebc47]111 /* Magazines */
[55b77d9]112 list_t magazines; /**< List o full magazines */
[4d194be]113 IRQ_SPINLOCK_DECLARE(maglock);
[a35b458]114
[a294ad0]115 /** CPU cache */
[8e1ea655]116 slab_mag_cache_t *mag_cache;
[f3272e98]117} slab_cache_t;
[4e147a6]118
[a000878c]119extern slab_cache_t *slab_cache_create(const char *, size_t, size_t,
[b7fd2a0]120 errno_t (*)(void *, unsigned int), size_t (*)(void *), unsigned int);
[1a48bcd]121extern void slab_cache_destroy(slab_cache_t *);
[4e147a6]122
[273b958]123extern void *slab_alloc(slab_cache_t *, unsigned int)
124 __attribute__((malloc));
[1a48bcd]125extern void slab_free(slab_cache_t *, void *);
[da1bafb]126extern size_t slab_reclaim(unsigned int);
[4e147a6]127
[80bcaed]128/* slab subsytem initialization */
[a294ad0]129extern void slab_cache_init(void);
[8e1ea655]130extern void slab_enable_cpucache(void);
[4e147a6]131
[2cb5e64]132/* kconsole debug */
[a294ad0]133extern void slab_print_list(void);
[4e147a6]134
135#endif
[b45c443]136
[06e1e95]137/** @}
[b45c443]138 */
Note: See TracBrowser for help on using the repository browser.