Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Propagate file renaming to windows
[simgrid.git] / tools / tesh2 / include / allocator.h
1 #ifndef __ALLOCATOR_H
2 #define __ALLOCATOR_H
3
4 #ifdef __cplusplus
5 extern "C" {
6 #endif
7
8
9 #ifndef __FN_FINALIZE_T_DEFINED
10 typedef int (*fn_finalize_t)(void**);
11 #define __FN_FINALIZE_T_DEFINED
12 #endif
13
14 #ifndef __BYTE_T_DEFINED
15 typedef unsigned char byte;
16 #define __BYTE_T_DEFINED
17 #endif
18
19 /* forward declarations */
20 struct s_allocator;                     /* allocator struct                     */
21 struct s_allocator_block;       /* allocator block struct       */
22 struct s_allocator_node;        /* allocator node struct        */
23
24
25 /* the allocator node type */
26 typedef struct s_allocator_node
27 {
28         struct s_allocator_node* next;                          /* the next node in the block                                                                                   */      
29         struct s_allocator_block* block;                        /* the block which contains the node                                                                    */      
30         int is_allocated;                                                               /* if 1 the node is allocated (used)                                                                    */
31 }s_allocator_node_t,* allocator_node_t;
32
33 /* the allocator block type     */
34 typedef struct s_allocator_block
35 {
36         struct s_allocator* allocator;                  /* the allocator which contains the block                                                               */      
37         struct s_allocator_block* next;                 /* the next block                                                                                                               */
38 }s_allocator_block_t,* allocator_block_t;
39
40 /* the allocator type */
41 typedef struct s_allocator
42 {
43         allocator_block_t head;                                 /* the head of the allocator blocks                                                                             */
44         unsigned int block_capacity;                    /* the capacity of the allocator blocks (nodes per block)                               */
45         int capacity;                                                   /* the current capacity of the allocator (nodes per block x block count)*/                                                                                      
46     int type_size;                                              /* size of allocated type                                                                                               */
47         int size;                                                               /* current node (object) count                                                                                  */
48         allocator_node_t free;                                  /* pointer to the next free node                                                                                */
49         allocator_node_t first;                                 /* pointer to the first free node                                                                               */
50         fn_finalize_t fn_finalize;      /* pointer to the function used to destroy the allocated objects                */
51 }s_allocator_t,* allocator_t;
52
53 allocator_t
54 allocator_new(int block_capacity, int type_size,  fn_finalize_t fn_finalize);
55
56 int
57 allocator_free(allocator_t* allocator_ptr);
58
59 void* 
60 allocator_alloc(allocator_t allocator);
61
62 int
63 allocator_dealloc(allocator_t allocator,void* block);
64
65 int
66 allocator_get_size(allocator_t allocator);
67
68 int
69 allocator_get_capacity(allocator_t allocator);
70
71 int
72 allocator_get_type_size(allocator_t allocator);
73
74 int
75 allocator_get_block_capacity(allocator_t allocator);
76
77 int
78 allocator_clear(allocator_t allocator);
79
80 int
81 allocator_get_capacity_available(allocator_t allocator);
82
83 int
84 allocator_is_empty(allocator_t allocator);
85
86 int
87 allocator_is_full(allocator_t allocator);
88
89
90 #ifdef __cplusplus
91 }
92 #endif
93
94 #endif /* !__allocator_H */