···4646following:47474848 dd if=/proc/sequence of=out1 count=14949- dd if=/proc/sequence skip=1 out=out2 count=14949+ dd if=/proc/sequence skip=1 of=out2 count=150505151Then concatenate the output files out1 and out2 and get the right5252result. Yes, it is a thoroughly useless module, but the point is to show
+99
Documentation/flexible-arrays.txt
···11+Using flexible arrays in the kernel22+Last updated for 2.6.3133+Jonathan Corbet <corbet@lwn.net>44+55+Large contiguous memory allocations can be unreliable in the Linux kernel.66+Kernel programmers will sometimes respond to this problem by allocating77+pages with vmalloc(). This solution not ideal, though. On 32-bit systems,88+memory from vmalloc() must be mapped into a relatively small address space;99+it's easy to run out. On SMP systems, the page table changes required by1010+vmalloc() allocations can require expensive cross-processor interrupts on1111+all CPUs. And, on all systems, use of space in the vmalloc() range1212+increases pressure on the translation lookaside buffer (TLB), reducing the1313+performance of the system.1414+1515+In many cases, the need for memory from vmalloc() can be eliminated by1616+piecing together an array from smaller parts; the flexible array library1717+exists to make this task easier.1818+1919+A flexible array holds an arbitrary (within limits) number of fixed-sized2020+objects, accessed via an integer index. Sparse arrays are handled2121+reasonably well. Only single-page allocations are made, so memory2222+allocation failures should be relatively rare. The down sides are that the2323+arrays cannot be indexed directly, individual object size cannot exceed the2424+system page size, and putting data into a flexible array requires a copy2525+operation. It's also worth noting that flexible arrays do no internal2626+locking at all; if concurrent access to an array is possible, then the2727+caller must arrange for appropriate mutual exclusion.2828+2929+The creation of a flexible array is done with:3030+3131+ #include <linux/flex_array.h>3232+3333+ struct flex_array *flex_array_alloc(int element_size,3434+ unsigned int total,3535+ gfp_t flags);3636+3737+The individual object size is provided by element_size, while total is the3838+maximum number of objects which can be stored in the array. The flags3939+argument is passed directly to the internal memory allocation calls. With4040+the current code, using flags to ask for high memory is likely to lead to4141+notably unpleasant side effects.4242+4343+Storing data into a flexible array is accomplished with a call to:4444+4545+ int flex_array_put(struct flex_array *array, unsigned int element_nr,4646+ void *src, gfp_t flags);4747+4848+This call will copy the data from src into the array, in the position4949+indicated by element_nr (which must be less than the maximum specified when5050+the array was created). If any memory allocations must be performed, flags5151+will be used. The return value is zero on success, a negative error code5252+otherwise.5353+5454+There might possibly be a need to store data into a flexible array while5555+running in some sort of atomic context; in this situation, sleeping in the5656+memory allocator would be a bad thing. That can be avoided by using5757+GFP_ATOMIC for the flags value, but, often, there is a better way. The5858+trick is to ensure that any needed memory allocations are done before5959+entering atomic context, using:6060+6161+ int flex_array_prealloc(struct flex_array *array, unsigned int start,6262+ unsigned int end, gfp_t flags);6363+6464+This function will ensure that memory for the elements indexed in the range6565+defined by start and end has been allocated. Thereafter, a6666+flex_array_put() call on an element in that range is guaranteed not to6767+block.6868+6969+Getting data back out of the array is done with:7070+7171+ void *flex_array_get(struct flex_array *fa, unsigned int element_nr);7272+7373+The return value is a pointer to the data element, or NULL if that7474+particular element has never been allocated.7575+7676+Note that it is possible to get back a valid pointer for an element which7777+has never been stored in the array. Memory for array elements is allocated7878+one page at a time; a single allocation could provide memory for several7979+adjacent elements. The flexible array code does not know if a specific8080+element has been written; it only knows if the associated memory is8181+present. So a flex_array_get() call on an element which was never stored8282+in the array has the potential to return a pointer to random data. If the8383+caller does not have a separate way to know which elements were actually8484+stored, it might be wise, at least, to add GFP_ZERO to the flags argument8585+to ensure that all elements are zeroed.8686+8787+There is no way to remove a single element from the array. It is possible,8888+though, to remove all elements with a call to:8989+9090+ void flex_array_free_parts(struct flex_array *array);9191+9292+This call frees all elements, but leaves the array itself in place.9393+Freeing the entire array is done with:9494+9595+ void flex_array_free(struct flex_array *array);9696+9797+As of this writing, there are no users of flexible arrays in the mainline9898+kernel. The functions described here are also not exported to modules;9999+that will probably be fixed when somebody comes up with a need for it.