1 /* frame_malloc.h -*-C++-*-
3 *************************************************************************
6 * Copyright (C) 2009-2013, Intel Corporation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * * Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
20 * * Neither the name of Intel Corporation nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
32 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
33 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
35 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 **************************************************************************/
40 * @file frame_malloc.h
42 * @brief The frame allocation routines manage memory in a per-worker pool.
44 * The name "frame malloc" refers to an earlier implementation of Cilk which
45 * allocated frames from the heap using this allocator.
48 #ifndef INCLUDED_FRAME_MALLOC_DOT_H
49 #define INCLUDED_FRAME_MALLOC_DOT_H
51 #include "worker_mutex.h"
52 #include "rts-common.h"
53 #include <internal/abi.h> // __cilkrts_worker
61 __CILKRTS_BEGIN_EXTERN_C
64 * Number of buckets. Gives us buckets to hold 64, 128, 256, 512, 1024
67 #define FRAME_MALLOC_NBUCKETS 6
69 /** Layout of frames when unallocated */
71 /** Pointer to next free frame */
72 struct free_list
*cdr
;
75 /** per-worker memory cache */
76 struct __cilkrts_frame_cache
78 /** Mutex to serialize access */
81 /** Linked list of frames */
82 struct pool_cons
*pool_list
;
84 /** Low bound of memory in pool */
87 /** High bound of memory in pool */
90 /** Global free-list buckets */
91 struct free_list
*global_free_list
[FRAME_MALLOC_NBUCKETS
];
94 * How many bytes to obtain at once from the global pool
99 /** Garbage-collect a bucket when its potential exceeds the limit */
100 size_t potential_limit
;
102 /** If TRUE, check for memory leaks at the end of execution */
105 /** Bytes of memory allocated from the OS by the global cache */
106 size_t allocated_from_os
;
108 /** Tracks memory allocated by a chunk that isn't a full bucket size */
111 /** Bytes of memory allocated from the global cache */
112 size_t allocated_from_global_pool
;
116 * Allocate memory from the per-worker pool. If the size is too large, or
117 * if we're given a NULL worker, the memory is allocated using
118 * __cilkrts_malloc().
120 * @param w The worker to allocate the memory from.
121 * @param size The number of bytes to allocate.
123 * @return pointer to allocated memory block.
126 void *__cilkrts_frame_malloc(__cilkrts_worker
*w
,
127 size_t size
) cilk_nothrow
;
130 * Return memory to the per-worker pool. If the size is too large, or
131 * if we're given a NULL worker, the memory is freed using
134 * @param w The worker to allocate the memory from.
135 * @param p The memory block to be released.
136 * @param size The size of the block, in bytes.
139 void __cilkrts_frame_free(__cilkrts_worker
*w
,
141 size_t size
) cilk_nothrow
;
144 * Destroy the global cache stored in the global state, freeing all memory
145 * to the global heap. Checks whether any memory has been allocated but
148 * @param g The global state.
151 void __cilkrts_frame_malloc_global_cleanup(global_state_t
*g
);
154 * Initialize a worker's memory cache. Initially it is empty.
156 * @param w The worker who's memory cache is to be initialized.
159 void __cilkrts_frame_malloc_per_worker_init(__cilkrts_worker
*w
);
162 * If check_for_leaks is set in the global state's memory cache, free any
163 * memory in the worker's memory cache.
165 * If check_for_leask is not set, nothing happens.
167 * @param w The worker who's memory cache is to be cleaned up.
170 void __cilkrts_frame_malloc_per_worker_cleanup(__cilkrts_worker
*w
);
173 * Round a number of bytes to the size of the smallest bucket that will
174 * hold it. If the size is bigger than the largest bucket, the value is
177 * @param size Number of bytes to be rounded up to the nearest bucket size.
179 * @return The size of the smallest bucket that will hold the specified bytes.
182 size_t __cilkrts_frame_malloc_roundup(size_t size
) cilk_nothrow
;
185 * Return the number of bytes that can fit into a bucket.
188 * - The index must be in the range 0 - FRAME_MALLOC_NBUCKETS
190 * @param bucket Index of the bucket to be sized.
193 size_t __cilkrts_size_of_bucket(int bucket
) cilk_nothrow
;
196 * Initialize the global memory cache.
198 * @param g The global state.
201 void __cilkrts_frame_malloc_global_init(global_state_t
*g
);
203 __CILKRTS_END_EXTERN_C
205 #endif // ! defined(INCLUDED_FRAME_MALLOC_DOT_H)