CS318 - Pintos
Pintos source browser for JHU CS318 course
Data Structures | Macros | Functions | Variables
malloc.c File Reference
#include "threads/malloc.h"
#include <debug.h>
#include <list.h>
#include <round.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "threads/palloc.h"
#include "threads/synch.h"
#include "threads/vaddr.h"
Include dependency graph for malloc.c:

Go to the source code of this file.

Data Structures

struct  desc
 A simple implementation of malloc(). More...
 
struct  arena
 Arena. More...
 
struct  block
 A block device. More...
 

Macros

#define ARENA_MAGIC   0x9a548eed
 Magic number for detecting arena corruption. More...
 

Functions

static struct arenablock_to_arena (struct block *b)
 Returns the arena that block B is inside. More...
 
static struct blockarena_to_block (struct arena *a, size_t idx)
 Returns the (IDX - 1)'th block within arena A. More...
 
void malloc_init (void)
 Initializes the malloc() descriptors. More...
 
void * malloc (size_t size)
 Obtains and returns a new block of at least SIZE bytes. More...
 
void * calloc (size_t a, size_t b)
 Allocates and return A times B bytes initialized to zeroes. More...
 
static size_t block_size (void *block)
 Returns the number of bytes allocated for BLOCK. More...
 
void * realloc (void *old_block, size_t new_size)
 Attempts to resize OLD_BLOCK to NEW_SIZE bytes, possibly moving it in the process. More...
 
void free (void *p)
 Frees block P, which must have been previously allocated with malloc(), calloc(), or realloc(). More...
 

Variables

static struct desc descs [10]
 Our set of descriptors. More...
 
static size_t desc_cnt
 Number of descriptors. More...
 

Macro Definition Documentation

◆ ARENA_MAGIC

#define ARENA_MAGIC   0x9a548eed

Magic number for detecting arena corruption.

Definition at line 47 of file malloc.c.

Function Documentation

◆ arena_to_block()

static struct block * arena_to_block ( struct arena a,
size_t  idx 
)
static

Returns the (IDX - 1)'th block within arena A.

Definition at line 286 of file malloc.c.

References ARENA_MAGIC, ASSERT, desc::block_size, desc::blocks_per_arena, arena::desc, arena::magic, and NULL.

Referenced by free(), and malloc().

Here is the caller graph for this function:

◆ block_size()

static size_t block_size ( void *  block)
static

Returns the number of bytes allocated for BLOCK.

Definition at line 179 of file malloc.c.

References desc::block_size, block_to_arena(), arena::desc, arena::free_cnt, NULL, pg_ofs(), and PGSIZE.

Referenced by malloc_init(), and realloc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ block_to_arena()

static struct arena * block_to_arena ( struct block b)
static

Returns the arena that block B is inside.

Definition at line 268 of file malloc.c.

References ARENA_MAGIC, ASSERT, desc::block_size, arena::desc, arena::magic, NULL, pg_ofs(), and pg_round_down().

Referenced by block_size(), free(), and malloc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ calloc()

void* calloc ( size_t  a,
size_t  b 
)

Allocates and return A times B bytes initialized to zeroes.

Returns a null pointer if memory is not available.

Definition at line 159 of file malloc.c.

References malloc(), memset(), NULL, and block::size.

Referenced by dir_open(), file_open(), and inode_create().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ free()

void free ( void *  p)

◆ malloc()

void* malloc ( size_t  size)

◆ malloc_init()

void malloc_init ( void  )

Initializes the malloc() descriptors.

Definition at line 72 of file malloc.c.

References ASSERT, desc::block_size, block_size(), desc::blocks_per_arena, desc_cnt, descs, desc::free_list, list_init(), desc::lock, lock_init(), and PGSIZE.

Referenced by pintos_init().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ realloc()

void* realloc ( void *  old_block,
size_t  new_size 
)

Attempts to resize OLD_BLOCK to NEW_SIZE bytes, possibly moving it in the process.

If successful, returns the new block; on failure, returns a null pointer. A call with null OLD_BLOCK is equivalent to malloc(NEW_SIZE). A call with zero NEW_SIZE is equivalent to free(OLD_BLOCK).

Definition at line 195 of file malloc.c.

References block_size(), free(), malloc(), memcpy(), and NULL.

Here is the call graph for this function:

Variable Documentation

◆ desc_cnt

size_t desc_cnt
static

Number of descriptors.

Definition at line 65 of file malloc.c.

Referenced by malloc(), and malloc_init().

◆ descs

struct desc descs[10]
static

Our set of descriptors.

Descriptors.

Definition at line 64 of file malloc.c.

Referenced by malloc(), and malloc_init().