Skip to content

Commit

Permalink
slab: Add kmalloc, kzalloc and kfree wrappers
Browse files Browse the repository at this point in the history
Also apply minor cleanup:
  - move include/slab.h to include/mm/slab.h
  - make debug message of ktf_alloc() debug only

Signed-off-by: Pawel Wieczorkiewicz <[email protected]>
  • Loading branch information
wipawel committed Oct 23, 2020
1 parent 7dcd88c commit 9b390ef
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
6 changes: 3 additions & 3 deletions arch/x86/ioapic.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <ioapic.h>
#include <ktf.h>
#include <list.h>
#include <slab.h>
#include <mm/slab.h>
#include <string.h>

#define IOAPIC_SYSTEM_ISA_BUS_NAME "ISA"
Expand Down Expand Up @@ -79,7 +79,7 @@ static bus_t *get_system_bus_by_id(uint8_t id) {

static bus_t *__add_system_bus(uint8_t id,
uint8_t bus_name[IOAPIC_SYSTEM_BUS_NAME_SIZE]) {
bus_t *new_bus = ktf_alloc(sizeof(*new_bus));
bus_t *new_bus = kzalloc(sizeof(*new_bus));
BUG_ON(!new_bus);

new_bus->id = id;
Expand Down Expand Up @@ -121,7 +121,7 @@ int add_system_bus_irq_override(uint8_t bus_id, irq_override_t *override) {
if (!bus)
return -ENODEV;

new_override = ktf_alloc(sizeof(*new_override));
new_override = kzalloc(sizeof(*new_override));
if (!new_override)
return -ENOMEM;

Expand Down
2 changes: 1 addition & 1 deletion common/setup.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@
#include <traps.h>

#include <mm/pmm.h>
#include <mm/slab.h>
#include <mm/vmm.h>
#include <smp/mptables.h>
#include <smp/smp.h>

#include <drivers/pic.h>
#include <drivers/pit.h>
#include <drivers/serial.h>
#include <slab.h>

bool opt_debug = false;
bool_cmd("debug", opt_debug);
Expand Down
5 changes: 3 additions & 2 deletions include/slab.h → include/mm/slab.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ struct meta_slab {
typedef struct meta_slab meta_slab_t;

int init_slab(void);
extern void *ktf_alloc(unsigned int size);
extern void ktf_free(void *ptr);
extern void *kmalloc(size_t size);
extern void *kzalloc(size_t size);
extern void kfree(void *ptr);

#endif /* KTF_ALLOC_SLAB_H */
4 changes: 2 additions & 2 deletions include/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#ifndef KTF_STRING_H
#define KTF_STRING_H
#include <asm-macros.h>
#include <slab.h>
#include <mm/slab.h>

static inline __used int isspace(int c) { return c == ' ' || c == '\t'; }

Expand Down Expand Up @@ -248,7 +248,7 @@ static inline char *strdup(const char *s1) {
return NULL;

len = strlen(s1);
s2 = (char *) ktf_alloc(len);
s2 = (char *) kmalloc(len);

if (NULL == s2)
return NULL;
Expand Down
21 changes: 16 additions & 5 deletions mm/slab.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
#include <errno.h>
#include <ktf.h>
#include <lib.h>
#include <mm/slab.h>
#include <mm/vmm.h>
#include <page.h>
#include <processor.h>
#include <sched.h>
#include <slab.h>
#include <smp/smp.h>
#include <string.h>

Expand Down Expand Up @@ -117,7 +117,7 @@ static void slab_free(meta_slab_t *slab, void *ptr) {
* Round up to nearest power of 2
* If greater than max size or less than min size, return null
*/
void *ktf_alloc(unsigned int size) {
static void *ktf_alloc(unsigned int size) {
unsigned int size_power2 = 0, temp = 0, order_index = 0;
meta_slab_t *slab = NULL, *meta_slab = NULL;
void *alloc = NULL, *free_page = NULL;
Expand All @@ -143,8 +143,8 @@ void *ktf_alloc(unsigned int size) {
*/
order_index -= 4;

printk("Alloc size %u, powerof 2 size %u, order %u\n", size, size_power2,
order_index);
dprintk("Alloc size %u, powerof 2 size %u, order %u\n", size, size_power2,
order_index);
/* Go through list of meta_slab_t and try to allocate a free slab */
list_for_each_entry (slab, &meta_slab_list[order_index], list) {
alloc = slab_alloc(slab);
Expand Down Expand Up @@ -198,12 +198,21 @@ void *ktf_alloc(unsigned int size) {
return alloc;
}

void *kmalloc(size_t size) { return ktf_alloc(size); }

void *kzalloc(size_t size) {
void *ptr = ktf_alloc(size);

memset(ptr, 0, size);
return ptr;
}

/*
* Loop through all the orders and check where does this memory belong
* Then link it back into free list. Not a O(1) of implementation but we're looking for
* simple now
*/
void ktf_free(void *ptr) {
static void ktf_free(void *ptr) {
int alloc_order;
meta_slab_t *slab = NULL;

Expand All @@ -223,6 +232,8 @@ void ktf_free(void *ptr) {
UNREACHABLE();
}

void kfree(void *ptr) { ktf_free(ptr); }

int init_slab(void) {
int ret = 0;
int i = 0;
Expand Down

0 comments on commit 9b390ef

Please sign in to comment.