Per-device parameter support (1/16)
dp_01_vector.diff
This is the 1st patch of 16 patches for devparam.
This patch implements dynamically expandable/shrinkable generic array
which also supports constructing/destructing.
Signed-off-by Tejun Heo <[email protected]>
include/linux/vector.h new file +174 -0
@@ -0,0 +1,174 @@ +/* +* vector.h - dynamically expandable/shrinkable generic array +* +* Copyright (C) 2004 Tejun Heo <[email protected]> +* +* This file is released under the GPLv2. +* +* The interface is fairly self-explanatory. However, note that +* +* - vector_elem() and vector_clear() expands the vector as necessary, +* and, naturally, may fail; however, they're guaranteed to succeed if +* the specified element is inside the current length of the vector. +* +* - If gfp_flags is 0, you're explicitly specifying that the +* specified element should be inside the current length of the +* vector. If gfp_flags is 0 but expansion is necessary, it's BUG(). +* +* - Elements inside a vector with its VECTOR_ALLOCATE bit off may +* move while expanding the vector, so its address may change during +* vector_elem(), vector_expand() or vector_shrink() call. However, +* it's guaranteed that once a vector is expanded to be larger than a +* page, addresses of the elements won't change during tail +* expansion/shrink. Hence, the following vector_elem() invocation, +* when successful, guarantee that unless expanded or shrinked in the +* middle, elements retain their addresses. +* +* vector_elem(my_vec, PAGE_SIZE / elem_size - 1, GFP_KERNEL) +* +* - If VECTOR_ALLOCATE is set, each element is allocated separately, +* so above problem won't appear. Unless the size of the element is +* quite small, using VECTOR_ALLOCATE can be better in many cases. +* +* - If VECTOR_CLEAR is set, newly expanded elements are cleared with +* zero. If constructor is also specified, elements are cleared +* before passed to the constructor. +* +* - Expanding and shrinking in the middle of a vector involves +* memmoving elements and thus can be somewhat costly. However, if +* the expansion or shrink is multiple of PAGE_SIZE, vector will avoid +* copying memory whenever possible. +*/ + +#ifndef __LINUX_VECTOR_H +#define __LINUX_VECTOR_H + +#ifndef __ASSEMBLY__ + +#include <linux/types.h> + +#define VECTOR_MAX_INLINE_SIZE (PAGE_SIZE / 4) + +enum { +VECTOR_CLEAR =0x1 , +VECTOR_ALLOCATE =0x2 +}; + +typedef int (*vector_ctor_t )(void *elem,void *arg); +typedef void (*vector_dtor_t )(void *elem,void *arg); + +struct vector { +u16 first, flags; +int elem_size; +void *chunk; + +int real_size, nr_per_chunk; +int len, nr_allocated; +vector_ctor_t ctor; +vector_dtor_t dtor; +void *arg; +struct vector_ext *ext; +}; + +#define VECTOR_INIT (Elemsize, Flags, Ctor, Dtor, Arg) { \ + .elem_size = (Flags) & VECTOR_ALLOCATE \ + ? sizeof(void *) : (Elemsize),\ + .flags = (Flags), \ + .real_size = (Elemsize), \ + .nr_per_chunk = (Flags) & VECTOR_ALLOCATE \ + ? PAGE_SIZE / sizeof(void *) \ + : PAGE_SIZE / (Elemsize), \ + .ctor = Ctor, \ + .dtor = Dtor, \ + .arg = Arg \ + } + +/** +* vector_init - initialize a vector +* @vec: vector to initialize +* @elem_size: vector element size (cannot be bigger than PAGE_SIZE / 4) +* @flags: vector flags +* @ctor: elem constructor +* @dtor: elem destructor +* @arg: constructor/destructor argument +* +* Initialize a vector which contains elements of size @elem_size. +*/ +void vector_init (struct vector *vec,size_t elem_size,unsigned flags, +vector_ctor_t ctor,vector_dtor_t dtor,void *arg); + +void *vector_elem_slow (struct vector *vec,int index,int gfp_flags); + +/** +* vector_elem - reference a vector element +* @vec: target vector +* @index: index of the element to reference +* @gfp_flags: allocation flags +* +* Returns the pointer to the element at @index of @vec. The vector is +* expanded as necessary. +*/ +static inline void *vector_elem (struct vector *vec,int index,int gfp_flags) +{ +if (index < vec->first) { +void *p = vec->chunk + index * vec->elem_size; +return !(vec->flags &VECTOR_ALLOCATE ) ? p : *(void **)p; + }else +return vector_elem_slow (vec, index, gfp_flags); +} + +/** +* vector_len - length of the vector +* @vec: target vector +* +* Returns the length of the vector (maximum successfully referenced index + 1) +*/ +static inline int vector_len (struct vector *vec) +{ +return vec->len; +} + +/** +* vector_clear - clear the vector with given data +* @vec: target vector +* @at: from this element +* @nr: number of elements to clear +* @clear_data: element is cleared using this data +* @cdlen: length of clear_data +* @gfp_flags: allocation flags +* +* Clear @nr elements starting from @at using @cdlen bytes of @clear_data. +* The vector is expanded as necessary. +*/ +int vector_clear (struct vector *vec,int at,int nr, +const void *clear_data,size_t cdlen,int gfp_flags); + +/** +* vector_expand - expand the vector +* @vec: target vector +* @at: insert new elements at this index +* @nr: number of new elements +* @gfp_flags: allocation flags +* +* Insert @nr elements at @at. @at can range from 0 to vector_len. +*/ +int vector_expand (struct vector *vec,int at,int nr,int gfp_flags); + +/** +* vector_shrink - shrink the vector +* @vec: target vector +* @at: remove elements from this index +* @nr: number of elements to remove +* +* Remove @nr elements starting from @at. +*/ +void vector_shrink (struct vector *vec,int at,int nr); + +/** +* vector_destroy - destroy the vector +* @vec: target vector +*/ +void vector_destroy (struct vector *vec); + +#endif /*__ASSEMBLY__*/ +#endif /*__LINUX_VECTOR_H*/
lib/Makefile +1 -1
@@ -6,7 +6,7 @@ lib-y := errno.o ctype.o string.o vsprintf.o cmdline.o \ bust_spinlocks.o rbtree.o radix-tree.o dump_stack.o \ kobject.o kref.o idr.o div64.o parser.o int_sqrt.o \ - bitmap.o extable.o kobject_uevent.o + bitmap.o extable.o kobject_uevent.o vector.o lib-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
lib/vector.c new file +377 -0
377 lines not on this page · raw message