Overview
Comment: | add memory functions |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA3-256: |
5393623a84cb48a17c52c45274ffa544 |
User & Date: | lexi on 2019-08-18 11:34:59 |
Other Links: | manifest | tags |
Context
2019-08-18
| ||
13:42 | add functions, generate C syscall table check-in: a8d93823f1 user: lexi tags: trunk | |
11:34 | add memory functions check-in: 5393623a84 user: lexi tags: trunk | |
10:20 | fix kmheapa() and add kmheapf() check-in: 5279674525 user: lexi tags: trunk | |
Changes
Modified kcore/testbin.exe.c from [8406c82300] to [a4a4905883].
17 17 maybe = no; 18 18 19 19 if (kiosend(e.std, ptr, null) == kiocond_ok) { 20 20 /* great, continue */ 21 21 } else { 22 22 return kbad_io; 23 23 } 24 + kmptr object = kmheapao(sizeof (struct object) * 16); 25 + if (object.kind == kmkind_fail) return kbad_mem; 24 26 25 - struct object* block = kmheapa(sizeof (struct object) * 4); 26 - if (block == null) return kbad_mem; 27 + /* struct object* block = kmheapa(sizeof (struct object) * 4); */ 27 28 28 - block[1].a = 5; 29 + struct object* block = object.ref; 30 + block[5].a = 5; 29 31 30 - if (kmheapf(block) != kmcond_ok) return kbad_mem; 32 + if (kmfree(object) != kmcond_ok) return kbad_mem; 31 33 32 34 return kbad_ok; 33 35 }
Modified kmem/heapa.fn.c from [aa6d2182a2] to [8818444ab8].
1 +#include <k/mem.h> 1 2 #include <k/core.h> 2 3 #include <k/def.h> 3 4 #include <k/type.h> 4 5 #include <posix.h> 5 6 /* heapa.c - kmheapa() "heap alloc" 6 7 * ~ lexi hale <lexi@hale.su> 7 8 * kmheapa() allocates a pointer on the heap à la libc malloc() ................................................................................ 16 17 extern void* kmem_platform_mmap(void* addr, 17 18 unsigned long sz, unsigned long prot, unsigned long flags, 18 19 unsigned long fd, unsigned long off); 19 20 20 21 void* kmheapa(sz len) { 21 22 /* allocate an object on the heap and return 22 23 * a pointer, or NULL if the allocation failed. */ 23 - void* val; 24 + union { 25 + void* raw; 26 + ubyte* byte; 27 + kmbox* header; 28 + } region; 29 + /* we need to allocate space for the header and 30 + * for the actual object */ 31 + sz region_size = sizeof(kmbox) + len; 24 32 25 33 # ifdef KFenv_posix 26 34 /* posix APIs - we've got it easy. currently for nonlinear 27 35 * heap allocation kmheapa simply uses m(un)map and lets the 28 36 * kernel worry about it. it may ultimately be worth replacing 29 37 * this with a more sophisticated implementation, most likely 30 38 * an existing allocator like jemalloc, though i'm wary of ................................................................................ 36 44 /* because munmap needs to be informed of the size of 37 45 * the region it is going to unmap, we need to store 38 46 * that information in the allocated region itself. 39 47 * the user will be given a pointer that can be 40 48 * adjusted to point a field of type size_t that 41 49 * contains the size of the allocate space.*/ 42 50 43 - sz const region_total = len + sizeof len; 44 - ubyte* const region = kmem_platform_mmap(null, region_total, 51 + region.byte = kmem_platform_mmap(null, region_size, 45 52 posix_prot_read | posix_prot_write, 46 53 posix_flag_anonymous | posix_map_shared, -1, 0); 47 54 /* impl note: while per manpage fd is "ignored" 48 55 * for MAP_ANONYMOUS, "some implementations" require 49 56 * a value of -1 */ 50 57 51 - if (region == (void*) -1) return null; 58 + if (region.raw == (void*) -1) return null; 52 59 /* worth retrieving errno? discuss */ 53 60 54 - *((sz*)region) = len; 55 - val = region + sizeof len; 56 - 57 61 # else 58 62 Knoimpl(kmheapa,KVos); 59 63 # error missing implementation 60 64 # endif 65 + 66 + void* const object = (region.byte + sizeof (kmbox)); 67 + 68 + region.header -> kind = kmkind_heap; 69 + region.header -> size = len; 61 70 62 - return val; 71 + return object; 63 72 }
Modified kmem/kmem.md from [5405a3e53e] to [73d0a96333].
8 8 9 9 kmem supplies two module-level functions, used to interact with the `kmptr` container type. 10 10 11 11 * `kmfree(kmptr) → void` - free, downref, or ignore the pasted object as appropriate 12 12 * `kmshred(kmptr) → void` - free, downref, or ignore the pasted object as appropriate. if deallocating, zero its contents 13 13 * `kmstat(void*) → kmptr` - convenience function to wrap a pointer to a non-managed object in a `kmptr` struct, so it can be passed to functions that accept arbitrary objects. `kmptr p = kmstat(raw)` is equivalent to `kmptr p = { kmkind_none, raw, NULL }`. 14 14 * `kmtaint(&kmptr) → void` - "taints" a `kmptr` object by setting it to be shredded when freed. this may be desirable if the object pointed to contains privileged information. 15 + * `kmzero(void*,sz) → void` - zeroes a region of memory 16 + * `kmozero(kmptr) → void` - zeroes an object in memory 17 + * `kmcopy(void* dest, void* src, sz) → void` - copies one region of memory to another 18 + * `kmdup(kmptr) → kmptr` - duplicates an object in memory, allocating it as sibling of the original 15 19 16 20 ## types 17 21 18 22 kmem defines the following types: 19 23 20 24 * `enum kmkind` - enumerates allocation strategies 21 25 * `struct kmptr` - abstract pointer object
Modified kmem/mem.h from [09be45a2b3] to [bec7e80543].
8 8 9 9 #ifdef __cplusplus 10 10 extern "C" { 11 11 #endif 12 12 13 13 typedef enum kmcond { 14 14 kmcond_ok, 15 + kmcond_unnecessary, 16 + 17 + kmcond_fail, 18 + kmcond_unhandled, 19 + kmcond_mismatch, 15 20 kmcond_bad_address, 16 21 } kmcond; 17 22 18 23 typedef enum kmkind { 19 24 kmkind_none, 25 + kmkind_fail, 26 + kmkind_linear, 20 27 kmkind_heap, 21 28 kmkind_pool, 22 29 kmkind_ref, 23 30 kmkind_tree 24 31 } kmkind; 25 32 26 33 typedef enum kmshred { 27 34 kmshred_yes, 28 35 kmshred_no 29 36 } kmshred; 30 37 31 -typedef struct kmcell { 38 +typedef struct kmbox { 32 39 kmkind kind; 33 40 sz size; 34 41 kmshred shred; 42 +} kmbox; 43 + 44 +typedef struct kmcell { 45 + kmbox header; 35 46 sz refs; 36 47 struct kmcell* src; 37 48 } kmcell; 49 + 50 +typedef struct kmnode { 51 + kmbox header; 52 + struct kmnode* origin, 53 + * branch, 54 + * next, 55 + * prev; 56 +} kmnode; 38 57 39 58 typedef struct kmptr { 40 59 kmkind kind; 41 60 kmshred shred; 42 61 void* ref; 43 - kmcell* cell; 44 62 } kmptr; 45 63 46 64 /* heap functions */ 47 65 48 66 void* kmheapa(sz); 67 +kmptr kmheapao(sz); 49 68 kmcond kmheapf(void*); 69 + 70 +/* generic functions */ 71 + 72 +kmcond kmfree(kmptr); 73 +kmkind kmtell(void*); 74 +void kmzero(void*,sz); 75 +void kmozero(kmptr); 76 + 50 77 51 78 #ifdef __cplusplus 52 79 } 53 80 #endif 54 81 55 82 #endif