MCPcopy Create free account
hub / github.com/SamyPesse/How-to-Make-a-Computer-Operating-System / kmalloc

Function kmalloc

src/kernel/arch/x86/alloc.cc:43–110  ·  view source on GitHub ↗

allocate memory block */

Source from the content-addressed store, hash-verified

41
42 /* allocate memory block */
43 void *kmalloc(unsigned long size)
44 {
45 if (size==0)
46 return 0;
47
48 unsigned long realsize; /* taille totale de l'enregistrement */
49 struct kmalloc_header *chunk, *other;
50
51 if ((realsize =
52 sizeof(struct kmalloc_header) + size) < KMALLOC_MINSIZE)
53 realsize = KMALLOC_MINSIZE;
54
55 /*
56 * On recherche un bloc libre de 'size' octets en parcourant le HEAP
57 * kernel a partir du debut
58 */
59 chunk = (struct kmalloc_header *) KERN_HEAP;
60 while (chunk->used || chunk->size < realsize) {
61 if (chunk->size == 0) {
62 io.print
63 ("\nPANIC: kmalloc(): corrupted chunk on %x with null size (heap %x) !\nSystem halted\n",
64 chunk, kern_heap);
65 //error
66 asm("hlt");
67 return 0;
68 }
69
70 chunk =
71 (struct kmalloc_header *) ((char *) chunk +
72 chunk->size);
73
74 if (chunk == (struct kmalloc_header *) kern_heap) {
75 if ((int)(ksbrk((realsize / PAGESIZE) + 1)) < 0) {
76 io.print
77 ("\nPANIC: kmalloc(): no memory left for kernel !\nSystem halted\n");
78 asm("hlt");
79 return 0;
80 }
81 } else if (chunk > (struct kmalloc_header *) kern_heap) {
82 io.print
83 ("\nPANIC: kmalloc(): chunk on %x while heap limit is on %x !\nSystem halted\n",
84 chunk, kern_heap);
85 asm("hlt");
86 return 0;
87 }
88 }
89
90 /*
91 * Found free block with size >= 'size'
92 * We limit size block
93 */
94 if (chunk->size - realsize < KMALLOC_MINSIZE)
95 chunk->used = 1;
96 else {
97 other =
98 (struct kmalloc_header *) ((char *) chunk + realsize);
99 other->size = chunk->size - realsize;
100 other->used = 0;

Callers 15

isr_PF_excFunction · 0.85
createProcMethod · 0.85
get_page_from_heapFunction · 0.85
release_page_from_heapFunction · 0.85
Memory_initFunction · 0.85
pd_createFunction · 0.85
page_copy_in_pdFunction · 0.85
pd_copyFunction · 0.85
DosPartitionMethod · 0.85
ext2_get_disk_infoFunction · 0.85
ext2_check_diskFunction · 0.85
ext2_read_inodeFunction · 0.85

Calls 2

ksbrkFunction · 0.85
printMethod · 0.80

Tested by

no test coverage detected