File tree Expand file tree Collapse file tree 4 files changed +98
-0
lines changed
Expand file tree Collapse file tree 4 files changed +98
-0
lines changed Original file line number Diff line number Diff line change @@ -124,6 +124,9 @@ endif
124124ifneq (,$(filter nanocoap,$(USEMODULE ) ) )
125125 DIRS += net/application_layer/nanocoap
126126endif
127+ ifneq (,$(filter memarray,$(USEMODULE ) ) )
128+ DIRS += memarray
129+ endif
127130
128131DIRS += $(dir $(wildcard $(addsuffix /Makefile, ${USEMODULE}) ) )
129132
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright (C) 2017 Tobias Heider <[email protected] > 3+ *
4+ * This file is subject to the terms and conditions of the GNU Lesser
5+ * General Public License v2.1. See the file LICENSE in the top level
6+ * directory for more details.
7+ */
8+
9+ #ifndef MEMARRAY_H
10+ #define MEMARRAY_H
11+
12+ #include "stdint.h"
13+ #include "stdlib.h"
14+
15+ #ifdef __cplusplus
16+ extern "C" {
17+ #endif
18+
19+ /**
20+ * @defgroup sys_memarray memory array allocator
21+ * @ingroup sys
22+ * @brief memory array allocator
23+ * @{
24+ *
25+ * @brief pseudo dynamic allocation in static memory arrays
26+ * @author Tobias Heider <[email protected] > 27+ */
28+ typedef struct {
29+ size_t size ;
30+ size_t count ;
31+ void * first_free ;
32+ void * data ;
33+ } memarray_t ;
34+
35+ /**
36+ * Initialize memory
37+ */
38+ #define MEMARRAY (name , structure , num ) \
39+ static structure _data_##name[num]; \
40+ static memarray_t name = {.size = sizeof(structure), \
41+ .count = num, \
42+ .first_free = _data_##name, \
43+ .data = _data_##name};
44+
45+ void memarray_init (memarray_t * mem );
46+
47+ void * memarray_alloc (memarray_t * mem );
48+
49+ uint8_t memarray_free (memarray_t * mem , void * ptr );
50+
51+ #ifdef __cplusplus
52+ }
53+ #endif
54+
55+ #endif /* MEMARRAY_H */
Original file line number Diff line number Diff line change 1+ include $(RIOTBASE ) /Makefile.base
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright (C) 2017 Tobias Heider <[email protected] > 3+ *
4+ * This file is subject to the terms and conditions of the GNU Lesser
5+ * General Public License v2.1. See the file LICENSE in the top level
6+ * directory for more details.
7+ */
8+
9+ #include "memarray.h"
10+ #include "string.h"
11+
12+ void * memarray_alloc (memarray_t * mem ) {
13+ if (mem -> first_free == NULL ) {
14+ return NULL ;
15+ }
16+ void * free = mem -> first_free ;
17+ mem -> first_free = * ((void * * )mem -> first_free );
18+ return free ;
19+ }
20+
21+ void memarray_init (memarray_t * mem ) {
22+ for (size_t i = 0 ; i < mem -> count - 1 ; i ++ ) {
23+ void * next = ((char * )mem -> data ) + ((i + 1 ) * mem -> size );
24+ memcpy (((char * )mem -> data ) + (i * mem -> size ), & next , sizeof (void * ));
25+ }
26+ }
27+
28+ uint8_t memarray_free (memarray_t * mem , void * ptr ) {
29+ char * iter = mem -> data ;
30+ for (size_t i = 0 ; i < mem -> count ; i ++ ) {
31+ if (iter == ptr ) {
32+ memcpy (iter , & mem -> first_free , sizeof (void * ));
33+ mem -> first_free = iter ;
34+ return 0 ;
35+ }
36+ iter += mem -> size ;
37+ }
38+ return 1 ;
39+ }
You can’t perform that action at this time.
0 commit comments