CS350 COS
COS
Loading...
Searching...
No Matches
malloc.c File Reference
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/mman.h>
Include dependency graph for malloc.c:

Go to the source code of this file.

Data Structures

struct  Header
 
struct  HeapPool
 

Macros

#define HEAP_MAGIC   0x8051
 
#define HEAP_MIN_POOLSIZE   (64 - sizeof(Header))
 
#define HEAP_MAX_POOLSIZE   (128*1024 - sizeof(Header))
 
#define HEAP_POOLS   (12)
 
#define PGSIZE   4096
 
#define HEAP_INCREMENT   (PGSIZE / 64)
 
#define ROUNDUP(_x, _n)   (((_x) + (_n) - 1) & ~((_n) - 1))
 

Typedefs

typedef struct Header Header
 
typedef struct HeapPool HeapPool
 

Functions

static int size_to_idx (size_t sz)
 
static void grow_small (int idx)
 
static void * malloc_small (size_t sz)
 
static void free_small (Header *mem)
 
static void * malloc_large (size_t sz)
 
static void free_large (Header *mem)
 
void * calloc (size_t num, size_t sz)
 
void * malloc (size_t sz)
 
void free (void *mem)
 

Variables

static HeapPool pool [HEAP_POOLS]
 
static HeapPool largePool = { 0, 0, (Header *)0, 0x4C000000, 0x4C000000 }
 

Data Structure Documentation

◆ Header

struct Header

Definition at line 8 of file malloc.c.

Collaboration diagram for Header:
[legend]
Data Fields
uint16_t magic
struct Header * next
uint32_t size
uint16_t status

◆ HeapPool

struct HeapPool

Definition at line 24 of file malloc.c.

Collaboration diagram for HeapPool:
[legend]
Data Fields
uint64_t base
uint32_t blocksFree
uint32_t blocksInuse
Header * free
uint64_t top

Macro Definition Documentation

◆ HEAP_INCREMENT

#define HEAP_INCREMENT   (PGSIZE / 64)

Definition at line 22 of file malloc.c.

◆ HEAP_MAGIC

#define HEAP_MAGIC   0x8051

Definition at line 15 of file malloc.c.

◆ HEAP_MAX_POOLSIZE

#define HEAP_MAX_POOLSIZE   (128*1024 - sizeof(Header))

Definition at line 18 of file malloc.c.

◆ HEAP_MIN_POOLSIZE

#define HEAP_MIN_POOLSIZE   (64 - sizeof(Header))

Definition at line 17 of file malloc.c.

◆ HEAP_POOLS

#define HEAP_POOLS   (12)

Definition at line 19 of file malloc.c.

◆ PGSIZE

#define PGSIZE   4096

Definition at line 21 of file malloc.c.

◆ ROUNDUP

#define ROUNDUP (   _x,
  _n 
)    (((_x) + (_n) - 1) & ~((_n) - 1))

Definition at line 32 of file malloc.c.

Typedef Documentation

◆ Header

typedef struct Header Header

◆ HeapPool

typedef struct HeapPool HeapPool

Function Documentation

◆ calloc()

void * calloc ( size_t  num,
size_t  sz 
)

Definition at line 154 of file malloc.c.

155{
156 return malloc(num*sz);
157}
void * malloc(size_t sz)
Definition: malloc.c:160
Here is the call graph for this function:

◆ free()

void free ( void *  mem)

Definition at line 169 of file malloc.c.

170{
171 Header *hdr = (Header *)mem;
172 hdr--;
173
174 if (mem == NULL)
175 return;
176
177 assert(hdr->magic == HEAP_MAGIC);
178
179 if (hdr->size > HEAP_MAX_POOLSIZE)
180 free_large(hdr);
181 else
182 free_small(hdr);
183}
#define assert(_expr)
Definition: assert.h:7
#define HEAP_MAGIC
Definition: malloc.c:15
#define HEAP_MAX_POOLSIZE
Definition: malloc.c:18
uint32_t size
Definition: malloc.c:11
uint16_t magic
Definition: malloc.c:9
static void free_small(Header *mem)
Definition: malloc.c:115
static void free_large(Header *mem)
Definition: malloc.c:148
Definition: malloc.c:8
#define NULL
Definition: stddef.h:6
Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_large()

static void free_large ( Header mem)
static

Definition at line 148 of file malloc.c.

149{
150 munmap(mem, mem->size);
151}
int munmap(void *addr, size_t len)
Definition: mman.c:55
Here is the call graph for this function:
Here is the caller graph for this function:

◆ free_small()

static void free_small ( Header mem)
static

Definition at line 115 of file malloc.c.

116{
117 int idx = size_to_idx(mem->size);
118
119 mem->next = pool[idx].free;
120 pool[idx].free = mem;
121}
Header * free
Definition: malloc.c:27
static int size_to_idx(size_t sz)
Definition: malloc.c:52
struct Header * next
Definition: malloc.c:12
static HeapPool pool[HEAP_POOLS]
Definition: malloc.c:34
Here is the call graph for this function:
Here is the caller graph for this function:

◆ grow_small()

static void grow_small ( int  idx)
static

Definition at line 67 of file malloc.c.

