| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Flexible array managed in PAGE_SIZE parts | 
 | 3 |  * | 
 | 4 |  * This program is free software; you can redistribute it and/or modify | 
 | 5 |  * it under the terms of the GNU General Public License as published by | 
 | 6 |  * the Free Software Foundation; either version 2 of the License, or | 
 | 7 |  * (at your option) any later version. | 
 | 8 |  * | 
 | 9 |  * This program is distributed in the hope that it will be useful, | 
 | 10 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 11 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 12 |  * GNU General Public License for more details. | 
 | 13 |  * | 
 | 14 |  * You should have received a copy of the GNU General Public License | 
 | 15 |  * along with this program; if not, write to the Free Software | 
 | 16 |  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | 
 | 17 |  * | 
 | 18 |  * Copyright IBM Corporation, 2009 | 
 | 19 |  * | 
 | 20 |  * Author: Dave Hansen <dave@linux.vnet.ibm.com> | 
 | 21 |  */ | 
 | 22 |  | 
 | 23 | #include <linux/flex_array.h> | 
 | 24 | #include <linux/slab.h> | 
 | 25 | #include <linux/stddef.h> | 
 | 26 |  | 
 | 27 | struct flex_array_part { | 
 | 28 | 	char elements[FLEX_ARRAY_PART_SIZE]; | 
 | 29 | }; | 
 | 30 |  | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 31 | /* | 
 | 32 |  * If a user requests an allocation which is small | 
 | 33 |  * enough, we may simply use the space in the | 
 | 34 |  * flex_array->parts[] array to store the user | 
 | 35 |  * data. | 
 | 36 |  */ | 
 | 37 | static inline int elements_fit_in_base(struct flex_array *fa) | 
 | 38 | { | 
 | 39 | 	int data_size = fa->element_size * fa->total_nr_elements; | 
| David Rientjes | 45b588d | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 40 | 	if (data_size <= FLEX_ARRAY_BASE_BYTES_LEFT) | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 41 | 		return 1; | 
 | 42 | 	return 0; | 
 | 43 | } | 
 | 44 |  | 
 | 45 | /** | 
 | 46 |  * flex_array_alloc - allocate a new flexible array | 
 | 47 |  * @element_size:	the size of individual elements in the array | 
 | 48 |  * @total:		total number of elements that this should hold | 
| David Rientjes | fc0d8d9 | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 49 |  * @flags:		page allocation flags to use for base array | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 50 |  * | 
 | 51 |  * Note: all locking must be provided by the caller. | 
 | 52 |  * | 
 | 53 |  * @total is used to size internal structures.  If the user ever | 
 | 54 |  * accesses any array indexes >=@total, it will produce errors. | 
 | 55 |  * | 
 | 56 |  * The maximum number of elements is defined as: the number of | 
 | 57 |  * elements that can be stored in a page times the number of | 
 | 58 |  * page pointers that we can fit in the base structure or (using | 
 | 59 |  * integer math): | 
 | 60 |  * | 
 | 61 |  * 	(PAGE_SIZE/element_size) * (PAGE_SIZE-8)/sizeof(void *) | 
 | 62 |  * | 
 | 63 |  * Here's a table showing example capacities.  Note that the maximum | 
 | 64 |  * index that the get/put() functions is just nr_objects-1.   This | 
 | 65 |  * basically means that you get 4MB of storage on 32-bit and 2MB on | 
 | 66 |  * 64-bit. | 
 | 67 |  * | 
 | 68 |  * | 
 | 69 |  * Element size | Objects | Objects | | 
 | 70 |  * PAGE_SIZE=4k |  32-bit |  64-bit | | 
 | 71 |  * ---------------------------------| | 
 | 72 |  *      1 bytes | 4186112 | 2093056 | | 
 | 73 |  *      2 bytes | 2093056 | 1046528 | | 
 | 74 |  *      3 bytes | 1395030 |  697515 | | 
 | 75 |  *      4 bytes | 1046528 |  523264 | | 
 | 76 |  *     32 bytes |  130816 |   65408 | | 
 | 77 |  *     33 bytes |  126728 |   63364 | | 
 | 78 |  *   2048 bytes |    2044 |    1022 | | 
 | 79 |  *   2049 bytes |    1022 |     511 | | 
 | 80 |  *       void * | 1046528 |  261632 | | 
 | 81 |  * | 
 | 82 |  * Since 64-bit pointers are twice the size, we lose half the | 
 | 83 |  * capacity in the base structure.  Also note that no effort is made | 
 | 84 |  * to efficiently pack objects across page boundaries. | 
 | 85 |  */ | 
