Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
unwynd committed Dec 17, 2017
0 parents commit d19d722
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
21 changes: 21 additions & 0 deletions LICENCE
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.
35 changes: 35 additions & 0 deletions Makefile
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)/*

54 changes: 54 additions & 0 deletions README.md
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;
}


22 changes: 22 additions & 0 deletions inc/MemoryPool.h
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
45 changes: 45 additions & 0 deletions sample/main.c
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;
}

51 changes: 51 additions & 0 deletions src/MemoryPool.c
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++;
}
}


0 comments on commit d19d722

Please sign in to comment.