nfs-ganesha 1.4
|
00001 /* 00002 * 00003 * 00004 * Copyright © Linux box Corporation, 2012 00005 * Author: Adam C. Emerson <aemerson@linuxbox.com> 00006 * 00007 * 00008 * This program is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public License 00010 * as published by the Free Software Foundation; either version 3 of 00011 * the License, or (at your option) any later version. 00012 * 00013 * This program is distributed in the hope that it will be useful, but 00014 * WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 00021 * 02110-1301 USA 00022 * 00023 * --------------------------------------- 00024 */ 00025 00039 #ifndef _ABSTRACT_MEM_H 00040 #define _ABSTRACT_MEM_H 00041 00042 #include <stdlib.h> 00043 #include <string.h> 00044 #include <assert.h> 00045 00069 static inline void * 00070 gsh_malloc(size_t n) 00071 { 00072 return malloc(n); 00073 } 00074 00087 static inline void * 00088 gsh_malloc_aligned(size_t a, size_t n) 00089 { 00090 void *p; 00091 if(posix_memalign(&p, a, n) != 0) 00092 p = NULL; 00093 return p; 00094 } 00095 00108 static inline void * 00109 gsh_calloc(size_t s, size_t n) 00110 { 00111 return calloc(s, n); 00112 } 00113 00127 static inline void * 00128 gsh_realloc(void *p, size_t n) 00129 { 00130 return realloc(p, n); 00131 } 00132 00143 static inline char * 00144 gsh_strdup(const char *s) 00145 { 00146 return strdup(s); 00147 } 00148 00157 static inline void 00158 gsh_free(void *p) 00159 { 00160 free(p); 00161 } 00162 00173 static inline void 00174 gsh_free_size(void *p, size_t n __attribute__((unused))) 00175 { 00176 free(p); 00177 } 00178 00194 typedef struct pool pool_t; 00195 00212 typedef pool_t*(*pool_initializer_t)(size_t size, 00213 void* param); 00214 00225 typedef void (*pool_destroyer_t)(pool_t *pool); 00226 00239 typedef void *(*pool_allocator_t)(pool_t *pool); 00240 00253 typedef void (*pool_freer_t)(pool_t *pool, 00254 void* object); 00255 00256 00264 struct pool_substrate_vector { 00265 pool_initializer_t initializer; /*< Create an underlying pool */ 00266 pool_destroyer_t destroyer; /*< Destroy an underlying pool */ 00267 pool_allocator_t allocator; /*< Allocate an object */ 00268 pool_freer_t freer; /*< Free an object */ 00269 }; 00270 00281 typedef void(*pool_constructor_t)(void *object, 00282 void *parameters); 00283 00293 typedef void(*pool_destructor_t)(void *object); 00294 00305 struct pool { 00306 char *name; /*< The name of the pool */ 00307 size_t object_size; /*< The size of the objects created */ 00308 pool_constructor_t constructor; /*< The object constructor */ 00309 pool_destructor_t destructor; /*< The object destructor */ 00310 struct pool_substrate_vector *substrate_vector; /*< Pool operations */ 00311 char substrate_data[]; /*< The beginning of any substrate-specific 00312 data */ 00313 }; 00314 00343 static inline pool_t* 00344 pool_init(const char* name, 00345 size_t object_size, 00346 const struct pool_substrate_vector *substrate, 00347 void *substrate_params, 00348 pool_constructor_t constructor, 00349 pool_destructor_t destructor) 00350 { 00351 pool_t *pool = substrate->initializer(object_size, 00352 substrate_params); 00353 if (pool) { 00354 pool->substrate_vector = (struct pool_substrate_vector *)substrate; 00355 pool->object_size = object_size; 00356 pool->constructor = constructor; 00357 pool->destructor = destructor; 00358 if (name) { 00359 pool->name = gsh_strdup(name); 00360 } else { 00361 pool->name = NULL; 00362 } 00363 } 00364 return pool; 00365 } 00366 00376 static inline void 00377 pool_destroy(pool_t *pool) 00378 { 00379 pool->substrate_vector->destroyer(pool); 00380 } 00381 00402 static inline void* 00403 pool_alloc(pool_t *pool, void *parameters) 00404 { 00405 void *object = pool->substrate_vector->allocator(pool); 00406 if (object && (pool->constructor)) { 00407 (pool->constructor)(object, parameters); 00408 } 00409 return object; 00410 } 00411 00428 static inline void 00429 pool_free(pool_t *pool, void* object) 00430 { 00431 if (pool->destructor) { 00432 (pool->destructor)(object); 00433 } 00434 pool->substrate_vector->freer(pool, object); 00435 } 00436 00460 static inline pool_t* 00461 pool_basic_initializer(size_t size __attribute__((unused)), 00462 void* param __attribute__((unused))) 00463 { 00464 assert(param == NULL); /* We take no parameters */ 00465 return gsh_malloc(sizeof(pool_t)); 00466 } 00467 00476 static inline void 00477 pool_basic_destroy(pool_t *pool) 00478 { 00479 gsh_free(pool); 00480 } 00481 00493 static inline void* 00494 pool_basic_alloc(pool_t *pool) 00495 { 00496 if (pool->constructor) { 00497 return gsh_malloc(pool->object_size); 00498 } else { 00499 return gsh_calloc(1, pool->object_size); 00500 } 00501 } 00502 00512 static inline void 00513 pool_basic_free(pool_t *pool, 00514 void* object) 00515 { 00516 gsh_free(object); 00517 } 00518 00519 static const struct pool_substrate_vector pool_basic_substrate[]= { 00520 {.initializer = pool_basic_initializer, 00521 .destroyer = pool_basic_destroy, 00522 .allocator = pool_basic_alloc, 00523 .freer = pool_basic_free} 00524 }; 00525 00526 #endif /* _ABSTRACT_MEM_H */