68{
69 int i;
70 size_t bucketSz = (1 << idx) * 64; // Compute bucket size
71 char *addr = (char *)pool[idx].top;
72
73 addr = (char *)mmap(addr, bucketSz * HEAP_INCREMENT,
75 -1, 0);
76 if (addr == NULL) {
77 return;
78 }
79
80 // Update top pointer
81 pool[idx].top += bucketSz * HEAP_INCREMENT;
82
83 for (i = 0; i < HEAP_INCREMENT; i++) {
84 Header *obj = (Header *)(addr + i * bucketSz);
85
86 obj->magic = HEAP_MAGIC;
87 obj->status = 0;
88 obj->size = bucketSz - sizeof(Header);
89 obj->next = pool[idx].free;
90 pool[idx].free = obj;
91 }
92}
uint16_t status
Definition: malloc.c:10
uint64_t top
Definition: malloc.c:29
#define HEAP_INCREMENT
Definition: malloc.c:22
void * mmap(void *addr, size_t len, int prot, int flags, int fd, off_t offset)
Definition: mman.c:35
#define PROT_READ
Definition: mman.h:6
#define MAP_ANON
Definition: mman.h:11
#define PROT_WRITE
Definition: mman.h:7
#define MAP_FIXED
Definition: mman.h:12
uint64_t addr
Definition: multiboot.h:1
Here is the call graph for this function:
Here is the caller graph for this function:

◆ malloc()

void * malloc ( size_t  sz)

Definition at line 160 of file malloc.c.

161{
162 if (sz > HEAP_MAX_POOLSIZE)
163 return malloc_large(sz);
164 else
165 return malloc_small(sz);
166}
static void * malloc_large(size_t sz)
Definition: malloc.c:124
static void * malloc_small(size_t sz)
Definition: malloc.c:95
Here is the call graph for this function:
Here is the caller graph for this function:

◆ malloc_large()

static void * malloc_large ( size_t  sz)
static

Definition at line 124 of file malloc.c.

125{
126 uintptr_t ptr = largePool.top;
127 uintptr_t realSz = ROUNDUP(sz, 4096);
128 Header *addr;
129
130 addr = (Header *)mmap((void *)ptr, realSz,
132 -1, 0);
133 if (addr == NULL) {
134 return NULL;
135 }
136
137 addr->magic = HEAP_MAGIC;
138 addr->status = 0;
139 addr->size = realSz;
140 addr->next = 0;
141
142 largePool.top += realSz;
143
144 return (void *)(addr + 1);
145}
static HeapPool largePool
Definition: malloc.c:49
#define ROUNDUP(_x, _n)
Definition: malloc.c:32
uint64_t uintptr_t
Definition: types.h:16
Here is the call graph for this function:
Here is the caller graph for this function:

◆ malloc_small()

static void * malloc_small ( size_t  sz)
static

Definition at line 95 of file malloc.c.

96{
97 int idx = size_to_idx(sz);
98 Header *hdr;
99
100 // Grow pool if freelist is empty
101 if (pool[idx].free == 0)
102 grow_small(idx);
103
104 // Malloc failed
105 if (pool[idx].free == 0)
106 return NULL;
107
108 hdr = pool[idx].free;
109 pool[idx].free = hdr->next;
110
111 return (void *)(hdr + 1);
112}
void free(void *mem)
Definition: malloc.c:169
static void grow_small(int idx)
Definition: malloc.c:67
Here is the call graph for this function:
Here is the caller graph for this function:

◆ size_to_idx()

static int size_to_idx ( size_t  sz)
static

Definition at line 52 of file malloc.c.

53{
54 int i;
55 size_t bucketSz = 64;
56
57 for (i = 0; ; i++) {
58 if ((bucketSz - sizeof(Header)) >= sz) {
59 return i;
60 }
61
62 bucketSz *= 2;
63 }
64}
Here is the caller graph for this function:

Variable Documentation

◆ largePool

HeapPool largePool = { 0, 0, (Header *)0, 0x4C000000, 0x4C000000 }
static

Definition at line 49 of file malloc.c.

◆ pool

HeapPool pool[HEAP_POOLS]
static
Initial value:
= {
{ 0, 0, (Header *)0, 0x400000000, 0x400000000 },
{ 0, 0, (Header *)0, 0x410000000, 0x410000000 },
{ 0, 0, (Header *)0, 0x420000000, 0x420000000 },
{ 0, 0, (Header *)0, 0x430000000, 0x430000000 },
{ 0, 0, (Header *)0, 0x440000000, 0x440000000 },
{ 0, 0, (Header *)0, 0x450000000, 0x450000000 },
{ 0, 0, (Header *)0, 0x460000000, 0x460000000 },
{ 0, 0, (Header *)0, 0x470000000, 0x470000000 },
{ 0, 0, (Header *)0, 0x480000000, 0x480000000 },
{ 0, 0, (Header *)0, 0x490000000, 0x490000000 },
{ 0, 0, (Header *)0, 0x4A0000000, 0x4A0000000 },
{ 0, 0, (Header *)0, 0x4B0000000, 0x4B0000000 },
}

Definition at line 34 of file malloc.c.