Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6d937b5e1290ce7c9b6427e4599ab7e5ce066ba3 (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
/*******************************************************************************
 * 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)
 *
 *******************************************************************************/

#ifndef _ETMEMORY_H_
#define _ETMEMORY_H_

#include "etDatatypes.h"
#include "osal/etLock.h"

/**
 * this macro computes the memory aligned value for a given size. It uses the ALIGNMENT
 * defined in etDatatypes.h
 */
#define MEM_CEIL(n)          ((n)+((etALIGNMENT-((n)&(etALIGNMENT-1)))&(etALIGNMENT-1)))

struct etMemory;

/**
 * allocates memory from the heap
 *
 * \param heap pointer to the memory management
 * \param size the size of the requested memory in bytes
 */
typedef void* etMemory_alloc(struct etMemory* mem, etUInt16 size);

/**
 * frees memory previously allocated from the heap
 *
 * \param heap pointer to the memory management
 * \param obj pointer to the memory returned
 * \param size the size in bytes of the memory returned
 */
typedef void etMemory_free(struct etMemory* mem, void* obj);

typedef struct etMemoryStatistics {
	etUInt32 maxUsed;
	etUInt32 nFailingRequests;
}
etMemoryStatistics;

typedef struct etMemory {
	/** name */
	const char* name;

	/** linked list */
	struct etMemory* next;

	/** size of the heap in bytes */
	etUInt32 size;

	/** statistical data made available through the runtime */
	etMemoryStatistics statistics;

	/** the configured allocation method */
	etMemory_alloc* alloc;
	/** the configured freeing method */
	etMemory_free* free;

	/** user supplied lock functions */
	etLock* lock;
} etMemory;


/**
 * initializes the struct
 *
 * \param mem pointer to the heap to be managed
 * \param size the size in bytes of the heap
 * \param alloc the allocation function
 * \param free the free function
 *
 * \return the pointer to the initialized etMemory struct
 */
void etMemory_init(etMemory* mem, etUInt32 size, etMemory_alloc* alloc, etMemory_free* free);

/**
 * resets the statistical data to their initial values
 *
 * \param heap pointer to the memory management
 */
void etMemory_resetStatistics(etMemory* mem);

/**
 * supply optional user lock/unlock functions for usage in a multi-threaded environment.
 * \param mem pointer to the memory management struct
 * \lock pointer to a user supplied locking struct
 */
void etMemory_setUserLock(etMemory* mem, etLock* lock);

/**
 * destroys the memory management and unregisters it from the runtime
 * \param mem pointer to the memory management struct
 */
void etMemory_destroy(etMemory* mem);

#endif /* _ETMEMORY_H_ */

Back to the top