1 /* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil -*- */
2 /*
3  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
4  *                         University Research and Technology
5  *                         Corporation.  All rights reserved.
6  * Copyright (c) 2004-2005 The University of Tennessee and The University
7  *                         of Tennessee Research Foundation.  All rights
8  *                         reserved.
9  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
10  *                         University of Stuttgart.  All rights reserved.
11  * Copyright (c) 2004-2005 The Regents of the University of California.
12  *                         All rights reserved.
13  * Copyright (c) 2015      Los Alamos National Security, LLC. All rights
14  *                         reserved.
15  * $COPYRIGHT$
16  *
17  * Additional copyrights may follow
18  *
19  * $HEADER$
20  */
21 
22 /** @file
23  *  A generic memory basic allocator.
24  **/
25 
26 #ifndef ALLOCATOR_BASIC_H
27 #define ALLOCATOR_BASIC_H
28 
29 #include "opal_config.h"
30 #include <stdlib.h>
31 #include <string.h>
32 #include "opal/threads/mutex.h"
33 #include "opal/class/opal_free_list.h"
34 #include "opal/mca/allocator/allocator.h"
35 
36 
37 /*
38  * Free list of allocated segments
39  */
40 
41 struct mca_allocator_basic_segment_t {
42     opal_free_list_item_t seg_item;
43     unsigned char* seg_addr;
44     size_t seg_size;
45 };
46 typedef struct mca_allocator_basic_segment_t mca_allocator_basic_segment_t;
47 
48 
49 /*
50  * Basic allocator module
51  */
52 
53 struct mca_allocator_basic_module_t {
54     mca_allocator_base_module_t super;
55     mca_allocator_base_component_segment_alloc_fn_t seg_alloc;
56     mca_allocator_base_component_segment_free_fn_t seg_free;
57     opal_list_t seg_list;
58     opal_mutex_t seg_lock;
59     opal_free_list_t seg_descriptors;
60 };
61 typedef struct mca_allocator_basic_module_t mca_allocator_basic_module_t;
62 
63 
64 BEGIN_C_DECLS
65 
66 /*
67  * Component open/cleanup.
68  */
69 
70 int mca_allocator_basic_component_open(void);
71 int mca_allocator_basic_component_close(void);
72 
73 /**
74   * The function used to initialize the component.
75   */
76 mca_allocator_base_module_t* mca_allocator_basic_component_init(
77     bool enable_mpi_threads,
78     mca_allocator_base_component_segment_alloc_fn_t segment_alloc,
79     mca_allocator_base_component_segment_free_fn_t segment_free,
80     void *ctx
81 );
82 
83 /**
84    * Accepts a request for memory in a specific region defined by the
85    * mca_allocator_basic_options_t struct and returns a pointer to memory in that
86    * region or NULL if there was an error
87    *
88    * @param mem A pointer to the appropriate struct for the area of memory.
89    * @param size The size of the requested area of memory
90    *
91    * @retval Pointer to the area of memory if the allocation was successful
92    * @retval NULL if the allocation was unsuccessful
93    */
94     void * mca_allocator_basic_alloc(
95         mca_allocator_base_module_t * mem,
96         size_t size,
97         size_t align);
98 
99 /**
100    * Attempts to resize the passed region of memory into a larger or a smaller
101    * region. If it is unsuccessful, it will return NULL and the passed area of
102    * memory will be untouched.
103    *
104    * @param mem A pointer to the appropriate struct for the area of
105    * memory.
106    * @param size The size of the requested area of memory
107    * @param ptr A pointer to the region of memory to be resized
108    *
109    * @retval Pointer to the area of memory if the reallocation was successful
110    * @retval NULL if the allocation was unsuccessful
111    *
112    */
113     void * mca_allocator_basic_realloc(
114         mca_allocator_base_module_t * mem,
115         void * ptr,
116         size_t size);
117 
118 /**
119    * Frees the passed region of memory
120    *
121    * @param mem A pointer to the appropriate struct for the area of
122    * memory.
123    * @param ptr A pointer to the region of memory to be freed
124    *
125    * @retval None
126    *
127    */
128     void mca_allocator_basic_free(
129         mca_allocator_base_module_t * mem,
130         void * ptr);
131 
132 /**
133    * Frees all the memory from all the basics back to the system. Note that
134    * this function only frees memory that was previously freed with
135    * mca_allocator_basic_free().
136    *
137    * @param mem A pointer to the appropriate struct for the area of
138    * memory.
139    *
140    * @retval None
141    *
142    */
143     int mca_allocator_basic_compact(
144         mca_allocator_base_module_t * mem);
145 
146 /**
147    * Cleanup all resources held by this allocator.
148    *
149    * @param mem A pointer to the appropriate struct for the area of
150    * memory.
151    *
152    * @retval None
153    *
154    */
155     int mca_allocator_basic_finalize(
156         mca_allocator_base_module_t * mem);
157 
158 OPAL_DECLSPEC extern mca_allocator_base_component_t mca_allocator_basic_component;
159 
160 END_C_DECLS
161 
162 #endif /* ALLOCATOR_BUCKET_ALLOC_H */
163 
164 
165 
166 
167 
168 
169 
170