| David Rientjes | b62e408 | 2009-08-26 14:29:22 -0700 | [diff] [blame] | 86 | struct flex_array *flex_array_alloc(int element_size, unsigned int total, | 
 | 87 | 					gfp_t flags) | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 88 | { | 
 | 89 | 	struct flex_array *ret; | 
| David Rientjes | 45b588d | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 90 | 	int max_size = FLEX_ARRAY_NR_BASE_PTRS * | 
 | 91 | 				FLEX_ARRAY_ELEMENTS_PER_PART(element_size); | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 92 |  | 
 | 93 | 	/* max_size will end up 0 if element_size > PAGE_SIZE */ | 
 | 94 | 	if (total > max_size) | 
 | 95 | 		return NULL; | 
 | 96 | 	ret = kzalloc(sizeof(struct flex_array), flags); | 
 | 97 | 	if (!ret) | 
 | 98 | 		return NULL; | 
 | 99 | 	ret->element_size = element_size; | 
 | 100 | 	ret->total_nr_elements = total; | 
| David Rientjes | 19da3dd | 2009-09-21 17:04:31 -0700 | [diff] [blame] | 101 | 	if (elements_fit_in_base(ret) && !(flags & __GFP_ZERO)) | 
| Changli Gao | e59464c | 2010-04-23 13:17:45 -0400 | [diff] [blame] | 102 | 		memset(&ret->parts[0], FLEX_ARRAY_FREE, | 
| David Rientjes | 45b588d | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 103 | 						FLEX_ARRAY_BASE_BYTES_LEFT); | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 104 | 	return ret; | 
 | 105 | } | 
 | 106 |  | 
| David Rientjes | b62e408 | 2009-08-26 14:29:22 -0700 | [diff] [blame] | 107 | static int fa_element_to_part_nr(struct flex_array *fa, | 
 | 108 | 					unsigned int element_nr) | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 109 | { | 
| David Rientjes | 45b588d | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 110 | 	return element_nr / FLEX_ARRAY_ELEMENTS_PER_PART(fa->element_size); | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 111 | } | 
 | 112 |  | 
 | 113 | /** | 
 | 114 |  * flex_array_free_parts - just free the second-level pages | 
| David Rientjes | fc0d8d9 | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 115 |  * @fa:		the flex array from which to free parts | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 116 |  * | 
 | 117 |  * This is to be used in cases where the base 'struct flex_array' | 
 | 118 |  * has been statically allocated and should not be free. | 
 | 119 |  */ | 
 | 120 | void flex_array_free_parts(struct flex_array *fa) | 
 | 121 | { | 
 | 122 | 	int part_nr; | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 123 |  | 
 | 124 | 	if (elements_fit_in_base(fa)) | 
 | 125 | 		return; | 
| David Rientjes | 45b588d | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 126 | 	for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++) | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 127 | 		kfree(fa->parts[part_nr]); | 
 | 128 | } | 
 | 129 |  | 
 | 130 | void flex_array_free(struct flex_array *fa) | 
 | 131 | { | 
 | 132 | 	flex_array_free_parts(fa); | 
 | 133 | 	kfree(fa); | 
 | 134 | } | 
 | 135 |  | 
| David Rientjes | b62e408 | 2009-08-26 14:29:22 -0700 | [diff] [blame] | 136 | static unsigned int index_inside_part(struct flex_array *fa, | 
 | 137 | 					unsigned int element_nr) | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 138 | { | 
| David Rientjes | b62e408 | 2009-08-26 14:29:22 -0700 | [diff] [blame] | 139 | 	unsigned int part_offset; | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 140 |  | 
| David Rientjes | 45b588d | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 141 | 	part_offset = element_nr % | 
 | 142 | 				FLEX_ARRAY_ELEMENTS_PER_PART(fa->element_size); | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 143 | 	return part_offset * fa->element_size; | 
 | 144 | } | 
 | 145 |  | 
 | 146 | static struct flex_array_part * | 
 | 147 | __fa_get_part(struct flex_array *fa, int part_nr, gfp_t flags) | 
 | 148 | { | 
 | 149 | 	struct flex_array_part *part = fa->parts[part_nr]; | 
 | 150 | 	if (!part) { | 
| David Rientjes | 19da3dd | 2009-09-21 17:04:31 -0700 | [diff] [blame] | 151 | 		part = kmalloc(sizeof(struct flex_array_part), flags); | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 152 | 		if (!part) | 
 | 153 | 			return NULL; | 
| David Rientjes | 19da3dd | 2009-09-21 17:04:31 -0700 | [diff] [blame] | 154 | 		if (!(flags & __GFP_ZERO)) | 
 | 155 | 			memset(part, FLEX_ARRAY_FREE, | 
 | 156 | 				sizeof(struct flex_array_part)); | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 157 | 		fa->parts[part_nr] = part; | 
 | 158 | 	} | 
 | 159 | 	return part; | 
 | 160 | } | 
 | 161 |  | 
 | 162 | /** | 
 | 163 |  * flex_array_put - copy data into the array at @element_nr | 
| David Rientjes | fc0d8d9 | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 164 |  * @fa:		the flex array to copy data into | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 165 |  * @element_nr:	index of the position in which to insert | 
 | 166 |  * 		the new element. | 
| David Rientjes | fc0d8d9 | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 167 |  * @src:	address of data to copy into the array | 
 | 168 |  * @flags:	page allocation flags to use for array expansion | 
 | 169 |  * | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 170 |  * | 
 | 171 |  * Note that this *copies* the contents of @src into | 
 | 172 |  * the array.  If you are trying to store an array of | 
 | 173 |  * pointers, make sure to pass in &ptr instead of ptr. | 
| Eric Paris | ea98eed | 2010-08-09 17:20:56 -0700 | [diff] [blame] | 174 |  * You may instead wish to use the flex_array_put_ptr() | 
 | 175 |  * helper function. | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 176 |  * | 
 | 177 |  * Locking must be provided by the caller. | 
 | 178 |  */ | 
| David Rientjes | b62e408 | 2009-08-26 14:29:22 -0700 | [diff] [blame] | 179 | int flex_array_put(struct flex_array *fa, unsigned int element_nr, void *src, | 
 | 180 | 			gfp_t flags) | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 181 | { | 
 | 182 | 	int part_nr = fa_element_to_part_nr(fa, element_nr); | 
 | 183 | 	struct flex_array_part *part; | 
 | 184 | 	void *dst; | 
 | 185 |  | 
 | 186 | 	if (element_nr >= fa->total_nr_elements) | 
 | 187 | 		return -ENOSPC; | 
 | 188 | 	if (elements_fit_in_base(fa)) | 
 | 189 | 		part = (struct flex_array_part *)&fa->parts[0]; | 
| David Rientjes | a30b595 | 2009-08-26 14:29:20 -0700 | [diff] [blame] | 190 | 	else { | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 191 | 		part = __fa_get_part(fa, part_nr, flags); | 
| David Rientjes | a30b595 | 2009-08-26 14:29:20 -0700 | [diff] [blame] | 192 | 		if (!part) | 
 | 193 | 			return -ENOMEM; | 
 | 194 | 	} | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 195 | 	dst = &part->elements[index_inside_part(fa, element_nr)]; | 
 | 196 | 	memcpy(dst, src, fa->element_size); | 
 | 197 | 	return 0; | 
 | 198 | } | 
 | 199 |  | 
 | 200 | /** | 
| David Rientjes | e6de398 | 2009-09-21 17:04:30 -0700 | [diff] [blame] | 201 |  * flex_array_clear - clear element in array at @element_nr | 
| David Rientjes | fc0d8d9 | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 202 |  * @fa:		the flex array of the element. | 
| David Rientjes | e6de398 | 2009-09-21 17:04:30 -0700 | [diff] [blame] | 203 |  * @element_nr:	index of the position to clear. | 
 | 204 |  * | 
 | 205 |  * Locking must be provided by the caller. | 
 | 206 |  */ | 
 | 207 | int flex_array_clear(struct flex_array *fa, unsigned int element_nr) | 
 | 208 | { | 
 | 209 | 	int part_nr = fa_element_to_part_nr(fa, element_nr); | 
 | 210 | 	struct flex_array_part *part; | 
 | 211 | 	void *dst; | 
 | 212 |  | 
 | 213 | 	if (element_nr >= fa->total_nr_elements) | 
 | 214 | 		return -ENOSPC; | 
 | 215 | 	if (elements_fit_in_base(fa)) | 
 | 216 | 		part = (struct flex_array_part *)&fa->parts[0]; | 
 | 217 | 	else { | 
 | 218 | 		part = fa->parts[part_nr]; | 
 | 219 | 		if (!part) | 
 | 220 | 			return -EINVAL; | 
 | 221 | 	} | 
 | 222 | 	dst = &part->elements[index_inside_part(fa, element_nr)]; | 
| David Rientjes | 19da3dd | 2009-09-21 17:04:31 -0700 | [diff] [blame] | 223 | 	memset(dst, FLEX_ARRAY_FREE, fa->element_size); | 
| David Rientjes | e6de398 | 2009-09-21 17:04:30 -0700 | [diff] [blame] | 224 | 	return 0; | 
 | 225 | } | 
 | 226 |  | 
 | 227 | /** | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 228 |  * flex_array_prealloc - guarantee that array space exists | 
| David Rientjes | fc0d8d9 | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 229 |  * @fa:		the flex array for which to preallocate parts | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 230 |  * @start:	index of first array element for which space is allocated | 
 | 231 |  * @end:	index of last (inclusive) element for which space is allocated | 
| David Rientjes | fc0d8d9 | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 232 |  * @flags:	page allocation flags | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 233 |  * | 
 | 234 |  * This will guarantee that no future calls to flex_array_put() | 
 | 235 |  * will allocate memory.  It can be used if you are expecting to | 
 | 236 |  * be holding a lock or in some atomic context while writing | 
 | 237 |  * data into the array. | 
 | 238 |  * | 
 | 239 |  * Locking must be provided by the caller. | 
 | 240 |  */ | 
