Skip to content

Commit 7de789d

Browse files
committed
Lecture 115 - Making the process_realloc function
1 parent 3ec9810 commit 7de789d

16 files changed

Lines changed: 113 additions & 0 deletions

File tree

376 Bytes
Binary file not shown.
376 Bytes
Binary file not shown.
32 Bytes
Binary file not shown.
856 Bytes
Binary file not shown.

PeachOS64Bit/programs/stdlib/src/peachos.asm

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ global peachos_fclose:function
1616
global peachos_fread:function
1717
global peachos_fseek:function
1818
global peachos_fstat:function
19+
global peachos_realloc:function
1920

2021
; void print(const char* filename)
2122
print:
@@ -131,4 +132,14 @@ peachos_fstat:
131132
push qword rdi ; fd
132133
int 0x80 ; call kernel
133134
add rsp, 16 ; restore stack
135+
ret
136+
137+
; void* peachos_realloc(void* old_ptr, size_t new_size);
138+
peachos_realloc:
139+
mov rax, 15 ; Command 15 realloc
140+
push qword rsi ; new_size
141+
push qword rdi ; old_ptr
142+
int 0x80
143+
add rsp, 16
144+
; RAX = new the pointer address
134145
ret

PeachOS64Bit/programs/stdlib/src/peachos.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,5 @@ void peachos_fclose(size_t fd);
3939
long peachos_fread(void* buffer, size_t size, size_t count, long fd);
4040
long peachos_fseek(long fd, long offset, long whence);
4141
long peachos_fstat(long fd, struct file_stat* file_stat_out);
42+
void* peachos_realloc(void* old_ptr, size_t new_size);
4243
#endif

PeachOS64Bit/programs/stdlib/src/stdlib.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "stdlib.h"
22
#include "peachos.h"
3+
#include "memory.h"
34

45
char* itoa(int i)
56
{
@@ -32,6 +33,23 @@ void* malloc(size_t size)
3233
{
3334
return peachos_malloc(size);
3435
}
36+
void* calloc(size_t n_memb, size_t size)
37+
{
38+
size_t b_size = n_memb * size;
39+
void* ptr = malloc(b_size);
40+
if (!ptr)
41+
{
42+
return NULL;
43+
}
44+
45+
memset(ptr, 0, b_size);
46+
return ptr;
47+
}
48+
49+
void* realloc(void* ptr, size_t new_size)
50+
{
51+
return peachos_realloc(ptr, new_size);
52+
}
3553

3654
void free(void* ptr)
3755
{

PeachOS64Bit/programs/stdlib/src/stdlib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#define PEACHOS_STDLIB_H
33
#include <stddef.h>
44

5+
void* calloc(size_t n_memb, size_t size);
6+
void* realloc(void* ptr, size_t new_size);
57
void* malloc(size_t size);
68
void free(void* ptr);
79
char* itoa(int i);
848 Bytes
Binary file not shown.

PeachOS64Bit/src/isr80h/heap.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
#include "task/task.h"
33
#include "task/process.h"
44
#include <stddef.h>
5+
6+
void* isr80h_command15_realloc(struct interrupt_frame* frame)
7+
{
8+
void* userland_virt_addr = (void*) task_get_stack_item(task_current(), 0);
9+
void* new_alloc_addr = NULL;
10+
size_t new_ptr_size = (size_t) task_get_stack_item(task_current(), 1);
11+
new_alloc_addr = process_realloc(task_current()->process, userland_virt_addr, new_ptr_size);
12+
return new_alloc_addr;
13+
}
14+
515
void* isr80h_command4_malloc(struct interrupt_frame* frame)
616
{
717
size_t size = (uintptr_t)task_get_stack_item(task_current(), 0);

0 commit comments

Comments
 (0)