[Krafton Jungle] PintOS 2.0.0
크래프톤 정글 PintOS
 
Loading...
Searching...
No Matches
malloc.h File Reference
#include <debug.h>
#include <stddef.h>
Include dependency graph for malloc.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void malloc_init (void)
 
void * malloc (size_t) __attribute__((malloc))
 
void * calloc (size_t, size_t) __attribute__((malloc))
 
void * realloc (void *, size_t)
 
void free (void *)
 

Function Documentation

◆ calloc()

void * calloc ( size_t  a,
size_t  b 
)
149 {
150 void *p;
151 size_t size;
152
153 /* Calculate block size and make sure it fits in size_t. */
154 size = a * b;
155 if (size < a || size < b)
156 return NULL;
157
158 /* Allocate and zero memory. */
159 p = malloc (size);
160 if (p != NULL)
161 memset (p, 0, size);
162
163 return p;
164}
void * malloc(size_t size)
Definition: malloc.c:85
uint16_t size
Definition: mmu.h:0
#define NULL
Definition: stddef.h:4
void * memset(void *, int, size_t)
Definition: string.c:258
Here is the call graph for this function:
Here is the caller graph for this function:

◆ free()

void free ( void *  p)
202 {
203 if (p != NULL) {
204 struct block *b = p;
205 struct arena *a = block_to_arena (b);
206 struct desc *d = a->desc;
207
208 if (d != NULL) {
209 /* It's a normal block. We handle it here. */
210
211#ifndef NDEBUG
212 /* Clear the block to help detect use-after-free bugs. */
213 memset (b, 0xcc, d->block_size);
214#endif
215
216 lock_acquire (&d->lock);
217
218 /* Add block to free list. */
220
221 /* If the arena is now entirely unused, free it. */
222 if (++a->free_cnt >= d->blocks_per_arena) {
223 size_t i;
224
226 for (i = 0; i < d->blocks_per_arena; i++) {
227 struct block *b = arena_to_block (a, i);
229 }
231 }
232
233 lock_release (&d->lock);
234 } else {
235 /* It's a big block. Free its pages. */
237 return;
238 }
239 }
240}
#define ASSERT(CONDITION)
Definition: debug.h:30
void list_push_front(struct list *, struct list_elem *)
Definition: list.c:195
struct list_elem * list_remove(struct list_elem *)
Definition: list.c:241
static struct arena * block_to_arena(struct block *)
Definition: malloc.c:244
static struct block * arena_to_block(struct arena *, size_t idx)
Definition: malloc.c:261
void palloc_free_multiple(void *, size_t page_cnt)
Definition: palloc.c:307
void palloc_free_page(void *)
Definition: palloc.c:333
Definition: malloc.c:49
size_t free_cnt
Definition: malloc.c:52
struct desc * desc
Definition: malloc.c:51
Definition: malloc.c:56
struct list_elem free_elem
Definition: malloc.c:57
Definition: malloc.c:38
size_t block_size
Definition: malloc.c:39
struct list free_list
Definition: malloc.c:41
struct lock lock
Definition: malloc.c:42
size_t blocks_per_arena
Definition: malloc.c:40
void lock_release(struct lock *)
Definition: synch.c:243
void lock_acquire(struct lock *)
Definition: synch.c:202
Here is the call graph for this function:
Here is the caller graph for this function:

◆ malloc()

void * malloc ( size_t  size)
85 {
86 struct desc *d;
87 struct block *b;
88 struct arena *a;
89
90 /* A null pointer satisfies a request for 0 bytes. */
91 if (size == 0)
92 return NULL;
93
94 /* Find the smallest descriptor that satisfies a SIZE-byte
95 request. */
96 for (d = descs; d < descs + desc_cnt; d++)
97 if (d->block_size >= size)
98 break;
99 if (d == descs + desc_cnt) {
100 /* SIZE is too big for any descriptor.
101 Allocate enough pages to hold SIZE plus an arena. */
102 size_t page_cnt = DIV_ROUND_UP (size + sizeof *a, PGSIZE);
103 a = palloc_get_multiple (0, page_cnt);
104 if (a == NULL)
105 return NULL;
106
107 /* Initialize the arena to indicate a big block of PAGE_CNT
108 pages, and return it. */
109 a->magic = ARENA_MAGIC;
110 a->desc = NULL;
111 a->free_cnt = page_cnt;
112 return a + 1;
113 }
114
115 lock_acquire (&d->lock);
116
117 /* If the free list is empty, create a new arena. */
118 if (list_empty (&d->free_list)) {
119 size_t i;
120
121 /* Allocate a page. */
122 a = palloc_get_page (0);
123 if (a == NULL) {
124 lock_release (&d->lock);
125 return NULL;
126 }
127
128 /* Initialize arena and add its blocks to the free list. */
129 a->magic = ARENA_MAGIC;
130 a->desc = d;
132 for (i = 0; i < d->blocks_per_arena; i++) {
133 struct block *b = arena_to_block (a, i);
135 }
136 }
137
138 /* Get a block from free list and return it. */
140 a = block_to_arena (b);
141 a->free_cnt--;
142 lock_release (&d->lock);
143 return b;
144}
struct list_elem * list_pop_front(struct list *)
Definition: list.c:251
void list_push_back(struct list *, struct list_elem *)
Definition: list.c:202
bool list_empty(struct list *)
Definition: list.c:296
#define list_entry(LIST_ELEM, STRUCT, MEMBER)
Definition: list.h:103
static size_t desc_cnt
Definition: malloc.c:62
#define ARENA_MAGIC
Definition: malloc.c:46
static struct desc descs[10]
Definition: malloc.c:61
void * palloc_get_page(enum palloc_flags)
Definition: palloc.c:301
void * palloc_get_multiple(enum palloc_flags, size_t page_cnt)
Definition: palloc.c:263
#define DIV_ROUND_UP(X, STEP)
Definition: round.h:10
unsigned magic
Definition: malloc.c:50
#define PGSIZE
Definition: vaddr.h:20
Here is the call graph for this function:
Here is the caller graph for this function:

◆ malloc_init()

void malloc_init ( void  )
69 {
70 size_t block_size;
71
72 for (block_size = 16; block_size < PGSIZE / 2; block_size *= 2) {
73 struct desc *d = &descs[desc_cnt++];
74 ASSERT (desc_cnt <= sizeof descs / sizeof *descs);
76 d->blocks_per_arena = (PGSIZE - sizeof (struct arena)) / block_size;
77 list_init (&d->free_list);
78 lock_init (&d->lock);
79 }
80}
void list_init(struct list *)
Definition: list.c:58
static size_t block_size(void *block)
Definition: malloc.c:168
void lock_init(struct lock *)
Definition: synch.c:186
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 
)
183 {
184 if (new_size == 0) {
185 free (old_block);
186 return NULL;
187 } else {
188 void *new_block = malloc (new_size);
189 if (old_block != NULL && new_block != NULL) {
190 size_t old_size = block_size (old_block);
191 size_t min_size = new_size < old_size ? new_size : old_size;
192 memcpy (new_block, old_block, min_size);
193 free (old_block);
194 }
195 return new_block;
196 }
197}
void free(void *p)
Definition: malloc.c:202
void * memcpy(void *, const void *, size_t)
Definition: string.c:7
Here is the call graph for this function: