Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: bd3160c31d97ba51c63f1190d577d5ceaa7e1f1f (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
/*******************************************************************************
 * Copyright (c) 2016 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 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * CONTRIBUTORS:
 * 		Jan Belle (initial contribution)
 *
 *******************************************************************************/

#include "common/messaging/StaticMessageMemory.h"
#include <new>

namespace etRuntime {

StaticMessageMemory::StaticMessageMemory(IRTObject* parent, const String& name, size_t size, int number) :
		RTObject(parent, name), m_size(size), m_msgPool(NULL, "memory"), m_buffer(0) {
	m_buffer = new uint8_t[m_size * number];
	uint8_t* ptr = m_buffer;
	for(int i = 0; i < number; ++i) {
		Message* msg = new (ptr) Message(Address::EMPTY, 0);
		m_msgPool.push(msg);
		ptr += m_size;
	}
}

StaticMessageMemory::~StaticMessageMemory() {
	delete[] m_buffer;
}

Message* StaticMessageMemory::getMessageBuffer(size_t size) {
	if(size > m_size) {
		printf("Could not provide message buffer (message too big)!\n");
	}
	else if(m_msgPool.getSize() <= 0) {
		printf("Could not provide message buffer (message pool is empty)!\n");
	}
	else {
		return const_cast<Message*>(m_msgPool.pop());
	}
	return NULL;
}

void StaticMessageMemory::returnMessageBuffer(const Message* buffer) {
	m_msgPool.push(const_cast<Message*>(buffer));
}

} /* namespace etRuntime */

Back to the top