| David Rientjes | b62e408 | 2009-08-26 14:29:22 -0700 | [diff] [blame] | 241 | int flex_array_prealloc(struct flex_array *fa, unsigned int start, | 
 | 242 | 			unsigned int end, gfp_t flags) | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 243 | { | 
 | 244 | 	int start_part; | 
 | 245 | 	int end_part; | 
 | 246 | 	int part_nr; | 
 | 247 | 	struct flex_array_part *part; | 
 | 248 |  | 
 | 249 | 	if (start >= fa->total_nr_elements || end >= fa->total_nr_elements) | 
 | 250 | 		return -ENOSPC; | 
 | 251 | 	if (elements_fit_in_base(fa)) | 
 | 252 | 		return 0; | 
 | 253 | 	start_part = fa_element_to_part_nr(fa, start); | 
 | 254 | 	end_part = fa_element_to_part_nr(fa, end); | 
 | 255 | 	for (part_nr = start_part; part_nr <= end_part; part_nr++) { | 
 | 256 | 		part = __fa_get_part(fa, part_nr, flags); | 
 | 257 | 		if (!part) | 
 | 258 | 			return -ENOMEM; | 
 | 259 | 	} | 
 | 260 | 	return 0; | 
 | 261 | } | 
 | 262 |  | 
 | 263 | /** | 
 | 264 |  * flex_array_get - pull data back out of the array | 
| David Rientjes | fc0d8d9 | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 265 |  * @fa:		the flex array from which to extract data | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 266 |  * @element_nr:	index of the element to fetch from the array | 
 | 267 |  * | 
 | 268 |  * Returns a pointer to the data at index @element_nr.  Note | 
 | 269 |  * that this is a copy of the data that was passed in.  If you | 
| Eric Paris | ea98eed | 2010-08-09 17:20:56 -0700 | [diff] [blame] | 270 |  * are using this to store pointers, you'll get back &ptr.  You | 
 | 271 |  * may instead wish to use the flex_array_get_ptr helper. | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 272 |  * | 
 | 273 |  * Locking must be provided by the caller. | 
 | 274 |  */ | 
