1 /* Copyright (c) 2009 The Regents of the University of California.
2 * See the COPYRIGHT files at the top of this source tree for full
5 * Kevin Klues <klueska@cs.berkeley.edu>
10 #include <ros/common.h>
13 #define NUM_KMALLOC_CACHES 6
14 #define KMALLOC_ALIGNMENT 16
15 #define KMALLOC_SMALLEST (sizeof(struct kmalloc_tag) << 1)
16 #define KMALLOC_LARGEST KMALLOC_SMALLEST << NUM_KMALLOC_CACHES
18 void kmalloc_init(void);
19 void *kmalloc(size_t size, int flags);
20 void *kmalloc_array(size_t nmemb, size_t size, int flags);
21 void *kzmalloc(size_t size, int flags);
22 void *kmalloc_align(size_t size, int flags, size_t align);
23 void *kzmalloc_align(size_t size, int flags, size_t align);
24 void *krealloc(void *buf, size_t size, int flags);
25 void *kreallocarray(void *buf, size_t nmemb, size_t size, int flags);
26 int kmalloc_refcnt(void *buf);
27 void kmalloc_incref(void *buf);
28 void kfree(void *buf);
29 void kmalloc_canary_check(char *str);
32 #define MEM_ATOMIC (1 << 1)
33 #define MEM_WAIT (1 << 2)
34 #define MEM_ERROR (1 << 3)
35 #define MEM_FLAGS (MEM_ATOMIC | MEM_WAIT | MEM_ERROR)
37 /* Kmalloc tag flags looks like this:
39 * +--------------28---------------+-----4------+
40 * | Flag specific data | Flags |
41 * +-------------------------------+------------+
43 #define KMALLOC_TAG_CACHE 1 /* memory came from slabs */
44 #define KMALLOC_TAG_PAGES 2 /* memory came from page allocator */
45 #define KMALLOC_TAG_UNALIGN 3 /* not a real tag, jump back by offset */
46 #define KMALLOC_ALIGN_SHIFT 4 /* max flag is 16 */
47 #define KMALLOC_FLAG_MASK ((1 << KMALLOC_ALIGN_SHIFT) - 1)
49 #define KMALLOC_CANARY 0xdeadbabe
51 /* The kmalloc align/free paths require that flags is at the end of this
52 * struct, and that it is not padded. */
55 struct kmem_cache *my_cache;
57 uint64_t unused_force_align;
64 /* This is aligned so that the buf is aligned to the usual kmalloc alignment. */
68 } __attribute__((aligned(KMALLOC_ALIGNMENT)));
70 /* Allocate a sized_alloc, big enough to hold size bytes. Free with kfree. */
71 struct sized_alloc *sized_kzmalloc(size_t size, int flags);