aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/beam/fix_alloc.c
blob: 5637281597b90bfb00e8e7a66b50809e1e8df2d1 (plain) (blame)
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 1996-2009. All Rights Reserved.
 * 
 * The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved online at http://www.erlang.org/.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * %CopyrightEnd%
 */
/* General purpose Memory allocator for fixed block size objects         */
/* This allocater is at least an order of magnitude faster than malloc() */


#define NOPERBLOCK 20
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include "sys.h"
#include "erl_vm.h"
#include "global.h"
#include "erl_db.h"

#ifdef ERTS_ALC_N_MIN_A_FIXED_SIZE

#if ERTS_ALC_MTA_FIXED_SIZE
#include "erl_threads.h"
#include "erl_smp.h"
#  ifdef ERTS_SMP
#    define FA_LOCK(FA) erts_smp_spin_lock(&(FA)->slck)
#    define FA_UNLOCK(FA) erts_smp_spin_unlock(&(FA)->slck)
#  else
#    define FA_LOCK(FA) erts_mtx_lock(&(FA)->mtx)
#    define FA_UNLOCK(FA) erts_mtx_unlock(&(FA)->mtx)
#  endif
#else
#  define FA_LOCK(FA)
#  define FA_UNLOCK(FA)
#endif

typedef union {double d; long l;} align_t;

typedef struct fix_alloc_block {
    struct fix_alloc_block *next;
    align_t mem[1];
} FixAllocBlock;

typedef struct fix_alloc {
    Uint item_size;
    void *freelist;
    Uint no_free;
    Uint no_blocks;
    FixAllocBlock *blocks;
#if ERTS_ALC_MTA_FIXED_SIZE
#  ifdef ERTS_SMP
    erts_smp_spinlock_t slck;
#  else
    erts_mtx_t mtx;
#  endif
#endif
} FixAlloc;

static void *(*core_alloc)(Uint); 
static Uint xblk_sz;

static FixAlloc **fa;
#define FA_SZ (1 + ERTS_ALC_N_MAX_A_FIXED_SIZE - ERTS_ALC_N_MIN_A_FIXED_SIZE)

#define FIX_IX(N) ((N) - ERTS_ALC_N_MIN_A_FIXED_SIZE)

#define FIX_POOL_SZ(I_SZ) \
  ((I_SZ)*NOPERBLOCK + sizeof(FixAllocBlock) - sizeof(align_t))

#if defined(DEBUG) && !ERTS_ALC_MTA_FIXED_SIZE
static int first_time;
#endif

void erts_init_fix_alloc(Uint extra_block_size,
			 void *(*alloc)(Uint))
{
    int i;

    xblk_sz = extra_block_size;
    core_alloc = alloc;

    fa = (FixAlloc **) (*core_alloc)(FA_SZ * sizeof(FixAlloc *));
    if (!fa)
	erts_alloc_enomem(ERTS_ALC_T_UNDEF, FA_SZ * sizeof(FixAlloc *));

    for (i = 0; i < FA_SZ; i++)
	fa[i] = NULL;
#if defined(DEBUG) && !ERTS_ALC_MTA_FIXED_SIZE
    first_time = 1;
#endif
}

Uint
erts_get_fix_size(ErtsAlcType_t type)
{
    Uint i = FIX_IX(ERTS_ALC_T2N(type));
    return i < FA_SZ && fa[i] ? fa[i]->item_size : 0;
}

void
erts_set_fix_size(ErtsAlcType_t type, Uint size)
{
    Uint sz;
    Uint i;
    FixAlloc *fs;
    ErtsAlcType_t t_no = ERTS_ALC_T2N(type);
    sz = xblk_sz + size;

#ifdef DEBUG
    ASSERT(ERTS_ALC_N_MIN_A_FIXED_SIZE <= t_no);
    ASSERT(t_no <= ERTS_ALC_N_MAX_A_FIXED_SIZE);
#endif

    while (sz % sizeof(align_t) != 0)     /* Alignment */
	sz++;

    i = FIX_IX(t_no);
    fs = (FixAlloc *) (*core_alloc)(sizeof(FixAlloc));
    if (!fs)
	erts_alloc_n_enomem(t_no, sizeof(FixAlloc));
    
    fs->item_size = sz;
    fs->no_blocks = 0;
    fs->no_free = 0;
    fs->blocks = NULL;
    fs->freelist = NULL;
    if (fa[i])
	erl_exit(-1, "Attempt to overwrite existing fix size (%d)", i);
    fa[i] = fs;

#if ERTS_ALC_MTA_FIXED_SIZE
#ifdef ERTS_SMP
    erts_smp_spinlock_init_x(&fs->slck, "fix_alloc", make_small(i));
#else
    erts_mtx_init_x(&fs->mtx, "fix_alloc", make_small(i));
#endif
#endif

}

