-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit d19d722
Showing
7 changed files
with
229 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2017 Abdullah Murad | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Makefile \ | ||
.PHONY: clean all | ||
|
||
CC = gcc | ||
CFLAGS = -g -Wall | ||
CFLAGS_OBJS = -g -Wall -c | ||
|
||
INC = -I inc/ | ||
|
||
BUILD_DIR = build | ||
SOURCES = sample/main.c src/MemoryPool.c | ||
OBJECTS = $(BUILD_DIR)/main.o $(BUILD_DIR)/MemoryPool.o | ||
|
||
TARGET = $(BUILD_DIR)/MemoryPool/ | ||
|
||
all: dir $(TARGET) | ||
|
||
dir: | ||
mkdir -p $(BUILD_DIR) | ||
|
||
$(TARGET): $(OBJECTS) | ||
@echo "generate target" | ||
$(CC) $(CFLAGS) $^ -o $@ | ||
|
||
$(BUILD_DIR)/main.o: sample/main.c | ||
@echo "generate Sample object" | ||
$(CC) $(CFLAGS_OBJS) $(INC) $< -o $@ | ||
$(BUILD_DIR)/MemoryPool.o: src/MemoryPool.c | ||
@echo "generate memory pool object" | ||
$(CC) $(CFLAGS_OBJS) $(INC) $< -o $@ | ||
|
||
clean: | ||
@echo "Cleaning" | ||
@rm -r $(BUILD_DIR)/* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
Statically Allocated Memory Pool for Embedded Systems | ||
====================================== | ||
This Module is an implementation of memory pool in C. | ||
|
||
## Compiling | ||
$ make | ||
|
||
## Example | ||
|
||
#include <stdio.h> | ||
#include <string.h> | ||
#include <MemPool.h> | ||
|
||
#define num_of_blks 3 | ||
#define block_size 10 | ||
static mp_t txMemPool; | ||
static int8_t txMem[num_of_blks][block_size]; | ||
|
||
int main() | ||
{ | ||
|
||
mp_ctor(&txMemPool, txMem, num_of_blks, block_size); | ||
|
||
int32_t * p = mp_get(&txMemPool); | ||
if (p != NULL){ | ||
memset(p, 1, block_size); | ||
mp_put(&txMemPool, p); | ||
} | ||
p = mp_get(&txMemPool); | ||
|
||
int32_t *q = mp_get(&txMemPool); | ||
int32_t *k = mp_get(&txMemPool); | ||
|
||
memset(p, 1, block_size); | ||
memset(q, 1, block_size); | ||
memset(k, 1, block_size); | ||
|
||
int32_t *f = mp_get(&txMemPool); | ||
if (f != NULL) { | ||
|
||
} else { | ||
printf("there is no available memory\n"); | ||
} | ||
|
||
mp_put(&txMemPool, f); | ||
mp_put(&txMemPool, q); | ||
mp_put(&txMemPool, k); | ||
mp_put(&txMemPool, p); | ||
|
||
return 0; | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#ifndef __MEMORYPOOL_H__ | ||
#define __MEMORYPOOL_H__ | ||
|
||
#include <stdint.h> | ||
#include <stdbool.h> | ||
#include <stddef.h> | ||
|
||
typedef struct{ | ||
|
||
void *mem_start; | ||
void *mem_free_blk; | ||
uint32_t mem_num_blks; | ||
uint32_t mem_blk_size; | ||
uint32_t mem_num_free; | ||
|
||
}mp_t; | ||
|
||
void mp_ctor(mp_t * const me, void * const addr, size_t num_blks, size_t blk_size); | ||
void *mp_get(mp_t * const me); | ||
void mp_put(mp_t * const me, void *pblk); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
#include <MemoryPool.h> | ||
|
||
#define num_of_blks 3 | ||
#define block_size 10 | ||
static mp_t txMemPool; | ||
static int8_t txMem[num_of_blks][block_size]; | ||
|
||
int main() | ||
{ | ||
|
||
mp_ctor(&txMemPool, txMem, num_of_blks, block_size); | ||
|
||
int32_t * p = mp_get(&txMemPool); | ||
if (p != NULL){ | ||
memset(p, 1, block_size); | ||
mp_put(&txMemPool, p); | ||
} | ||
p = mp_get(&txMemPool); | ||
|
||
int32_t *q = mp_get(&txMemPool); | ||
int32_t *k = mp_get(&txMemPool); | ||
|
||
memset(p, 1, block_size); | ||
memset(q, 1, block_size); | ||
memset(k, 1, block_size); | ||
|
||
int32_t *f = mp_get(&txMemPool); | ||
if (f != NULL) { | ||
|
||
} else { | ||
printf("there is no available memory\n"); | ||
} | ||
|
||
mp_put(&txMemPool, f); | ||
mp_put(&txMemPool, q); | ||
mp_put(&txMemPool, k); | ||
|
||
|
||
mp_put(&txMemPool, p); | ||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <MemoryPool.h> | ||
|
||
|
||
void mp_ctor(mp_t * const me, void * const addr, size_t num_blks, size_t blk_size) | ||
{ | ||
int8_t *pblk; | ||
void **pnext_blk; | ||
|
||
pnext_blk = (void **)addr; | ||
pblk = (int8_t *)addr + blk_size; | ||
|
||
uint32_t i; | ||
for(i = 0; i < num_blks - 1; i++){ | ||
*pnext_blk = pblk; | ||
pnext_blk = (void **)pblk; | ||
pblk = pblk + blk_size; | ||
|
||
} | ||
pnext_blk = (void *)0; | ||
|
||
me->mem_start = addr; | ||
me->mem_free_blk = addr; | ||
me->mem_num_blks = num_blks; | ||
me->mem_num_free = num_blks; | ||
me->mem_blk_size = blk_size; | ||
} | ||
|
||
void *mp_get(mp_t * const me) | ||
{ | ||
void *pblk; | ||
|
||
if (me->mem_num_free > 0){ | ||
pblk = me->mem_free_blk; | ||
me->mem_free_blk = *(void **)pblk; | ||
me->mem_num_free--; | ||
return pblk; | ||
} | ||
|
||
return (void *)0; | ||
} | ||
|
||
void mp_put(mp_t * const me, void *pblk) | ||
{ | ||
if (pblk != NULL) { | ||
*(void **)pblk = me->mem_free_blk; | ||
me->mem_free_blk = pblk; | ||
me->mem_num_free++; | ||
} | ||
} | ||
|
||
|