⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 libdevmapper.h

📁 Linux Device Mapper Source Code
💻 H
📖 第 1 页 / 共 2 页
字号:
/***************************************************************************** * Library functions *****************************************************************************//******************* * Memory management *******************/void *dm_malloc_aux(size_t s, const char *file, int line);void *dm_malloc_aux_debug(size_t s, const char *file, int line);char *dm_strdup_aux(const char *str, const char *file, int line);void dm_free_aux(void *p);void *dm_realloc_aux(void *p, unsigned int s, const char *file, int line);int dm_dump_memory_debug(void);void dm_bounds_check_debug(void);#ifdef DEBUG_MEM#  define dm_malloc(s) dm_malloc_aux_debug((s), __FILE__, __LINE__)#  define dm_strdup(s) dm_strdup_aux((s), __FILE__, __LINE__)#  define dm_free(p) dm_free_aux(p)#  define dm_realloc(p, s) dm_realloc_aux(p, s, __FILE__, __LINE__)#  define dm_dump_memory() dm_dump_memory_debug()#  define dm_bounds_check() dm_bounds_check_debug()#else#  define dm_malloc(s) dm_malloc_aux((s), __FILE__, __LINE__)#  define dm_strdup(s) strdup(s)#  define dm_free(p) free(p)#  define dm_realloc(p, s) realloc(p, s)#  define dm_dump_memory() {}#  define dm_bounds_check() {}#endif/* * The pool allocator is useful when you are going to allocate * lots of memory, use the memory for a bit, and then free the * memory in one go.  A surprising amount of code has this usage * profile. * * You should think of the pool as an infinite, contiguous chunk * of memory.  The front of this chunk of memory contains * allocated objects, the second half is free.  dm_pool_alloc grabs * the next 'size' bytes from the free half, in effect moving it * into the allocated half.  This operation is very efficient. * * dm_pool_free frees the allocated object *and* all objects * allocated after it.  It is important to note this semantic * difference from malloc/free.  This is also extremely * efficient, since a single dm_pool_free can dispose of a large * complex object. * * dm_pool_destroy frees all allocated memory. * * eg, If you are building a binary tree in your program, and * know that you are only ever going to insert into your tree, * and not delete (eg, maintaining a symbol table for a * compiler).  You can create yourself a pool, allocate the nodes * from it, and when the tree becomes redundant call dm_pool_destroy * (no nasty iterating through the tree to free nodes). * * eg, On the other hand if you wanted to repeatedly insert and * remove objects into the tree, you would be better off * allocating the nodes from a free list; you cannot free a * single arbitrary node with pool. */struct dm_pool;/* constructor and destructor */struct dm_pool *dm_pool_create(const char *name, size_t chunk_hint);void dm_pool_destroy(struct dm_pool *p);/* simple allocation/free routines */void *dm_pool_alloc(struct dm_pool *p, size_t s);void *dm_pool_alloc_aligned(struct dm_pool *p, size_t s, unsigned alignment);void dm_pool_empty(struct dm_pool *p);void dm_pool_free(struct dm_pool *p, void *ptr);/* * Object building routines: * * These allow you to 'grow' an object, useful for * building strings, or filling in dynamic * arrays. * * It's probably best explained with an example: * * char *build_string(struct dm_pool *mem) * { *      int i; *      char buffer[16]; * *      if (!dm_pool_begin_object(mem, 128)) *              return NULL; * *      for (i = 0; i < 50; i++) { *              snprintf(buffer, sizeof(buffer), "%d, ", i); *              if (!dm_pool_grow_object(mem, buffer, strlen(buffer))) *                      goto bad; *      } * *	// add null *      if (!dm_pool_grow_object(mem, "\0", 1)) *              goto bad; * *      return dm_pool_end_object(mem); * * bad: * *      dm_pool_abandon_object(mem); *      return NULL; *} * * So start an object by calling dm_pool_begin_object * with a guess at the final object size - if in * doubt make the guess too small. * * Then append chunks of data to your object with * dm_pool_grow_object.  Finally get your object with * a call to dm_pool_end_object. * */int dm_pool_begin_object(struct dm_pool *p, size_t hint);int dm_pool_grow_object(struct dm_pool *p, const void *extra, size_t delta);void *dm_pool_end_object(struct dm_pool *p);void dm_pool_abandon_object(struct dm_pool *p);/* utilities */char *dm_pool_strdup(struct dm_pool *p, const char *str);char *dm_pool_strndup(struct dm_pool *p, const char *str, size_t n);void *dm_pool_zalloc(struct dm_pool *p, size_t s);/****************** * bitset functions ******************/typedef uint32_t *dm_bitset_t;dm_bitset_t dm_bitset_create(struct dm_pool *mem, unsigned num_bits);void dm_bitset_destroy(dm_bitset_t bs);void dm_bit_union(dm_bitset_t out, dm_bitset_t in1, dm_bitset_t in2);int dm_bit_get_first(dm_bitset_t bs);int dm_bit_get_next(dm_bitset_t bs, int last_bit);#define DM_BITS_PER_INT (sizeof(int) * CHAR_BIT)#define dm_bit(bs, i) \   (bs[(i / DM_BITS_PER_INT) + 1] & (0x1 << (i & (DM_BITS_PER_INT - 1))))#define dm_bit_set(bs, i) \   (bs[(i / DM_BITS_PER_INT) + 1] |= (0x1 << (i & (DM_BITS_PER_INT - 1))))#define dm_bit_clear(bs, i) \   (bs[(i / DM_BITS_PER_INT) + 1] &= ~(0x1 << (i & (DM_BITS_PER_INT - 1))))#define dm_bit_set_all(bs) \   memset(bs + 1, -1, ((*bs / DM_BITS_PER_INT) + 1) * sizeof(int))#define dm_bit_clear_all(bs) \   memset(bs + 1, 0, ((*bs / DM_BITS_PER_INT) + 1) * sizeof(int))#define dm_bit_copy(bs1, bs2) \   memcpy(bs1 + 1, bs2 + 1, ((*bs1 / DM_BITS_PER_INT) + 1) * sizeof(int))/* Returns number of set bits */static inline unsigned hweight32(uint32_t i){	unsigned r = (i & 0x55555555) + ((i >> 1) & 0x55555555);	r =    (r & 0x33333333) + ((r >>  2) & 0x33333333);	r =    (r & 0x0F0F0F0F) + ((r >>  4) & 0x0F0F0F0F);	r =    (r & 0x00FF00FF) + ((r >>  8) & 0x00FF00FF);	return (r & 0x0000FFFF) + ((r >> 16) & 0x0000FFFF);}/**************** * hash functions ****************/struct dm_hash_table;struct dm_hash_node;typedef void (*dm_hash_iterate_fn) (void *data);struct dm_hash_table *dm_hash_create(unsigned size_hint);void dm_hash_destroy(struct dm_hash_table *t);void dm_hash_wipe(struct dm_hash_table *t);void *dm_hash_lookup(struct dm_hash_table *t, const char *key);int dm_hash_insert(struct dm_hash_table *t, const char *key, void *data);void dm_hash_remove(struct dm_hash_table *t, const char *key);void *dm_hash_lookup_binary(struct dm_hash_table *t, const char *key, uint32_t len);int dm_hash_insert_binary(struct dm_hash_table *t, const char *key, uint32_t len,		       void *data);void dm_hash_remove_binary(struct dm_hash_table *t, const char *key, uint32_t len);unsigned dm_hash_get_num_entries(struct dm_hash_table *t);void dm_hash_iter(struct dm_hash_table *t, dm_hash_iterate_fn f);char *dm_hash_get_key(struct dm_hash_table *t, struct dm_hash_node *n);void *dm_hash_get_data(struct dm_hash_table *t, struct dm_hash_node *n);struct dm_hash_node *dm_hash_get_first(struct dm_hash_table *t);struct dm_hash_node *dm_hash_get_next(struct dm_hash_table *t, struct dm_hash_node *n);#define dm_hash_iterate(v, h) \	for (v = dm_hash_get_first(h); v; \	     v = dm_hash_get_next(h, v))/********* * selinux *********/int dm_set_selinux_context(const char *path, mode_t mode);/********************* * string manipulation *********************//* * Break up the name of a mapped device into its constituent * Volume Group, Logical Volume and Layer (if present). */int dm_split_lvm_name(struct dm_pool *mem, const char *dmname,		      char **vgname, char **lvname, char **layer);/* * Destructively split buffer into NULL-separated words in argv. * Returns number of words. */int dm_split_words(char *buffer, unsigned max,		   unsigned ignore_comments, /* Not implemented */		   char **argv);/*  * Returns -1 if buffer too small */int dm_snprintf(char *buf, size_t bufsize, const char *format, ...);/* * Returns pointer to the last component of the path. */char *dm_basename(const char *path);/************************** * file/stream manipulation **************************//* * Create a directory (with parent directories if necessary). * Returns 1 on success, 0 on failure. */int dm_create_dir(const char *dir);/* * Close a stream, with nicer error checking than fclose's. * Derived from gnulib's close-stream.c. * * Close "stream".  Return 0 if successful, and EOF (setting errno) * otherwise.  Upon failure, set errno to 0 if the error number * cannot be determined.  Useful mainly for writable streams. */int dm_fclose(FILE *stream);/* * Returns size of a buffer which is allocated with dm_malloc. * Pointer to the buffer is stored in *buf. * Returns -1 on failure leaving buf undefined. */int dm_asprintf(char **buf, const char *format, ...);/********************* * regular expressions *********************/struct dm_regex;/* * Initialise an array of num patterns for matching. * Uses memory from mem. */struct dm_regex *dm_regex_create(struct dm_pool *mem, const char **patterns,				 unsigned num_patterns);/* * Match string s against the patterns. * Returns the index of the highest pattern in the array that matches, * or -1 if none match. */int dm_regex_match(struct dm_regex *regex, const char *s);/********************* * reporting functions *********************/struct dm_report_object_type {	uint32_t id;			/* Powers of 2 */	const char *desc;	const char *prefix;		/* field id string prefix (optional) */	void *(*data_fn)(void *object);	/* callback from report_object() */};struct dm_report_field;/* * dm_report_field_type flags */#define DM_REPORT_FIELD_MASK		0x000000FF#define DM_REPORT_FIELD_ALIGN_MASK	0x0000000F#define DM_REPORT_FIELD_ALIGN_LEFT	0x00000001#define DM_REPORT_FIELD_ALIGN_RIGHT	0x00000002#define DM_REPORT_FIELD_TYPE_MASK	0x000000F0#define DM_REPORT_FIELD_TYPE_STRING	0x00000010#define DM_REPORT_FIELD_TYPE_NUMBER	0x00000020struct dm_report;struct dm_report_field_type {	uint32_t type;		/* object type id */	uint32_t flags;		/* DM_REPORT_FIELD_* */	uint32_t offset;	/* byte offset in the object */	int32_t width;		/* default width */	const char id[32];	/* string used to specify the field */	const char heading[32];	/* string printed in header */	int (*report_fn)(struct dm_report *rh, struct dm_pool *mem,			 struct dm_report_field *field, const void *data,			 void *private);	const char *desc;	/* description of the field */};/* * dm_report_init output_flags */#define DM_REPORT_OUTPUT_MASK		0x000000FF#define DM_REPORT_OUTPUT_ALIGNED	0x00000001#define DM_REPORT_OUTPUT_BUFFERED	0x00000002#define DM_REPORT_OUTPUT_HEADINGS	0x00000004struct dm_report *dm_report_init(uint32_t *report_types,				 const struct dm_report_object_type *types,				 const struct dm_report_field_type *fields,				 const char *output_fields,				 const char *output_separator,				 uint32_t output_flags,				 const char *sort_keys,				 void *private);int dm_report_object(struct dm_report *rh, void *object);int dm_report_output(struct dm_report *rh);void dm_report_free(struct dm_report *rh);/* * Report functions are provided for simple data types. * They take care of allocating copies of the data. */int dm_report_field_string(struct dm_report *rh, struct dm_report_field *field,			   const char **data);int dm_report_field_int32(struct dm_report *rh, struct dm_report_field *field,			  const int32_t *data);int dm_report_field_uint32(struct dm_report *rh, struct dm_report_field *field,			   const uint32_t *data);int dm_report_field_int(struct dm_report *rh, struct dm_report_field *field,			const int *data);int dm_report_field_uint64(struct dm_report *rh, struct dm_report_field *field,			   const uint64_t *data);/* * For custom fields, allocate the data in 'mem' and use * dm_report_field_set_value(). * 'sortvalue' may be NULL if it matches 'value' */void dm_report_field_set_value(struct dm_report_field *field, const void *value,			       const void *sortvalue);#endif				/* LIB_DEVICE_MAPPER_H */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -