allocate memory block */
| 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; |
no test coverage detected