Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/teigland/dlm
[linux-2.6/openmoko-kernel/knife-kernel.git] / include / linux / textsearch.h
blob6f371f24160b10ac57176bd2f9b3da36d01d445b
1 #ifndef __LINUX_TEXTSEARCH_H
2 #define __LINUX_TEXTSEARCH_H
4 #include <linux/types.h>
5 #include <linux/list.h>
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/err.h>
9 #include <linux/slab.h>
11 struct ts_config;
13 /**
14 * TS_AUTOLOAD - Automatically load textsearch modules when needed
16 #define TS_AUTOLOAD 1
18 /**
19 * struct ts_state - search state
20 * @offset: offset for next match
21 * @cb: control buffer, for persistent variables of get_next_block()
23 struct ts_state
25 unsigned int offset;
26 char cb[40];
29 /**
30 * struct ts_ops - search module operations
31 * @name: name of search algorithm
32 * @init: initialization function to prepare a search
33 * @find: find the next occurrence of the pattern
34 * @destroy: destroy algorithm specific parts of a search configuration
35 * @get_pattern: return head of pattern
36 * @get_pattern_len: return length of pattern
37 * @owner: module reference to algorithm
39 struct ts_ops
41 const char *name;
42 struct ts_config * (*init)(const void *, unsigned int, gfp_t);
43 unsigned int (*find)(struct ts_config *,
44 struct ts_state *);
45 void (*destroy)(struct ts_config *);
46 void * (*get_pattern)(struct ts_config *);
47 unsigned int (*get_pattern_len)(struct ts_config *);
48 struct module *owner;
49 struct list_head list;
52 /**
53 * struct ts_config - search configuration
54 * @ops: operations of chosen algorithm
55 * @get_next_block: callback to fetch the next block to search in
56 * @finish: callback to finalize a search
58 struct ts_config
60 struct ts_ops *ops;
62 /**
63 * get_next_block - fetch next block of data
64 * @consumed: number of bytes consumed by the caller
65 * @dst: destination buffer
66 * @conf: search configuration
67 * @state: search state
69 * Called repeatedly until 0 is returned. Must assign the
70 * head of the next block of data to &*dst and return the length
71 * of the block or 0 if at the end. consumed == 0 indicates
72 * a new search. May store/read persistent values in state->cb.
74 unsigned int (*get_next_block)(unsigned int consumed,
75 const u8 **dst,
76 struct ts_config *conf,
77 struct ts_state *state);
79 /**
80 * finish - finalize/clean a series of get_next_block() calls
81 * @conf: search configuration
82 * @state: search state
84 * Called after the last use of get_next_block(), may be used
85 * to cleanup any leftovers.
87 void (*finish)(struct ts_config *conf,
88 struct ts_state *state);
91 /**
92 * textsearch_next - continue searching for a pattern
93 * @conf: search configuration
94 * @state: search state
96 * Continues a search looking for more occurrences of the pattern.
97 * textsearch_find() must be called to find the first occurrence
98 * in order to reset the state.
100 * Returns the position of the next occurrence of the pattern or
101 * UINT_MAX if not match was found.
103 static inline unsigned int textsearch_next(struct ts_config *conf,
104 struct ts_state *state)
106 unsigned int ret = conf->ops->find(conf, state);
108 if (conf->finish)
109 conf->finish(conf, state);
111 return ret;
115 * textsearch_find - start searching for a pattern
116 * @conf: search configuration
117 * @state: search state
119 * Returns the position of first occurrence of the pattern or
120 * UINT_MAX if no match was found.
122 static inline unsigned int textsearch_find(struct ts_config *conf,
123 struct ts_state *state)
125 state->offset = 0;
126 return textsearch_next(conf, state);
130 * textsearch_get_pattern - return head of the pattern
131 * @conf: search configuration
133 static inline void *textsearch_get_pattern(struct ts_config *conf)
135 return conf->ops->get_pattern(conf);
139 * textsearch_get_pattern_len - return length of the pattern
140 * @conf: search configuration
142 static inline unsigned int textsearch_get_pattern_len(struct ts_config *conf)
144 return conf->ops->get_pattern_len(conf);
147 extern int textsearch_register(struct ts_ops *);
148 extern int textsearch_unregister(struct ts_ops *);
149 extern struct ts_config *textsearch_prepare(const char *, const void *,
150 unsigned int, gfp_t, int);
151 extern void textsearch_destroy(struct ts_config *conf);
152 extern unsigned int textsearch_find_continuous(struct ts_config *,
153 struct ts_state *,
154 const void *, unsigned int);
157 #define TS_PRIV_ALIGNTO 8
158 #define TS_PRIV_ALIGN(len) (((len) + TS_PRIV_ALIGNTO-1) & ~(TS_PRIV_ALIGNTO-1))
160 static inline struct ts_config *alloc_ts_config(size_t payload,
161 gfp_t gfp_mask)
163 struct ts_config *conf;
165 conf = kmalloc(TS_PRIV_ALIGN(sizeof(*conf)) + payload, gfp_mask);
166 if (conf == NULL)
167 return ERR_PTR(-ENOMEM);
169 memset(conf, 0, TS_PRIV_ALIGN(sizeof(*conf)) + payload);
170 return conf;
173 static inline void *ts_config_priv(struct ts_config *conf)
175 return ((u8 *) conf + TS_PRIV_ALIGN(sizeof(struct ts_config)));
178 #endif