1 #ifndef _NET_MEMORY_H__
2 #define _NET_MEMORY_H__
3 
4 #include <stdlib.h>
5 #include "net_defs.h"
6 #include "net_debug.h"
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 struct mem_element{
13     unsigned int size;
14     unsigned char data[0];
15 };
16 
17 void rt_system_heap_init(void *begin_addr, void *end_addr);
18 void *rt_malloc(size_t size);
19 void *rt_realloc(void *rmem, size_t newsize);
20 void *rt_calloc(size_t count, size_t size);
21 void rt_free(void *rmem);
22 void *rt_kmalloc(size_t size, gfp_t flags);
23 void *rt_kzalloc(size_t size, gfp_t flags);
24 int rt_kfree(void * mem);
25 void *rt_krealloc(const void *p, size_t new_size, gfp_t flags);
26 uint32 rt_get_free_heap_size(void);
27 void print_mem_dump(void * mem);
28 #if 1
29 #define  kmalloc(size, flags) (\
30    {\
31        void* ret = NULL;\
32        ret = rt_kmalloc(size, (gfp_t)flags);\
33        ret;\
34     }\
35 )
36 #else
37 #define  kmalloc(size, flags) (\
38        {\
39            uint8_t* ret = NULL;\
40            ret = rt_kmalloc(size, flags);\
41            printk(KERN_DEBUG, "kmalloc %s %d mem = 0x%x size:%d\n", __func__, __LINE__, ret, size);\
42            ret;\
43         }\
44     )
45 #endif
46 /**
47  * kzalloc - allocate memory. The memory is set to zero.
48  * @size: how many bytes of memory are required.
49  * @flags: the type of memory to allocate (see kmalloc).
50  */
51  #if 1
52  #define kzalloc(size, flags) (\
53    {   \
54        void* ret = NULL;\
55        ret = rt_kzalloc(size, (gfp_t)flags);\
56        ret;\
57    }\
58 )
59  #else
60 #define kzalloc(size, flags) (\
61    {   \
62        uint8_t* ret = NULL;\
63        ret = rt_kzalloc(size, flags);\
64        printk(KERN_DEBUG, "kzalloc %s %d mem = 0x%x\n", __func__, __LINE__, ret);\
65        ret;\
66    }\
67 )
68 #endif
69 /**
70  * kcalloc - allocate memory for an array. The memory is set to zero.
71  * @n: number of elements.
72  * @size: element size.
73  * @flags: the type of memory to allocate (see kmalloc).
74  */
75 #define kcalloc( n,  size, flags) kzalloc(n*size, flags)
76 #ifndef mem_calloc_skb
77 #define mem_calloc_skb(n, size) kzalloc(n*size, 1)
78 #endif
79 
ksize(const void * objp)80 static __INLINE size_t ksize(const void *objp)
81 {
82     struct mem_element* mem_p = NULL;
83     mem_p = (struct mem_element *)((size_t)objp - offsetof(struct mem_element, data));
84     return mem_p->size;
85 }
86 #if 1
87 #define kzfree(mem) ({ \
88     uint32_t ret;\
89     if(mem) \
90         ret = rt_kfree(mem);\
91     else\
92         ret = 0;\
93         ret;\
94 })
95 #else
96 #define kzfree(mem) ({ \
97     uint32_t ret;\
98     printk(KERN_DEBUG, "kzfree %s %d %x \n", __func__, __LINE__, mem);\
99     if(mem) \
100         ret = rt_kfree(mem);\
101     else\
102         ret = 0;\
103         ret;\
104 })
105 
106 #endif
107 #if 1
108 #define kfree(ptr) ({ \
109     uint32_t ret;\
110     if(ptr) \
111         ret = rt_kfree((void *)ptr);\
112     else\
113         ret = 0;\
114         ret;\
115 })
116 
117 #define krealloc(p, new_size, flags) ({\
118     void* ret = NULL;\
119     ret = rt_krealloc(p, new_size, (gfp_t)flags);\
120     ret;\
121 })
122 #else
123 #define kfree(ptr) ({ \
124     uint32_t ret;\
125     printk(KERN_DEBUG, "kfree %s %d %x \n", __func__, __LINE__, ptr);\
126     if(ptr) \
127         ret = rt_kfree(ptr);\
128     else\
129         ret = 0;\
130         ret;\
131 })
132 
133 #define krealloc(p, new_size, flags) ({\
134     uint8_t* ret = NULL;\
135     printk(KERN_DEBUG, "krealloc %s %d src_mem = 0x%x new_size:%d\n", __func__, __LINE__, p, new_size);\
136     ret = rt_krealloc(p, new_size, flags);\
137     printk(KERN_DEBUG, "krealloc mem = 0x%x\n", ret);\
138     ret;\
139 })
140 
141 #endif
142 extern void *kmemdup(const void *src, size_t len, gfp_t gfp);
143 
144 #ifdef __cplusplus
145 }
146 #endif
147 
148 #endif
149 
150