summaryrefslogtreecommitdiff
path: root/src/mi/mempool.c
blob: 536b64e815760e618c69f59ebc5b58a8c9e52a7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* libunwind - a platform-independent unwind library
   Copyright (C) 2002-2003, 2005 Hewlett-Packard Co
        Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
   Copyright (C) 2012 Tommi Rantala <tt.rantala@gmail.com>

This file is part of libunwind.

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.  */

#include "libunwind_i.h"

/* From GCC docs: ``Gcc also provides a target specific macro
 * __BIGGEST_ALIGNMENT__, which is the largest alignment ever used for any data
 * type on the target machine you are compiling for.'' */
#ifdef __BIGGEST_ALIGNMENT__
# define MAX_ALIGN      __BIGGEST_ALIGNMENT__
#else
/* Crude hack to check that MAX_ALIGN is power-of-two.
 * sizeof(long double) = 12 on i386. */
# define MAX_ALIGN_(n)  (n < 8 ? 8 : \
                         n < 16 ? 16 : n)
# define MAX_ALIGN      MAX_ALIGN_(sizeof (long double))
#endif

static char sos_memory[SOS_MEMORY_SIZE] ALIGNED(MAX_ALIGN);
static size_t sos_memory_freepos;
static size_t pg_size;

HIDDEN void *
sos_alloc (size_t size)
{
  size_t pos;

  size = UNW_ALIGN(size, MAX_ALIGN);

#if defined(__GNUC__) && defined(HAVE_FETCH_AND_ADD)
  /* Assume `sos_memory' is suitably aligned. */
  assert(((uintptr_t) &sos_memory[0] & (MAX_ALIGN-1)) == 0);

  pos = fetch_and_add (&sos_memory_freepos, size);
#else
  static define_lock (sos_lock);
  intrmask_t saved_mask;

  lock_acquire (&sos_lock, saved_mask);
  {
    /* No assumptions about `sos_memory' alignment. */
    if (sos_memory_freepos == 0)
      {
        unsigned align = UNW_ALIGN((uintptr_t) &sos_memory[0], MAX_ALIGN)
                                - (uintptr_t) &sos_memory[0];
        sos_memory_freepos = align;
      }
    pos = sos_memory_freepos;
    sos_memory_freepos += size;
  }
  lock_release (&sos_lock, saved_mask);
#endif

  assert (((uintptr_t) &sos_memory[pos] & (MAX_ALIGN-1)) == 0);
  assert ((pos+size) <= SOS_MEMORY_SIZE);

  return &sos_memory[pos];
}

/* Must be called while holding the mempool lock. */

static void
free_object (struct mempool *pool, void *object)
{
  struct object *obj = object;

  obj->next = pool->free_list;
  pool->free_list = obj;
  ++pool->num_free;
}

static void
add_memory (struct mempool *pool, char *mem, size_t size, size_t obj_size)
{
  char *obj;

  for (obj = mem; obj <= mem + size - obj_size; obj += obj_size)
    free_object (pool, obj);
}

static void
expand (struct mempool *pool)
{
  size_t size;
  char *mem;

  size = pool->chunk_size;
  GET_MEMORY (mem, size);
  if (!mem)
    {
      size = UNW_ALIGN(pool->obj_size, pg_size);
      GET_MEMORY (mem, size);
      if (!mem)
        {
          /* last chance: try to allocate one object from the SOS memory */
          size = pool->obj_size;
          mem = sos_alloc (size);
        }
    }
  add_memory (pool, mem, size, pool->obj_size);
}

HIDDEN void
mempool_init (struct mempool *pool, size_t obj_size, size_t reserve)
{
  if (pg_size == 0)
    pg_size = getpagesize ();

  memset (pool, 0, sizeof (*pool));

  lock_init (&pool->lock);

  /* round object-size up to integer multiple of MAX_ALIGN */
  obj_size = UNW_ALIGN(obj_size, MAX_ALIGN);

  if (!reserve)
    {
      reserve = pg_size / obj_size / 4;
      if (!reserve)
        reserve = 16;
    }

  pool->obj_size = obj_size;
  pool->reserve = reserve;
  pool->chunk_size = UNW_ALIGN(2*reserve*obj_size, pg_size);

  expand (pool);
}

HIDDEN void *
mempool_alloc (struct mempool *pool)
{
  intrmask_t saved_mask;
  struct object *obj;

  lock_acquire (&pool->lock, saved_mask);
  {
    if (pool->num_free <= pool->reserve)
      expand (pool);

    assert (pool->num_free > 0);

    --pool->num_free;
    obj = pool->free_list;
    pool->free_list = obj->next;
  }
  lock_release (&pool->lock, saved_mask);
  return obj;
}

HIDDEN void
mempool_free (struct mempool *pool, void *object)
{
  intrmask_t saved_mask;

  lock_acquire (&pool->lock, saved_mask);
  {
    free_object (pool, object);
  }
  lock_release (&pool->lock, saved_mask);
}