SST/macro
stack_alloc.h
Go to the documentation of this file.
1 /*
2  * This file is part of SST/macroscale:
3  * The macroscale architecture simulator from the SST suite.
4  * Copyright (c) 2009 Sandia Corporation.
5  * This software is distributed under the BSD License.
6  * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7  * the U.S. Government retains certain rights in this software.
8  * For more information, see the LICENSE file in the top
9  * SST/macroscale directory.
10  */
11 
12 #ifndef SSTMAC_SOFTWARE_THREADING_STACKALLOC_H_INCLUDED
13 #define SSTMAC_SOFTWARE_THREADING_STACKALLOC_H_INCLUDED
14 
15 #include <cstring>
16 #include <vector>
17 
18 namespace sstmac {
19 namespace sw {
20 
21 /**
22  * A management type to handle dividing mmap-ed memory for use
23  * as ucontext stack(s). This is basically a very simple malloc
24  * which allocates uniform-size chunks (with the NX bit unset)
25  * and sets guard pages on each side of the allocated stacks.
26  *
27  * This allocator does not return memory to the system until it is
28  * deleted, but regions can be allocated and free-d repeatedly.
29  */
31 {
32  private:
33  /// The memory regions get allocated in chunks.
34  class chunk;
35  /// This is where we store the memory regions.
36  typedef std::vector<chunk*> chunk_vec_t;
37  chunk_vec_t chunks_;
38  /// Each chunk is of this suggested size.
40  /// Each stack request is of this size:
41  size_t stacksize_;
42  /// Do we want stacks separated by an mprot region?
43  bool use_mprot_;
44 
45  /// This is our list of un-allocated chunks:
46  typedef std::vector<void*> available_vec_t;
47  available_vec_t available_;
48 
49  public:
50  /// Build.
51  stack_alloc();
52 
53  size_t
54  stacksize() const {
55  return stacksize_;
56  }
57 
58  bool
59  use_mprot() const {
60  return use_mprot_;
61  }
62 
63  size_t
64  chunksize() const {
65  return suggested_chunk_;
66  }
67 
68  /// Goodbye.
69  virtual ~stack_alloc();
70 
71  /// Get a stack memory region.
72  void* alloc();
73 
74  /// Return the given memory region.
75  void free(void*);
76 
77  void init(size_t stacksize, size_t alloc_unit, bool use_mprot);
78 
79  bool
80  initialized() const {
81  return stacksize_;
82  }
83 
84  void clear();
85 
86 };
87 
88 }
89 } // end of namespace sstmac
90 
91 #endif
92 
virtual ~stack_alloc()
Goodbye.
size_t chunksize() const
Definition: stack_alloc.h:64
void init(size_t stacksize, size_t alloc_unit, bool use_mprot)
bool use_mprot_
Do we want stacks separated by an mprot region?
Definition: stack_alloc.h:43
bool use_mprot() const
Definition: stack_alloc.h:59
size_t stacksize() const
Definition: stack_alloc.h:54
A management type to handle dividing mmap-ed memory for use as ucontext stack(s). ...
Definition: stack_alloc.h:30
std::vector< chunk * > chunk_vec_t
This is where we store the memory regions.
Definition: stack_alloc.h:34
std::vector< void * > available_vec_t
This is our list of un-allocated chunks:
Definition: stack_alloc.h:46
A chunk of allocated memory to be divided into fixed-size stacks.
SUMI = Simulator unified messagine interface It is also the name for a solid ink in Japanese - i...
available_vec_t available_
Definition: stack_alloc.h:47
bool initialized() const
Definition: stack_alloc.h:80
void * alloc()
Get a stack memory region.
void free(void *)
Return the given memory region.
size_t suggested_chunk_
Each chunk is of this suggested size.
Definition: stack_alloc.h:39
size_t stacksize_
Each stack request is of this size:
Definition: stack_alloc.h:41