fast_mblock.h 2.4 KB
Newer Older
Y
yuqing 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/**
* Copyright (C) 2008 Happy Fish / YuQing
*
* FastDFS may be copied only under the terms of the GNU General
* Public License V3, which may be found in the FastDFS source kit.
* Please visit the FastDFS Home Page http://www.csource.org/ for more detail.
**/

//fast_mblock.h

#ifndef _FAST_MBLOCK_H
#define _FAST_MBLOCK_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "common_define.h"
#include "chain.h"

/* free node chain */ 
struct fast_mblock_node
{
	struct fast_mblock_node *next;
	char data[0];   //the data buffer
};

/* malloc chain */
struct fast_mblock_malloc
{
	struct fast_mblock_malloc *next;
};

struct fast_mblock_man
{
	struct fast_mblock_node *free_chain_head;     //free node chain
	struct fast_mblock_malloc *malloc_chain_head; //malloc chain to be freed
	int element_size;         //element size
	int alloc_elements_once;  //alloc elements once
Y
yuqing 已提交
40
    int total_count;          //total element count
Y
yuqing 已提交
41 42 43
	pthread_mutex_t lock;     //the lock for read / write free node chain
};

Y
yuqing 已提交
44 45 46 47
#define fast_mblock_to_node_ptr(data_ptr) \
        (struct fast_mblock_node *)(data_ptr - ((size_t)(char *) \
                    &((struct fast_mblock_node *)0)->data))

Y
yuqing 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#ifdef __cplusplus
extern "C" {
#endif

/**
mblock init
parameters:
	mblock: the mblock pointer
	element_size: element size, such as sizeof(struct xxx)
	alloc_elements_once: malloc elements once, 0 for malloc 1MB once
return error no, 0 for success, != 0 fail
*/
int fast_mblock_init(struct fast_mblock_man *mblock, const int element_size, \
		const int alloc_elements_once);

/**
mblock destroy
parameters:
	mblock: the mblock pointer
*/
void fast_mblock_destroy(struct fast_mblock_man *mblock);

/**
alloc a node from the mblock
parameters:
	mblock: the mblock pointer
return the alloced node, return NULL if fail
*/
struct fast_mblock_node *fast_mblock_alloc(struct fast_mblock_man *mblock);

/**
free a node (put a node to the mblock)
parameters:
	mblock: the mblock pointer
	pNode: the node to free
return the alloced node, return NULL if fail
*/
int fast_mblock_free(struct fast_mblock_man *mblock, \
		     struct fast_mblock_node *pNode);

/**
get node count of the mblock
parameters:
	mblock: the mblock pointer
return the free node count of the mblock, return -1 if fail
*/
Y
yuqing 已提交
94 95 96
int fast_mblock_free_count(struct fast_mblock_man *mblock);

#define fast_mblock_total_count(mblock) (mblock)->total_count
Y
yuqing 已提交
97 98 99 100 101 102 103

#ifdef __cplusplus
}
#endif

#endif