PKUOS - Pintos
Pintos source browser for PKU Operating System course
|
#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"
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 arena * | block_to_arena (struct block *b) |
Returns the arena that block B is inside. More... | |
static struct block * | arena_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... | |
#define ARENA_MAGIC 0x9a548eed |
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().
|
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().
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().
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().
void free | ( | void * | p | ) |
Frees block P, which must have been previously allocated with malloc(), calloc(), or realloc().
Definition at line 219 of file malloc.c.
References arena_to_block(), ASSERT, desc::block_size, block_to_arena(), desc::blocks_per_arena, arena::desc, arena::free_cnt, block::free_elem, desc::free_list, list_push_front(), list_remove(), desc::lock, lock_acquire(), lock_release(), memset(), NULL, palloc_free_multiple(), and palloc_free_page().
Referenced by bitmap_create(), bitmap_destroy(), dir_close(), dir_open(), file_close(), file_open(), fsutil_append(), fsutil_extract(), hash_destroy(), inode_close(), inode_create(), inode_read_at(), inode_write_at(), read_partition_table(), realloc(), rehash(), and test_sleep().
void * malloc | ( | size_t | size | ) |
Obtains and returns a new block of at least SIZE bytes.
Returns a null pointer if memory is not available.
Definition at line 90 of file malloc.c.
References ARENA_MAGIC, arena_to_block(), desc::block_size, block_to_arena(), desc::blocks_per_arena, arena::desc, desc_cnt, descs, DIV_ROUND_UP, arena::free_cnt, block::free_elem, desc::free_list, list_empty(), list_entry, list_pop_front(), list_push_back(), desc::lock, lock_acquire(), lock_release(), arena::magic, NULL, palloc_get_multiple(), palloc_get_page(), and PGSIZE.
Referenced by bitmap_create(), block_register(), calloc(), found_partition(), fsutil_append(), fsutil_extract(), hash_init(), inode_open(), inode_read_at(), inode_write_at(), read_partition_table(), realloc(), rehash(), and test_sleep().
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().
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.
|
static |
Number of descriptors.
Definition at line 65 of file malloc.c.
Referenced by malloc(), and malloc_init().
|
static |
Our set of descriptors.
Descriptors.
Definition at line 64 of file malloc.c.
Referenced by malloc(), and malloc_init().