allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / include / linux / netfilter_ipv4 / ip_set_malloc.h
blob2a80443f0abca75382969029495efa975019db74
1 #ifndef _IP_SET_MALLOC_H
2 #define _IP_SET_MALLOC_H
4 #ifdef __KERNEL__
5 #include <linux/vmalloc.h>
7 static size_t max_malloc_size = 0, max_page_size = 0;
8 static size_t default_max_malloc_size = 131072; /* Guaranteed: slab.c */
10 static inline int init_max_page_size(void)
12 /* Compatibility glues to support 2.4.36 */
13 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
14 #define __GFP_NOWARN 0
16 /* Guaranteed: slab.c */
17 max_malloc_size = max_page_size = default_max_malloc_size;
18 #else
19 size_t page_size = 0;
21 #define CACHE(x) if (max_page_size == 0 || x < max_page_size) \
22 page_size = x;
23 #include <linux/kmalloc_sizes.h>
24 #undef CACHE
25 if (page_size) {
26 if (max_malloc_size == 0)
27 max_malloc_size = page_size;
29 max_page_size = page_size;
31 return 1;
33 #endif
34 return 0;
37 struct harray {
38 size_t max_elements;
39 void *arrays[0];
42 static inline void *
43 __harray_malloc(size_t hashsize, size_t typesize, gfp_t flags)
45 struct harray *harray;
46 size_t max_elements, size, i, j;
48 BUG_ON(max_page_size == 0);
50 if (typesize > max_page_size)
51 return NULL;
53 max_elements = max_page_size/typesize;
54 size = hashsize/max_elements;
55 if (hashsize % max_elements)
56 size++;
58 /* Last pointer signals end of arrays */
59 harray = kmalloc(sizeof(struct harray) + (size + 1) * sizeof(void *),
60 flags);
62 if (!harray)
63 return NULL;
65 for (i = 0; i < size - 1; i++) {
66 harray->arrays[i] = kmalloc(max_elements * typesize, flags);
67 if (!harray->arrays[i])
68 goto undo;
69 memset(harray->arrays[i], 0, max_elements * typesize);
71 harray->arrays[i] = kmalloc((hashsize - i * max_elements) * typesize,
72 flags);
73 if (!harray->arrays[i])
74 goto undo;
75 memset(harray->arrays[i], 0, (hashsize - i * max_elements) * typesize);
77 harray->max_elements = max_elements;
78 harray->arrays[size] = NULL;
80 return (void *)harray;
82 undo:
83 for (j = 0; j < i; j++) {
84 kfree(harray->arrays[j]);
86 kfree(harray);
87 return NULL;
90 static inline void *
91 harray_malloc(size_t hashsize, size_t typesize, gfp_t flags)
93 void *harray;
95 do {
96 harray = __harray_malloc(hashsize, typesize, flags|__GFP_NOWARN);
97 } while (harray == NULL && init_max_page_size());
99 return harray;
102 static inline void harray_free(void *h)
104 struct harray *harray = (struct harray *) h;
105 size_t i;
107 for (i = 0; harray->arrays[i] != NULL; i++)
108 kfree(harray->arrays[i]);
109 kfree(harray);
112 static inline void harray_flush(void *h, size_t hashsize, size_t typesize)
114 struct harray *harray = (struct harray *) h;
115 size_t i;
117 for (i = 0; harray->arrays[i+1] != NULL; i++)
118 memset(harray->arrays[i], 0, harray->max_elements * typesize);
119 memset(harray->arrays[i], 0,
120 (hashsize - i * harray->max_elements) * typesize);
123 #define HARRAY_ELEM(h, type, which) \
124 ({ \
125 struct harray *__h = (struct harray *)(h); \
126 ((type)((__h)->arrays[(which)/(__h)->max_elements]) \
127 + (which)%(__h)->max_elements); \
130 /* General memory allocation and deallocation */
131 static inline void * ip_set_malloc(size_t bytes)
133 BUG_ON(max_malloc_size == 0);
135 if (bytes > default_max_malloc_size)
136 return vmalloc(bytes);
137 else
138 return kmalloc(bytes, GFP_KERNEL | __GFP_NOWARN);
141 static inline void ip_set_free(void * data, size_t bytes)
143 BUG_ON(max_malloc_size == 0);
145 if (bytes > default_max_malloc_size)
146 vfree(data);
147 else
148 kfree(data);
151 #endif /* __KERNEL__ */
153 #endif /*_IP_SET_MALLOC_H*/