1
2
3
4
5
6
7
..
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
..
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
#include <k/core.h>
#include <k/def.h>
#include <k/type.h>
#include <posix.h>
/* heapa.c - kmheapa() "heap alloc"
* ~ lexi hale <lexi@hale.su>
* kmheapa() allocates a pointer on the heap à la libc malloc()
................................................................................
extern void* kmem_platform_mmap(void* addr,
unsigned long sz, unsigned long prot, unsigned long flags,
unsigned long fd, unsigned long off);
void* kmheapa(sz len) {
/* allocate an object on the heap and return
* a pointer, or NULL if the allocation failed. */
void* val;
# ifdef KFenv_posix
/* posix APIs - we've got it easy. currently for nonlinear
* heap allocation kmheapa simply uses m(un)map and lets the
* kernel worry about it. it may ultimately be worth replacing
* this with a more sophisticated implementation, most likely
* an existing allocator like jemalloc, though i'm wary of
................................................................................
/* because munmap needs to be informed of the size of
* the region it is going to unmap, we need to store
* that information in the allocated region itself.
* the user will be given a pointer that can be
* adjusted to point a field of type size_t that
* contains the size of the allocate space.*/
sz const region_total = len + sizeof len;
ubyte* const region = kmem_platform_mmap(null, region_total,
posix_prot_read | posix_prot_write,
posix_flag_anonymous | posix_map_shared, -1, 0);
/* impl note: while per manpage fd is "ignored"
* for MAP_ANONYMOUS, "some implementations" require
* a value of -1 */
if (region == (void*) -1) return null;
/* worth retrieving errno? discuss */
*((sz*)region) = len;
val = region + sizeof len;
# else
Knoimpl(kmheapa,KVos);
# error missing implementation
# endif
return val;
}
|
>
>
|
>
>
>
>
>
>
<
|
|
<
<
<
|
>
>
>
>
>
|
|
1
2
3
4
5
6
7
8
..
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
..
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
#include <k/mem.h>
#include <k/core.h>
#include <k/def.h>
#include <k/type.h>
#include <posix.h>
/* heapa.c - kmheapa() "heap alloc"
* ~ lexi hale <lexi@hale.su>
* kmheapa() allocates a pointer on the heap à la libc malloc()
................................................................................
extern void* kmem_platform_mmap(void* addr,
unsigned long sz, unsigned long prot, unsigned long flags,
unsigned long fd, unsigned long off);
void* kmheapa(sz len) {
/* allocate an object on the heap and return
* a pointer, or NULL if the allocation failed. */
union {
void* raw;
ubyte* byte;
kmbox* header;
} region;
/* we need to allocate space for the header and
* for the actual object */
sz region_size = sizeof(kmbox) + len;
# ifdef KFenv_posix
/* posix APIs - we've got it easy. currently for nonlinear
* heap allocation kmheapa simply uses m(un)map and lets the
* kernel worry about it. it may ultimately be worth replacing
* this with a more sophisticated implementation, most likely
* an existing allocator like jemalloc, though i'm wary of
................................................................................
/* because munmap needs to be informed of the size of
* the region it is going to unmap, we need to store
* that information in the allocated region itself.
* the user will be given a pointer that can be
* adjusted to point a field of type size_t that
* contains the size of the allocate space.*/
region.byte = kmem_platform_mmap(null, region_size,
posix_prot_read | posix_prot_write,
posix_flag_anonymous | posix_map_shared, -1, 0);
/* impl note: while per manpage fd is "ignored"
* for MAP_ANONYMOUS, "some implementations" require
* a value of -1 */
if (region.raw == (void*) -1) return null;
/* worth retrieving errno? discuss */
# else
Knoimpl(kmheapa,KVos);
# error missing implementation
# endif
void* const object = (region.byte + sizeof (kmbox));
region.header -> kind = kmkind_heap;
region.header -> size = len;
return object;
}
|