1 #ifndef _IP_SET_MALLOC_H
2 #define _IP_SET_MALLOC_H
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
;
21 #define CACHE(x) if (max_page_size == 0 || x < max_page_size) \
23 #include <linux/kmalloc_sizes.h>
26 if (max_malloc_size
== 0)
27 max_malloc_size
= page_size
;
29 max_page_size
= page_size
;
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
)
53 max_elements
= max_page_size
/typesize
;
54 size
= hashsize
/max_elements
;
55 if (hashsize
% max_elements
)
58 /* Last pointer signals end of arrays */
59 harray
= kmalloc(sizeof(struct harray
) + (size
+ 1) * sizeof(void *),
65 for (i
= 0; i
< size
- 1; i
++) {
66 harray
->arrays
[i
] = kmalloc(max_elements
* typesize
, flags
);
67 if (!harray
->arrays
[i
])
69 memset(harray
->arrays
[i
], 0, max_elements
* typesize
);
71 harray
->arrays
[i
] = kmalloc((hashsize
- i
* max_elements
) * typesize
,
73 if (!harray
->arrays
[i
])
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
;
83 for (j
= 0; j
< i
; j
++) {
84 kfree(harray
->arrays
[j
]);
91 harray_malloc(size_t hashsize
, size_t typesize
, gfp_t flags
)
96 harray
= __harray_malloc(hashsize
, typesize
, flags
|__GFP_NOWARN
);
97 } while (harray
== NULL
&& init_max_page_size());
102 static inline void harray_free(void *h
)
104 struct harray
*harray
= (struct harray
*) h
;
107 for (i
= 0; harray
->arrays
[i
] != NULL
; i
++)
108 kfree(harray
->arrays
[i
]);
112 static inline void harray_flush(void *h
, size_t hashsize
, size_t typesize
)
114 struct harray
*harray
= (struct harray
*) h
;
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) \
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
);
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
)
151 #endif /* __KERNEL__ */
153 #endif /*_IP_SET_MALLOC_H*/