Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 724059bac88b24efd161a0acd40377ecae774cce (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
/*******************************************************************************
 * Copyright (c) 2012 protos software gmbh (http://www.protos.de).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * CONTRIBUTORS:
 * 		Henrik Rentz-Reichert (initial contribution)
 *
 *******************************************************************************/

#include "base/etMemory_FreeList.h"
#include "debugging/etLogger.h"
#include "debugging/etMSCLogger.h"

#define UNUSED_LIST			0
#define DEBUG_FREE_LISTS	1
#define OBJ_OFFSET			etALIGNMENT

#define DO_LOCK	\
	if (self->base.lock!=NULL) {								\
		self->base.lock->lockFct(self->base.lock->lockData);	\
	}

#define DO_UNLOCK	\
	if (self->base.lock!=NULL) {								\
		self->base.lock->unlockFct(self->base.lock->lockData);	\
	}

typedef struct etFreeListObj {
	struct etFreeListObj* next;
} etFreeListObj;

typedef struct etFreeListInfo {
	etUInt16 objsize;		/**< the size in bytes of the objects in this list */
	etFreeListObj* head;	/**< the list head */

#if DEBUG_FREE_LISTS
	etUInt16 nobjects;		/**< the number of objects in the list */
#endif

} etFreeListInfo;

typedef struct etFreeListMemory {
	etMemory base;					/** the "base class" */
	etUInt8* current;				/**< next free position on the heap */
	etUInt16 nslots;				/**< number of free lists */
	roundUpSize* roundUp;			/**< rounding method (identity by default) */
	etFreeListInfo freelists[1];	/**< array of free list infos (array used with size nslots) */
} etFreeListMemory;

/*
 * private functions
 */
static void* etMemory_FreeList_getHeapMem(etFreeListMemory* self, etUInt16 size) {
	etUInt8* obj = NULL;
	ET_MSC_LOGGER_SYNC_ENTRY("etMemory_FreeList", "getHeapListMem")

	if (self->current + size < ((etUInt8*)self) + self->base.size)
	{
		etUInt32 used;

		// store size in the first bytes
		*((etUInt16*)self->current) = size;

		// object pointer at offset
		obj = self->current + OBJ_OFFSET;

		// shift current
		self->current += size;

		used = ((etUInt8*)self) + self->base.size - self->current;
		if (used > self->base.statistics.maxUsed) {
			self->base.statistics.maxUsed = used;
		}
	}

	if (obj==NULL) {
		self->base.statistics.nFailingRequests++;
	}

	ET_MSC_LOGGER_SYNC_EXIT
	return obj;
}

static void* etMemory_FreeList_getFreeListMem(etFreeListMemory* self, etUInt16 size) {
	etUInt8* mem = NULL;
	int asize, slot_offset, slot, slot_size;
	ET_MSC_LOGGER_SYNC_ENTRY("etMemory_FreeList", "getFreeListMem")

	asize = (size / etALIGNMENT);
	for (slot_offset = 0; slot_offset < self->nslots; slot_offset++) {
		slot = (asize + slot_offset) % self->nslots;
		slot_size = self->freelists[slot].objsize;
		if (slot_size == size) {
			if (self->freelists[slot].head != NULL) {
				etFreeListObj* obj = self->freelists[slot].head;
				self->freelists[slot].head = obj->next;
				mem = (void *) obj;
#if DEBUG_FREE_LISTS
				--self->freelists[slot].nobjects;
#endif
			}
			break;
		}
		else if (slot_size == UNUSED_LIST)
			break;
	}
	ET_MSC_LOGGER_SYNC_EXIT
	return mem;
}

static void etMemory_FreeList_putFreeListMem(etFreeListMemory* self, void* obj) {
	ET_MSC_LOGGER_SYNC_ENTRY("etMemory_FreeList", "putFreeListMem")
	{
		int asize, slot_offset, slot, slot_size;
		etUInt16 size = *((etUInt16*) (((etUInt8*) obj) - OBJ_OFFSET));

		asize = (size / etALIGNMENT);
		for (slot_offset = 0; slot_offset < self->nslots; slot_offset++) {
			slot = (asize + slot_offset) % self->nslots;
			slot_size = self->freelists[slot].objsize;
			if (slot_size == size) {
				/* we insert the object as new head */
				((etFreeListObj*)obj)->next = self->freelists[slot].head;
				self->freelists[slot].head = (etFreeListObj*)obj;
#if DEBUG_FREE_LISTS
				++(self->freelists[slot].nobjects);
#endif
				break;
			}
			else if (slot_size == UNUSED_LIST) {
				/* initialize unused list and insert the object as new head */
				self->freelists[slot].objsize = size;
				((etFreeListObj*)obj)->next = NULL;
				self->freelists[slot].head = (etFreeListObj*)obj;
#if DEBUG_FREE_LISTS
				self->freelists[slot].nobjects = 1;
#endif
				break;
			}
		}
	}
	ET_MSC_LOGGER_SYNC_EXIT
}

static void* etMemory_FreeList_alloc(etMemory* heap, etUInt16 size) {
	etFreeListMemory* self = (etFreeListMemory*) heap;
	void* mem;

	ET_MSC_LOGGER_SYNC_ENTRY("etMemory_FreeList", "alloc")

	// rounded required size + space to store the size
	size = MEM_CEIL(self->roundUp(size) + OBJ_OFFSET);

	DO_LOCK
	mem = etMemory_FreeList_getFreeListMem((etFreeListMemory*) heap, size);
	if (mem==NULL) {
		mem = etMemory_FreeList_getHeapMem((etFreeListMemory*) heap, size);
	}
	DO_UNLOCK

	ET_MSC_LOGGER_SYNC_EXIT
	return mem;
}

static void etMemory_FreeList_free(etMemory* heap, void* obj) {
	etFreeListMemory* self = (etFreeListMemory*) heap;
	ET_MSC_LOGGER_SYNC_ENTRY("etMemory_FreeList", "free")

	DO_LOCK
	etMemory_FreeList_putFreeListMem(self, obj);
	DO_UNLOCK

	ET_MSC_LOGGER_SYNC_EXIT
}

static etUInt16 etMemory_FreeList_identity(etUInt16 size) {
	return size;
}

etUInt16 etMemory_FreeList_power2(etUInt16 v) {
	/* https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 */
	v--;
	v |= v >> 1;
	v |= v >> 2;
	v |= v >> 4;
	v |= v >> 8;
	v++;
	return v;
}


/*
 * the public interface
 */
etMemory* etMemory_FreeList_init(void* heap, etUInt32 size, etUInt16 nslots) {
	etFreeListMemory* self = (etFreeListMemory*) heap;
	ET_MSC_LOGGER_SYNC_ENTRY("etMemory_FreeList_init", "init")
	etUInt32 data_size = MEM_CEIL(sizeof(etFreeListMemory)+(nslots-1)*sizeof(etFreeListInfo));
	etUInt32 actual_size = size - data_size;
	etMemory* result = NULL;

	if (heap!=NULL & size > data_size) {
		result = &self->base;

		etMemory_init(result, actual_size, etMemory_FreeList_alloc, etMemory_FreeList_free);

		self->roundUp = etMemory_FreeList_identity;
		self->nslots = nslots;
		self->current = ((etUInt8*) self) + data_size;

		/* initialize the free lists */
		{
			int i;
			for (i=0; i<self->nslots; ++i)
				self->freelists[i].objsize = UNUSED_LIST;
		}
	}

	ET_MSC_LOGGER_SYNC_EXIT
	return result;
}

void etMemory_FreeList_setUserRounding(etMemory* mem, roundUpSize* roundup) {
	etFreeListMemory* self = (etFreeListMemory*) mem;
	ET_MSC_LOGGER_SYNC_ENTRY("etMemory_FreeList_init", "setUserRounding")
	self->roundUp = roundup;
	ET_MSC_LOGGER_SYNC_EXIT
}

etUInt32 etMemory_FreeList_getFreeHeapMem(etMemory* mem) {
	etFreeListMemory* self = (etFreeListMemory*) mem;

	DO_LOCK
	etUInt32 result = ((etUInt8*) self) + self->base.size - self->current;
	DO_UNLOCK

	return result;
}

etUInt16 etMemory_FreeList_freeSlots(etMemory* mem) {
	etFreeListMemory* self = (etFreeListMemory*) mem;
	etUInt16 free = 0;
	int slot;

	DO_LOCK
	for (slot=0; slot<self->nslots; ++slot)
		if (self->freelists[slot].objsize==UNUSED_LIST)
			++free;
	DO_UNLOCK

	return free;
}

etUInt16 etMemory_FreeList_nObjects(etMemory* mem, etUInt16 slot) {
	etUInt16 result = 0;
#if DEBUG_FREE_LISTS
	etFreeListMemory* self = (etFreeListMemory*) mem;
	DO_LOCK
	if (slot<self->nslots) {
		result = self->freelists[slot].nobjects;
	}
	DO_UNLOCK
#endif
	return result;
}

etUInt16 etMemory_FreeList_sizeObjects(etMemory* mem, etUInt16 slot) {
	etFreeListMemory* self = (etFreeListMemory*) mem;
	etUInt16 result = 0;
	DO_LOCK
	if (slot<self->nslots) {
		result = self->freelists[slot].objsize - OBJ_OFFSET;
	}
	DO_UNLOCK
	return result;
}

etUInt16 etMemory_FreeList_MgmtDataPerObject() {
	return OBJ_OFFSET;
}

Back to the top