void
erts_fix_info(ErtsAlcType_t type, ErtsFixInfo *efip)
{
    Uint i;
    FixAlloc *f;
#ifdef DEBUG
    FixAllocBlock *b;
    void *fp;
#endif
    Uint real_item_size;
    ErtsAlcType_t t_no = ERTS_ALC_T2N(type);

    ASSERT(ERTS_ALC_N_MIN_A_FIXED_SIZE <= t_no);
    ASSERT(t_no <= ERTS_ALC_N_MAX_A_FIXED_SIZE);

    i = FIX_IX(t_no);
    f = fa[i];

    efip->total	= sizeof(FixAlloc *);
    efip->used	= 0;
    if (!f)
	return;

    real_item_size = f->item_size - xblk_sz;

    FA_LOCK(f);

    efip->total += sizeof(FixAlloc);
    efip->total += f->no_blocks*FIX_POOL_SZ(real_item_size);
    efip->used = efip->total - f->no_free*real_item_size;

#ifdef DEBUG
    ASSERT(efip->total >= efip->used);
    for(i = 0, b = f->blocks; b; i++, b = b->next);
    ASSERT(f->no_blocks == i);
    for (i = 0, fp = f->freelist; fp; i++, fp = *((void **) fp));
    ASSERT(f->no_free == i);
#endif

    FA_UNLOCK(f);

}

void
erts_fix_free(ErtsAlcType_t t_no, void *extra, void* ptr)
{
    Uint i;
    FixAlloc *f;

    ASSERT(ERTS_ALC_N_MIN_A_FIXED_SIZE <= t_no);
    ASSERT(t_no <= ERTS_ALC_N_MAX_A_FIXED_SIZE);

    i = FIX_IX(t_no);
    f = fa[i];

    FA_LOCK(f);
    *((void **) ptr) = f->freelist;
    f->freelist = ptr;
    f->no_free++;
    FA_UNLOCK(f);
}


void *erts_fix_realloc(ErtsAlcType_t t_no, void *extra, void* ptr, Uint size)
{
    erts_alc_fatal_error(ERTS_ALC_E_NOTSUP, ERTS_ALC_O_REALLOC, t_no);
    return NULL;
}

void *erts_fix_alloc(ErtsAlcType_t t_no, void *extra, Uint size)
{
    void *ret;
    int i;
    FixAlloc *f;

#if defined(DEBUG) && !ERTS_ALC_MTA_FIXED_SIZE
    ASSERT(ERTS_ALC_N_MIN_A_FIXED_SIZE <= t_no);
    ASSERT(t_no <= ERTS_ALC_N_MAX_A_FIXED_SIZE);
    if (first_time) { /* Check that all sizes have been initialized */
	int i;
	for (i = 0; i < FA_SZ; i++)
	    ASSERT(fa[i]);
	first_time = 0;
    }
#endif


    i = FIX_IX(t_no);
    f = fa[i];

    ASSERT(f);
    ASSERT(f->item_size >= size);

    FA_LOCK(f);
    if (f->freelist == NULL) {  /* Gotta alloc some more mem */
	char *ptr;
	FixAllocBlock *bl;
	Uint n;


	FA_UNLOCK(f);
	bl = (*core_alloc)(FIX_POOL_SZ(f->item_size));
	if (!bl)
	    return NULL;

	FA_LOCK(f);
	bl->next = f->blocks;  /* link in first */
	f->blocks = bl;

	n = NOPERBLOCK;
	ptr = (char *) &f->blocks->mem[0];
	while(n--) {
	    *((void **) ptr) = f->freelist;
	    f->freelist = (void *) ptr;
	    ptr += f->item_size;
	}
#if !ERTS_ALC_MTA_FIXED_SIZE 
	ASSERT(f->no_free == 0);
#endif
	f->no_free += NOPERBLOCK;
	f->no_blocks++;
    }

    ret = f->freelist;
    f->freelist = *((void **) f->freelist);
    ASSERT(f->no_free > 0);
    f->no_free--;

    FA_UNLOCK(f);

    return ret;
}

#endif /* #ifdef ERTS_ALC_N_MIN_A_FIXED_SIZE */