@@ -1,4 +1,5 @@ +#include #include #include #include #include @@ -19,9 +20,16 @@ void* kmheapa(sz len) { /* allocate an object on the heap and return * a pointer, or NULL if the allocation failed. */ - void* val; + 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 @@ -39,25 +47,26 @@ * 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, + 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 == (void*) -1) return null; + if (region.raw == (void*) -1) return null; /* worth retrieving errno? discuss */ - *((sz*)region) = len; - val = region + sizeof len; - # 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 val; + return object; }