libk  Diff

Differences From Artifact [f32eb209ee]:

To Artifact [aa6d2182a2]:


     1      1   #include <k/core.h>
     2      2   #include <k/def.h>
            3  +#include <k/type.h>
            4  +#include <posix.h>
     3      5   /* heapa.c - kmheapa() "heap alloc"
     4      6    * ~ lexi hale <lexi@hale.su>
     5      7    * kmheapa() allocates a pointer on the heap à la libc malloc()
     6      8    * see also: kmheapf() "heap free"
     7      9    */
     8     10   
     9     11   /* we define all platform functions here,
    10     12    * whether or not they're for the correct
    11     13    * platform - only the ones actually called
    12     14    * by the generated code will be linked,
    13     15    * linker errors are our friend here! */
    14         -extern void* kmem_posix_mmap(void* addr,
           16  +extern void* kmem_platform_mmap(void* addr,
    15     17   		unsigned long sz, unsigned long prot, unsigned long flags,
    16     18   		unsigned long fd, unsigned long off);
    17     19   
    18         -enum posix_prot {
    19         -	posix_prot_none  = 0,
    20         -	posix_prot_read  = 1 << 0,
    21         -	posix_prot_write = 1 << 1,
    22         -	posix_prot_exec  = 1 << 2
    23         -};
    24         -
    25         -enum posix_map {
    26         -	posix_map_shared  = 1,
    27         -	posix_map_private = 2
    28         -};
    29         -
    30         -enum posix_flag {
    31         -	posix_flag_fixed     = 0x10,
    32         -	posix_flag_anonymous = 0x20,
    33         -
    34         -	/* platform flags */
    35         -	posix_flag_linux_hugetlb = 0x40000
    36         -};
    37         -
    38     20   void* kmheapa(sz len) {
    39     21   	/* allocate an object on the heap and return
    40     22   	 * a pointer, or NULL if the allocation failed. */
    41     23   	void* val;
    42     24   
    43     25   #	ifdef KFenv_posix
    44         - 	   /* posix APIs - we've got it easy */
    45         -		val = kmem_posix_mmap(null, len, posix_prot_read | posix_prot_write,
    46         -				posix_flag_anonymous, -1, 0);
           26  + 	   /* posix APIs - we've got it easy. currently for nonlinear
           27  +		* heap allocation kmheapa simply uses m(un)map and lets the 
           28  +		* kernel worry about it. it may ultimately be worth replacing
           29  +		* this with a more sophisticated implementation, most likely
           30  +		* an existing allocator like jemalloc, though i'm wary of
           31  +		* including outside code - it creates a licensing mess and
           32  +		* i'd prefer libk to be AGPLv3 across the board. possibility:
           33  +		* include hooks for multiple allocators, allowing the user
           34  +		* to select & link in her preferred allocator at compile time? */
           35  +
           36  +		/* because munmap needs to be informed of the size of
           37  +		 * the region it is going to unmap, we need to store
           38  +		 * that information in the allocated region itself.
           39  +		 * the user will be given a pointer that can be
           40  +		 * adjusted to point a field of type size_t that
           41  +		 * contains the size of the allocate space.*/
           42  +
           43  +		sz     const region_total = len + sizeof len;
           44  +		ubyte* const region = kmem_platform_mmap(null, region_total,
           45  +				posix_prot_read | posix_prot_write,
           46  +				posix_flag_anonymous | posix_map_shared, -1, 0);
    47     47   		/* impl note: while per manpage fd is "ignored"
    48     48   		 * for MAP_ANONYMOUS, "some implementations" require
    49     49   		 * a value of -1 */
    50     50   
    51         -		if (val == (void*) -1) return null;
           51  +		if (region == (void*) -1) return null;
    52     52   		/* worth retrieving errno? discuss */
    53     53   
           54  +		*((sz*)region) = len;
           55  +		val = region + sizeof len;
           56  +
    54     57   #	else
    55     58    	   Knoimpl(kmheapa,KVos);
    56     59   #		error missing implementation
    57     60   #	endif
    58     61   
    59     62   	return val;
    60     63   }