File: allocator_bucket_alloc.h

package info (click to toggle)
openmpi 5.0.8-8
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 201,692 kB
  • sloc: ansic: 613,078; makefile: 42,350; sh: 11,194; javascript: 9,244; f90: 7,052; java: 6,404; perl: 5,179; python: 1,859; lex: 740; fortran: 61; cpp: 20; tcl: 12
file content (211 lines) | stat: -rw-r--r-- 7,757 bytes parent folder | download | duplicates (4)
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
40
41
42
43
44
45
46
47
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
/*
 * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
 *                         University Research and Technology
 *                         Corporation.  All rights reserved.
 * Copyright (c) 2004-2005 The University of Tennessee and The University
 *                         of Tennessee Research Foundation.  All rights
 *                         reserved.
 * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
 *                         University of Stuttgart.  All rights reserved.
 * Copyright (c) 2004-2005 The Regents of the University of California.
 *                         All rights reserved.
 * Copyright (c) 2015      Los Alamos National Security, LLC. All rights
 *                         reserved.
 * $COPYRIGHT$
 *
 * Additional copyrights may follow
 *
 * $HEADER$
 */

/** @file
 *  A generic memory bucket allocator.
 **/

#ifndef ALLOCATOR_BUCKET_ALLOC_H
#define ALLOCATOR_BUCKET_ALLOC_H

#include "opal_config.h"
#include "opal/mca/allocator/allocator.h"
#include "opal/mca/threads/mutex.h"
#include <stdlib.h>
#include <string.h>

BEGIN_C_DECLS

/**
 * Structure for the header of each memory chunk
 */
struct mca_allocator_bucket_chunk_header_t {
    struct mca_allocator_bucket_chunk_header_t *next_in_segment;
    /**< The next chunk in the memory segment */
    /**
     * Union which holds either a pointer to the next free chunk
     * or the bucket number. Based on the current location of the chunk
     * we use one or the other of these fields. If the chunk is owned
     * by the user, then the bucket field is set, which allow us to know
     * in which specific bucket we have to put it back on free (as the
     * chunk don't have the size attached). When the allocator own the
     * chunk, the next_free fild is used, which allow us to put these
     * chunks a list of free elements.
     */
    union u {
        struct mca_allocator_bucket_chunk_header_t *next_free;
        /**< if the chunk is free this will point to the next free chunk in the bucket */
        int bucket; /**< the bucket number it belongs to */
    } u;            /**< the union */
};
/**
 * Typedef so we don't have to use struct
 */
typedef struct mca_allocator_bucket_chunk_header_t mca_allocator_bucket_chunk_header_t;

/**
 * Structure that heads each segment
 */
struct mca_allocator_bucket_segment_head_t {
    struct mca_allocator_bucket_chunk_header_t *first_chunk;  /**< the first chunk of the header */
    struct mca_allocator_bucket_segment_head_t *next_segment; /**< the next segment in the
                                                bucket */
};
/**
 * Typedef so we don't have to use struct
 */
typedef struct mca_allocator_bucket_segment_head_t mca_allocator_bucket_segment_head_t;

/**
 * Structure for each bucket
 */
struct mca_allocator_bucket_bucket_t {
    mca_allocator_bucket_chunk_header_t *free_chunk;   /**< the first free chunk of memory */
    opal_mutex_t lock;                                 /**< the lock on the bucket */
    mca_allocator_bucket_segment_head_t *segment_head; /**< the list of segment headers */
};
/**
 * Typedef so we don't have to use struct
 */
typedef struct mca_allocator_bucket_bucket_t mca_allocator_bucket_bucket_t;

/**
 * Structure that holds the necessary information for each area of memory
 */
struct mca_allocator_bucket_t {
    mca_allocator_base_module_t super;      /**< makes this a child of class mca_allocator_t */
    mca_allocator_bucket_bucket_t *buckets; /**< the array of buckets */
    int num_buckets;                        /**< the number of buckets */
    mca_allocator_base_component_segment_alloc_fn_t get_mem_fn;
    /**< pointer to the function to get more memory */
    mca_allocator_base_component_segment_free_fn_t free_mem_fn;
    /**< pointer to the function to free memory */
};
/**
 * Typedef so we don't have to use struct
 */
