]> git.buserror.net Git - polintos/scott/priv.git/blob - kernel/lib/libc.cc
Cause excessively large malloc()s to fail rather than assert.
[polintos/scott/priv.git] / kernel / lib / libc.cc
1 // lib/libc.cc -- Standard C-library functions
2 //
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
4 // 
5 // This software is provided 'as-is', without any express or implied warranty.
6 // In no event will the authors or contributors be held liable for any damages
7 // arising from the use of this software.
8 // 
9 // Permission is hereby granted to everyone, free of charge, to use, copy,
10 // modify, prepare derivative works of, publish, distribute, perform,
11 // sublicense, and/or sell copies of the Software, provided that the above
12 // copyright notice and disclaimer of warranty be included in all copies or
13 // substantial portions of this software.
14
15
16 #include <kern/types.h>
17 #include <kern/libc.h>
18 #include <kern/console.h>
19
20 #include <stdarg.h>
21 #include <limits.h>
22
23 void bzero(void *b, size_t len)
24 {
25         char *c = static_cast<char *>(b);
26         
27         while (len--)
28                 *c++ = 0;
29 }
30
31 #include <kern/pagealloc.h>
32
33 // Temporary hack until slab allocator is added
34
35 void *malloc(size_t len)
36 {
37         if (len > Arch::page_size - sizeof(size_t))
38                 return NULL;
39
40         len = (len + sizeof(size_t) + Arch::page_size - 1) / Arch::page_size;
41         Mem::Page *page = Mem::PageAlloc::alloc(len);
42         
43         size_t *ptr = (size_t *)Mem::page_to_kvirt(page);
44         *ptr = len;
45         
46         return ptr + 1;
47 }
48
49 void free(void *addr)
50 {
51         if (addr) {
52                 size_t *ptr = (size_t *)addr - 1;
53                 size_t len = *ptr;
54                 Mem::Page *page = Mem::kvirt_to_page(addr);
55                 Mem::PageAlloc::free(page, len);
56         }
57 }
58
59 void *operator new(size_t len)
60 {
61         return malloc(len);
62 }
63
64 void *operator new[](size_t len)
65 {
66         return malloc(len);
67 }
68
69 void operator delete(void *addr)
70 {
71         free(addr);
72 }
73
74 void operator delete[](void *addr)
75 {
76         free(addr);
77 }
78
79 extern "C" void __cxa_pure_virtual()
80 {
81         BUG();
82 }
83
84 void abort()
85 {
86         in_fault++;
87         printf("abort() called in kernel\n");
88         __builtin_trap();
89 }