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