annotate libcilkrts/runtime/frame_malloc.h @ 136:4627f235cf2a

fix c-next example
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 08 Nov 2018 14:11:56 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* frame_malloc.h -*-C++-*-
kono
parents:
diff changeset
2 *
kono
parents:
diff changeset
3 *************************************************************************
kono
parents:
diff changeset
4 *
kono
parents:
diff changeset
5 * Copyright (C) 2009-2016, Intel Corporation
kono
parents:
diff changeset
6 * All rights reserved.
kono
parents:
diff changeset
7 *
kono
parents:
diff changeset
8 * Redistribution and use in source and binary forms, with or without
kono
parents:
diff changeset
9 * modification, are permitted provided that the following conditions
kono
parents:
diff changeset
10 * are met:
kono
parents:
diff changeset
11 *
kono
parents:
diff changeset
12 * * Redistributions of source code must retain the above copyright
kono
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer.
kono
parents:
diff changeset
14 * * Redistributions in binary form must reproduce the above copyright
kono
parents:
diff changeset
15 * notice, this list of conditions and the following disclaimer in
kono
parents:
diff changeset
16 * the documentation and/or other materials provided with the
kono
parents:
diff changeset
17 * distribution.
kono
parents:
diff changeset
18 * * Neither the name of Intel Corporation nor the names of its
kono
parents:
diff changeset
19 * contributors may be used to endorse or promote products derived
kono
parents:
diff changeset
20 * from this software without specific prior written permission.
kono
parents:
diff changeset
21 *
kono
parents:
diff changeset
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
kono
parents:
diff changeset
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
kono
parents:
diff changeset
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
kono
parents:
diff changeset
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
kono
parents:
diff changeset
26 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
kono
parents:
diff changeset
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
kono
parents:
diff changeset
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
kono
parents:
diff changeset
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
kono
parents:
diff changeset
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
kono
parents:
diff changeset
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
kono
parents:
diff changeset
32 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
kono
parents:
diff changeset
33 * POSSIBILITY OF SUCH DAMAGE.
kono
parents:
diff changeset
34 *
kono
parents:
diff changeset
35 * *********************************************************************
kono
parents:
diff changeset
36 *
kono
parents:
diff changeset
37 * PLEASE NOTE: This file is a downstream copy of a file mainitained in
kono
parents:
diff changeset
38 * a repository at cilkplus.org. Changes made to this file that are not
kono
parents:
diff changeset
39 * submitted through the contribution process detailed at
kono
parents:
diff changeset
40 * http://www.cilkplus.org/submit-cilk-contribution will be lost the next
kono
parents:
diff changeset
41 * time that a new version is released. Changes only submitted to the
kono
parents:
diff changeset
42 * GNU compiler collection or posted to the git repository at
kono
parents:
diff changeset
43 * https://bitbucket.org/intelcilkruntime/intel-cilk-runtime.git are
kono
parents:
diff changeset
44 * not tracked.
kono
parents:
diff changeset
45 *
kono
parents:
diff changeset
46 * We welcome your contributions to this open source project. Thank you
kono
parents:
diff changeset
47 * for your assistance in helping us improve Cilk Plus.
kono
parents:
diff changeset
48 **************************************************************************/
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 /**
kono
parents:
diff changeset
51 * @file frame_malloc.h
kono
parents:
diff changeset
52 *
kono
parents:
diff changeset
53 * @brief The frame allocation routines manage memory in a per-worker pool.
kono
parents:
diff changeset
54 *
kono
parents:
diff changeset
55 * The name "frame malloc" refers to an earlier implementation of Cilk which
kono
parents:
diff changeset
56 * allocated frames from the heap using this allocator.
kono
parents:
diff changeset
57 */
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 #ifndef INCLUDED_FRAME_MALLOC_DOT_H
kono
parents:
diff changeset
60 #define INCLUDED_FRAME_MALLOC_DOT_H
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 #include "worker_mutex.h"
kono
parents:
diff changeset
63 #include "rts-common.h"
kono
parents:
diff changeset
64 #include <internal/abi.h> // __cilkrts_worker
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 #ifdef __cplusplus
kono
parents:
diff changeset
67 # include <cstddef>
kono
parents:
diff changeset
68 #else
kono
parents:
diff changeset
69 # include <stddef.h>
kono
parents:
diff changeset
70 #endif
kono
parents:
diff changeset
71
kono
parents:
diff changeset
72 __CILKRTS_BEGIN_EXTERN_C
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 /**
kono
parents:
diff changeset
75 * Number of buckets. Gives us buckets to hold 64, 128, 256, 512, 1024
kono
parents:
diff changeset
76 * and 2048 bytes
kono
parents:
diff changeset
77 */
kono
parents:
diff changeset
78 #define FRAME_MALLOC_NBUCKETS 6
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 /** Layout of frames when unallocated */
kono
parents:
diff changeset
81 struct free_list {
kono
parents:
diff changeset
82 /** Pointer to next free frame */
kono
parents:
diff changeset
83 struct free_list *cdr;
kono
parents:
diff changeset
84 };
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 /** per-worker memory cache */
kono
parents:
diff changeset
87 struct __cilkrts_frame_cache
kono
parents:
diff changeset
88 {
kono
parents:
diff changeset
89 /** Mutex to serialize access */
kono
parents:
diff changeset
90 struct mutex lock;
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 /** Linked list of frames */
kono
parents:
diff changeset
93 struct pool_cons *pool_list;
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 /** Low bound of memory in pool */
kono
parents:
diff changeset
96 char *pool_begin;
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 /** High bound of memory in pool */
kono
parents:
diff changeset
99 char *pool_end;
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 /** Global free-list buckets */
kono
parents:
diff changeset
102 struct free_list *global_free_list[FRAME_MALLOC_NBUCKETS];
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 /**
kono
parents:
diff changeset
105 * How many bytes to obtain at once from the global pool
kono
parents:
diff changeset
106 * (approximately)
kono
parents:
diff changeset
107 */
kono
parents:
diff changeset
108 size_t batch_size;
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 /** Garbage-collect a bucket when its potential exceeds the limit */
kono
parents:
diff changeset
111 size_t potential_limit;
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 /** If TRUE, check for memory leaks at the end of execution */
kono
parents:
diff changeset
114 int check_for_leaks;
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 /** Bytes of memory allocated from the OS by the global cache */
kono
parents:
diff changeset
117 size_t allocated_from_os;
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 /** Tracks memory allocated by a chunk that isn't a full bucket size */
kono
parents:
diff changeset
120 size_t wasted;
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 /** Bytes of memory allocated from the global cache */
kono
parents:
diff changeset
123 size_t allocated_from_global_pool;
kono
parents:
diff changeset
124 };
kono
parents:
diff changeset
125
kono
parents:
diff changeset
126 /**
kono
parents:
diff changeset
127 * Allocate memory from the per-worker pool. If the size is too large, or
kono
parents:
diff changeset
128 * if we're given a NULL worker, the memory is allocated using
kono
parents:
diff changeset
129 * __cilkrts_malloc().
kono
parents:
diff changeset
130 *
kono
parents:
diff changeset
131 * @param w The worker to allocate the memory from.
kono
parents:
diff changeset
132 * @param size The number of bytes to allocate.
kono
parents:
diff changeset
133 *
kono
parents:
diff changeset
134 * @return pointer to allocated memory block.
kono
parents:
diff changeset
135 */
kono
parents:
diff changeset
136 COMMON_PORTABLE
kono
parents:
diff changeset
137 void *__cilkrts_frame_malloc(__cilkrts_worker *w,
kono
parents:
diff changeset
138 size_t size) cilk_nothrow;
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 /**
kono
parents:
diff changeset
141 * Return memory to the per-worker pool. If the size is too large, or
kono
parents:
diff changeset
142 * if we're given a NULL worker, the memory is freed using
kono
parents:
diff changeset
143 * __cilkrts_free().
kono
parents:
diff changeset
144 *
kono
parents:
diff changeset
145 * @param w The worker to allocate the memory from.
kono
parents:
diff changeset
146 * @param p The memory block to be released.
kono
parents:
diff changeset
147 * @param size The size of the block, in bytes.
kono
parents:
diff changeset
148 */
kono
parents:
diff changeset
149 COMMON_PORTABLE
kono
parents:
diff changeset
150 void __cilkrts_frame_free(__cilkrts_worker *w,
kono
parents:
diff changeset
151 void* p,
kono
parents:
diff changeset
152 size_t size) cilk_nothrow;
kono
parents:
diff changeset
153
kono
parents:
diff changeset
154 /**
kono
parents:
diff changeset
155 * Destroy the global cache stored in the global state, freeing all memory
kono
parents:
diff changeset
156 * to the global heap. Checks whether any memory has been allocated but
kono
parents:
diff changeset
157 * not freed.
kono
parents:
diff changeset
158 *
kono
parents:
diff changeset
159 * @param g The global state.
kono
parents:
diff changeset
160 */
kono
parents:
diff changeset
161 COMMON_PORTABLE
kono
parents:
diff changeset
162 void __cilkrts_frame_malloc_global_cleanup(global_state_t *g);
kono
parents:
diff changeset
163
kono
parents:
diff changeset
164 /**
kono
parents:
diff changeset
165 * Initialize a worker's memory cache. Initially it is empty.
kono
parents:
diff changeset
166 *
kono
parents:
diff changeset
167 * @param w The worker who's memory cache is to be initialized.
kono
parents:
diff changeset
168 */
kono
parents:
diff changeset
169 COMMON_PORTABLE
kono
parents:
diff changeset
170 void __cilkrts_frame_malloc_per_worker_init(__cilkrts_worker *w);
kono
parents:
diff changeset
171
kono
parents:
diff changeset
172 /**
kono
parents:
diff changeset
173 * If check_for_leaks is set in the global state's memory cache, free any
kono
parents:
diff changeset
174 * memory in the worker's memory cache.
kono
parents:
diff changeset
175 *
kono
parents:
diff changeset
176 * If check_for_leask is not set, nothing happens.
kono
parents:
diff changeset
177 *
kono
parents:
diff changeset
178 * @param w The worker who's memory cache is to be cleaned up.
kono
parents:
diff changeset
179 */
kono
parents:
diff changeset
180 COMMON_PORTABLE
kono
parents:
diff changeset
181 void __cilkrts_frame_malloc_per_worker_cleanup(__cilkrts_worker *w);
kono
parents:
diff changeset
182
kono
parents:
diff changeset
183 /**
kono
parents:
diff changeset
184 * Round a number of bytes to the size of the smallest bucket that will
kono
parents:
diff changeset
185 * hold it. If the size is bigger than the largest bucket, the value is
kono
parents:
diff changeset
186 * unchanged.
kono
parents:
diff changeset
187 *
kono
parents:
diff changeset
188 * @param size Number of bytes to be rounded up to the nearest bucket size.
kono
parents:
diff changeset
189 *
kono
parents:
diff changeset
190 * @return The size of the smallest bucket that will hold the specified bytes.
kono
parents:
diff changeset
191 */
kono
parents:
diff changeset
192 COMMON_PORTABLE
kono
parents:
diff changeset
193 size_t __cilkrts_frame_malloc_roundup(size_t size) cilk_nothrow;
kono
parents:
diff changeset
194
kono
parents:
diff changeset
195 /**
kono
parents:
diff changeset
196 * Return the number of bytes that can fit into a bucket.
kono
parents:
diff changeset
197 *
kono
parents:
diff changeset
198 * Preconditions:
kono
parents:
diff changeset
199 * - The index must be in the range 0 - FRAME_MALLOC_NBUCKETS
kono
parents:
diff changeset
200 *
kono
parents:
diff changeset
201 * @param bucket Index of the bucket to be sized.
kono
parents:
diff changeset
202 */
kono
parents:
diff changeset
203 COMMON_PORTABLE
kono
parents:
diff changeset
204 size_t __cilkrts_size_of_bucket(int bucket) cilk_nothrow;
kono
parents:
diff changeset
205
kono
parents:
diff changeset
206 /**
kono
parents:
diff changeset
207 * Initialize the global memory cache.
kono
parents:
diff changeset
208 *
kono
parents:
diff changeset
209 * @param g The global state.
kono
parents:
diff changeset
210 */
kono
parents:
diff changeset
211 COMMON_PORTABLE
kono
parents:
diff changeset
212 void __cilkrts_frame_malloc_global_init(global_state_t *g);
kono
parents:
diff changeset
213
kono
parents:
diff changeset
214 __CILKRTS_END_EXTERN_C
kono
parents:
diff changeset
215
kono
parents:
diff changeset
216 #endif // ! defined(INCLUDED_FRAME_MALLOC_DOT_H)