Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/klib/blob/master/kalloc.h # klib [Ported from github.com/attractivechaos/klib] added block arena Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `klib / kalloc.h` 87 lines ยท 2630 bytes Raw Blame History 1 #ifndef _KALLOC_H_ 2 #define _KALLOC_H_ 3 4 #include /* for size_t */ 5 6 #ifdef __cplusplus 7 extern "C" { 8 #endif 9 10 typedef struct { 11 size_t capacity, available, n_blocks, n_cores, largest; 12 } km_stat_t; 13 14 void *kmalloc(void *km, size_t size); 15 void *krealloc(void *km, void *ptr, size_t size); 16 void *krelocate(void *km, void *ap, size_t n_bytes); 17 void *kcalloc(void *km, size_t count, size_t size); 18 void kfree(void *km, void *ptr); 19 20 void *km_init(void); 21 void *km_init2(void *km_par, size_t min_core_size); 22 void km_destroy(void *km); 23 void km_stat(const void *_km, km_stat_t *s); 24 void km_stat_print(const void *km); 25 26 #ifdef __cplusplus 27 } 28 #endif 29 30 #define Kmalloc(km, type, cnt) ((type*)kmalloc((km), (cnt) * sizeof(type))) 31 #define Kcalloc(km, type, cnt) ((type*)kcalloc((km), (cnt), sizeof(type))) 32 #define Krealloc(km, type, ptr, cnt) ((type*)krealloc((km), (ptr), (cnt) * sizeof(type))) 33 34 #define Kexpand(km, type, a, m) do { \ 35 (m) = (m) >= 4? (m) + ((m)>>1) : 16; \ 36 (a) = Krealloc(km, type, (a), (m)); \ 37 } while (0) 38 39 #define KMALLOC(km, ptr, len) ((ptr) = (__typeof__(ptr))kmalloc((km), (len) * sizeof(*(ptr)))) 40 #define KCALLOC(km, ptr, len) ((ptr) = (__typeof__(ptr))kcalloc((km), (len), sizeof(*(ptr)))) 41 #define KREALLOC(km, ptr, len) ((ptr) = (__typeof__(ptr))krealloc((km), (ptr), (len) * sizeof(*(ptr)))) 42 43 #define KEXPAND(km, a, m) do { \ 44 (m) = (m) >= 4? (m) + ((m)>>1) : 16; \ 45 KREALLOC((km), (a), (m)); \ 46 } while (0) 47 48 #ifndef klib_unused 49 #if (defined __clang__ && __clang_major__ >= 3) || (defined __GNUC__ && __GNUC__ >= 3) 50 #define klib_unused __attribute__ ((__unused__)) 51 #else 52 #define klib_unused 53 #endif 54 #endif /* klib_unused */ 55 56 #define KALLOC_POOL_INIT2(SCOPE, name, kmptype_t) \ 57 typedef struct { \ 58 size_t cnt, n, max; \ 59 kmptype_t **buf; \ 60 void *km; \ 61 } kmp_##name##_t; \ 62 SCOPE kmp_##name##_t *kmp_init_##name(void *km) { \ 63 kmp_##name##_t *mp; \ 64 mp = Kcalloc(km, kmp_##name##_t, 1); \ 65 mp->km = km; \ 66 return mp; \ 67 } \ 68 SCOPE void kmp_destroy_##name(kmp_##name##_t *mp) { \ 69 size_t k; \ 70 for (k = 0; k < mp->n; ++k) kfree(mp->km, mp->buf[k]); \ 71 kfree(mp->km, mp->buf); kfree(mp->km, mp); \ 72 } \ 73 SCOPE kmptype_t *kmp_alloc_##name(kmp_##name##_t *mp) { \ 74 ++mp->cnt; \ 75 if (mp->n == 0) return (kmptype_t*)kcalloc(mp->km, 1, sizeof(kmptype_t)); \ 76 return mp->buf[--mp->n]; \ 77 } \ 78 SCOPE void kmp_free_##name(kmp_##name##_t *mp, kmptype_t *p) { \ 79 --mp->cnt; \ 80 if (mp->n == mp->max) Kexpand(mp->km, kmptype_t*, mp->buf, mp->max); \ 81 mp->buf[mp->n++] = p; \ 82 } 83 84 #define KALLOC_POOL_INIT(name, kmptype_t) \ 85 KALLOC_POOL_INIT2(static inline klib_unused, name, kmptype_t) 86 87 #endif