Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenrik Rentz-Reichert2012-11-05 09:29:27 -0500
committerHenrik Rentz-Reichert2012-11-05 09:30:18 -0500
commit89b829cb81af40cc0f819c8e1390a0b054877778 (patch)
tree6a2299a4ef40f86d61454e72bf8e72cce79e3ff2
parenta00264aeb6cc771776702626e8da689418cf2949 (diff)
downloadorg.eclipse.etrice-89b829cb81af40cc0f819c8e1390a0b054877778.tar.gz
org.eclipse.etrice-89b829cb81af40cc0f819c8e1390a0b054877778.tar.xz
org.eclipse.etrice-89b829cb81af40cc0f819c8e1390a0b054877778.zip
[runtime.c] memory management access through function pointers
-rw-r--r--runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory.h20
-rw-r--r--runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FixedSize.c67
-rw-r--r--runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FixedSize.h30
-rw-r--r--runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FreeList.c69
-rw-r--r--runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FreeList.h29
5 files changed, 140 insertions, 75 deletions
diff --git a/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory.h b/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory.h
index 17581b74e..a7c96fc8f 100644
--- a/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory.h
+++ b/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory.h
@@ -17,13 +17,7 @@
#define CEIL_ALIGN(n) ((n)+((ALIGNMENT-((n)&(ALIGNMENT-1)))&(ALIGNMENT-1)))
-/**
- * initializes the heap
- *
- * \param heap pointer to the heap memory
- * \param size the size of the heap in bytes
- */
-void etMemory_init(void* heap, etUInt16 size);
+struct etMemory;
/**
* allocates memory from the heap
@@ -31,7 +25,7 @@ void etMemory_init(void* heap, etUInt16 size);
* \param heap pointer to the heap memory
* \param size the size of the requested memory in bytes
*/
-void* etMemory_alloc(void* heap, etUInt16 size);
+typedef void* etMemory_alloc(struct etMemory* heap, etUInt16 size);
/**
* frees memory previously allocated from the heap
@@ -40,6 +34,14 @@ void* etMemory_alloc(void* heap, etUInt16 size);
* \param obj pointer to the memory returned
* \param size the size in bytes of the memory returned
*/
-void etMemory_free(void* heap, void* obj, etUInt16 size);
+typedef void etMemory_free(struct etMemory* heap, void* obj, etUInt16 size);
+
+typedef struct etMemory {
+ /** size of the heap in bytes */
+ etUInt16 size;
+
+ etMemory_alloc* alloc;
+ etMemory_free* free;
+} etMemory;
#endif /* _ETMEMORY_H_ */
diff --git a/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FixedSize.c b/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FixedSize.c
index e3584819e..b0dfe4a6d 100644
--- a/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FixedSize.c
+++ b/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FixedSize.c
@@ -10,48 +10,23 @@
*
*******************************************************************************/
-#include "base/etMemory.h"
+#include "base/etMemory_FixedSize.h"
#include "base/etQueue.h"
#include "debugging/etLogger.h"
#include "debugging/etMSCLogger.h"
-#define BLOCK_SIZE 128
-
typedef struct etFixedSizeMemory {
+ etMemory base;
+
etUInt8 *buffer;
etUInt16 maxBlocks;
etUInt16 blockSize;
etQueue blockPool;
} etFixedSizeMemory;
-/*
- * the public interface
- */
-void etMemory_init(void* heap, etUInt16 size) {
- etFixedSizeMemory* self = (etFixedSizeMemory*) heap;
- size_t data_size = CEIL_ALIGN(sizeof(etFixedSizeMemory));
- int i;
-
- ET_MSC_LOGGER_SYNC_ENTRY("etMemory", "init")
-
- if (size > data_size) {
- self->buffer = ((etUInt8*) self) + data_size;
- self->blockSize = BLOCK_SIZE;
- self->maxBlocks = (size - data_size) / self->blockSize;
- for (i=0; i<self->maxBlocks; i++){
- void* block = &(self->buffer[i*self->blockSize]);
- etQueue_push(&self->blockPool, block);
- }
- }
- else {
- self->blockSize = 0;
- }
-
- ET_MSC_LOGGER_SYNC_EXIT
-}
-void* etMemory_alloc(void* heap, etUInt16 size) {
+void* etMemory_FixedSize_alloc(etMemory* heap, etUInt16 size) {
etFixedSizeMemory* self = (etFixedSizeMemory*) heap;
void* mem = NULL;
size = CEIL_ALIGN(size);
@@ -68,7 +43,7 @@ void* etMemory_alloc(void* heap, etUInt16 size) {
return mem;
}
-void etMemory_free(void* heap, void* obj, etUInt16 size) {
+void etMemory_FixedSize_free(etMemory* heap, void* obj, etUInt16 size) {
etFixedSizeMemory* self = (etFixedSizeMemory*) heap;
ET_MSC_LOGGER_SYNC_ENTRY("etMemory", "free")
@@ -77,3 +52,35 @@ void etMemory_free(void* heap, void* obj, etUInt16 size) {
ET_MSC_LOGGER_SYNC_EXIT
}
+
+/*
+ * the public interface
+ */
+etMemory* etMemory_FixedSize_init(void* heap, etUInt16 size, etUInt16 blockSize) {
+ etFixedSizeMemory* self = (etFixedSizeMemory*) heap;
+ size_t data_size = CEIL_ALIGN(sizeof(etFixedSizeMemory));
+ int i;
+
+ ET_MSC_LOGGER_SYNC_ENTRY("etMemory", "init")
+
+ self->base.size = size;
+ self->base.alloc = etMemory_FixedSize_alloc;
+ self->base.free = etMemory_FixedSize_free;
+
+ if (size > data_size) {
+ self->buffer = ((etUInt8*) self) + data_size;
+ self->blockSize = blockSize;
+ self->maxBlocks = (size - data_size) / self->blockSize;
+ for (i=0; i<self->maxBlocks; i++){
+ void* block = &(self->buffer[i*self->blockSize]);
+ etQueue_push(&self->blockPool, block);
+ }
+ }
+ else {
+ self->blockSize = 0;
+ }
+
+ ET_MSC_LOGGER_SYNC_EXIT
+
+ return &self->base;
+}
diff --git a/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FixedSize.h b/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FixedSize.h
new file mode 100644
index 000000000..c804f254d
--- /dev/null
+++ b/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FixedSize.h
@@ -0,0 +1,30 @@
+/*******************************************************************************
+ * 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_FIXED_SIZE_H_
+#define _ETMEMORY_FIXED_SIZE_H_
+
+#include "base/etMemory.h"
+#include "base/etQueue.h"
+
+/**
+ * initializes the heap with a simple block management
+ *
+ * \param heap pointer to the heap to be managed
+ * \param size the size in bytes of the heap
+ * \param blockSize the size of the (equal sized) blocks
+ *
+ * \return the pointer to the initialized etMemory struct
+ */
+etMemory* etMemory_FixedSize_init(void* heap, etUInt16 size, etUInt16 blockSize);
+
+#endif /* _ETMEMORY_FIXED_SIZE_H_ */
diff --git a/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FreeList.c b/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FreeList.c
index 452377a39..d474292b6 100644
--- a/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FreeList.c
+++ b/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FreeList.c
@@ -10,16 +10,11 @@
*
*******************************************************************************/
-#include "base/etMemory.h"
+#include "base/etMemory_FreeList.h"
#include "debugging/etLogger.h"
#include "debugging/etMSCLogger.h"
#define UNUSED_LIST 0
-#define NSLOTS 128
-
-/*
- * private data structures
- */
typedef struct etFreeListObj {
struct etFreeListObj* next;
@@ -34,8 +29,7 @@ typedef struct etFreeListInfo {
} etFreeListInfo;
typedef struct etFreeListMemory {
- /** size of the heap in bytes */
- etUInt16 size;
+ etMemory base;
/** next free position on the heap */
etUInt8* current;
@@ -54,7 +48,7 @@ static void* etMemory_getHeapMem(etFreeListMemory* self, etUInt16 size) {
etUInt8* obj = NULL;
ET_MSC_LOGGER_SYNC_ENTRY("etMemory", "getHeapListMem")
- if (self->current < ((etUInt8*)self)+self->size)
+ if (self->current < ((etUInt8*)self)+self->base.size)
{
obj = self->current;
self->current += size;
@@ -88,10 +82,9 @@ static void* etMemory_getFreeListMem(etFreeListMemory* self, etUInt16 size) {
return mem;
}
-static void etMemory_putFreeListMem(void* heap, void* obj, etUInt16 size) {
+static void etMemory_putFreeListMem(etFreeListMemory* self, void* obj, etUInt16 size) {
ET_MSC_LOGGER_SYNC_ENTRY("etMemory", "putFreeListMem")
{
- etFreeListMemory* self = (etFreeListMemory*) heap;
int asize, slot_offset, slot, slot_size;
asize = (size / ALIGNMENT);
@@ -116,30 +109,7 @@ static void etMemory_putFreeListMem(void* heap, void* obj, etUInt16 size) {
ET_MSC_LOGGER_SYNC_EXIT
}
-/*
- * the public interface
- */
-void etMemory_init(void* heap, etUInt16 size) {
- etFreeListMemory* self = (etFreeListMemory*) heap;
- ET_MSC_LOGGER_SYNC_ENTRY("etMemory", "init")
-
- self->size = size;
- self->nslots = NSLOTS;
- {
- int used = sizeof(etFreeListMemory)+(self->nslots-1)*sizeof(etFreeListObj);
- self->current = ((etUInt8*)self)+CEIL_ALIGN(used);
- }
-
- /* initialize the free lists */
- {
- int i;
- for (i=0; i<self->nslots; ++i)
- self->freelists[i].objsize = UNUSED_LIST;
- }
- ET_MSC_LOGGER_SYNC_EXIT
-}
-
-void* etMemory_alloc(void* heap, etUInt16 size) {
+void* etMemory_FreeList_alloc(etMemory* heap, etUInt16 size) {
void* mem;
size = CEIL_ALIGN(size);
ET_MSC_LOGGER_SYNC_ENTRY("etMemory", "alloc")
@@ -152,7 +122,7 @@ void* etMemory_alloc(void* heap, etUInt16 size) {
return mem;
}
-void etMemory_free(void* heap, void* obj, etUInt16 size) {
+void etMemory_FreeList_free(etMemory* heap, void* obj, etUInt16 size) {
ET_MSC_LOGGER_SYNC_ENTRY("etMemory", "free")
{
size = CEIL_ALIGN(size);
@@ -160,3 +130,30 @@ void etMemory_free(void* heap, void* obj, etUInt16 size) {
}
ET_MSC_LOGGER_SYNC_EXIT
}
+
+/*
+ * the public interface
+ */
+etMemory* etMemory_FreeList_init(void* heap, etUInt16 size, etUInt16 nslots) {
+ etFreeListMemory* self = (etFreeListMemory*) heap;
+ ET_MSC_LOGGER_SYNC_ENTRY("etMemory_FreeList_init", "init")
+
+ self->base.size = size;
+ self->base.alloc = etMemory_FreeList_alloc;
+ self->base.free = etMemory_FreeList_free;
+ self->nslots = nslots;
+ {
+ int used = sizeof(etFreeListMemory)+(self->nslots-1)*sizeof(etFreeListObj);
+ self->current = ((etUInt8*)self)+CEIL_ALIGN(used);
+ }
+
+ /* 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 &self->base;
+}
diff --git a/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FreeList.h b/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FreeList.h
new file mode 100644
index 000000000..e21d1d750
--- /dev/null
+++ b/runtime/org.eclipse.etrice.runtime.c/src/common/base/etMemory_FreeList.h
@@ -0,0 +1,29 @@
+/*******************************************************************************
+ * 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_FREE_LIST_H_
+#define _ETMEMORY_FREE_LIST_H_
+
+#include "base/etMemory.h"
+
+/**
+ * initializes the free list memory management on the given heap
+ *
+ * \param heap pointer to the heap to be managed
+ * \param size the size in bytes of the heap
+ * \nslots the number of slots to be used (should be greater or equal the number of different object sizes)
+ *
+ * \return the pointer to the initialized etMemory struct
+ */
+etMemory* etMemory_FreeList_init(void* heap, etUInt16 size, etUInt16 nslots);
+
+#endif /* _ETMEMORY_FREE_LIST_H_ */

Back to the top