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
|
/*
* %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%
*/
/*
* Function: General purpose Memory allocator for fixed block
* size objects. This allocater is at least an order of
* magnitude faster than malloc().
*/
#include "eidef.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ei_locking.h"
#include "erl_interface.h"
#include "erl_error.h"
#include "erl_malloc.h"
#include "erl_fix_alloc.h"
#include "erl_eterm.h"
#define WIPE_CHAR ((char)0xaa) /* 10101010 */
/* the freelist is a singly linked list of these */
/* i.e. the user structure and a link pointer */
struct fix_block {
ETERM term;
struct fix_block *next;
int free;
};
/* this is a struct just to keep namespace pollution low on VxWorks */
struct eterm_stateinfo {
struct fix_block *freelist;
unsigned long freed;
unsigned long allocated;
#ifdef _REENTRANT
ei_mutex_t *lock;
#endif /* _REENTRANT */
};
/* FIXME problem for threaded ? */
static struct eterm_stateinfo *erl_eterm_state=NULL;
int erl_init_eterm_alloc (void)
{
#if defined(PURIFY) && defined (DEBUG)
fprintf(stderr,"erl_fix_alloc() compiled for Purify - using \"real\" malloc()");
#endif
erl_eterm_state = malloc(sizeof(*erl_eterm_state));
if (erl_eterm_state == NULL) goto err1;
erl_eterm_state->freelist = NULL;
erl_eterm_state->freed = 0;
erl_eterm_state->allocated = 0;
#ifdef _REENTRANT
erl_eterm_state->lock = ei_mutex_create();
if (erl_eterm_state->lock == NULL) goto err2;
#endif /* _REENTRANT */
return 1;
/* Error cleanup */
#ifdef _REENTRANT
err2:
/* FIXME ENOMEM is not what went wrong... */
free(erl_eterm_state);
#endif /* _REENTRANT */
err1:
erl_errno = ENOMEM;
return 0;
}
/* get an eterm, from the freelist if possible or from malloc() */
void *erl_eterm_alloc (void)
{
#ifdef PURIFY
ETERM *p;
if ((p = malloc(sizeof(*p)))) {
memset(p, WIPE_CHAR, sizeof(*p));
}
return p;
#else
struct fix_block *b;
#ifdef _REENTRANT
ei_mutex_lock(erl_eterm_state->lock, 0);
#endif /* _REENTRANT */
/* try to pop block from head of freelist */
if ((b = erl_eterm_state->freelist) != NULL) {
erl_eterm_state->freelist = b->next;
erl_eterm_state->freed--;
} else if ((b = malloc(sizeof(*b))) == NULL) {
erl_errno = ENOMEM;
#ifdef _REENTRANT
ei_mutex_unlock(erl_eterm_state->lock);
#endif /* _REENTRANT */
return NULL;
}
erl_eterm_state->allocated++;
b->free = 0;
b->next = NULL;
#ifdef _REENTRANT
ei_mutex_unlock(erl_eterm_state->lock);
#endif /* _REENTRANT */
return (void *) &b->term;
#endif /* !PURIFY */
}
/* free an eterm back to the freelist */
void erl_eterm_free(void *p)
{
#ifdef PURIFY
if (p) {
memset(p, WIPE_CHAR, sizeof(ETERM));
}
free(p);
#else
struct fix_block *b = p;
if (b) {
if (b->free) {
#ifdef DEBUG
fprintf(stderr,"erl_eterm_free: attempt to free already freed block %p\n",b);
#endif
return;
}
#ifdef _REENTRANT
ei_mutex_lock(erl_eterm_state->lock,0);
#endif /* _REENTRANT */
b->free = 1;
b->next = erl_eterm_state->freelist;
erl_eterm_state->freelist = b;
erl_eterm_state->freed++;
erl_eterm_state->allocated--;
#ifdef _REENTRANT
ei_mutex_unlock(erl_eterm_state->lock);
#endif /* _REENTRANT */
}
#endif /* !PURIFY */
}
/* really free the freelist */
void erl_eterm_release (void)
{
#if !defined(PURIFY)
struct fix_block *b;
#ifdef _REENTRANT
ei_mutex_lock(erl_eterm_state->lock,0);
#endif /* _REENTRANT */
{
while (erl_eterm_state->freelist != NULL) {
b = erl_eterm_state->freelist;
erl_eterm_state->freelist = b->next;
free(b);
erl_eterm_state->freed--;
}
}
#ifdef _REENTRANT
ei_mutex_unlock(erl_eterm_state->lock);
#endif /* _REENTRANT */
#endif /* !PURIFY */
}
void erl_eterm_statistics (unsigned long *allocd, unsigned long *freed)
{
if (allocd) *allocd = erl_eterm_state->allocated;
if (freed) *freed = erl_eterm_state->freed;
return;
}
/*
* Local Variables:
* compile-command: "cd ..; ERL_TOP=/clearcase/otp/erts make -k"
* End:
*/
|