AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
slab.c File Reference

Slabs: the kernel's small objects. More...

#include <core/system.h>
#include <core/panic.h>
#include <core/string.h>
#include <ds/queue.h>
#include <mm/buddy.h>
#include <mm/mm.h>
#include <mm/pmap.h>
#include <mm/vm.h>

Go to the source code of this file.

Data Structures

struct  slab
 A slab's header, at the start of its page. More...

Macros

#define SLAB_HEADER   ((sizeof(struct slab) + 15) & ~(size_t) 15)
#define SLAB_PAGES   (ARCH_SLAB_SIZE / PAGE_SIZE)

Functions

static int slab_class (size_t size)
 Returns the size class for objects of size bytes: the smallest power of two from SLAB_MIN that holds them, as an index (0 for SLAB_MIN).
static struct slab * slab_page_alloc (void)
 Maps a page of memory at a free page of the slab region, for a new slab.
static void slab_page_free (struct slab *slab)
 Unmaps a slab's page, and gives it back to the buddy allocator.
static void slab_unlink (struct slab **list, struct slab *slab)
 Takes slab off a list of slabs with free objects.
static void slab_link (struct slab **list, struct slab *slab)
 Puts slab at the head of a list of slabs with free objects.
void * slab_alloc (size_t size, struct malloc_type *type)
 Allocates a small object, from a slab of its malloc type and size class, making a new slab if none has a free object.
int slab_owns (void *ptr)
 Says whether an object is a slab's, for kfree().
void slab_free (void *ptr)
 Frees an object of a slab.

Variables

static uint8_t slab_map [SLAB_PAGES/8]
static size_t slab_first_free
size_t slab_used

Detailed Description

Slabs: the kernel's small objects.

kmalloc() takes objects of up to SLAB_MAX bytes from here, and the bigger ones from kvmem (mm/kvmem.c). A slab is one page of objects of one size (a power of two, from 16 bytes) and one malloc type, with its header at the start of the page. Each malloc type has, for each size, a list of its slabs that have free objects, and each slab keeps a list of its free objects. So allocating and freeing take the same time, however many objects there are.

The slabs' pages are mapped in a region of their own in the kernel's address space (ARCH_SLAB_BASE), which tells kfree() that an object is a slab's, and a bitmap says which pages of the region are in use. A slab whose objects are all free gives its page back to the buddy allocator.

Definition in file slab.c.

Macro Definition Documentation

◆ SLAB_HEADER

#define SLAB_HEADER   ((sizeof(struct slab) + 15) & ~(size_t) 15)

Definition at line 39 of file slab.c.

Referenced by slab_alloc().

◆ SLAB_PAGES

#define SLAB_PAGES   (ARCH_SLAB_SIZE / PAGE_SIZE)

Definition at line 40 of file slab.c.

Referenced by slab_page_alloc().

Function Documentation

◆ slab_alloc()

void * slab_alloc ( size_t size,
struct malloc_type * type )

Allocates a small object, from a slab of its malloc type and size class, making a new slab if none has a free object.

kmalloc() calls it for objects of up to SLAB_MAX bytes.

Parameters
sizethe size wanted, in bytes, at most SLAB_MAX
typethe malloc type it's counted as
Returns
the object, or NULL if there was no memory for a new slab

Definition at line 142 of file slab.c.

References slab::free, malloc_type::nr, NULL, PAGE_SIZE, slab::size, slab_alloc(), SLAB_HEADER, slab_link(), SLAB_MIN, slab_page_alloc(), slab_unlink(), malloc_type::slabs, malloc_type::total, slab::type, and slab::used.

Referenced by kmalloc(), and slab_alloc().

◆ slab_class()

int slab_class ( size_t size)
static

Returns the size class for objects of size bytes: the smallest power of two from SLAB_MIN that holds them, as an index (0 for SLAB_MIN).

Definition at line 49 of file slab.c.

References slab_class(), and SLAB_MIN.

Referenced by slab_class(), and slab_free().

◆ slab_free()

void slab_free ( void * ptr)

Frees an object of a slab.

The slab goes back on its list if it was full, and gives its page back if it is empty now.

Parameters
ptrthe object, which slab_alloc() gave

Definition at line 199 of file slab.c.

References slab::free, malloc_type::nr, PAGE_ALIGN, slab::size, slab_class(), slab_free(), slab_link(), slab_page_free(), slab_unlink(), malloc_type::slabs, malloc_type::total, slab::type, and slab::used.

Referenced by kfree(), and slab_free().

◆ slab_link()

void slab_link ( struct slab ** list,
struct slab * slab )
static

Puts slab at the head of a list of slabs with free objects.

Definition at line 122 of file slab.c.

References slab::next, NULL, slab::prev, and slab_link().

Referenced by slab_alloc(), slab_free(), and slab_link().

◆ slab_owns()

int slab_owns ( void * ptr)

Says whether an object is a slab's, for kfree().

Parameters
ptrthe object
Returns
whether it is in the slab region

Definition at line 187 of file slab.c.

References ARCH_SLAB_BASE, ARCH_SLAB_SIZE, and slab_owns().

Referenced by kfree(), and slab_owns().

◆ slab_page_alloc()

struct slab * slab_page_alloc ( void )
static

Maps a page of memory at a free page of the slab region, for a new slab.

Returns NULL if the region is full or there is no memory, and then kmalloc() returns NULL.

Definition at line 62 of file slab.c.

References ARCH_SLAB_BASE, kvm_space, mm_page_alloc(), mm_page_dealloc(), NULL, vm_page::paddr, PAGE_SIZE, vm_space::pmap, pmap_add(), slab_first_free, slab_map, slab_page_alloc(), SLAB_PAGES, slab_used, and VM_KRW.

Referenced by slab_alloc(), and slab_page_alloc().

◆ slab_page_free()

void slab_page_free ( struct slab * slab)
static

Unmaps a slab's page, and gives it back to the buddy allocator.

Definition at line 91 of file slab.c.

References arch_page_get_mapping(), ARCH_SLAB_BASE, kvm_space, mm_page_dealloc(), vm_page::paddr, PAGE_SIZE, vm_space::pmap, pmap_remove(), slab_first_free, slab_map, slab_page_free(), and slab_used.

Referenced by slab_free(), and slab_page_free().

◆ slab_unlink()

void slab_unlink ( struct slab ** list,
struct slab * slab )
static

Takes slab off a list of slabs with free objects.

Definition at line 108 of file slab.c.

References slab::next, NULL, slab::prev, and slab_unlink().

Referenced by slab_alloc(), slab_free(), and slab_unlink().

Variable Documentation

◆ slab_first_free

size_t slab_first_free
static

Definition at line 43 of file slab.c.

Referenced by slab_page_alloc(), and slab_page_free().

◆ slab_map

uint8_t slab_map[SLAB_PAGES/8]
static

Definition at line 42 of file slab.c.

Referenced by slab_page_alloc(), and slab_page_free().

◆ slab_used

size_t slab_used

Definition at line 45 of file slab.c.

Referenced by procfs_meminfo(), slab_page_alloc(), and slab_page_free().