1 #ifndef _IP_SET_MALLOC_H
2 #define _IP_SET_MALLOC_H
6 /* Memory allocation and deallocation */
7 static size_t max_malloc_size = 0;
9 static inline void init_max_malloc_size(void)
11 #define CACHE(x) max_malloc_size = x;
12 #include <linux/kmalloc_sizes.h>
16 static inline void * ip_set_malloc(size_t bytes)
18 if (bytes > max_malloc_size)
19 return vmalloc(bytes);
21 return kmalloc(bytes, GFP_KERNEL);
24 static inline void ip_set_free(void * data, size_t bytes)
26 if (bytes > max_malloc_size)
38 harray_malloc(size_t hashsize, size_t typesize, int flags)
40 struct harray *harray;
41 size_t max_elements, size, i, j;
44 init_max_malloc_size();
46 if (typesize > max_malloc_size)
49 max_elements = max_malloc_size/typesize;
50 size = hashsize/max_elements;
51 if (hashsize % max_elements)
54 /* Last pointer signals end of arrays */
55 harray = kmalloc(sizeof(struct harray) + (size + 1) * sizeof(void *),
61 for (i = 0; i < size - 1; i++) {
62 harray->arrays[i] = kmalloc(max_elements * typesize, flags);
63 if (!harray->arrays[i])
65 memset(harray->arrays[i], 0, max_elements * typesize);
67 harray->arrays[i] = kmalloc((hashsize - i * max_elements) * typesize,
69 if (!harray->arrays[i])
71 memset(harray->arrays[i], 0, (hashsize - i * max_elements) * typesize);
73 harray->max_elements = max_elements;
74 harray->arrays[size] = NULL;
76 return (void *)harray;
79 for (j = 0; j < i; j++) {
80 kfree(harray->arrays[j]);
86 static inline void harray_free(void *h)
88 struct harray *harray = (struct harray *) h;
91 for (i = 0; harray->arrays[i] != NULL; i++)
92 kfree(harray->arrays[i]);
96 static inline void harray_flush(void *h, size_t hashsize, size_t typesize)
98 struct harray *harray = (struct harray *) h;
101 for (i = 0; harray->arrays[i+1] != NULL; i++)
102 memset(harray->arrays[i], 0, harray->max_elements * typesize);
103 memset(harray->arrays[i], 0,
104 (hashsize - i * harray->max_elements) * typesize);
107 #define HARRAY_ELEM(h, type, which) \
109 struct harray *__h = (struct harray *)(h); \
110 ((type)((__h)->arrays[(which)/(__h)->max_elements]) \
111 + (which)%(__h)->max_elements); \
114 #endif /* __KERNEL__ */
116 #endif /*_IP_SET_MALLOC_H*/