typedef struct mca_allocator_bucket_t mca_allocator_bucket_t;

/**
 * Initializes the mca_allocator_bucket_options_t data structure for the passed
 * parameters.
 * @param mem a pointer to the mca_allocator_t struct to be filled in
 * @param num_buckets The number of buckets the allocator will use
 * @param get_mem_funct A pointer to the function that the allocator
 * will use to get more memory
 * @param free_mem_funct A pointer to the function that the allocator
 * will use to free memory
 *
 * @retval Pointer to the initialized mca_allocator_bucket_options_t structure
 * @retval NULL if there was an error
 */
mca_allocator_bucket_t *
mca_allocator_bucket_init(mca_allocator_base_module_t *mem, int num_buckets,
                          mca_allocator_base_component_segment_alloc_fn_t get_mem_funct,
                          mca_allocator_base_component_segment_free_fn_t free_mem_funct);
/**
 * Accepts a request for memory in a specific region defined by the
 * mca_allocator_bucket_options_t struct and returns a pointer to memory in that
 * region or NULL if there was an error
 *
 * @param mem A pointer to the appropriate struct for the area of memory.
 * @param size The size of the requested area of memory
 *
 * @retval Pointer to the area of memory if the allocation was successful
 * @retval NULL if the allocation was unsuccessful
 */
void *mca_allocator_bucket_alloc(mca_allocator_base_module_t *mem, size_t size);

/**
 * Accepts a request for memory in a specific region defined by the
 * mca_allocator_bucket_options_t struct and aligned by the specified amount and
 * returns a pointer to memory in that region or NULL if there was an error
 *
 * @param mem A pointer to the appropriate struct for the area of
 * memory.
 * @param size The size of the requested area of memory
 * @param alignment The requested alignment of the new area of memory. This
 * MUST be a power of 2.
 *
 * @retval Pointer to the area of memory if the allocation was successful
 * @retval NULL if the allocation was unsuccessful
 *
 */
void *mca_allocator_bucket_alloc_align(mca_allocator_base_module_t *mem, size_t size,
                                       size_t alignment);

/**
 * Attempts to resize the passed region of memory into a larger or a smaller
 * region. If it is unsuccessful, it will return NULL and the passed area of
 * memory will be untouched.
 *
 * @param mem A pointer to the appropriate struct for the area of
 * memory.
 * @param size The size of the requested area of memory
 * @param ptr A pointer to the region of memory to be resized
 *
 * @retval Pointer to the area of memory if the reallocation was successful
 * @retval NULL if the allocation was unsuccessful
 *
 */
void *mca_allocator_bucket_realloc(mca_allocator_base_module_t *mem, void *ptr, size_t size);

/**
 * Frees the passed region of memory
 *
 * @param mem A pointer to the appropriate struct for the area of
 * memory.
 * @param ptr A pointer to the region of memory to be freed
 *
 * @retval None
 *
 */
void mca_allocator_bucket_free(mca_allocator_base_module_t *mem, void *ptr);

/**
 * Frees all the memory from all the buckets back to the system. Note that
 * this function only frees memory that was previously freed with
 * mca_allocator_bucket_free().
 *
 * @param mem A pointer to the appropriate struct for the area of
 * memory.
 *
 * @retval None
 *
 */
int mca_allocator_bucket_cleanup(mca_allocator_base_module_t *mem);

/**
 * Cleanup all resources held by this allocator.
 *
 * @param mem A pointer to the appropriate struct for the area of
 * memory.
 *
 * @retval None
 *
 */
int mca_allocator_bucket_finalize(mca_allocator_base_module_t *mem);

OPAL_DECLSPEC extern mca_allocator_base_component_t mca_allocator_bucket_component;

END_C_DECLS

#endif /* ALLOCATOR_BUCKET_ALLOC_H */