| David Rientjes | b62e408 | 2009-08-26 14:29:22 -0700 | [diff] [blame] | 275 | void *flex_array_get(struct flex_array *fa, unsigned int element_nr) | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 276 | { | 
 | 277 | 	int part_nr = fa_element_to_part_nr(fa, element_nr); | 
 | 278 | 	struct flex_array_part *part; | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 279 |  | 
 | 280 | 	if (element_nr >= fa->total_nr_elements) | 
 | 281 | 		return NULL; | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 282 | 	if (elements_fit_in_base(fa)) | 
 | 283 | 		part = (struct flex_array_part *)&fa->parts[0]; | 
| David Rientjes | a30b595 | 2009-08-26 14:29:20 -0700 | [diff] [blame] | 284 | 	else { | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 285 | 		part = fa->parts[part_nr]; | 
| David Rientjes | a30b595 | 2009-08-26 14:29:20 -0700 | [diff] [blame] | 286 | 		if (!part) | 
 | 287 | 			return NULL; | 
 | 288 | 	} | 
| Dave Hansen | 534acc0 | 2009-07-29 15:04:18 -0700 | [diff] [blame] | 289 | 	return &part->elements[index_inside_part(fa, element_nr)]; | 
 | 290 | } | 
| David Rientjes | 4af5a2f | 2009-09-21 17:04:31 -0700 | [diff] [blame] | 291 |  | 
| Eric Paris | ea98eed | 2010-08-09 17:20:56 -0700 | [diff] [blame] | 292 | /** | 
 | 293 |  * flex_array_get_ptr - pull a ptr back out of the array | 
 | 294 |  * @fa:		the flex array from which to extract data | 
 | 295 |  * @element_nr:	index of the element to fetch from the array | 
 | 296 |  * | 
 | 297 |  * Returns the pointer placed in the flex array at element_nr using | 
 | 298 |  * flex_array_put_ptr().  This function should not be called if the | 
 | 299 |  * element in question was not set using the _put_ptr() helper. | 
 | 300 |  */ | 
 | 301 | void *flex_array_get_ptr(struct flex_array *fa, unsigned int element_nr) | 
 | 302 | { | 
 | 303 | 	void **tmp; | 
 | 304 |  | 
 | 305 | 	tmp = flex_array_get(fa, element_nr); | 
 | 306 | 	if (!tmp) | 
 | 307 | 		return NULL; | 
 | 308 |  | 
 | 309 | 	return *tmp; | 
 | 310 | } | 
 | 311 |  | 
| David Rientjes | 4af5a2f | 2009-09-21 17:04:31 -0700 | [diff] [blame] | 312 | static int part_is_free(struct flex_array_part *part) | 
 | 313 | { | 
 | 314 | 	int i; | 
 | 315 |  | 
 | 316 | 	for (i = 0; i < sizeof(struct flex_array_part); i++) | 
 | 317 | 		if (part->elements[i] != FLEX_ARRAY_FREE) | 
 | 318 | 			return 0; | 
 | 319 | 	return 1; | 
 | 320 | } | 
 | 321 |  | 
 | 322 | /** | 
 | 323 |  * flex_array_shrink - free unused second-level pages | 
| David Rientjes | fc0d8d9 | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 324 |  * @fa:		the flex array to shrink | 
| David Rientjes | 4af5a2f | 2009-09-21 17:04:31 -0700 | [diff] [blame] | 325 |  * | 
 | 326 |  * Frees all second-level pages that consist solely of unused | 
 | 327 |  * elements.  Returns the number of pages freed. | 
 | 328 |  * | 
 | 329 |  * Locking must be provided by the caller. | 
 | 330 |  */ | 
 | 331 | int flex_array_shrink(struct flex_array *fa) | 
 | 332 | { | 
 | 333 | 	struct flex_array_part *part; | 
| David Rientjes | 4af5a2f | 2009-09-21 17:04:31 -0700 | [diff] [blame] | 334 | 	int part_nr; | 
 | 335 | 	int ret = 0; | 
 | 336 |  | 
 | 337 | 	if (elements_fit_in_base(fa)) | 
 | 338 | 		return ret; | 
| David Rientjes | 45b588d | 2009-09-21 17:04:33 -0700 | [diff] [blame] | 339 | 	for (part_nr = 0; part_nr < FLEX_ARRAY_NR_BASE_PTRS; part_nr++) { | 
| David Rientjes | 4af5a2f | 2009-09-21 17:04:31 -0700 | [diff] [blame] | 340 | 		part = fa->parts[part_nr]; | 
 | 341 | 		if (!part) | 
 | 342 | 			continue; | 
 | 343 | 		if (part_is_free(part)) { | 
 | 344 | 			fa->parts[part_nr] = NULL; | 
 | 345 | 			kfree(part); | 
 | 346 | 			ret++; | 
 | 347 | 		} | 
 | 348 | 	} | 
 | 349 | 	return ret; | 
 | 350 | } |